2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
13 #define ASSERT_EQUAL(x, y) CuAssertIntEquals(tc, x, y)
15 typedef struct worker_data {
22 int64_t ht_set (hash_table_t *ht, const char *key, uint32_t key_len, int64_t val) {
23 return ht_compare_and_set(ht, key, key_len, HT_EXPECT_WHATEVER, val);
26 int64_t ht_add (hash_table_t *ht, const char *key, uint32_t key_len, int64_t val) {
27 return ht_compare_and_set(ht, key, key_len, HT_EXPECT_NOT_EXISTS, val);
30 int64_t ht_replace (hash_table_t *ht, const char *key, uint32_t key_len, int64_t val) {
31 return ht_compare_and_set(ht, key, key_len, HT_EXPECT_EXISTS, val);
34 // Test some basic stuff; add a few keys, remove a few keys
35 void basic_test (CuTest* tc) {
37 hash_table_t *ht = ht_alloc();
39 ASSERT_EQUAL( 0, ht_count(ht) );
40 ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"a",2,10) );
41 ASSERT_EQUAL( 1, ht_count(ht) );
42 ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"b",2,20) );
43 ASSERT_EQUAL( 2, ht_count(ht) );
44 ASSERT_EQUAL( 20, ht_get(ht,"b",2) );
45 ASSERT_EQUAL( 10, ht_set(ht,"a",2,11) );
46 ASSERT_EQUAL( 20, ht_set(ht,"b",2,21) );
47 ASSERT_EQUAL( 2, ht_count(ht) );
48 ASSERT_EQUAL( 21, ht_add(ht,"b",2,22) );
49 ASSERT_EQUAL( 11, ht_remove(ht,"a",2) );
50 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"a",2) );
51 ASSERT_EQUAL( 1, ht_count(ht) );
52 ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"a",2) );
53 ASSERT_EQUAL( 21, ht_remove(ht,"b",2) );
54 ASSERT_EQUAL( 0, ht_count(ht) );
55 ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"b",2) );
56 ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"c",2) );
57 ASSERT_EQUAL( 0, ht_count(ht) );
59 ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"d",2,40) );
60 ASSERT_EQUAL( 40, ht_get(ht,"d",2) );
61 ASSERT_EQUAL( 1, ht_count(ht) );
62 ASSERT_EQUAL( 40, ht_remove(ht,"d",2) );
63 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2) );
64 ASSERT_EQUAL( 0, ht_count(ht) );
66 ASSERT_EQUAL( DOES_NOT_EXIST, ht_replace(ht,"d",2,10) );
67 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2) );
68 ASSERT_EQUAL( DOES_NOT_EXIST, ht_set(ht,"d",2,40) );
69 ASSERT_EQUAL( 40, ht_replace(ht,"d",2,41) );
70 ASSERT_EQUAL( 41, ht_get(ht,"d",2) );
71 ASSERT_EQUAL( 41, ht_remove(ht,"d",2) );
72 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2) );
73 ASSERT_EQUAL( 0, ht_count(ht) );
75 ASSERT_EQUAL( DOES_NOT_EXIST, ht_replace(ht,"b",2,20) );
76 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"b",2) );
77 // In the end, all members should be removed
78 ASSERT_EQUAL( DOES_NOT_EXIST, ht_set(ht,"b",2,20) );
79 ASSERT_EQUAL( 20, ht_replace(ht,"b",2,21) );
80 ASSERT_EQUAL( 21, ht_get(ht,"b",2) );
81 ASSERT_EQUAL( 21, ht_remove(ht,"b",2) );
82 ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"b",2) );
83 ASSERT_EQUAL( 0, ht_count(ht) );
87 // In a quiecent state; it is safe to free.
91 void *simple_worker (void *arg) {
92 worker_data_t *wd = (worker_data_t *)arg;
93 hash_table_t *ht = wd->ht;
98 SYNC_ADD(wd->wait, -1);
99 do { } while (*((volatile worker_data_t *)wd)->wait); // wait for all workers to be ready
102 for (j = 0; j < 10; ++j) {
103 for (i = d; i < iters; i+=2) {
105 sprintf(key, "k%u", i);
106 ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht, key, strlen(key)+1, d+1) );
108 for (i = d; i < iters; i+=2) {
110 sprintf(key, "k%u", i);
111 ASSERT_EQUAL( d+1, ht_remove(ht, key, strlen(key)+1) );
118 // Do some simple concurrent testing
119 void simple_add_remove (CuTest* tc) {
124 hash_table_t *ht = ht_alloc();
126 // In 2 threads, add & remove even & odd elements concurrently
128 for (i = 0; i < 2; ++i) {
133 int rc = nbd_thread_create(thread + i, i, simple_worker, wd + i);
134 if (rc != 0) { perror("nbd_thread_create"); return; }
136 for (i = 0; i < 2; ++i) {
137 pthread_join(thread[i], NULL);
140 // In the end, all members should be removed
141 ASSERT_EQUAL( 0, ht_count(ht) );
143 // In a quiecent state; it is safe to free.
148 void *inserter_worker (void *arg) {
149 //pthread_t thread[NUM_THREADS];
151 //hash_table_t *ht = ht_alloc();
155 // Concurrent insertion
156 void concurrent_insert (CuTest* tc) {
162 //lwt_set_trace_level("h4");
164 // Create and run test suite
165 CuString *output = CuStringNew();
166 CuSuite* suite = CuSuiteNew();
168 SUITE_ADD_TEST(suite, basic_test);
169 SUITE_ADD_TEST(suite, simple_add_remove);
172 CuSuiteSummary(suite, output);
173 CuSuiteDetails(suite, output);
174 printf("%s\n", output->buffer);