First official checkin.
5 * Created by Jens Alfke on 9/2/09.
6 * Copyright 2009 Jens Alfke. All rights reserved.
7 * BSD-Licensed: See the file "LICENSE.txt" for details.
10 #include <gtest/gtest.h>
11 #include "TestUtils.h"
16 using namespace Mooseyard;
19 static bool verbose = false;
24 #pragma mark UTILITIES:
28 static char* sKeys[100];
29 if (i>=0 && i<100 && sKeys[i] != NULL)
33 char *key = strdup(str);
39 static char* Insert(Hash &hash, const char* key) {
41 sprintf(string, "value for %s", key);
43 fprintf(stderr, "\nInserting %s -> '%s'\n", key,string);
44 int count = hash.count();
45 char *leaf = strdup(string);
51 EXPECT_EQ( count+1, hash.count() );
52 EXPECT_EQ( leaf, hash.get(key) );
56 static char* Insert(Hash &hash, int i) {
57 return Insert(hash, mkKey(i));
60 static void Remove(Hash &hash, int i) {
63 sprintf(string, "value for %s", key);
65 fprintf(stderr, "\nRemoving %s\n", key);
66 int count = hash.count();
67 char *value = (char*) hash.get(key);
68 EXPECT_TRUE(value!=NULL);
69 EXPECT_EQ(0, strcmp(value,string));
71 EXPECT_TRUE(hash.remove(key));
75 EXPECT_EQ( count-1, hash.count() );
76 EXPECT_EQ( NULL, hash.get(key) );
88 EXPECT_EQ(0, hash.count());
90 char *seventeen = Insert(hash, "seventeen");
92 EXPECT_EQ(NULL, hash.get("zero"));
93 EXPECT_EQ(NULL, hash.get("eighteen"));
95 char *four = Insert(hash, "four");
96 EXPECT_EQ(seventeen, hash.get("seventeen"));
98 char *zero = Insert(hash, "zero");
99 char *hundred = Insert(hash, "one hundred");
100 char *eight = Insert(hash, "eight");
101 char *twenty = Insert(hash, "twenty");
103 EXPECT_EQ(zero, hash.get("zero"));
104 EXPECT_EQ(four, hash.get("four"));
105 EXPECT_EQ(eight, hash.get("eight"));
106 EXPECT_EQ(seventeen, hash.get("seventeen"));
107 EXPECT_EQ(twenty, hash.get("twenty"));
108 EXPECT_EQ(hundred, hash.get("one hundred"));
111 for (Hash::Iterator iter(&hash); iter; ++iter, ++i) {
112 Blob key = iter.key();
114 fprintf(stderr, " %*s -> %s\n",
115 (int)key.length,(const char*)key.bytes,
116 ((char*)iter.value()));
121 TEST(Hash,InsertLotsAtRandom) {
123 for (int i=0; i<num; i++)
127 for (int i=0; i<num; i++)
128 fprintf(stderr, "%3d ",map[i]);
129 fprintf(stderr, "\n");
134 memset(&expected,0,sizeof(expected));
135 for (int i=0; i<num; i++) {
136 expected[map[i]] = Insert(hash, map[i]);
137 for (int j=0; j<num; j++)
138 EXPECT_EQ(expected[map[j]], hash.get(mkKey(map[j])));
142 TEST(Hash,DeleteAtRandom) {
144 for (int i=0; i<num; i++)
148 for (int i=0; i<num; i++)
152 for (int i=0; i<num; i++)
153 fprintf(stderr, "%3d ",map[i]);
154 fprintf(stderr, "\n");
157 for (int i=0; i<num; i++)
162 const int kIterations = 3;
164 double totalAdd = 0.0, totalGet = 0.0, totalRemove = 0.0;
165 for (int iter=0; iter<kIterations; iter++) {
166 printf("Adding words to Hash...\n");
170 for( int i=0; i<sNWords; i++)
171 hash.put(sWords[i], sWords[i]);
172 EXPECT_EQ(sNWords, hash.count());
173 totalAdd += t.elapsed();
177 for( int i=0; i<sNWords; i++)
178 EXPECT_EQ( sWords[i] , hash.get(sWords[i]) );
179 totalGet += t.elapsed();
183 for( int i=0; i<sNWords; i++)
184 EXPECT_TRUE( hash.remove(sWords[i]) );
185 totalRemove += t.elapsed();
188 printf("Average put = %.0lfns\n", totalAdd/kIterations/sNWords*1e9);
189 printf("Average get = %.0lfns\n", totalGet/kIterations/sNWords*1e9);
190 printf("Average remove= %.0lfns\n", totalRemove/kIterations/sNWords*1e9);