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;
100 SYNC_ADD(wd->wait, -1);
101 do { } while (*((volatile worker_data_t *)wd)->wait); // wait for all workers to be ready
104 for (j = 0; j < 10; ++j) {
105 for (i = d; i < iters; i+=2) {
107 sprintf(key, "k%u", i);
108 ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht, key, strlen(key)+1, d+1) );
110 for (i = d; i < iters; i+=2) {
112 sprintf(key, "k%u", i);
113 ASSERT_EQUAL( 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 = pthread_create(thread + i, NULL, simple_worker, wd + i);
136 if (rc != 0) { perror("pthread_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) {
166 //lwt_set_trace_level("h4");
168 // Create and run test suite
169 CuString *output = CuStringNew();
170 CuSuite* suite = CuSuiteNew();
172 SUITE_ADD_TEST(suite, basic_test);
173 SUITE_ADD_TEST(suite, simple_add_remove);
176 CuSuiteSummary(suite, output);
177 CuSuiteDetails(suite, output);
178 printf("%s\n", output->buffer);