xref: /4.0.0/couchstore/tests/views/bitmaps.c (revision 81a8545e)
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3/**
4 * @copyright 2013 Couchbase, Inc.
5 *
6 * @author Filipe Manana  <filipe@couchbase.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 * use this file except in compliance with the License. You may obtain a copy of
10 * the License at
11 *
12 *  http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 **/
20
21#include "view_tests.h"
22
23void test_bitmaps(void)
24{
25    bitmap_t bm, bm1, bm2;
26    uint16_t one_bits[] = {1023, 1013, 500, 401, 1, 7, 666, 69};
27    int set;
28    uint16_t i, j;
29
30    fprintf(stderr, "Running view bitmap tests\n");
31    memset(&bm, 0, sizeof(bitmap_t));
32    for (j = 0; j < (sizeof(one_bits) / sizeof(uint16_t)); ++j) {
33        set_bit(&bm, one_bits[j]);
34    }
35
36    for (i = 0; i < 1024; ++i) {
37        set = 0;
38        for (j = 0; j < (sizeof(one_bits) / sizeof(uint16_t)); ++j) {
39            if (one_bits[j] == i) {
40                set = 1;
41                break;
42            }
43        }
44
45        if (set) {
46            cb_assert(is_bit_set(&bm, i));
47        } else {
48            cb_assert(!is_bit_set(&bm, i));
49        }
50
51        unset_bit(&bm, i);
52        cb_assert(!is_bit_set(&bm, i));
53    }
54
55    memset(&bm, 0, sizeof(bitmap_t));
56    set_bit(&bm, 1023);
57    set_bit(&bm, 514);
58    set_bit(&bm, 0);
59    for (i = 0; i < (sizeof((bm.chunks)) / sizeof(bm.chunks[0])); ++i) {
60        switch (i) {
61        case 0:
62            cb_assert(bm.chunks[i] == 0x80);
63            break;
64        case 63:
65            cb_assert(bm.chunks[i] == 0x04);
66            break;
67        case 127:
68            cb_assert(bm.chunks[i] == 0x01);
69            break;
70        default:
71            cb_assert(bm.chunks[i] == 0);
72        }
73    }
74
75    memset(&bm1, 0, sizeof(bitmap_t));
76    memset(&bm2, 0, sizeof(bitmap_t));
77    set_bit(&bm1, 1023);
78    set_bit(&bm2, 0);
79
80    union_bitmaps(&bm1, &bm2);
81    cb_assert(bm1.chunks[0] == 0x80);
82    cb_assert(bm1.chunks[127] == 0x01);
83
84    /* Tests for intersection operation */
85    memset(&bm1, 0, sizeof(bitmap_t));
86    memset(&bm2, 0, sizeof(bitmap_t));
87    set_bit(&bm1, 0);
88    set_bit(&bm1, 7);
89    set_bit(&bm2, 800);
90    set_bit(&bm2, 801);
91    intersect_bitmaps(&bm1, &bm2);
92    cb_assert(bm1.chunks[0] == 0x0);
93    cb_assert(bm1.chunks[100] == 0x0);
94
95    set_bit(&bm1, 0);
96    set_bit(&bm1, 1023);
97    set_bit(&bm2, 7);
98    set_bit(&bm2, 1023);
99
100    intersect_bitmaps(&bm1, &bm2);
101    cb_assert(bm1.chunks[0] == 0x80);
102    cb_assert(bm1.chunks[127] == 0x0);
103
104    /* Tests for is_equal operation */
105    memset(&bm1, 0, sizeof(bitmap_t));
106    memset(&bm2, 0, sizeof(bitmap_t));
107    cb_assert(is_equal_bitmap(&bm1, &bm2));
108    set_bit(&bm1, 7);
109    set_bit(&bm1, 500);
110    set_bit(&bm2, 7);
111    set_bit(&bm2, 500);
112    cb_assert(is_equal_bitmap(&bm1, &bm2));
113    set_bit(&bm2, 1000);
114    cb_assert(!is_equal_bitmap(&bm1, &bm2));
115
116}
117