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 int64_t ht_set (hash_table_t *ht, const char *key, uint32_t key_len, int64_t val) {
24 return ht_compare_and_set(ht, key, key_len, HT_EXPECT_WHATEVER, val);
27 int64_t ht_add (hash_table_t *ht, const char *key, uint32_t key_len, int64_t val) {
28 return ht_compare_and_set(ht, key, key_len, HT_EXPECT_NOT_EXISTS, val);
31 int64_t ht_replace (hash_table_t *ht, const char *key, uint32_t key_len, int64_t val) {
32 return ht_compare_and_set(ht, key, key_len, HT_EXPECT_EXISTS, val);
35 // Test some basic stuff; add a few keys, remove a few keys
36 void basic_test (CuTest* tc) {
38 hash_table_t *ht = ht_alloc();
40 ASSERT_EQUAL( 0, ht_count(ht) );
41 ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"a",2,10) );
42 ASSERT_EQUAL( 1, ht_count(ht) );
43 ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"b",2,20) );
44 ASSERT_EQUAL( 2, ht_count(ht) );
45 ASSERT_EQUAL( 20, ht_get(ht,"b",2) );
46 ASSERT_EQUAL( 10, ht_set(ht,"a",2,11) );
47 ASSERT_EQUAL( 20, ht_set(ht,"b",2,21) );
48 ASSERT_EQUAL( 2, ht_count(ht) );
49 ASSERT_EQUAL( 21, ht_add(ht,"b",2,22) );
50 ASSERT_EQUAL( 11, ht_remove(ht,"a",2) );
51 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"a",2) );
52 ASSERT_EQUAL( 1, ht_count(ht) );
53 ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"a",2) );
54 ASSERT_EQUAL( 21, ht_remove(ht,"b",2) );
55 ASSERT_EQUAL( 0, ht_count(ht) );
56 ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"b",2) );
57 ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"c",2) );
58 ASSERT_EQUAL( 0, ht_count(ht) );
60 ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"d",2,40) );
61 ASSERT_EQUAL( 40, ht_get(ht,"d",2) );
62 ASSERT_EQUAL( 1, ht_count(ht) );
63 ASSERT_EQUAL( 40, ht_remove(ht,"d",2) );
64 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2) );
65 ASSERT_EQUAL( 0, ht_count(ht) );
67 ASSERT_EQUAL( DOES_NOT_EXIST, ht_replace(ht,"d",2,10) );
68 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2) );
69 ASSERT_EQUAL( DOES_NOT_EXIST, ht_set(ht,"d",2,40) );
70 ASSERT_EQUAL( 40, ht_replace(ht,"d",2,41) );
71 ASSERT_EQUAL( 41, ht_get(ht,"d",2) );
72 ASSERT_EQUAL( 41, ht_remove(ht,"d",2) );
73 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2) );
74 ASSERT_EQUAL( 0, ht_count(ht) );
76 ASSERT_EQUAL( DOES_NOT_EXIST, ht_replace(ht,"b",2,20) );
77 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"b",2) );
78 // In the end, all members should be removed
79 ASSERT_EQUAL( DOES_NOT_EXIST, ht_set(ht,"b",2,20) );
80 ASSERT_EQUAL( 20, ht_replace(ht,"b",2,21) );
81 ASSERT_EQUAL( 21, ht_get(ht,"b",2) );
82 ASSERT_EQUAL( 21, ht_remove(ht,"b",2) );
83 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"b",2) );
84 ASSERT_EQUAL( 0, ht_count(ht) );
88 // In a quiecent state; it is safe to free.
92 void *simple_worker (void *arg) {
93 worker_data_t *wd = (worker_data_t *)arg;
94 hash_table_t *ht = wd->ht;
99 SYNC_ADD(wd->wait, -1);
100 do { } while (*((volatile worker_data_t *)wd)->wait); // wait for all workers to be ready
102 for (int j = 0; j < 10; ++j) {
103 for (int i = d; i < iters; i+=2) {
105 sprintf(key, "k%u", i);
106 TRACE("t0", "test ht_add() iteration (%llu, %llu)", j, i);
107 CuAssertIntEquals_Msg(tc, key, DOES_NOT_EXIST, ht_add(ht, key, strlen(key)+1, d+1) );
109 for (int i = d; i < iters; i+=2) {
111 sprintf(key, "k%u", i);
112 TRACE("t0", "test ht_remove() iteration (%llu, %llu)", j, i);
113 CuAssertIntEquals_Msg(tc, key, d+1, ht_remove(ht, key, strlen(key)+1) );
120 // Do some simple concurrent testing
121 void simple_add_remove (CuTest* tc) {
126 hash_table_t *ht = ht_alloc();
128 // In 2 threads, add & remove even & odd elements concurrently
130 for (i = 0; i < 2; ++i) {
135 int rc = nbd_thread_create(thread + i, i, simple_worker, wd + i);
136 if (rc != 0) { perror("nbd_thread_create"); return; }
138 for (i = 0; i < 2; ++i) {
139 pthread_join(thread[i], NULL);
142 // In the end, all members should be removed
143 ASSERT_EQUAL( 0, ht_count(ht) );
145 // In a quiecent state; it is safe to free.
150 void *inserter_worker (void *arg) {
151 //pthread_t thread[NUM_THREADS];
153 //hash_table_t *ht = ht_alloc();
157 // Concurrent insertion
158 void concurrent_insert (CuTest* tc) {
164 lwt_set_trace_level("h4t9");
166 // Create and run test suite
167 CuString *output = CuStringNew();
168 CuSuite* suite = CuSuiteNew();
170 SUITE_ADD_TEST(suite, basic_test);
171 SUITE_ADD_TEST(suite, simple_add_remove);
174 CuSuiteSummary(suite, output);
175 CuSuiteDetails(suite, output);
176 printf("%s\n", output->buffer);