test/Ottoman_test.cpp
author Jens Alfke <jens@mooseyard.com>
Sun Sep 20 15:14:12 2009 -0700 (2009-09-20)
changeset 0 31a43d94cc26
permissions -rw-r--r--
First official checkin.
     1 /*
     2  *  Ottoman_test.cpp
     3  *  Ottoman
     4  *
     5  *  Created by Jens Alfke on 9/11/09.
     6  *  Copyright 2009 Jens Alfke. All rights reserved.
     7  *  BSD-Licensed: See the file "LICENSE.txt" for details.
     8  */
     9 
    10 #include <gtest/gtest.h>
    11 #include "TestUtils.h"
    12 #include "Chunk.h"
    13 #include "Ottoman.h"
    14 #include "VersionDictionary.h"
    15 #include <fcntl.h>
    16 #include <stdio.h>
    17 
    18 using namespace Mooseyard;
    19 
    20 
    21 TEST(Ottoman,Build) {
    22     ASSERT_EQ(8, sizeof(Chunk));
    23     ASSERT_EQ(8, sizeof(KeyValueChunk));
    24         
    25     time_t startTime = ::time(NULL);
    26     time_t createTime;
    27     {
    28         Ottoman otto;
    29         EXPECT_EQ((void*)NULL, otto.lastVersion());
    30         ASSERT_NE((void*)NULL, otto.currentVersion());
    31         readWords();
    32         for( int i=0; i<sNWords; i++) {
    33             Blob kv(sWords[i]);
    34             otto.currentVersion()->put(Key(kv),kv);
    35         }
    36         Timer t("Saving Ottoman", sNWords);
    37         otto.saveAs("/tmp/test.ottoman");
    38         
    39         VersionDictionary *last = otto.lastVersion();
    40         ASSERT_NE((void*)NULL, last);
    41         EXPECT_EQ(sNWords, last->count());
    42         ASSERT_NE((void*)NULL, otto.currentVersion());
    43         EXPECT_FALSE(otto.currentVersion()->isChanged());
    44         createTime = last->timestamp();
    45         EXPECT_GE(createTime, startTime);
    46         EXPECT_LE(createTime, ::time(NULL));
    47     }
    48     {
    49         Ottoman otto("/tmp/test.ottoman");
    50         VersionDictionary *last = otto.lastVersion();
    51         ASSERT_NE((void*)NULL, last);
    52         ASSERT_EQ(0, last->generation());
    53         ASSERT_EQ(createTime, last->timestamp());
    54         {
    55             Timer t("Reading from Ottoman", sNWords);
    56             EXPECT_EQ( sNWords ,  last->count() );
    57             for( int i=0; i<sNWords; i++) {
    58                 Key key(sWords[i]);
    59                 Blob value = last->get(key);
    60                 ASSERT_TRUE(value);
    61                 ASSERT_TRUE( value.equals(key) ) << "expected '" << key << "', got '" << value << "' (i=" << i <<")";
    62             }
    63         }
    64         
    65         printf("Iterating through the Ottoman...\n");
    66         Timer t("Iterating Ottoman", sNWords);
    67         int n=0;
    68         for( VersionDictionary::Iterator it(last); it; ++it) {
    69             n++;
    70             ASSERT_TRUE(it.key().length > 0 && it.key().length < 50);
    71             ASSERT_TRUE(it.key().equals(it.value()));
    72             ASSERT_EQ( 0, ((size_t)it.value().bytes & 0x3) );  // 4-byte aligned
    73         }
    74         ASSERT_EQ(sNWords, n);
    75     }
    76     {
    77         printf("Opening Ottoman...\n");
    78         Ottoman otto("/tmp/test.ottoman");
    79         OverlayDictionary *current = otto.currentVersion();
    80         ASSERT_TRUE(current != NULL);
    81         EXPECT_EQ( sNWords ,  current->count() );
    82         EXPECT_TRUE(current->get("asparagus").equals("asparagus"));
    83         
    84         current->put("animal", "AMINAL");
    85         current->put("growf", "growf");
    86         EXPECT_TRUE(current->remove("asparagus"));
    87         
    88         EXPECT_EQ( sNWords, current->count() );
    89         EXPECT_TRUE(current->get("animal").equals("AMINAL"));
    90         EXPECT_TRUE(current->get("growf").equals("growf"));
    91         EXPECT_EQ( NULL, current->get("asparagus").bytes );
    92         EXPECT_TRUE(!current->contains("asparagus"));
    93         
    94         int n=0;
    95         for( OverlayDictionary::Iterator it(*current); it; ++it) {
    96             n++;
    97             ASSERT_TRUE(!it.key().equals("asparagus"));
    98         }
    99         ASSERT_EQ(sNWords, n);
   100         
   101         printf("Saving Ottoman...\n");
   102         {
   103             Timer t("Saving Ottoman");
   104             otto.save();
   105         }
   106         
   107         EXPECT_EQ( sNWords, current->count() );
   108         EXPECT_TRUE(current->get("animal").equals("AMINAL"));
   109         EXPECT_TRUE(current->get("growf").equals("growf"));
   110         EXPECT_EQ( NULL, current->get("asparagus").bytes );
   111         EXPECT_TRUE(!current->contains("asparagus"));
   112         
   113         n=0;
   114         for( OverlayDictionary::Iterator it(*current); it; ++it) {
   115             n++;
   116             ASSERT_TRUE(!it.key().equals("asparagus"));
   117         }
   118         ASSERT_EQ(sNWords, n);
   119         
   120         EXPECT_EQ(1, otto.lastVersion()->generation());
   121         const VersionDictionary *prev = otto.lastVersion()->previousVersion();
   122         ASSERT_TRUE(prev != NULL);
   123         EXPECT_EQ(0, prev->generation());
   124         EXPECT_TRUE(prev->get("asparagus").equals("asparagus"));
   125         EXPECT_TRUE(prev->get("growf").equals(NULL));
   126     }
   127     {
   128         printf("Re-opening Ottoman...\n");
   129         Ottoman otto("/tmp/test.ottoman", false);
   130         ASSERT_EQ(NULL, otto.currentVersion());
   131         VersionDictionary *last = otto.lastVersion();
   132         ASSERT_TRUE(last != NULL);
   133         EXPECT_EQ(1, last->generation());
   134         EXPECT_GE(last->timestamp(), createTime);
   135         EXPECT_LE(last->timestamp(), ::time(NULL));
   136         EXPECT_EQ( sNWords ,  last->count() );
   137         EXPECT_TRUE(last->get("animal").equals("AMINAL"));
   138         EXPECT_TRUE(last->get("growf").equals("growf"));
   139         EXPECT_EQ( NULL, last->get("asparagus").bytes );
   140         EXPECT_TRUE(!last->contains("asparagus"));
   141         
   142         int n=0;
   143         for( VersionDictionary::Iterator it(last); it; ++it) {
   144             n++;
   145             ASSERT_TRUE(!it.key().equals("asparagus"));
   146         }
   147         ASSERT_EQ(sNWords, n);
   148     }
   149     {
   150         printf("Writing Ottoman to a new file...\n");
   151         Ottoman oldhf("/tmp/test.ottoman");
   152         
   153         oldhf.saveAs("/tmp/test2.ottoman");
   154     }
   155     {
   156         printf("Opening new file...\n");
   157         Ottoman otto("/tmp/test2.ottoman");
   158         OverlayDictionary *current = otto.currentVersion();
   159         ASSERT_TRUE(current != NULL);
   160         
   161         EXPECT_EQ( sNWords ,  current->count() );
   162         EXPECT_TRUE(current->get("animal").equals("AMINAL"));
   163         EXPECT_TRUE(current->get("growf").equals("growf"));
   164         EXPECT_EQ( NULL, current->get("asparagus").bytes );
   165         EXPECT_TRUE(!current->contains("asparagus"));
   166         
   167         int n=0;
   168         for( OverlayDictionary::Iterator it(*current); it; ++it) {
   169             n++;
   170             ASSERT_TRUE(!it.key().equals("asparagus"));
   171         }
   172         ASSERT_EQ(sNWords, n);
   173 
   174         printf("Iterating the chunks...\n");
   175         int lastType = -1;
   176         int count = 0;
   177         n = 0;
   178         ChunkIterator *it = otto.chunkIterator();
   179         for (; *it; it->next()) {
   180             uint16_t type = it->chunk()->type();
   181             if (type != lastType) {
   182                 if (count > 0)
   183                     printf("%6d\n", count);
   184                 printf("type %u ... ", type);
   185                 lastType = type;
   186                 count = 0;
   187             }
   188             count++;
   189             ASSERT_LE(type, 3);
   190             if (type != 0)      // don't count padding chunks
   191                 n++;
   192         }
   193         if (count > 0)
   194             printf("%6d\n", count);
   195         printf("Iterated over %i chunks\n",n);
   196         EXPECT_EQ(sNWords+256+1, n);
   197         EXPECT_TRUE(it->atEOF());
   198         
   199         printf("Scavenging...\n");
   200         EXPECT_TRUE( otto.scavenge() );
   201     }
   202 }    
   203