1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 
3 #include "view_tests.h"
4 
5 
test_view_btree_key_decoding(const char *key_bin, size_t len)6 static view_btree_key_t *test_view_btree_key_decoding(const char *key_bin, size_t len)
7 {
8     view_btree_key_t *k = NULL;
9 
10     assert(decode_view_btree_key(key_bin, len, &k) == COUCHSTORE_SUCCESS);
11     assert(k != NULL);
12     assert(k->json_key.size == 4);
13     assert(memcmp(k->json_key.buf, "\"23\"", k->json_key.size) == 0);
14     assert(k->doc_id.size == 12);
15     assert(memcmp(k->doc_id.buf, "doc_00000023", k->doc_id.size) == 0);
16 
17     return k;
18 }
19 
test_view_id_btree_key_decoding(const char *id_btree_key_bin, size_t len)20 static view_id_btree_key_t *test_view_id_btree_key_decoding(const char *id_btree_key_bin, size_t len)
21 {
22     view_id_btree_key_t *k = NULL;
23 
24     assert(decode_view_id_btree_key(id_btree_key_bin, len, &k) == COUCHSTORE_SUCCESS);
25     assert(k != NULL);
26 
27     assert(k->partition == 57);
28     assert(k->doc_id.size == 12);
29     assert(memcmp(k->doc_id.buf, "doc_00000057", k->doc_id.size) == 0);
30 
31     return k;
32 }
33 
test_view_btree_key_encoding(const view_btree_key_t *k, char **buffer, size_t *size)34 static void test_view_btree_key_encoding(const view_btree_key_t *k,
35                                        char **buffer,
36                                        size_t *size)
37 {
38     couchstore_error_t res;
39 
40     res = encode_view_btree_key(k, buffer, size);
41     assert(res == COUCHSTORE_SUCCESS);
42 }
43 
44 
test_view_id_btree_key_encoding(const view_id_btree_key_t *k, char **buffer, size_t *size)45 static void test_view_id_btree_key_encoding(const view_id_btree_key_t *k,
46                                        char **buffer,
47                                        size_t *size)
48 {
49     couchstore_error_t res;
50 
51     res = encode_view_id_btree_key(k, buffer, size);
52     assert(res == COUCHSTORE_SUCCESS);
53 }
54 
test_keysnull55 void test_keys()
56 {
57     char key_bin[] = {
58         0,4,34,50,51,34,100,111,99,95,48,48,48,48,48,48,50,51
59     };
60     char id_btree_key_bin[] = {
61         0,57,100,111,99,95,48,48,48,48,48,48,53,55
62     };
63     char *k_bin2 = NULL;
64     size_t k_bin2_size = 0;
65     char *id_btree_k_bin2 = NULL;
66     size_t id_btree_k_bin2_size = 0;
67     char *k_bin3 = NULL;
68     size_t k_bin3_size = 0;
69     char *id_btree_k_bin3 = NULL;
70     size_t id_btree_k_bin3_size = 0;
71     view_btree_key_t *k;
72     view_id_btree_key_t *id_btree_k;
73     view_btree_key_t *k2;
74     view_id_btree_key_t *id_btree_k2;
75 
76     fprintf(stderr, "Decoding a view btree key ...\n");
77     k = test_view_btree_key_decoding(key_bin, sizeof(key_bin));
78 
79     fprintf(stderr, "Decoding a view id btree key ...\n");
80     id_btree_k = test_view_id_btree_key_decoding(id_btree_key_bin, sizeof(id_btree_key_bin));
81 
82     fprintf(stderr, "Encoding the previously decoded view btree key ...\n");
83     test_view_btree_key_encoding(k, &k_bin2, &k_bin2_size);
84 
85     assert(k_bin2_size == sizeof(key_bin));
86     assert(memcmp(k_bin2, key_bin, k_bin2_size) == 0);
87 
88     fprintf(stderr, "Encoding the previously decoded view id btree key ...\n");
89     test_view_id_btree_key_encoding(id_btree_k, &id_btree_k_bin2, &id_btree_k_bin2_size);
90 
91     assert(id_btree_k_bin2_size == sizeof(id_btree_key_bin));
92     assert(memcmp(id_btree_k_bin2, id_btree_key_bin, id_btree_k_bin2_size) == 0);
93 
94     fprintf(stderr, "Decoding the previously encoded view btree key ...\n");
95     k2 = test_view_btree_key_decoding(k_bin2, k_bin2_size);
96 
97     fprintf(stderr, "Decoding the previously encoded view id btree key ...\n");
98     id_btree_k2 = test_view_id_btree_key_decoding(id_btree_k_bin2, id_btree_k_bin2_size);
99 
100     fprintf(stderr, "Encoding the previously decoded view btree key ...\n");
101     test_view_btree_key_encoding(k2, &k_bin3, &k_bin3_size);
102 
103     assert(k_bin3_size == sizeof(key_bin));
104     assert(memcmp(k_bin3, key_bin, k_bin3_size) == 0);
105 
106     fprintf(stderr, "Encoding the previously decoded view id btree key ...\n");
107     test_view_id_btree_key_encoding(id_btree_k2, &id_btree_k_bin3, &id_btree_k_bin3_size);
108 
109     assert(id_btree_k_bin3_size == sizeof(id_btree_key_bin));
110     assert(memcmp(id_btree_k_bin3, id_btree_key_bin, id_btree_k_bin3_size) == 0);
111 
112     free_view_btree_key(k);
113     free_view_btree_key(k2);
114     free(k_bin2);
115     free(k_bin3);
116 
117     free_view_id_btree_key(id_btree_k);
118     free_view_id_btree_key(id_btree_k2);
119     free(id_btree_k_bin2);
120     free(id_btree_k_bin3);
121 }
122