test/Dictionary_test.cpp
changeset 0 31a43d94cc26
child 3 8e3ae153e2c9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/Dictionary_test.cpp	Sun Sep 20 15:14:12 2009 -0700
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 + *  Dictionary_test.cpp
     1.6 + *  Ottoman
     1.7 + *
     1.8 + *  Created by Jens Alfke on 9/4/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 +
    1.14 +#include <gtest/gtest.h>
    1.15 +#include "TestUtils.h"
    1.16 +#include "File.h"
    1.17 +#include "Dictionary.h"
    1.18 +#include "Hash.h"
    1.19 +#include <fcntl.h>
    1.20 +#include <stdio.h>
    1.21 +
    1.22 +using namespace Mooseyard;
    1.23 +
    1.24 +static const HashDictionary& getDict() {
    1.25 +    static HashDictionary *sDict;
    1.26 +    if (!sDict) {
    1.27 +        printf("Building large HashDictionary...\n");
    1.28 +        sDict = new HashDictionary();
    1.29 +        readWords();
    1.30 +        for( int i=0; i<sNWords; i++) {
    1.31 +            Blob kv(sWords[i]);
    1.32 +            sDict->put(Key(kv),kv);
    1.33 +        }
    1.34 +    }
    1.35 +    return *sDict;
    1.36 +}
    1.37 +
    1.38 +TEST(Dictionary,GetAll) {
    1.39 +    const Dictionary &dict = getDict();
    1.40 +    EXPECT_EQ( sNWords ,  dict.count() );
    1.41 +    for( int i=0; i<sNWords; i++) {
    1.42 +        Key key(sWords[i]);
    1.43 +        Blob value = dict.get(key);
    1.44 +        EXPECT_TRUE(value);
    1.45 +        EXPECT_TRUE( value.equals(key) );
    1.46 +    }
    1.47 +}
    1.48 +
    1.49 +TEST(Dictionary,Iterate) {
    1.50 +    const HashDictionary &dict = getDict();
    1.51 +    Timer t("Iterating Dictionary", sNWords);
    1.52 +    int n=0;
    1.53 +    for( HashDictionary::Iterator it(dict); it; ++it) {
    1.54 +        n++;
    1.55 +        EXPECT_TRUE(it.key().length > 0 && it.key().length < 50);
    1.56 +        EXPECT_TRUE(it.key().equals(it.value()));
    1.57 +        EXPECT_EQ( 0, ((size_t)it.value().bytes & 0x3) );  // 4-byte aligned
    1.58 +    }
    1.59 +    EXPECT_EQ(sNWords, n);
    1.60 +}
    1.61 +
    1.62 +TEST(Dictionary,Overlay) {
    1.63 +    const Dictionary &dict = getDict();
    1.64 +    OverlayDictionary overlay(&dict);
    1.65 +    
    1.66 +    EXPECT_EQ( sNWords ,  overlay.count() );
    1.67 +    EXPECT_TRUE(overlay.get("animal").equals("animal"));
    1.68 +    EXPECT_TRUE(overlay.get("asparagus").equals("asparagus"));
    1.69 +    EXPECT_FALSE(overlay.get("growf"));
    1.70 +    
    1.71 +    overlay.put("animal", "AMINAL");
    1.72 +    overlay.put("growf", "growf");
    1.73 +    EXPECT_TRUE(overlay.remove("asparagus"));
    1.74 +    
    1.75 +    EXPECT_EQ( sNWords, overlay.count() );
    1.76 +    EXPECT_TRUE(overlay.get("animal").equals("AMINAL"));
    1.77 +    EXPECT_TRUE(overlay.get("growf").equals("growf"));
    1.78 +    EXPECT_TRUE(overlay.contains("growf"));
    1.79 +    EXPECT_FALSE(overlay.get("asparagus"));
    1.80 +    EXPECT_FALSE(overlay.contains("asparagus"));
    1.81 +    
    1.82 +    int n=0;
    1.83 +    for( OverlayDictionary::Iterator it(overlay); it; ++it) {
    1.84 +        n++;
    1.85 +        EXPECT_TRUE(!it.key().equals("asparagus"));
    1.86 +    }
    1.87 +    EXPECT_EQ(sNWords, n);
    1.88 +    
    1.89 +    printf("Testing ChangeIterator...\n");
    1.90 +    n=0;
    1.91 +    int foundAsparagus=0, foundAnimal=0, foundGrowf=0;
    1.92 +    for( Dictionary::ChangeIterator it(&overlay); it; ++it) {
    1.93 +        n++;
    1.94 +        if (it.key().equals("animal")) {
    1.95 +            foundAnimal++;
    1.96 +            EXPECT_TRUE(it.value().equals("AMINAL"));
    1.97 +            EXPECT_TRUE(it.otherValue().equals("animal"));
    1.98 +        } else if (it.key().equals("asparagus")) {
    1.99 +            foundAsparagus++;
   1.100 +            EXPECT_FALSE(it.value());
   1.101 +            EXPECT_TRUE(it.otherValue().equals("asparagus"));
   1.102 +        } else if (it.key().equals("growf")) {
   1.103 +            foundGrowf++;
   1.104 +            EXPECT_TRUE(it.value().equals("growf"));
   1.105 +            EXPECT_FALSE(it.otherValue());
   1.106 +        } else {
   1.107 +            EXPECT_TRUE(false);
   1.108 +        }
   1.109 +    }
   1.110 +    EXPECT_EQ(1, foundAnimal);
   1.111 +    EXPECT_EQ(1, foundAsparagus);
   1.112 +    EXPECT_EQ(1, foundGrowf);
   1.113 +    EXPECT_EQ(3, n);
   1.114 +}