varnish-cache/include/vbm_test.c
0
/*-
1
 * Copyright 2016 UPLEX - Nils Goroll Systemoptimierung
2
 * All rights reserved.
3
 *
4
 * Author: Nils Goroll <nils.goroll@uplex.de>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * Test Self-sizeing bitmap operations static initialization with dynamic growth
30
 */
31
32
#include <assert.h>
33
#include <stdio.h>
34
35
#include "vbm.h"
36
37
int
38 6
main(void)
39
{
40
41 6
        const unsigned sz = VBITMAP_SZ(1);
42 6
        char spc[sz];
43 6
        struct vbitmap *vb = vbit_init(spc, sz);
44
45
        VBITMAP_TYPE    *obits;
46
        unsigned        nbits;
47
48 6
        assert(vb);
49 6
        obits = vb->bits;
50 6
        nbits = vb->nbits;
51 6
        assert(nbits == VBITMAP_WORD);
52
53 6
        vbit_set(vb, nbits - 1);
54 6
        assert(vbit_test(vb, nbits - 1));
55
56 6
        assert(vb->bits);
57
        /* nothing malloc'ed - null ops */
58 6
        vbit_destroy(vb);
59 6
        assert(vb->bits);
60 6
        assert(vb->bits == obits);
61
62
        /* re-alloc */
63 6
        vbit_set(vb, nbits);
64 6
        assert(vbit_test(vb, nbits - 1));
65 6
        assert(vbit_test(vb, nbits));
66 6
        assert(vb->nbits == VBITMAP_LUMP);
67 6
        assert(vb->bits != obits);
68 6
        assert(vb->flags & VBITMAP_FL_MALLOC_BITS);
69
70 6
        assert(vb->bits);
71
        /* free the bits */
72 6
        vbit_destroy(vb);
73 6
        assert(vb->bits == NULL);
74 6
        assert(vb->nbits == 0);
75
76
        /* use again */
77
        assert(20 < VBITMAP_LUMP);
78 6
        vbit_set(vb, 20);
79 6
        assert(vbit_test(vb, 20));
80 6
        assert(vb->nbits == VBITMAP_LUMP);
81 6
        assert(vb->flags & VBITMAP_FL_MALLOC_BITS);
82
83
        /* grow */
84 6
        vbit_set(vb, VBITMAP_LUMP);
85 6
        assert(vbit_test(vb, 20));
86 6
        assert(vbit_test(vb, VBITMAP_LUMP));
87 6
        assert(vb->nbits == 2 * VBITMAP_LUMP);
88 6
        assert(vb->flags & VBITMAP_FL_MALLOC_BITS);
89
90 6
        vbit_destroy(vb);
91
92 6
        return (0);
93 6
}