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 static map_type_t map_type_;
25 // Test some basic stuff; add a few keys, remove a few keys
26 void basic_test (CuTest* tc) {
28 map_t *ht = map_alloc(map_type_);
30 ASSERT_EQUAL( 0, map_count(ht) );
31 ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"a",2,10) );
32 ASSERT_EQUAL( 1, map_count(ht) );
33 ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"b",2,20) );
34 ASSERT_EQUAL( 2, map_count(ht) );
35 ASSERT_EQUAL( 20, map_get(ht,"b",2) );
36 ASSERT_EQUAL( 10, map_set(ht,"a",2,11) );
37 ASSERT_EQUAL( 20, map_set(ht,"b",2,21) );
38 ASSERT_EQUAL( 2, map_count(ht) );
39 ASSERT_EQUAL( 21, map_add(ht,"b",2,22) );
40 ASSERT_EQUAL( 11, map_remove(ht,"a",2) );
41 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"a",2) );
42 ASSERT_EQUAL( 1, map_count(ht) );
43 ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"a",2) );
44 ASSERT_EQUAL( 21, map_remove(ht,"b",2) );
45 ASSERT_EQUAL( 0, map_count(ht) );
46 ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"b",2) );
47 ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"c",2) );
48 ASSERT_EQUAL( 0, map_count(ht) );
50 ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"d",2,40) );
51 ASSERT_EQUAL( 40, map_get(ht,"d",2) );
52 ASSERT_EQUAL( 1, map_count(ht) );
53 ASSERT_EQUAL( 40, map_remove(ht,"d",2) );
54 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) );
55 ASSERT_EQUAL( 0, map_count(ht) );
57 ASSERT_EQUAL( DOES_NOT_EXIST, map_replace(ht,"d",2,10) );
58 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) );
59 ASSERT_EQUAL( DOES_NOT_EXIST, map_set(ht,"d",2,40) );
60 ASSERT_EQUAL( 40, map_replace(ht,"d",2,41) );
61 ASSERT_EQUAL( 41, map_get(ht,"d",2) );
62 ASSERT_EQUAL( 41, map_remove(ht,"d",2) );
63 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2) );
64 ASSERT_EQUAL( 0, map_count(ht) );
66 ASSERT_EQUAL( DOES_NOT_EXIST, map_replace(ht,"b",2,20) );
67 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"b",2) );
69 // In the end, all entries should be removed
70 ASSERT_EQUAL( DOES_NOT_EXIST, map_set(ht,"b",2,20) );
71 ASSERT_EQUAL( 20, map_replace(ht,"b",2,21) );
72 ASSERT_EQUAL( 21, map_get(ht,"b",2) );
73 ASSERT_EQUAL( 21, map_remove(ht,"b",2) );
74 ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"b",2) );
75 ASSERT_EQUAL( 0, map_count(ht) );
79 // In a quiecent state; it is safe to free.
83 void *simple_worker (void *arg) {
84 worker_data_t *wd = (worker_data_t *)arg;
88 int iters = map_type_ == MAP_TYPE_LIST ? 100 : 1000000;
90 SYNC_ADD(wd->wait, -1);
91 do { } while (*((volatile worker_data_t *)wd)->wait); // wait for all workers to be ready
93 for (int j = 0; j < 10; ++j) {
94 for (int i = d; i < iters; i+=2) {
96 sprintf(key, "k%u", i);
97 TRACE("t0", "test map_add() iteration (%llu, %llu)", j, i);
98 CuAssertIntEquals_Msg(tc, key, DOES_NOT_EXIST, map_add(ht, key, strlen(key)+1, d+1) );
101 for (int i = d; i < iters; i+=2) {
103 sprintf(key, "k%u", i);
104 TRACE("t0", "test map_remove() iteration (%llu, %llu)", j, i);
105 CuAssertIntEquals_Msg(tc, key, d+1, map_remove(ht, key, strlen(key)+1) );
112 // Do some simple concurrent testing
113 void simple_add_remove (CuTest* tc) {
118 map_t *ht = map_alloc(map_type_);
120 // In 2 threads, add & remove even & odd elements concurrently
122 for (i = 0; i < 2; ++i) {
127 int rc = nbd_thread_create(thread + i, i, simple_worker, wd + i);
128 if (rc != 0) { perror("nbd_thread_create"); return; }
130 for (i = 0; i < 2; ++i) {
131 pthread_join(thread[i], NULL);
134 // In the end, all members should be removed
135 ASSERT_EQUAL( 0, map_count(ht) );
137 // In a quiecent state; it is safe to free.
142 void *inserter_worker (void *arg) {
143 //pthread_t thread[NUM_THREADS];
145 //map_t *ht = map_alloc(MAP_TYPE_HASHTABLE);
149 // Concurrent insertion
150 void concurrent_insert (CuTest* tc) {
156 //lwt_set_trace_level("h0");
158 map_type_t map_types[] = { MAP_TYPE_LIST, MAP_TYPE_SKIPLIST, MAP_TYPE_HASHTABLE };
159 for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) {
160 map_type_ = map_types[i];
162 // Create and run test suite
163 CuString *output = CuStringNew();
164 CuSuite* suite = CuSuiteNew();
166 SUITE_ADD_TEST(suite, basic_test);
167 SUITE_ADD_TEST(suite, simple_add_remove);
170 CuSuiteSummary(suite, output);
171 CuSuiteDetails(suite, output);
172 printf("%s\n", output->buffer);