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