• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "iwkv.h"
2 #include "iwlog.h"
3 #include "iwutils.h"
4 #include "iwcfg.h"
5 #include "iwkv_tests.h"
6 #include "iwkv_internal.h"
7 
8 #define KBUFSZ 128
9 #define VBUFSZ 128
10 static char kbuf[KBUFSZ];
11 static char vbuf[VBUFSZ];
12 
13 /**
14  * Test cursor consistency
15  */
16 
init_suite(void)17 int init_suite(void) {
18   iwrc rc = iwkv_init();
19   return rc;
20 }
21 
clean_suite(void)22 int clean_suite(void) {
23   return 0;
24 }
25 
iwkv_test5_2()26 static void iwkv_test5_2() {
27   iwrc rc;
28   IWKV iwkv;
29   IWDB db;
30   IWKV_val key = {0};
31   IWKV_val val = {0};
32   IWKV_cursor cur1;
33   IWKV_OPTS opts = {
34     .path = "iwkv_test5_2.db",
35     .oflags = IWKV_TRUNC
36   };
37 
38   rc = iwkv_open(&opts, &iwkv);
39   CU_ASSERT_EQUAL_FATAL(rc, 0);
40   rc = iwkv_db(iwkv, 1, 0, &db);
41   CU_ASSERT_EQUAL_FATAL(rc, 0);
42 
43   for (int i = 1; i <= 64; ++i) {
44     snprintf(kbuf, KBUFSZ, "%03dkkk", i);
45     snprintf(vbuf, VBUFSZ, "%03dval", i);
46     key.data = kbuf;
47     key.size = strlen(key.data);
48     val.data = vbuf;
49     val.size = strlen(val.data);
50     rc = iwkv_put(db, &key, &val, 0);
51     CU_ASSERT_EQUAL_FATAL(rc, 0);
52   }
53 
54   // Remove all data from start
55   rc = iwkv_cursor_open(db, &cur1, IWKV_CURSOR_BEFORE_FIRST, 0);
56   CU_ASSERT_EQUAL_FATAL(rc, 0);
57   for (int i = 1; i <= 64; ++i) {
58     rc = iwkv_cursor_to(cur1, IWKV_CURSOR_NEXT);
59     CU_ASSERT_EQUAL_FATAL(rc, 0);
60     rc = iwkv_cursor_del(cur1, 0);
61     CU_ASSERT_EQUAL_FATAL(rc, 0);
62   }
63   rc = iwkv_cursor_close(&cur1);
64   CU_ASSERT_EQUAL_FATAL(rc, 0);
65 
66   rc = iwkv_cursor_open(db, &cur1, IWKV_CURSOR_BEFORE_FIRST, 0);
67   CU_ASSERT_EQUAL_FATAL(rc, 0);
68   rc = iwkv_cursor_to(cur1, IWKV_CURSOR_NEXT);
69   CU_ASSERT_EQUAL_FATAL(rc, IWKV_ERROR_NOTFOUND);
70   rc = iwkv_cursor_close(&cur1);
71   CU_ASSERT_EQUAL_FATAL(rc, 0);
72 
73 
74   // Refill
75   for (int i = 1; i <= 64; ++i) {
76     snprintf(kbuf, KBUFSZ, "%03dkkk", i);
77     snprintf(vbuf, VBUFSZ, "%03dval", i);
78     key.data = kbuf;
79     key.size = strlen(key.data);
80     val.data = vbuf;
81     val.size = strlen(val.data);
82     rc = iwkv_put(db, &key, &val, 0);
83     CU_ASSERT_EQUAL_FATAL(rc, 0);
84   }
85   // Remove all data from end
86   rc = iwkv_cursor_open(db, &cur1, IWKV_CURSOR_AFTER_LAST, 0);
87   CU_ASSERT_EQUAL_FATAL(rc, 0);
88   for (int i = 1; i <= 64; ++i) {
89     rc = iwkv_cursor_to(cur1, IWKV_CURSOR_PREV);
90     CU_ASSERT_EQUAL_FATAL(rc, 0);
91     rc = iwkv_cursor_del(cur1, 0);
92     CU_ASSERT_EQUAL_FATAL(rc, 0);
93   }
94   rc = iwkv_cursor_close(&cur1);
95   CU_ASSERT_EQUAL_FATAL(rc, 0);
96 
97   rc = iwkv_cursor_open(db, &cur1, IWKV_CURSOR_BEFORE_FIRST, 0);
98   CU_ASSERT_EQUAL_FATAL(rc, 0);
99   rc = iwkv_cursor_to(cur1, IWKV_CURSOR_NEXT);
100   CU_ASSERT_EQUAL_FATAL(rc, IWKV_ERROR_NOTFOUND);
101   rc = iwkv_cursor_close(&cur1);
102   CU_ASSERT_EQUAL_FATAL(rc, 0);
103 
104   rc = iwkv_close(&iwkv);
105   CU_ASSERT_EQUAL_FATAL(rc, 0);
106 }
107 
iwkv_test5_1(void)108 static void iwkv_test5_1(void) {
109   iwrc rc;
110   IWKV_val key = {0};
111   IWKV_val val = {0};
112   IWKV iwkv;
113   IWDB db;
114   IWKV_cursor cur1, cur2;
115   IWKV_OPTS opts = {
116     .path = "iwkv_test5_1.db",
117     .oflags = IWKV_TRUNC
118   };
119   rc = iwkv_open(&opts, &iwkv);
120   CU_ASSERT_EQUAL_FATAL(rc, 0);
121   rc = iwkv_db(iwkv, 1, 0, &db);
122   CU_ASSERT_EQUAL_FATAL(rc, 0);
123 
124   int c = 0;
125   for (int i = 1; i < 30 * 2; i = i + 2, ++c) {
126     snprintf(kbuf, KBUFSZ, "%03dkkk", i);
127     snprintf(vbuf, VBUFSZ, "%03dval", i);
128     key.data = kbuf;
129     key.size = strlen(key.data);
130     val.data = vbuf;
131     val.size = strlen(val.data);
132     rc = iwkv_put(db, &key, &val, 0);
133     CU_ASSERT_EQUAL_FATAL(rc, 0);
134   }
135 
136   //K: 023kkk
137   //K: 025kkk <--
138   //K: 027kkk
139   //K: 029kkk
140   snprintf(kbuf, KBUFSZ, "%03dkkk", 25);
141   key.data = kbuf;
142   key.size = strlen(key.data);
143   rc = iwkv_cursor_open(db, &cur1, IWKV_CURSOR_EQ, &key);
144   CU_ASSERT_EQUAL_FATAL(rc, 0);
145 
146   //K: 023kkk
147   //K: 025kkk
148   //K: 027kkk
149   //K: 029kkk <--
150   //K: 031kkk
151 
152   snprintf(kbuf, KBUFSZ, "%03dkkk", 29);
153   key.data = kbuf;
154   key.size = strlen(key.data);
155   rc = iwkv_cursor_open(db, &cur2, IWKV_CURSOR_EQ, &key);
156   CU_ASSERT_EQUAL_FATAL(rc, 0);
157 
158   CU_ASSERT_EQUAL(cur1->cnpos, 17);
159   CU_ASSERT_EQUAL(cur2->cnpos, 15);
160 
161   //K: 023kkk
162   //K: 025kkk <--
163   //K: 026kkk +
164   //K: 027kkk
165   //K: 029kkk <--
166   //K: 031kkk
167   snprintf(kbuf, KBUFSZ, "%03dkkk", 26);
168   snprintf(vbuf, VBUFSZ, "%03dval", 26);
169   key.data = kbuf;
170   key.size = strlen(key.data);
171   val.data = vbuf;
172   val.size = strlen(val.data);
173   rc = iwkv_put(db, &key, &val, 0);
174   CU_ASSERT_EQUAL_FATAL(rc, 0);
175 
176   CU_ASSERT_EQUAL(cur1->cnpos, 18);
177   CU_ASSERT_EQUAL(cur2->cnpos, 15);
178 
179   snprintf(kbuf, KBUFSZ, "%03dkkk", 0);
180   snprintf(vbuf, VBUFSZ, "%03dval", 0);
181   key.data = kbuf;
182   key.size = strlen(key.data);
183   val.data = vbuf;
184   val.size = strlen(val.data);
185   rc = iwkv_put(db, &key, &val, 0);
186   CU_ASSERT_EQUAL_FATAL(rc, 0);
187 
188   CU_ASSERT_EQUAL(cur1->cnpos, 18);
189   CU_ASSERT_EQUAL(cur2->cnpos, 15);
190 
191   //K: 023kkk
192   //K: 025kkk <--
193   //K: 026kkk +
194   //K: 027kkk
195   //K: 028kkk +
196   //K: 029kkk <--
197   //K: 031kkk
198 
199   // FORCE SPLIT:
200   snprintf(kbuf, KBUFSZ, "%03dkkk", 28);
201   snprintf(vbuf, VBUFSZ, "%03dval", 28);
202   key.data = kbuf;
203   key.size = strlen(key.data);
204   val.data = vbuf;
205   val.size = strlen(val.data);
206   rc = iwkv_put(db, &key, &val, 0);
207   CU_ASSERT_EQUAL_FATAL(rc, 0);
208 
209   CU_ASSERT_EQUAL(cur1->cnpos, 1);
210   CU_ASSERT_EQUAL(cur2->cnpos, 15);
211 
212   rc = iwkv_cursor_get(cur1, &key, &val);
213   CU_ASSERT_EQUAL_FATAL(rc, 0);
214   CU_ASSERT_FALSE(strncmp((char *)key.data, "025kkk", strlen("025kkk")));
215   CU_ASSERT_FALSE(strncmp((char *)val.data, "025val", strlen("025val")));
216   iwkv_kv_dispose(&key, &val);
217 
218   rc = iwkv_cursor_to(cur1, IWKV_CURSOR_NEXT);
219   CU_ASSERT_EQUAL_FATAL(rc, 0);
220   rc = iwkv_cursor_get(cur1, &key, &val);
221   CU_ASSERT_EQUAL_FATAL(rc, 0);
222   CU_ASSERT_FALSE(strncmp((char *)key.data, "023kkk", strlen("023kkk")));
223   CU_ASSERT_FALSE(strncmp((char *)val.data, "023val", strlen("023val")));
224   iwkv_kv_dispose(&key, &val);
225 
226   rc = iwkv_cursor_to(cur1, IWKV_CURSOR_PREV);
227   CU_ASSERT_EQUAL_FATAL(rc, 0);
228   rc = iwkv_cursor_to(cur1, IWKV_CURSOR_PREV);
229   CU_ASSERT_EQUAL_FATAL(rc, 0);
230   rc = iwkv_cursor_get(cur1, &key, &val);
231   CU_ASSERT_EQUAL_FATAL(rc, 0);
232   CU_ASSERT_FALSE(strncmp((char *)key.data, "026kkk", strlen("026kkk")));
233   CU_ASSERT_FALSE(strncmp((char *)val.data, "026val", strlen("026val")));
234   iwkv_kv_dispose(&key, &val);
235 
236   rc = iwkv_cursor_to(cur1, IWKV_CURSOR_PREV);
237   CU_ASSERT_EQUAL_FATAL(rc, 0);
238   rc = iwkv_cursor_get(cur1, &key, &val);
239   CU_ASSERT_EQUAL_FATAL(rc, 0);
240   CU_ASSERT_FALSE(strncmp((char *)key.data, "027kkk", strlen("027kkk")));
241   CU_ASSERT_FALSE(strncmp((char *)val.data, "027val", strlen("027val")));
242   iwkv_kv_dispose(&key, &val);
243 
244   CU_ASSERT_EQUAL(cur1->cnpos, 17);
245 
246   rc = iwkv_cursor_close(&cur1);
247   CU_ASSERT_EQUAL_FATAL(rc, 0);
248   rc = iwkv_cursor_close(&cur2);
249   CU_ASSERT_EQUAL_FATAL(rc, 0);
250   rc = iwkv_close(&iwkv);
251   CU_ASSERT_EQUAL_FATAL(rc, 0);
252 }
253 
254 
main()255 int main() {
256   CU_pSuite pSuite = NULL;
257 
258   /* Initialize the CUnit test registry */
259   if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error();
260 
261   /* Add a suite to the registry */
262   pSuite = CU_add_suite("iwkv_test5", init_suite, clean_suite);
263 
264   if (NULL == pSuite) {
265     CU_cleanup_registry();
266     return CU_get_error();
267   }
268 
269   /* Add the tests to the suite */
270   if (
271     (NULL == CU_add_test(pSuite, "iwkv_test5_1", iwkv_test5_1)) ||
272     (NULL == CU_add_test(pSuite, "iwkv_test5_2", iwkv_test5_2))
273 
274   )  {
275     CU_cleanup_registry();
276     return CU_get_error();
277   }
278 
279   /* Run all tests using the CUnit Basic interface */
280   CU_basic_set_mode(CU_BRM_VERBOSE);
281   CU_basic_run_tests();
282   int ret = CU_get_error() || CU_get_number_of_failures();
283   CU_cleanup_registry();
284   return ret;
285 }
286