test/Hash_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  *  Hash_test.cpp
     3  *  Ottoman
     4  *
     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.
     8  */
     9 
    10 #include <gtest/gtest.h>
    11 #include "TestUtils.h"
    12 #include "File.h"
    13 #include "Hash.h"
    14 #include <stdio.h>
    15 
    16 using namespace Mooseyard;
    17 
    18 
    19 static bool verbose = false;
    20 static int num = 100;
    21 
    22 
    23 #pragma mark -
    24 #pragma mark UTILITIES:
    25 
    26 
    27 char* mkKey (int i) {
    28     static char* sKeys[100];
    29     if (i>=0 && i<100 && sKeys[i] != NULL)
    30         return sKeys[i];
    31     char str[10];
    32     sprintf(str, "%i",i);
    33     char *key = strdup(str);
    34     if (i>=0 && i<100)
    35         sKeys[i] = key;
    36     return key;
    37 }
    38 
    39 static char* Insert(Hash &hash, const char* key) {
    40     char string[100];
    41     sprintf(string, "value for %s", key);
    42     if (verbose)
    43         fprintf(stderr, "\nInserting %s -> '%s'\n", key,string);
    44     int count = hash.count();
    45     char *leaf = strdup(string);
    46     
    47     hash.put(key,leaf);
    48     
    49     if (verbose)
    50         hash.dump();
    51     EXPECT_EQ( count+1, hash.count() );
    52     EXPECT_EQ( leaf, hash.get(key) );
    53     return leaf;
    54 }
    55 
    56 static char* Insert(Hash &hash, int i) {
    57     return Insert(hash, mkKey(i));
    58 }
    59 
    60 static void Remove(Hash &hash, int i) {
    61     char* key = mkKey(i);
    62     char string[100];
    63     sprintf(string, "value for %s", key);
    64     if (verbose)
    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));
    70 
    71     EXPECT_TRUE(hash.remove(key));
    72     
    73     if (verbose)
    74         hash.dump();
    75     EXPECT_EQ( count-1, hash.count() );
    76     EXPECT_EQ( NULL, hash.get(key) );
    77 }
    78 
    79 
    80 
    81 #pragma mark -
    82 #pragma mark TESTS:
    83 
    84 
    85 TEST(Hash,Simple) {
    86     Hash hash;
    87     hash.dump();
    88     EXPECT_EQ(0, hash.count());
    89     
    90     char *seventeen = Insert(hash, "seventeen");
    91     
    92     EXPECT_EQ(NULL, hash.get("zero"));
    93     EXPECT_EQ(NULL, hash.get("eighteen"));
    94     
    95     char *four = Insert(hash, "four");
    96     EXPECT_EQ(seventeen, hash.get("seventeen"));
    97     
    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");
   102     
   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"));
   109     
   110     int i=0;
   111     for (Hash::Iterator iter(&hash); iter; ++iter, ++i) {
   112         Blob key = iter.key();
   113         if (verbose)
   114             fprintf(stderr, "   %*s -> %s\n", 
   115                     (int)key.length,(const char*)key.bytes,
   116                     ((char*)iter.value()));
   117     }
   118     EXPECT_EQ(6, i);
   119 }
   120 
   121 TEST(Hash,InsertLotsAtRandom) {
   122     int map[num];
   123     for (int i=0; i<num; i++)
   124         map[i] = i;
   125     shuffle(map,num);
   126     if (verbose) {
   127         for (int i=0; i<num; i++)
   128             fprintf(stderr, "%3d ",map[i]);
   129         fprintf(stderr, "\n");
   130     }
   131     
   132     Hash hash(8);
   133     char* expected[num];
   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])));
   139     }
   140 }
   141 
   142 TEST(Hash,DeleteAtRandom) {
   143     Hash hash;
   144     for (int i=0; i<num; i++)
   145         Insert(hash,i);
   146 
   147     int map[num];
   148     for (int i=0; i<num; i++)
   149         map[i] = i;
   150     shuffle(map,num);
   151     if (verbose) {
   152         for (int i=0; i<num; i++)
   153             fprintf(stderr, "%3d ",map[i]);
   154         fprintf(stderr, "\n");
   155     }
   156     
   157     for (int i=0; i<num; i++)
   158         Remove(hash,map[i]);
   159 }
   160 
   161 TEST(Hash,Words) {
   162     const int kIterations = 3;
   163     readWords();
   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");
   167         Hash hash;
   168         {
   169             Timer t("adding");
   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();
   174         }        
   175         {
   176             Timer t("getting");
   177             for( int i=0; i<sNWords; i++)
   178                 EXPECT_EQ( sWords[i] ,  hash.get(sWords[i]) );
   179             totalGet += t.elapsed();
   180         }        
   181         {
   182             Timer t("removing");
   183             for( int i=0; i<sNWords; i++)
   184                 EXPECT_TRUE( hash.remove(sWords[i]) );
   185             totalRemove += t.elapsed();
   186         }        
   187     }
   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);
   191 }
   192 
   193