1a813b261Sfulu/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2a813b261Sfulu
3a813b261Sfulu#include "view_tests.h"
4a813b261Sfulu
5a813b261Sfulu
6a813b261Sfulustatic view_btree_key_t *test_view_btree_key_decoding(const char *key_bin, size_t len)
7a813b261Sfulu{
8a813b261Sfulu    view_btree_key_t *k = NULL;
9a813b261Sfulu
10a813b261Sfulu    assert(decode_view_btree_key(key_bin, len, &k) == COUCHSTORE_SUCCESS);
11a813b261Sfulu    assert(k != NULL);
120c7e07b9SFilipe David Borba Manana    assert(k->json_key.size == 4);
130c7e07b9SFilipe David Borba Manana    assert(memcmp(k->json_key.buf, "\"23\"", k->json_key.size) == 0);
140c7e07b9SFilipe David Borba Manana    assert(k->doc_id.size == 12);
150c7e07b9SFilipe David Borba Manana    assert(memcmp(k->doc_id.buf, "doc_00000023", k->doc_id.size) == 0);
16a813b261Sfulu
17a813b261Sfulu    return k;
18a813b261Sfulu}
19a813b261Sfulu
20a813b261Sfulustatic view_id_btree_key_t *test_view_id_btree_key_decoding(const char *id_btree_key_bin, size_t len)
21a813b261Sfulu{
22a813b261Sfulu    view_id_btree_key_t *k = NULL;
23a813b261Sfulu
24a813b261Sfulu    assert(decode_view_id_btree_key(id_btree_key_bin, len, &k) == COUCHSTORE_SUCCESS);
25a813b261Sfulu    assert(k != NULL);
26a813b261Sfulu
270c7e07b9SFilipe David Borba Manana    assert(k->partition == 57);
280c7e07b9SFilipe David Borba Manana    assert(k->doc_id.size == 12);
290c7e07b9SFilipe David Borba Manana    assert(memcmp(k->doc_id.buf, "doc_00000057", k->doc_id.size) == 0);
30a813b261Sfulu
31a813b261Sfulu    return k;
32a813b261Sfulu}
33a813b261Sfulu
34a813b261Sfulustatic void test_view_btree_key_encoding(const view_btree_key_t *k,
35a813b261Sfulu                                       char **buffer,
36a813b261Sfulu                                       size_t *size)
37a813b261Sfulu{
38a813b261Sfulu    couchstore_error_t res;
39a813b261Sfulu
40a813b261Sfulu    res = encode_view_btree_key(k, buffer, size);
41a813b261Sfulu    assert(res == COUCHSTORE_SUCCESS);
42a813b261Sfulu}
43a813b261Sfulu
44a813b261Sfulu
45a813b261Sfulustatic void test_view_id_btree_key_encoding(const view_id_btree_key_t *k,
46a813b261Sfulu                                       char **buffer,
47a813b261Sfulu                                       size_t *size)
48a813b261Sfulu{
49a813b261Sfulu    couchstore_error_t res;
50a813b261Sfulu
51a813b261Sfulu    res = encode_view_id_btree_key(k, buffer, size);
52a813b261Sfulu    assert(res == COUCHSTORE_SUCCESS);
53a813b261Sfulu}
54a813b261Sfulu
55a813b261Sfuluvoid test_keys()
56a813b261Sfulu{
57a813b261Sfulu    char key_bin[] = {
580c7e07b9SFilipe David Borba Manana        0,4,34,50,51,34,100,111,99,95,48,48,48,48,48,48,50,51
59a813b261Sfulu    };
60a813b261Sfulu    char id_btree_key_bin[] = {
610c7e07b9SFilipe David Borba Manana        0,57,100,111,99,95,48,48,48,48,48,48,53,55
62a813b261Sfulu    };
635acdd2b3Sfulu    char *k_bin2 = NULL;
645acdd2b3Sfulu    size_t k_bin2_size = 0;
655acdd2b3Sfulu    char *id_btree_k_bin2 = NULL;
665acdd2b3Sfulu    size_t id_btree_k_bin2_size = 0;
675acdd2b3Sfulu    char *k_bin3 = NULL;
685acdd2b3Sfulu    size_t k_bin3_size = 0;
695acdd2b3Sfulu    char *id_btree_k_bin3 = NULL;
705acdd2b3Sfulu    size_t id_btree_k_bin3_size = 0;
7154712fe7STrond Norbye    view_btree_key_t *k;
7254712fe7STrond Norbye    view_id_btree_key_t *id_btree_k;
7354712fe7STrond Norbye    view_btree_key_t *k2;
7454712fe7STrond Norbye    view_id_btree_key_t *id_btree_k2;
75a813b261Sfulu
76ed955bb8STrond Norbye    fprintf(stderr, "Decoding a view btree key ...\n");
7754712fe7STrond Norbye    k = test_view_btree_key_decoding(key_bin, sizeof(key_bin));
78a813b261Sfulu
79ed955bb8STrond Norbye    fprintf(stderr, "Decoding a view id btree key ...\n");
8054712fe7STrond Norbye    id_btree_k = test_view_id_btree_key_decoding(id_btree_key_bin, sizeof(id_btree_key_bin));
81a813b261Sfulu
82ed955bb8STrond Norbye    fprintf(stderr, "Encoding the previously decoded view btree key ...\n");
83a813b261Sfulu    test_view_btree_key_encoding(k, &k_bin2, &k_bin2_size);
84a813b261Sfulu
85a813b261Sfulu    assert(k_bin2_size == sizeof(key_bin));
86a813b261Sfulu    assert(memcmp(k_bin2, key_bin, k_bin2_size) == 0);
87a813b261Sfulu
88ed955bb8STrond Norbye    fprintf(stderr, "Encoding the previously decoded view id btree key ...\n");
89a813b261Sfulu    test_view_id_btree_key_encoding(id_btree_k, &id_btree_k_bin2, &id_btree_k_bin2_size);
90a813b261Sfulu
91a813b261Sfulu    assert(id_btree_k_bin2_size == sizeof(id_btree_key_bin));
92a813b261Sfulu    assert(memcmp(id_btree_k_bin2, id_btree_key_bin, id_btree_k_bin2_size) == 0);
93a813b261Sfulu
94ed955bb8STrond Norbye    fprintf(stderr, "Decoding the previously encoded view btree key ...\n");
9554712fe7STrond Norbye    k2 = test_view_btree_key_decoding(k_bin2, k_bin2_size);
96a813b261Sfulu
97ed955bb8STrond Norbye    fprintf(stderr, "Decoding the previously encoded view id btree key ...\n");
9854712fe7STrond Norbye    id_btree_k2 = test_view_id_btree_key_decoding(id_btree_k_bin2, id_btree_k_bin2_size);
99a813b261Sfulu
100ed955bb8STrond Norbye    fprintf(stderr, "Encoding the previously decoded view btree key ...\n");
101a813b261Sfulu    test_view_btree_key_encoding(k2, &k_bin3, &k_bin3_size);
102a813b261Sfulu
103a813b261Sfulu    assert(k_bin3_size == sizeof(key_bin));
104a813b261Sfulu    assert(memcmp(k_bin3, key_bin, k_bin3_size) == 0);
105a813b261Sfulu
106ed955bb8STrond Norbye    fprintf(stderr, "Encoding the previously decoded view id btree key ...\n");
107a813b261Sfulu    test_view_id_btree_key_encoding(id_btree_k2, &id_btree_k_bin3, &id_btree_k_bin3_size);
108a813b261Sfulu
109a813b261Sfulu    assert(id_btree_k_bin3_size == sizeof(id_btree_key_bin));
110a813b261Sfulu    assert(memcmp(id_btree_k_bin3, id_btree_key_bin, id_btree_k_bin3_size) == 0);
111a813b261Sfulu
112a813b261Sfulu    free_view_btree_key(k);
113a813b261Sfulu    free_view_btree_key(k2);
114a813b261Sfulu    free(k_bin2);
115a813b261Sfulu    free(k_bin3);
116a813b261Sfulu
117a813b261Sfulu    free_view_id_btree_key(id_btree_k);
118a813b261Sfulu    free_view_id_btree_key(id_btree_k2);
119a813b261Sfulu    free(id_btree_k_bin2);
120a813b261Sfulu    free(id_btree_k_bin3);
121a813b261Sfulu}
122