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 25
main(void)
39
{
40
41 25
        const unsigned sz = VBITMAP_SZ(1);
42 25
        char spc[sz];
43 25
        struct vbitmap *vb = vbit_init(spc, sz);
44
45
        VBITMAP_TYPE    *obits;
46
        unsigned        nbits;
47
48 25
        assert(vb);
49 25
        obits = vb->bits;
50 25
        nbits = vb->nbits;
51 25
        assert(nbits == VBITMAP_WORD);
52
53 25
        vbit_set(vb, nbits - 1);
54 25
        assert(vbit_test(vb, nbits - 1));
55
56 25
        assert(vb->bits);
57
        /* nothing malloc'ed - null ops */
58 25
        vbit_destroy(vb);
59 25
        assert(vb->bits);
60 25
        assert(vb->bits == obits);
61
62
        /* re-alloc */
63 25
        vbit_set(vb, nbits);
64 25
        assert(vbit_test(vb, nbits - 1));
65 25
        assert(vbit_test(vb, nbits));
66 25
        assert(vb->nbits == VBITMAP_LUMP);
67 25
        assert(vb->bits != obits);
68 25
        assert(vb->flags & VBITMAP_FL_MALLOC_BITS);
69
70 25
        assert(vb->bits);
71
        /* free the bits */
72 25
        vbit_destroy(vb);
73 25
        assert(vb->bits == NULL);
74 25
        assert(vb->nbits == 0);
75
76
        /* use again */
77
        assert(20 < VBITMAP_LUMP);
78 25
        vbit_set(vb, 20);
79 25
        assert(vbit_test(vb, 20));
80 25
        assert(vb->nbits == VBITMAP_LUMP);
81 25
        assert(vb->flags & VBITMAP_FL_MALLOC_BITS);
82
83
        /* grow */
84 25
        vbit_set(vb, VBITMAP_LUMP);
85 25
        assert(vbit_test(vb, 20));
86 25
        assert(vbit_test(vb, VBITMAP_LUMP));
87 25
        assert(vb->nbits == 2 * VBITMAP_LUMP);
88 25
        assert(vb->flags & VBITMAP_FL_MALLOC_BITS);
89
90 25
        vbit_destroy(vb);
91
92 25
        return (0);
93 25
}