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