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