END_TEST START_TEST(simplehash_with_empty_strings) { ck_assert_int_eq(simplehash(NULL, 10), 0); ck_assert_int_eq(simplehash("empty", 0), 0); }
END_TEST START_TEST(simplehash_with_simple_strings) { ck_assert_int_eq(simplehash("0", 1), 49); ck_assert_int_eq(simplehash("1", 1), 50); ck_assert_int_eq(simplehash("12", 2), 101); }
//test bucket_find_entry and bucket_overflow_remove static void test_bucket_find_entry(struct lruhash *table) { testkey *k1 = newkey(12); testkey *k2 = newkey(12 + 1024); testkey *k3 = newkey(14); testkey *k4 = newkey(12 + 1024*2); hashvalue_t h = simplehash(12); struct lruhash_bucket bucket; memset(&bucket, 0, sizeof(bucket)); //remove from empty list bucket_overflow_remove(&bucket, &k1->entry); //find in empty list unit_assert(bucket_find_entry(table, &bucket, h, k1) == NULL); //insert bucket.overflow_list = &k1->entry; unit_assert(bucket_find_entry(table, &bucket, simplehash(13), k1) == NULL); unit_assert(k1->entry.hash == k2->entry.hash); unit_assert(bucket_find_entry(table, &bucket, h, k2) == NULL); unit_assert(bucket_find_entry(table, &bucket, h, k1) == &k1->entry); //remove bucket_overflow_remove(&bucket, &k1->entry); unit_assert(bucket_find_entry(table, &bucket, h, k1) == NULL); //insert multi unit_assert(k1->entry.hash == k4->entry.hash); k4->entry.overflow_next = &k1->entry; k3->entry.overflow_next = &k4->entry; bucket.overflow_list = &k3->entry; unit_assert(bucket_find_entry(table, &bucket, simplehash(13), k1) == NULL); unit_assert(k1->entry.hash == k2->entry.hash); unit_assert(bucket_find_entry(table, &bucket, h, k2) == NULL); unit_assert(bucket_find_entry(table, &bucket, h, k1) == &k1->entry); //remove mid unit_assert(bucket_find_entry(table, &bucket, k4->entry.hash, k4) == &k4->entry); bucket_overflow_remove(&bucket, &k4->entry); unit_assert(bucket_find_entry(table, &bucket, k4->entry.hash, k4) == NULL); //remove last bucket_overflow_remove(&bucket, &k1->entry); unit_assert(bucket_find_entry(table, &bucket, h, k1) == NULL); delkey(k1); delkey(k2); delkey(k3); delkey(k4); }
/* increases frequency of ngram(p,len) */ static inline int increasefreq( table_t *t, char *p, int len ) { uint4 hash = simplehash( p, len ) & TABLEMASK; entry_t *entry = t->table[ hash ]; while ( entry ) { if ( issame( entry->str, p, len ) ) { /*** Found it! ***/ entry->cnt++; return 1; } else { entry = entry->next; } } /*** Not found, so create ***/ entry = wgmempool_alloc( t->pool, sizeof(entry_t) ); strcpy( entry->str, p ); entry->cnt = 1; entry->next = t->table[hash]; t->table[hash] = entry; return 1; }
//test remove a random element static void testremove(struct lruhash *table, testdata *ref[]) { int num = random() % MAXHASH; testkey *key = newkey(num); lruhash_remove(table, simplehash(num), key); if (ref) ref[num] = NULL; delkey(key); }
//test add a random element static void testadd(struct lruhash *table, testdata *ref[]) { int num = random() % MAXHASH; testdata *data = newdata(num); testkey *key = newkey(num); key->entry.data = data; lruhash_insert(table, simplehash(num), &key->entry, data); if(ref) ref[num] = data; }
static testkey *newkey(int id) { testkey *k = (testkey *)calloc(1, sizeof(testkey)); if(!k) { printf("calloc testkey: out of memory\n"); exit(1); } k->id = id; k->entry.hash = simplehash(id); k->entry.key = k; lock_basic_init(&k->entry.lock); return k; }
/* looks up ngram(p,len) */ static entry_t *findfreq( table_t *t, char *p, int len ) { uint4 hash = simplehash( p, len ) & TABLEMASK; entry_t *entry = t->table[ hash ]; while ( entry ) { if ( issame( entry->str, p, len ) ) { return entry; } else { entry = entry->next; } } return NULL; }
//test lookup a random element static void testlookup(struct lruhash *table, testdata *ref[]) { int num = random() % MAXHASH; testkey *key = newkey(num); struct lruhash_entry *e = lruhash_lookup(table, simplehash(num), key); testdata *data = e ? (testdata *)e->data : NULL; if(e) { unit_assert(e->key); unit_assert(e->data); lock_basic_unlock(&e->lock); } if (ref) unit_assert(data == ref[num]); delkey(key); }
END_TEST START_TEST(dbcheck_with_no_changes_in_iflist) { int forcesave = 0; uint32_t ifhash; char *ifacelist; linuxonly; ck_assert_int_eq(remove_directory(TESTDIR), 1); fake_proc_net_dev("w", "ethsomething", 1, 2, 3, 4); fake_proc_net_dev("a", "ethelse", 5, 6, 7, 8); ck_assert_int_ne(getiflist(&ifacelist, 0), 0); ifhash = simplehash(ifacelist, (int)strlen(ifacelist)); ck_assert_int_eq(dbcheck(ifhash, &forcesave), ifhash); ck_assert_int_eq(forcesave, 0); }
//test lruhash_insert, lruhash_lookup and lruhash_remove static void test_short_table(struct lruhash *table) { testkey *k1 = newkey(12); testkey *k2 = newkey(14); testdata *d1 = newdata(128); testdata *d2 = newdata(129); k1->entry.data = d1; k2->entry.data = d2; lruhash_insert(table, simplehash(12), &k1->entry, d1); lruhash_insert(table, simplehash(14), &k2->entry, d2); unit_assert(lruhash_lookup(table, simplehash(12), k1) == &k1->entry); lock_basic_unlock(&k1->entry.lock); unit_assert(lruhash_lookup(table, simplehash(14), k2) == &k2->entry); lock_basic_unlock(&k2->entry.lock ); lruhash_remove(table, simplehash(12), k1); lruhash_remove(table, simplehash(14), k2); }
uint32_t dbcheck(uint32_t dbhash, int *forcesave) { char *ifacelist, interface[32]; datanode *p = dataptr; uint32_t newhash; int offset, found; /* get list of currently visible interfaces */ if (getiflist(&ifacelist)==0) { free(ifacelist); return 0; } newhash = simplehash(ifacelist, (int)strlen(ifacelist)); /* search for changes if hash doesn't match */ if (newhash!=dbhash) { if (debug) { printf("ifacelist changed: '%s' %u <> %u\n", ifacelist, dbhash, newhash); } while (p != NULL) { if (p->filled) { found = offset = 0; while (offset <= (int)strlen(ifacelist)) { sscanf(ifacelist+offset, "%31s", interface); if (strcmp(p->data.iface.interface, interface)==0) { found = 1; break; } offset += (int)strlen(interface)+1; } if (p->data.iface.active==1 && found==0) { p->data.iface.active = 0; p->data.currx = p->data.curtx = 0; if (cfg.savestatus) { *forcesave = 1; } snprintf(errorstring, 512, "Interface \"%s\" disabled.", p->data.iface.interface); printe(PT_Info); } else if (p->data.iface.active==0 && found==1) { p->data.iface.active = 1; p->data.currx = p->data.curtx = 0; if (cfg.savestatus) { *forcesave = 1; } snprintf(errorstring, 512, "Interface \"%s\" enabled.", p->data.iface.interface); printe(PT_Info); } } p = p->next; } } free(ifacelist); return newhash; }