c7e6d57c1d34c59f9cd4ae2ce89750f1e544e0d1
[nbds] / struct / ht_test.c
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  */
5 #include <stdio.h>
6 #include <pthread.h>
7 #include "runtime.h"
8 #include "CuTest.h"
9 #include "common.h"
10 #include "ht.h"
11 #include "mem.h"
12
13 #define ASSERT_EQUAL(x, y) CuAssertIntEquals(tc, x, y)
14
15 typedef struct worker_data {
16     int id;
17     CuTest *tc;
18     hash_table_t *ht;
19     int *wait;
20 } worker_data_t;
21
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);
24 }
25
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);
28 }
29
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);
32 }
33
34 // Test some basic stuff; add a few keys, remove a few keys
35 void basic_test (CuTest* tc) {
36
37     hash_table_t *ht = ht_alloc();
38
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)          );
58     
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)          );
65
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)          );
74
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)          );
84
85     ht_free(ht);
86
87     // In a quiecent state; it is safe to free.
88     rcu_update();
89 }
90
91 void *simple_worker (void *arg) {
92     worker_data_t *wd = (worker_data_t *)arg;
93     hash_table_t *ht = wd->ht;
94     CuTest* tc = wd->tc;
95     uint64_t d = wd->id;
96     int iters = 20000;
97
98     SYNC_ADD(wd->wait, -1);
99     do { } while (*((volatile worker_data_t *)wd)->wait); // wait for all workers to be ready
100
101     int i, j;
102     for (j = 0; j < 10; ++j) {
103         for (i = d; i < iters; i+=2) {
104             char key[8];
105             sprintf(key, "k%u", i); 
106             ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht, key, strlen(key)+1, d+1) );
107         }
108         for (i = d; i < iters; i+=2) {
109             char key[8];
110             sprintf(key, "k%u", i); 
111             ASSERT_EQUAL( d+1, ht_remove(ht, key, strlen(key)+1) );
112         }
113         rcu_update();
114     }
115     return NULL;
116 }
117
118 // Do some simple concurrent testing
119 void simple_add_remove (CuTest* tc) {
120
121     pthread_t thread[2];
122     worker_data_t wd[2];
123     int wait = 2;
124     hash_table_t *ht = ht_alloc();
125
126     // In 2 threads, add & remove even & odd elements concurrently
127     int i;
128     for (i = 0; i < 2; ++i) {
129         wd[i].id = i;
130         wd[i].tc = tc;
131         wd[i].ht = ht;
132         wd[i].wait = &wait;
133         int rc = nbd_thread_create(thread + i, i, simple_worker, wd + i);
134         if (rc != 0) { perror("nbd_thread_create"); return; }
135     }
136     for (i = 0; i < 2; ++i) {
137         pthread_join(thread[i], NULL);
138     }
139
140     // In the end, all members should be removed
141     ASSERT_EQUAL( 0, ht_count(ht) );
142
143     // In a quiecent state; it is safe to free.
144     ht_free(ht);
145 }
146
147
148 void *inserter_worker (void *arg) {
149     //pthread_t thread[NUM_THREADS];
150
151     //hash_table_t *ht = ht_alloc();
152     return NULL;
153 }
154
155 // Concurrent insertion
156 void concurrent_insert (CuTest* tc) {
157 }
158
159 int main (void) {
160
161     nbd_init();
162     //lwt_set_trace_level("h4");
163
164     // Create and run test suite
165         CuString *output = CuStringNew();
166         CuSuite* suite = CuSuiteNew();
167
168     SUITE_ADD_TEST(suite, basic_test);
169     SUITE_ADD_TEST(suite, simple_add_remove);
170
171         CuSuiteRun(suite);
172         CuSuiteSummary(suite, output);
173         CuSuiteDetails(suite, output);
174         printf("%s\n", output->buffer);
175
176     return 0;
177 }