2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
14 #define ASSERT_EQUAL(x, y) CuAssertIntEquals(tc, x, y)
16 typedef struct worker_data {
23 // Test some basic stuff; add a few keys, remove a few keys
24 void basic_test (CuTest* tc) {
26 map_t *ht = map_alloc(MAP_TYPE_HASHTABLE);
28 ASSERT_EQUAL( 0, map_count(ht) );
29 ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"a",2,10) );
30 ASSERT_EQUAL( 1, map_count(ht) );
31 ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"b",2,20) );
32 ASSERT_EQUAL( 2, map_count(ht) );
33 ASSERT_EQUAL( 20, map_get(ht,"b",2) );
34 ASSERT_EQUAL( 10, map_set(ht,"a",2,11) );
35 ASSERT_EQUAL( 20, map_set(ht,"b",2,21) );
36 ASSERT_EQUAL( 2, map_count(ht) );
37 ASSERT_EQUAL( 21, map_add(ht,"b",2,22) );
38 ASSERT_EQUAL( 11, map_remove(ht,"a",2) );
39 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"a",2) );
40 ASSERT_EQUAL( 1, map_count(ht) );
41 ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"a",2) );
42 ASSERT_EQUAL( 21, map_remove(ht,"b",2) );
43 ASSERT_EQUAL( 0, map_count(ht) );
44 ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"b",2) );
45 ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"c",2) );
46 ASSERT_EQUAL( 0, map_count(ht) );
48 ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"d",2,40) );
49 ASSERT_EQUAL( 40, map_get(ht,"d",2) );
50 ASSERT_EQUAL( 1, map_count(ht) );
51 ASSERT_EQUAL( 40, map_remove(ht,"d",2) );
52 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) );
53 ASSERT_EQUAL( 0, map_count(ht) );
55 ASSERT_EQUAL( DOES_NOT_EXIST, map_replace(ht,"d",2,10) );
56 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) );
57 ASSERT_EQUAL( DOES_NOT_EXIST, map_set(ht,"d",2,40) );
58 ASSERT_EQUAL( 40, map_replace(ht,"d",2,41) );
59 ASSERT_EQUAL( 41, map_get(ht,"d",2) );
60 ASSERT_EQUAL( 41, map_remove(ht,"d",2) );
61 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) );
62 ASSERT_EQUAL( 0, map_count(ht) );
64 ASSERT_EQUAL( DOES_NOT_EXIST, map_replace(ht,"b",2,20) );
65 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"b",2) );
67 // In the end, all entries should be removed
68 ASSERT_EQUAL( DOES_NOT_EXIST, map_set(ht,"b",2,20) );
69 ASSERT_EQUAL( 20, map_replace(ht,"b",2,21) );
70 ASSERT_EQUAL( 21, map_get(ht,"b",2) );
71 ASSERT_EQUAL( 21, map_remove(ht,"b",2) );
72 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"b",2) );
73 ASSERT_EQUAL( 0, map_count(ht) );
77 // In a quiecent state; it is safe to free.
81 void *simple_worker (void *arg) {
82 worker_data_t *wd = (worker_data_t *)arg;
88 SYNC_ADD(wd->wait, -1);
89 do { } while (*((volatile worker_data_t *)wd)->wait); // wait for all workers to be ready
91 for (int j = 0; j < 10; ++j) {
92 for (int i = d; i < iters; i+=2) {
94 sprintf(key, "k%u", i);
95 TRACE("t0", "test map_add() iteration (%llu, %llu)", j, i);
96 CuAssertIntEquals_Msg(tc, key, DOES_NOT_EXIST, map_add(ht, key, strlen(key)+1, d+1) );
98 for (int i = d; i < iters; i+=2) {
100 sprintf(key, "k%u", i);
101 TRACE("t0", "test map_remove() iteration (%llu, %llu)", j, i);
102 CuAssertIntEquals_Msg(tc, key, d+1, map_remove(ht, key, strlen(key)+1) );
109 // Do some simple concurrent testing
110 void simple_add_remove (CuTest* tc) {
115 map_t *ht = map_alloc(MAP_TYPE_HASHTABLE);
117 // In 2 threads, add & remove even & odd elements concurrently
119 for (i = 0; i < 2; ++i) {
124 int rc = nbd_thread_create(thread + i, i, simple_worker, wd + i);
125 if (rc != 0) { perror("nbd_thread_create"); return; }
127 for (i = 0; i < 2; ++i) {
128 pthread_join(thread[i], NULL);
131 // In the end, all members should be removed
132 ASSERT_EQUAL( 0, map_count(ht) );
134 // In a quiecent state; it is safe to free.
139 void *inserter_worker (void *arg) {
140 //pthread_t thread[NUM_THREADS];
142 //map_t *ht = map_alloc(MAP_TYPE_HASHTABLE);
146 // Concurrent insertion
147 void concurrent_insert (CuTest* tc) {
153 //lwt_set_trace_level("h0");
155 // Create and run test suite
156 CuString *output = CuStringNew();
157 CuSuite* suite = CuSuiteNew();
159 SUITE_ADD_TEST(suite, basic_test);
160 SUITE_ADD_TEST(suite, simple_add_remove);
163 CuSuiteSummary(suite, output);
164 CuSuiteDetails(suite, output);
165 printf("%s\n", output->buffer);