test/Dictionary_test.cpp
author Jens Alfke <jens@mooseyard.com>
Sun Sep 20 15:14:12 2009 -0700 (2009-09-20)
changeset 0 31a43d94cc26
child 3 8e3ae153e2c9
permissions -rw-r--r--
First official checkin.
     1 /*
     2  *  Dictionary_test.cpp
     3  *  Ottoman
     4  *
     5  *  Created by Jens Alfke on 9/4/09.
     6  *  Copyright 2009 Jens Alfke. All rights reserved.
     7  *  BSD-Licensed: See the file "LICENSE.txt" for details.
     8  */
     9 
    10 
    11 #include <gtest/gtest.h>
    12 #include "TestUtils.h"
    13 #include "File.h"
    14 #include "Dictionary.h"
    15 #include "Hash.h"
    16 #include <fcntl.h>
    17 #include <stdio.h>
    18 
    19 using namespace Mooseyard;
    20 
    21 static const HashDictionary& getDict() {
    22     static HashDictionary *sDict;
    23     if (!sDict) {
    24         printf("Building large HashDictionary...\n");
    25         sDict = new HashDictionary();
    26         readWords();
    27         for( int i=0; i<sNWords; i++) {
    28             Blob kv(sWords[i]);
    29             sDict->put(Key(kv),kv);
    30         }
    31     }
    32     return *sDict;
    33 }
    34 
    35 TEST(Dictionary,GetAll) {
    36     const Dictionary &dict = getDict();
    37     EXPECT_EQ( sNWords ,  dict.count() );
    38     for( int i=0; i<sNWords; i++) {
    39         Key key(sWords[i]);
    40         Blob value = dict.get(key);
    41         EXPECT_TRUE(value);
    42         EXPECT_TRUE( value.equals(key) );
    43     }
    44 }
    45 
    46 TEST(Dictionary,Iterate) {
    47     const HashDictionary &dict = getDict();
    48     Timer t("Iterating Dictionary", sNWords);
    49     int n=0;
    50     for( HashDictionary::Iterator it(dict); it; ++it) {
    51         n++;
    52         EXPECT_TRUE(it.key().length > 0 && it.key().length < 50);
    53         EXPECT_TRUE(it.key().equals(it.value()));
    54         EXPECT_EQ( 0, ((size_t)it.value().bytes & 0x3) );  // 4-byte aligned
    55     }
    56     EXPECT_EQ(sNWords, n);
    57 }
    58 
    59 TEST(Dictionary,Overlay) {
    60     const Dictionary &dict = getDict();
    61     OverlayDictionary overlay(&dict);
    62     
    63     EXPECT_EQ( sNWords ,  overlay.count() );
    64     EXPECT_TRUE(overlay.get("animal").equals("animal"));
    65     EXPECT_TRUE(overlay.get("asparagus").equals("asparagus"));
    66     EXPECT_FALSE(overlay.get("growf"));
    67     
    68     overlay.put("animal", "AMINAL");
    69     overlay.put("growf", "growf");
    70     EXPECT_TRUE(overlay.remove("asparagus"));
    71     
    72     EXPECT_EQ( sNWords, overlay.count() );
    73     EXPECT_TRUE(overlay.get("animal").equals("AMINAL"));
    74     EXPECT_TRUE(overlay.get("growf").equals("growf"));
    75     EXPECT_TRUE(overlay.contains("growf"));
    76     EXPECT_FALSE(overlay.get("asparagus"));
    77     EXPECT_FALSE(overlay.contains("asparagus"));
    78     
    79     int n=0;
    80     for( OverlayDictionary::Iterator it(overlay); it; ++it) {
    81         n++;
    82         EXPECT_TRUE(!it.key().equals("asparagus"));
    83     }
    84     EXPECT_EQ(sNWords, n);
    85     
    86     printf("Testing ChangeIterator...\n");
    87     n=0;
    88     int foundAsparagus=0, foundAnimal=0, foundGrowf=0;
    89     for( Dictionary::ChangeIterator it(&overlay); it; ++it) {
    90         n++;
    91         if (it.key().equals("animal")) {
    92             foundAnimal++;
    93             EXPECT_TRUE(it.value().equals("AMINAL"));
    94             EXPECT_TRUE(it.otherValue().equals("animal"));
    95         } else if (it.key().equals("asparagus")) {
    96             foundAsparagus++;
    97             EXPECT_FALSE(it.value());
    98             EXPECT_TRUE(it.otherValue().equals("asparagus"));
    99         } else if (it.key().equals("growf")) {
   100             foundGrowf++;
   101             EXPECT_TRUE(it.value().equals("growf"));
   102             EXPECT_FALSE(it.otherValue());
   103         } else {
   104             EXPECT_TRUE(false);
   105         }
   106     }
   107     EXPECT_EQ(1, foundAnimal);
   108     EXPECT_EQ(1, foundAsparagus);
   109     EXPECT_EQ(1, foundGrowf);
   110     EXPECT_EQ(3, n);
   111 }