1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 
3 #include "view_tests.h"
4 #include "../src/couch_btree.h"
5 
test_view_btree_reduction_decoding(const char *reduction_bin, size_t len)6 static view_btree_reduction_t *test_view_btree_reduction_decoding(const char *reduction_bin,
7                                                                   size_t len)
8 {
9     view_btree_reduction_t *r = NULL;
10     uint16_t partition_bitset[] = {
11         0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
12         25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,
13         47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63
14     };
15     bitmap_t expected_part_bitmap;
16     unsigned i;
17 
18     assert(decode_view_btree_reduction(reduction_bin, len, &r) == COUCHSTORE_SUCCESS);
19 
20     assert(r != NULL);
21     assert(r->kv_count == 1582);
22 
23     memset(&expected_part_bitmap, 0, sizeof(expected_part_bitmap));
24     for (i = 0; i < (sizeof(partition_bitset) / sizeof(partition_bitset[0])); ++i) {
25         set_bit(&expected_part_bitmap, partition_bitset[i]);
26     }
27 
28     assert(memcmp(&r->partitions_bitmap, &expected_part_bitmap, sizeof(expected_part_bitmap)) == 0);
29     assert(r->num_values == 3);
30     assert(r->reduce_values[0].size == 4);
31     assert(r->reduce_values[1].size == 5);
32     assert(r->reduce_values[2].size == 9);
33 
34     assert(memcmp(r->reduce_values[0].buf, "1582", 4) == 0);
35     assert(memcmp(r->reduce_values[1].buf, "-1582", 5) == 0);
36     assert(memcmp(r->reduce_values[2].buf, "110120647", 9) == 0);
37 
38     return r;
39 }
40 
test_view_id_btree_reduction_decoding(const char *id_btree_reduction_bin)41 static view_id_btree_reduction_t *test_view_id_btree_reduction_decoding(const char *id_btree_reduction_bin)
42 {
43     view_id_btree_reduction_t *r = NULL;
44     uint16_t partition_bitset[] = { 49, 50, 51, 52 };
45     bitmap_t expected_part_bitmap;
46     unsigned i;
47 
48     assert(decode_view_id_btree_reduction(id_btree_reduction_bin, &r) == COUCHSTORE_SUCCESS);
49     assert(r != NULL);
50     assert(r->kv_count == 3026);
51     memset(&expected_part_bitmap, 0, sizeof(expected_part_bitmap));
52     for (i = 0; i < (sizeof(partition_bitset) / sizeof(partition_bitset[0])); ++i) {
53         set_bit(&expected_part_bitmap, partition_bitset[i]);
54     }
55     assert(memcmp(&r->partitions_bitmap, &expected_part_bitmap, sizeof(expected_part_bitmap)) == 0);
56 
57     return r;
58 }
59 
test_view_btree_reduction_encoding(const view_btree_reduction_t *r, char *buffer, size_t *size)60 static void test_view_btree_reduction_encoding(const view_btree_reduction_t *r,
61                                                char *buffer,
62                                                size_t *size)
63 {
64     couchstore_error_t res;
65 
66     res = encode_view_btree_reduction(r, buffer, size);
67     assert(res == COUCHSTORE_SUCCESS);
68 }
69 
70 
test_view_id_btree_reduction_encoding(const view_id_btree_reduction_t *r, char *buffer, size_t *size)71 static void test_view_id_btree_reduction_encoding(const view_id_btree_reduction_t *r,
72                                                   char *buffer,
73                                                   size_t *size)
74 {
75     couchstore_error_t res;
76 
77     res = encode_view_id_btree_reduction(r, buffer, size);
78     assert(res == COUCHSTORE_SUCCESS);
79 }
80 
test_reductionsnull81 void test_reductions()
82 {
83     unsigned char reduction_bin[] = {
84         0,0,0,6,46,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
85         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
86         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
87         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255,255,255,255,255,0,4,
88         49,53,56,50,0,5,45,49,53,56,50,0,9,49,49,48,49,50,48,54,52,55
89     };
90     unsigned char id_btree_reduction_bin[] = {
91         0,0,0,11,210,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
92         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
93         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
94         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30,0,0,0,0,0,0
95     };
96     char r_bin2[MAX_REDUCTION_SIZE];
97     size_t r_bin2_size = 0;
98     char id_btree_r_bin2[MAX_REDUCTION_SIZE];
99     size_t id_btree_r_bin2_size = 0;
100     char r_bin3[MAX_REDUCTION_SIZE];
101     size_t r_bin3_size = 0;
102     char id_btree_r_bin3[MAX_REDUCTION_SIZE];
103     size_t id_btree_r_bin3_size = 0;
104     view_btree_reduction_t *r;
105     view_id_btree_reduction_t *id_btree_r;
106     view_btree_reduction_t *r2;
107     view_id_btree_reduction_t *id_btree_r2;
108 
109     fprintf(stderr, "Decoding a view btree reduction ...\n");
110     r = test_view_btree_reduction_decoding((char*)reduction_bin,
111                                            sizeof(reduction_bin));
112 
113     fprintf(stderr, "Decoding a view id btree reduction ...\n");
114     id_btree_r = test_view_id_btree_reduction_decoding((char*)id_btree_reduction_bin);
115 
116     fprintf(stderr, "Encoding the previously decoded view btree reduction ...\n");
117     test_view_btree_reduction_encoding(r, r_bin2, &r_bin2_size);
118 
119     assert(r_bin2_size == sizeof(reduction_bin));
120     assert(memcmp(r_bin2, reduction_bin, r_bin2_size) == 0);
121 
122     fprintf(stderr, "Encoding the previously decoded view id btree reduction ...\n");
123     test_view_id_btree_reduction_encoding(id_btree_r, id_btree_r_bin2, &id_btree_r_bin2_size);
124 
125     assert(id_btree_r_bin2_size == sizeof(id_btree_reduction_bin));
126     assert(memcmp(id_btree_r_bin2, id_btree_reduction_bin, id_btree_r_bin2_size) == 0);
127 
128     fprintf(stderr, "Decoding the previously encoded view btree reduction ...\n");
129     r2 = test_view_btree_reduction_decoding(r_bin2, r_bin2_size);
130 
131     fprintf(stderr, "Decoding the previously encoded view id btree reduction ...\n");
132     id_btree_r2 = test_view_id_btree_reduction_decoding(id_btree_r_bin2);
133 
134     fprintf(stderr, "Encoding the previously decoded view btree reduciton ...\n");
135     test_view_btree_reduction_encoding(r2, r_bin3, &r_bin3_size);
136 
137     assert(r_bin3_size == sizeof(reduction_bin));
138     assert(memcmp(r_bin3, reduction_bin, r_bin3_size) == 0);
139 
140     fprintf(stderr, "Encoding the previously decoded view id btree reduciton ...\n");
141     test_view_id_btree_reduction_encoding(id_btree_r2, id_btree_r_bin3, &id_btree_r_bin3_size);
142 
143     assert(id_btree_r_bin3_size == sizeof(id_btree_reduction_bin));
144     assert(memcmp(id_btree_r_bin3, id_btree_reduction_bin, id_btree_r_bin3_size) == 0);
145 
146     free_view_btree_reduction(r);
147     free_view_btree_reduction(r2);
148 
149     free_view_id_btree_reduction(id_btree_r);
150     free_view_id_btree_reduction(id_btree_r2);
151 }
152