1 #include "iwkv.h"
2 #include "iwlog.h"
__attribute__null3 #include "iwutils.h"
4 #include "iwcfg.h"
5 #include "iwkv_tests.h"
6 #include "iwkv_internal.h"
7
8 #define KBUFSZ 1024
9 #define VBUFSZ 1024
10 char kbuf[KBUFSZ];
11 char vbuf[VBUFSZ];
12
13 uint32_t g_seed;
14
15 int init_suite(void) {
16 iwrc rc = iwkv_init();
17 RCRET(rc);
18 g_seed = 2681089616;
19 return rc;
20 }
21
22 int clean_suite(void) {
23 return 0;
24 }
25
26 static void iwkv_test7_1_impl(int direction) {
27 iwrc rc;
28 IWKV iwkv;
29 IWDB db;
30 IWKV_val key = { 0 };
31 IWKV_val val = { 0 };
32 IWKV_OPTS opts = {
33 .path = direction > 0 ? "iwkv_test7_2_fwd.db" : "iwkv_test7_2_back.db",
34 .oflags = IWKV_TRUNC,
35 .random_seed = g_seed
36 };
37 rc = iwkv_open(&opts, &iwkv);
38 CU_ASSERT_EQUAL_FATAL(rc, 0);
39 rc = iwkv_db(iwkv, 1, IWDB_COMPOUND_KEYS, &db);
40 CU_ASSERT_EQUAL_FATAL(rc, 0);
41
42 const int nrecords = 50000;
43
44 for (int i = 0; i < nrecords; ++i) {
45 snprintf(kbuf, KBUFSZ, "5368fce5-c138-4f0d-bfee-dbce07eb28e1%d", i);
46 snprintf(vbuf, VBUFSZ, "%04d", i);
47 key.data = kbuf;
48 key.size = strlen(key.data);
49 key.compound = direction > 0 ? i + 1 : nrecords - i;
50
51 val.data = vbuf;
52 val.size = strlen(val.data);
53 rc = iwkv_put(db, &key, &val, 0);
54 CU_ASSERT_EQUAL_FATAL(rc, 0);
55 }
56
57 for (int i = 0; i < nrecords; ++i) {
58 snprintf(kbuf, KBUFSZ, "5368fce5-c138-4f0d-bfee-dbce07eb28e1%d", i);
59 snprintf(vbuf, VBUFSZ, "%04d", i);
60 key.data = kbuf;
61 key.size = strlen(key.data);
62 key.compound = direction > 0 ? i + 1 : nrecords - i;
63 rc = iwkv_get(db, &key, &val);
64 CU_ASSERT_EQUAL_FATAL(rc, 0);
65 iwkv_val_dispose(&val);
66 }
67 rc = iwkv_close(&iwkv);
68 CU_ASSERT_EQUAL_FATAL(rc, 0);
69 }
70
71 static void iwkv_test7_1() {
72 iwkv_test7_1_impl(1);
73 iwkv_test7_1_impl(-1);
74 IWP_FILE_STAT fwd_s = { 0 };
75 IWP_FILE_STAT back_s = { 0 };
76 iwrc rc = iwp_fstat("iwkv_test7_2_fwd.db", &fwd_s);
77 CU_ASSERT_EQUAL_FATAL(rc, 0);
78 rc = iwp_fstat("iwkv_test7_2_back.db", &back_s);
79 CU_ASSERT_EQUAL_FATAL(rc, 0);
80 CU_ASSERT_TRUE((double) fwd_s.size / back_s.size < 1.1);
81 }
82
83 int main() {
84 CU_pSuite pSuite = NULL;
85
86 /* Initialize the CUnit test registry */
87 if (CUE_SUCCESS != CU_initialize_registry()) {
88 return CU_get_error();
89 }
90
91 /* Add a suite to the registry */
92 pSuite = CU_add_suite("iwkv_test7", init_suite, clean_suite);
93
94 if (NULL == pSuite) {
95 CU_cleanup_registry();
96 return CU_get_error();
97 }
98
99 /* Add the tests to the suite */
100 if (
101 (NULL == CU_add_test(pSuite, "iwkv_test7_1", iwkv_test7_1))) {
102 CU_cleanup_registry();
103 return CU_get_error();
104 }
105
106 /* Run all tests using the CUnit Basic interface */
107 CU_basic_set_mode(CU_BRM_VERBOSE);
108 CU_basic_run_tests();
109 int ret = CU_get_error() || CU_get_number_of_failures();
110 CU_cleanup_registry();
111 return ret;
112 }
113