diff options
Diffstat (limited to 'regress/unittests/bitmap')
-rw-r--r-- | regress/unittests/bitmap/Makefile | 12 | ||||
-rw-r--r-- | regress/unittests/bitmap/tests.c | 135 |
2 files changed, 147 insertions, 0 deletions
diff --git a/regress/unittests/bitmap/Makefile b/regress/unittests/bitmap/Makefile new file mode 100644 index 000000000..b704d22d6 --- /dev/null +++ b/regress/unittests/bitmap/Makefile | |||
@@ -0,0 +1,12 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2015/01/15 07:36:28 djm Exp $ | ||
2 | |||
3 | TEST_ENV= "MALLOC_OPTIONS=AFGJPRX" | ||
4 | |||
5 | PROG=test_bitmap | ||
6 | SRCS=tests.c | ||
7 | REGRESS_TARGETS=run-regress-${PROG} | ||
8 | |||
9 | run-regress-${PROG}: ${PROG} | ||
10 | env ${TEST_ENV} ./${PROG} | ||
11 | |||
12 | .include <bsd.regress.mk> | ||
diff --git a/regress/unittests/bitmap/tests.c b/regress/unittests/bitmap/tests.c new file mode 100644 index 000000000..23025f90a --- /dev/null +++ b/regress/unittests/bitmap/tests.c | |||
@@ -0,0 +1,135 @@ | |||
1 | /* $OpenBSD: tests.c,v 1.1 2015/01/15 07:36:28 djm Exp $ */ | ||
2 | /* | ||
3 | * Regress test for bitmap.h bitmap API | ||
4 | * | ||
5 | * Placed in the public domain | ||
6 | */ | ||
7 | |||
8 | #include "includes.h" | ||
9 | |||
10 | #include <sys/types.h> | ||
11 | #include <sys/param.h> | ||
12 | #include <stdio.h> | ||
13 | #ifdef HAVE_STDINT_H | ||
14 | #include <stdint.h> | ||
15 | #endif | ||
16 | #include <stdlib.h> | ||
17 | #include <string.h> | ||
18 | |||
19 | #include <openssl/bn.h> | ||
20 | |||
21 | #include "../test_helper/test_helper.h" | ||
22 | |||
23 | #include "bitmap.h" | ||
24 | |||
25 | #define NTESTS 131 | ||
26 | |||
27 | void | ||
28 | tests(void) | ||
29 | { | ||
30 | struct bitmap *b; | ||
31 | BIGNUM *bn; | ||
32 | size_t len; | ||
33 | int i, j, k, n; | ||
34 | u_char bbuf[1024], bnbuf[1024]; | ||
35 | int r; | ||
36 | |||
37 | TEST_START("bitmap_new"); | ||
38 | b = bitmap_new(); | ||
39 | ASSERT_PTR_NE(b, NULL); | ||
40 | bn = BN_new(); | ||
41 | ASSERT_PTR_NE(bn, NULL); | ||
42 | TEST_DONE(); | ||
43 | |||
44 | TEST_START("bitmap_set_bit / bitmap_test_bit"); | ||
45 | for (i = -1; i < NTESTS; i++) { | ||
46 | for (j = -1; j < NTESTS; j++) { | ||
47 | for (k = -1; k < NTESTS; k++) { | ||
48 | bitmap_zero(b); | ||
49 | BN_clear(bn); | ||
50 | |||
51 | test_subtest_info("set %d/%d/%d", i, j, k); | ||
52 | /* Set bits */ | ||
53 | if (i >= 0) { | ||
54 | ASSERT_INT_EQ(bitmap_set_bit(b, i), 0); | ||
55 | ASSERT_INT_EQ(BN_set_bit(bn, i), 1); | ||
56 | } | ||
57 | if (j >= 0) { | ||
58 | ASSERT_INT_EQ(bitmap_set_bit(b, j), 0); | ||
59 | ASSERT_INT_EQ(BN_set_bit(bn, j), 1); | ||
60 | } | ||
61 | if (k >= 0) { | ||
62 | ASSERT_INT_EQ(bitmap_set_bit(b, k), 0); | ||
63 | ASSERT_INT_EQ(BN_set_bit(bn, k), 1); | ||
64 | } | ||
65 | |||
66 | /* Check perfect match between bitmap and bn */ | ||
67 | test_subtest_info("match %d/%d/%d", i, j, k); | ||
68 | for (n = 0; n < NTESTS; n++) { | ||
69 | ASSERT_INT_EQ(BN_is_bit_set(bn, n), | ||
70 | bitmap_test_bit(b, n)); | ||
71 | } | ||
72 | |||
73 | /* Test length calculations */ | ||
74 | test_subtest_info("length %d/%d/%d", i, j, k); | ||
75 | ASSERT_INT_EQ(BN_num_bits(bn), | ||
76 | (int)bitmap_nbits(b)); | ||
77 | ASSERT_INT_EQ(BN_num_bytes(bn), | ||
78 | (int)bitmap_nbytes(b)); | ||
79 | |||
80 | /* Test serialisation */ | ||
81 | test_subtest_info("serialise %d/%d/%d", | ||
82 | i, j, k); | ||
83 | len = bitmap_nbytes(b); | ||
84 | memset(bbuf, 0xfc, sizeof(bbuf)); | ||
85 | ASSERT_INT_EQ(bitmap_to_string(b, bbuf, | ||
86 | sizeof(bbuf)), 0); | ||
87 | for (n = len; n < (int)sizeof(bbuf); n++) | ||
88 | ASSERT_U8_EQ(bbuf[n], 0xfc); | ||
89 | r = BN_bn2bin(bn, bnbuf); | ||
90 | ASSERT_INT_GE(r, 0); | ||
91 | ASSERT_INT_EQ(r, (int)len); | ||
92 | ASSERT_MEM_EQ(bbuf, bnbuf, len); | ||
93 | |||
94 | /* Test deserialisation */ | ||
95 | test_subtest_info("deserialise %d/%d/%d", | ||
96 | i, j, k); | ||
97 | bitmap_zero(b); | ||
98 | ASSERT_INT_EQ(bitmap_from_string(b, bnbuf, | ||
99 | len), 0); | ||
100 | for (n = 0; n < NTESTS; n++) { | ||
101 | ASSERT_INT_EQ(BN_is_bit_set(bn, n), | ||
102 | bitmap_test_bit(b, n)); | ||
103 | } | ||
104 | |||
105 | /* Test clearing bits */ | ||
106 | test_subtest_info("clear %d/%d/%d", | ||
107 | i, j, k); | ||
108 | for (n = 0; n < NTESTS; n++) { | ||
109 | ASSERT_INT_EQ(bitmap_set_bit(b, n), 0); | ||
110 | ASSERT_INT_EQ(BN_set_bit(bn, n), 1); | ||
111 | } | ||
112 | if (i >= 0) { | ||
113 | bitmap_clear_bit(b, i); | ||
114 | BN_clear_bit(bn, i); | ||
115 | } | ||
116 | if (j >= 0) { | ||
117 | bitmap_clear_bit(b, j); | ||
118 | BN_clear_bit(bn, j); | ||
119 | } | ||
120 | if (k >= 0) { | ||
121 | bitmap_clear_bit(b, k); | ||
122 | BN_clear_bit(bn, k); | ||
123 | } | ||
124 | for (n = 0; n < NTESTS; n++) { | ||
125 | ASSERT_INT_EQ(BN_is_bit_set(bn, n), | ||
126 | bitmap_test_bit(b, n)); | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | bitmap_free(b); | ||
132 | BN_free(bn); | ||
133 | TEST_DONE(); | ||
134 | } | ||
135 | |||