X-Git-Url: https://pd.if.org/git/?p=nbds;a=blobdiff_plain;f=test%2Fht_test.c;fp=test%2Fht_test.c;h=0000000000000000000000000000000000000000;hp=e6ec9ff46727993a3a983d01b18678621663fc74;hb=df360b20f11476e53534a53c9ce11493d7c7a764;hpb=8143ca0acc36e19d004431952e3b6f9b3d337f49 diff --git a/test/ht_test.c b/test/ht_test.c deleted file mode 100644 index e6ec9ff..0000000 --- a/test/ht_test.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Written by Josh Dybnis and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain - */ -#include -#include -#include "runtime.h" -#include "CuTest.h" -#include "common.h" -#include "map.h" -#include "mem.h" -#include "lwt.h" - -#define ASSERT_EQUAL(x, y) CuAssertIntEquals(tc, x, y) - -typedef struct worker_data { - int id; - CuTest *tc; - map_t *ht; - int *wait; -} worker_data_t; - -// Test some basic stuff; add a few keys, remove a few keys -void basic_test (CuTest* tc) { - - map_t *ht = map_alloc(MAP_TYPE_HASHTABLE); - - ASSERT_EQUAL( 0, map_count(ht) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"a",2,10) ); - ASSERT_EQUAL( 1, map_count(ht) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"b",2,20) ); - ASSERT_EQUAL( 2, map_count(ht) ); - ASSERT_EQUAL( 20, map_get(ht,"b",2) ); - ASSERT_EQUAL( 10, map_set(ht,"a",2,11) ); - ASSERT_EQUAL( 20, map_set(ht,"b",2,21) ); - ASSERT_EQUAL( 2, map_count(ht) ); - ASSERT_EQUAL( 21, map_add(ht,"b",2,22) ); - ASSERT_EQUAL( 11, map_remove(ht,"a",2) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"a",2) ); - ASSERT_EQUAL( 1, map_count(ht) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"a",2) ); - ASSERT_EQUAL( 21, map_remove(ht,"b",2) ); - ASSERT_EQUAL( 0, map_count(ht) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"b",2) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"c",2) ); - ASSERT_EQUAL( 0, map_count(ht) ); - - ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"d",2,40) ); - ASSERT_EQUAL( 40, map_get(ht,"d",2) ); - ASSERT_EQUAL( 1, map_count(ht) ); - ASSERT_EQUAL( 40, map_remove(ht,"d",2) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) ); - ASSERT_EQUAL( 0, map_count(ht) ); - - ASSERT_EQUAL( DOES_NOT_EXIST, map_replace(ht,"d",2,10) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_set(ht,"d",2,40) ); - ASSERT_EQUAL( 40, map_replace(ht,"d",2,41) ); - ASSERT_EQUAL( 41, map_get(ht,"d",2) ); - ASSERT_EQUAL( 41, map_remove(ht,"d",2) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) ); - ASSERT_EQUAL( 0, map_count(ht) ); - - ASSERT_EQUAL( DOES_NOT_EXIST, map_replace(ht,"b",2,20) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"b",2) ); - - // In the end, all entries should be removed - ASSERT_EQUAL( DOES_NOT_EXIST, map_set(ht,"b",2,20) ); - ASSERT_EQUAL( 20, map_replace(ht,"b",2,21) ); - ASSERT_EQUAL( 21, map_get(ht,"b",2) ); - ASSERT_EQUAL( 21, map_remove(ht,"b",2) ); - ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"b",2) ); - ASSERT_EQUAL( 0, map_count(ht) ); - - map_free(ht); - - // In a quiecent state; it is safe to free. - rcu_update(); -} - -void *simple_worker (void *arg) { - worker_data_t *wd = (worker_data_t *)arg; - map_t *ht = wd->ht; - CuTest* tc = wd->tc; - uint64_t d = wd->id; - int iters = 1000000; - - SYNC_ADD(wd->wait, -1); - do { } while (*((volatile worker_data_t *)wd)->wait); // wait for all workers to be ready - - for (int j = 0; j < 10; ++j) { - for (int i = d; i < iters; i+=2) { - char key[10]; - sprintf(key, "k%u", i); - TRACE("t0", "test map_add() iteration (%llu, %llu)", j, i); - CuAssertIntEquals_Msg(tc, key, DOES_NOT_EXIST, map_add(ht, key, strlen(key)+1, d+1) ); - } - for (int i = d; i < iters; i+=2) { - char key[10]; - sprintf(key, "k%u", i); - TRACE("t0", "test map_remove() iteration (%llu, %llu)", j, i); - CuAssertIntEquals_Msg(tc, key, d+1, map_remove(ht, key, strlen(key)+1) ); - } - rcu_update(); - } - return NULL; -} - -// Do some simple concurrent testing -void simple_add_remove (CuTest* tc) { - - pthread_t thread[2]; - worker_data_t wd[2]; - int wait = 2; - map_t *ht = map_alloc(MAP_TYPE_HASHTABLE); - - // In 2 threads, add & remove even & odd elements concurrently - int i; - for (i = 0; i < 2; ++i) { - wd[i].id = i; - wd[i].tc = tc; - wd[i].ht = ht; - wd[i].wait = &wait; - int rc = nbd_thread_create(thread + i, i, simple_worker, wd + i); - if (rc != 0) { perror("nbd_thread_create"); return; } - } - for (i = 0; i < 2; ++i) { - pthread_join(thread[i], NULL); - } - - // In the end, all members should be removed - ASSERT_EQUAL( 0, map_count(ht) ); - - // In a quiecent state; it is safe to free. - map_free(ht); -} - - -void *inserter_worker (void *arg) { - //pthread_t thread[NUM_THREADS]; - - //map_t *ht = map_alloc(MAP_TYPE_HASHTABLE); - return NULL; -} - -// Concurrent insertion -void concurrent_insert (CuTest* tc) { -} - -int main (void) { - - nbd_init(); - //lwt_set_trace_level("h0"); - - // Create and run test suite - CuString *output = CuStringNew(); - CuSuite* suite = CuSuiteNew(); - - SUITE_ADD_TEST(suite, basic_test); - SUITE_ADD_TEST(suite, simple_add_remove); - - CuSuiteRun(suite); - CuSuiteSummary(suite, output); - CuSuiteDetails(suite, output); - printf("%s\n", output->buffer); - - return 0; -}