test/Ottoman_test.cpp
changeset 0 31a43d94cc26
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/Ottoman_test.cpp	Sun Sep 20 15:14:12 2009 -0700
     1.3 @@ -0,0 +1,203 @@
     1.4 +/*
     1.5 + *  Ottoman_test.cpp
     1.6 + *  Ottoman
     1.7 + *
     1.8 + *  Created by Jens Alfke on 9/11/09.
     1.9 + *  Copyright 2009 Jens Alfke. All rights reserved.
    1.10 + *  BSD-Licensed: See the file "LICENSE.txt" for details.
    1.11 + */
    1.12 +
    1.13 +#include <gtest/gtest.h>
    1.14 +#include "TestUtils.h"
    1.15 +#include "Chunk.h"
    1.16 +#include "Ottoman.h"
    1.17 +#include "VersionDictionary.h"
    1.18 +#include <fcntl.h>
    1.19 +#include <stdio.h>
    1.20 +
    1.21 +using namespace Mooseyard;
    1.22 +
    1.23 +
    1.24 +TEST(Ottoman,Build) {
    1.25 +    ASSERT_EQ(8, sizeof(Chunk));
    1.26 +    ASSERT_EQ(8, sizeof(KeyValueChunk));
    1.27 +        
    1.28 +    time_t startTime = ::time(NULL);
    1.29 +    time_t createTime;
    1.30 +    {
    1.31 +        Ottoman otto;
    1.32 +        EXPECT_EQ((void*)NULL, otto.lastVersion());
    1.33 +        ASSERT_NE((void*)NULL, otto.currentVersion());
    1.34 +        readWords();
    1.35 +        for( int i=0; i<sNWords; i++) {
    1.36 +            Blob kv(sWords[i]);
    1.37 +            otto.currentVersion()->put(Key(kv),kv);
    1.38 +        }
    1.39 +        Timer t("Saving Ottoman", sNWords);
    1.40 +        otto.saveAs("/tmp/test.ottoman");
    1.41 +        
    1.42 +        VersionDictionary *last = otto.lastVersion();
    1.43 +        ASSERT_NE((void*)NULL, last);
    1.44 +        EXPECT_EQ(sNWords, last->count());
    1.45 +        ASSERT_NE((void*)NULL, otto.currentVersion());
    1.46 +        EXPECT_FALSE(otto.currentVersion()->isChanged());
    1.47 +        createTime = last->timestamp();
    1.48 +        EXPECT_GE(createTime, startTime);
    1.49 +        EXPECT_LE(createTime, ::time(NULL));
    1.50 +    }
    1.51 +    {
    1.52 +        Ottoman otto("/tmp/test.ottoman");
    1.53 +        VersionDictionary *last = otto.lastVersion();
    1.54 +        ASSERT_NE((void*)NULL, last);
    1.55 +        ASSERT_EQ(0, last->generation());
    1.56 +        ASSERT_EQ(createTime, last->timestamp());
    1.57 +        {
    1.58 +            Timer t("Reading from Ottoman", sNWords);
    1.59 +            EXPECT_EQ( sNWords ,  last->count() );
    1.60 +            for( int i=0; i<sNWords; i++) {
    1.61 +                Key key(sWords[i]);
    1.62 +                Blob value = last->get(key);
    1.63 +                ASSERT_TRUE(value);
    1.64 +                ASSERT_TRUE( value.equals(key) ) << "expected '" << key << "', got '" << value << "' (i=" << i <<")";
    1.65 +            }
    1.66 +        }
    1.67 +        
    1.68 +        printf("Iterating through the Ottoman...\n");
    1.69 +        Timer t("Iterating Ottoman", sNWords);
    1.70 +        int n=0;
    1.71 +        for( VersionDictionary::Iterator it(last); it; ++it) {
    1.72 +            n++;
    1.73 +            ASSERT_TRUE(it.key().length > 0 && it.key().length < 50);
    1.74 +            ASSERT_TRUE(it.key().equals(it.value()));
    1.75 +            ASSERT_EQ( 0, ((size_t)it.value().bytes & 0x3) );  // 4-byte aligned
    1.76 +        }
    1.77 +        ASSERT_EQ(sNWords, n);
    1.78 +    }
    1.79 +    {
    1.80 +        printf("Opening Ottoman...\n");
    1.81 +        Ottoman otto("/tmp/test.ottoman");
    1.82 +        OverlayDictionary *current = otto.currentVersion();
    1.83 +        ASSERT_TRUE(current != NULL);
    1.84 +        EXPECT_EQ( sNWords ,  current->count() );
    1.85 +        EXPECT_TRUE(current->get("asparagus").equals("asparagus"));
    1.86 +        
    1.87 +        current->put("animal", "AMINAL");
    1.88 +        current->put("growf", "growf");
    1.89 +        EXPECT_TRUE(current->remove("asparagus"));
    1.90 +        
    1.91 +        EXPECT_EQ( sNWords, current->count() );
    1.92 +        EXPECT_TRUE(current->get("animal").equals("AMINAL"));
    1.93 +        EXPECT_TRUE(current->get("growf").equals("growf"));
    1.94 +        EXPECT_EQ( NULL, current->get("asparagus").bytes );
    1.95 +        EXPECT_TRUE(!current->contains("asparagus"));
    1.96 +        
    1.97 +        int n=0;
    1.98 +        for( OverlayDictionary::Iterator it(*current); it; ++it) {
    1.99 +            n++;
   1.100 +            ASSERT_TRUE(!it.key().equals("asparagus"));
   1.101 +        }
   1.102 +        ASSERT_EQ(sNWords, n);
   1.103 +        
   1.104 +        printf("Saving Ottoman...\n");
   1.105 +        {
   1.106 +            Timer t("Saving Ottoman");
   1.107 +            otto.save();
   1.108 +        }
   1.109 +        
   1.110 +        EXPECT_EQ( sNWords, current->count() );
   1.111 +        EXPECT_TRUE(current->get("animal").equals("AMINAL"));
   1.112 +        EXPECT_TRUE(current->get("growf").equals("growf"));
   1.113 +        EXPECT_EQ( NULL, current->get("asparagus").bytes );
   1.114 +        EXPECT_TRUE(!current->contains("asparagus"));
   1.115 +        
   1.116 +        n=0;
   1.117 +        for( OverlayDictionary::Iterator it(*current); it; ++it) {
   1.118 +            n++;
   1.119 +            ASSERT_TRUE(!it.key().equals("asparagus"));
   1.120 +        }
   1.121 +        ASSERT_EQ(sNWords, n);
   1.122 +        
   1.123 +        EXPECT_EQ(1, otto.lastVersion()->generation());
   1.124 +        const VersionDictionary *prev = otto.lastVersion()->previousVersion();
   1.125 +        ASSERT_TRUE(prev != NULL);
   1.126 +        EXPECT_EQ(0, prev->generation());
   1.127 +        EXPECT_TRUE(prev->get("asparagus").equals("asparagus"));
   1.128 +        EXPECT_TRUE(prev->get("growf").equals(NULL));
   1.129 +    }
   1.130 +    {
   1.131 +        printf("Re-opening Ottoman...\n");
   1.132 +        Ottoman otto("/tmp/test.ottoman", false);
   1.133 +        ASSERT_EQ(NULL, otto.currentVersion());
   1.134 +        VersionDictionary *last = otto.lastVersion();
   1.135 +        ASSERT_TRUE(last != NULL);
   1.136 +        EXPECT_EQ(1, last->generation());
   1.137 +        EXPECT_GE(last->timestamp(), createTime);
   1.138 +        EXPECT_LE(last->timestamp(), ::time(NULL));
   1.139 +        EXPECT_EQ( sNWords ,  last->count() );
   1.140 +        EXPECT_TRUE(last->get("animal").equals("AMINAL"));
   1.141 +        EXPECT_TRUE(last->get("growf").equals("growf"));
   1.142 +        EXPECT_EQ( NULL, last->get("asparagus").bytes );
   1.143 +        EXPECT_TRUE(!last->contains("asparagus"));
   1.144 +        
   1.145 +        int n=0;
   1.146 +        for( VersionDictionary::Iterator it(last); it; ++it) {
   1.147 +            n++;
   1.148 +            ASSERT_TRUE(!it.key().equals("asparagus"));
   1.149 +        }
   1.150 +        ASSERT_EQ(sNWords, n);
   1.151 +    }
   1.152 +    {
   1.153 +        printf("Writing Ottoman to a new file...\n");
   1.154 +        Ottoman oldhf("/tmp/test.ottoman");
   1.155 +        
   1.156 +        oldhf.saveAs("/tmp/test2.ottoman");
   1.157 +    }
   1.158 +    {
   1.159 +        printf("Opening new file...\n");
   1.160 +        Ottoman otto("/tmp/test2.ottoman");
   1.161 +        OverlayDictionary *current = otto.currentVersion();
   1.162 +        ASSERT_TRUE(current != NULL);
   1.163 +        
   1.164 +        EXPECT_EQ( sNWords ,  current->count() );
   1.165 +        EXPECT_TRUE(current->get("animal").equals("AMINAL"));
   1.166 +        EXPECT_TRUE(current->get("growf").equals("growf"));
   1.167 +        EXPECT_EQ( NULL, current->get("asparagus").bytes );
   1.168 +        EXPECT_TRUE(!current->contains("asparagus"));
   1.169 +        
   1.170 +        int n=0;
   1.171 +        for( OverlayDictionary::Iterator it(*current); it; ++it) {
   1.172 +            n++;
   1.173 +            ASSERT_TRUE(!it.key().equals("asparagus"));
   1.174 +        }
   1.175 +        ASSERT_EQ(sNWords, n);
   1.176 +
   1.177 +        printf("Iterating the chunks...\n");
   1.178 +        int lastType = -1;
   1.179 +        int count = 0;
   1.180 +        n = 0;
   1.181 +        ChunkIterator *it = otto.chunkIterator();
   1.182 +        for (; *it; it->next()) {
   1.183 +            uint16_t type = it->chunk()->type();
   1.184 +            if (type != lastType) {
   1.185 +                if (count > 0)
   1.186 +                    printf("%6d\n", count);
   1.187 +                printf("type %u ... ", type);
   1.188 +                lastType = type;
   1.189 +                count = 0;
   1.190 +            }
   1.191 +            count++;
   1.192 +            ASSERT_LE(type, 3);
   1.193 +            if (type != 0)      // don't count padding chunks
   1.194 +                n++;
   1.195 +        }
   1.196 +        if (count > 0)
   1.197 +            printf("%6d\n", count);
   1.198 +        printf("Iterated over %i chunks\n",n);
   1.199 +        EXPECT_EQ(sNWords+256+1, n);
   1.200 +        EXPECT_TRUE(it->atEOF());
   1.201 +        
   1.202 +        printf("Scavenging...\n");
   1.203 +        EXPECT_TRUE( otto.scavenge() );
   1.204 +    }
   1.205 +}    
   1.206 +