diff options
author | djm@openbsd.org <djm@openbsd.org> | 2015-01-15 07:36:28 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2015-01-15 21:39:18 +1100 |
commit | d333f89abf7179021e5c3f28673f469abe032062 (patch) | |
tree | 8399e567e65d2d0c241f344c09b4ed1eb2cfbae3 /regress/unittests/bitmap/tests.c | |
parent | 7613f828f49c55ff356007ae9645038ab6682556 (diff) |
upstream commit
unit tests for KRL bitmap
Diffstat (limited to 'regress/unittests/bitmap/tests.c')
-rw-r--r-- | regress/unittests/bitmap/tests.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/regress/unittests/bitmap/tests.c b/regress/unittests/bitmap/tests.c new file mode 100644 index 000000000..6789661ce --- /dev/null +++ b/regress/unittests/bitmap/tests.c | |||
@@ -0,0 +1,131 @@ | |||
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 <sys/types.h> | ||
9 | #include <sys/param.h> | ||
10 | #include <stdio.h> | ||
11 | #include <stdint.h> | ||
12 | #include <stdlib.h> | ||
13 | #include <string.h> | ||
14 | |||
15 | #include <openssl/bn.h> | ||
16 | |||
17 | #include "test_helper.h" | ||
18 | |||
19 | #include "bitmap.h" | ||
20 | |||
21 | #define NTESTS 131 | ||
22 | |||
23 | void | ||
24 | tests(void) | ||
25 | { | ||
26 | struct bitmap *b; | ||
27 | BIGNUM *bn; | ||
28 | size_t len; | ||
29 | int i, j, k, n; | ||
30 | u_char bbuf[1024], bnbuf[1024]; | ||
31 | int r; | ||
32 | |||
33 | TEST_START("bitmap_new"); | ||
34 | b = bitmap_new(); | ||
35 | ASSERT_PTR_NE(b, NULL); | ||
36 | bn = BN_new(); | ||
37 | ASSERT_PTR_NE(bn, NULL); | ||
38 | TEST_DONE(); | ||
39 | |||
40 | TEST_START("bitmap_set_bit / bitmap_test_bit"); | ||
41 | for (i = -1; i < NTESTS; i++) { | ||
42 | for (j = -1; j < NTESTS; j++) { | ||
43 | for (k = -1; k < NTESTS; k++) { | ||
44 | bitmap_zero(b); | ||
45 | BN_clear(bn); | ||
46 | |||
47 | test_subtest_info("set %d/%d/%d", i, j, k); | ||
48 | /* Set bits */ | ||
49 | if (i >= 0) { | ||
50 | ASSERT_INT_EQ(bitmap_set_bit(b, i), 0); | ||
51 | ASSERT_INT_EQ(BN_set_bit(bn, i), 1); | ||
52 | } | ||
53 | if (j >= 0) { | ||
54 | ASSERT_INT_EQ(bitmap_set_bit(b, j), 0); | ||
55 | ASSERT_INT_EQ(BN_set_bit(bn, j), 1); | ||
56 | } | ||
57 | if (k >= 0) { | ||
58 | ASSERT_INT_EQ(bitmap_set_bit(b, k), 0); | ||
59 | ASSERT_INT_EQ(BN_set_bit(bn, k), 1); | ||
60 | } | ||
61 | |||
62 | /* Check perfect match between bitmap and bn */ | ||
63 | test_subtest_info("match %d/%d/%d", i, j, k); | ||
64 | for (n = 0; n < NTESTS; n++) { | ||
65 | ASSERT_INT_EQ(BN_is_bit_set(bn, n), | ||
66 | bitmap_test_bit(b, n)); | ||
67 | } | ||
68 | |||
69 | /* Test length calculations */ | ||
70 | test_subtest_info("length %d/%d/%d", i, j, k); | ||
71 | ASSERT_INT_EQ(BN_num_bits(bn), | ||
72 | (int)bitmap_nbits(b)); | ||
73 | ASSERT_INT_EQ(BN_num_bytes(bn), | ||
74 | (int)bitmap_nbytes(b)); | ||
75 | |||
76 | /* Test serialisation */ | ||
77 | test_subtest_info("serialise %d/%d/%d", | ||
78 | i, j, k); | ||
79 | len = bitmap_nbytes(b); | ||
80 | memset(bbuf, 0xfc, sizeof(bbuf)); | ||
81 | ASSERT_INT_EQ(bitmap_to_string(b, bbuf, | ||
82 | sizeof(bbuf)), 0); | ||
83 | for (n = len; n < (int)sizeof(bbuf); n++) | ||
84 | ASSERT_U8_EQ(bbuf[n], 0xfc); | ||
85 | r = BN_bn2bin(bn, bnbuf); | ||
86 | ASSERT_INT_GE(r, 0); | ||
87 | ASSERT_INT_EQ(r, (int)len); | ||
88 | ASSERT_MEM_EQ(bbuf, bnbuf, len); | ||
89 | |||
90 | /* Test deserialisation */ | ||
91 | test_subtest_info("deserialise %d/%d/%d", | ||
92 | i, j, k); | ||
93 | bitmap_zero(b); | ||
94 | ASSERT_INT_EQ(bitmap_from_string(b, bnbuf, | ||
95 | len), 0); | ||
96 | for (n = 0; n < NTESTS; n++) { | ||
97 | ASSERT_INT_EQ(BN_is_bit_set(bn, n), | ||
98 | bitmap_test_bit(b, n)); | ||
99 | } | ||
100 | |||
101 | /* Test clearing bits */ | ||
102 | test_subtest_info("clear %d/%d/%d", | ||
103 | i, j, k); | ||
104 | for (n = 0; n < NTESTS; n++) { | ||
105 | ASSERT_INT_EQ(bitmap_set_bit(b, n), 0); | ||
106 | ASSERT_INT_EQ(BN_set_bit(bn, n), 1); | ||
107 | } | ||
108 | if (i >= 0) { | ||
109 | bitmap_clear_bit(b, i); | ||
110 | BN_clear_bit(bn, i); | ||
111 | } | ||
112 | if (j >= 0) { | ||
113 | bitmap_clear_bit(b, j); | ||
114 | BN_clear_bit(bn, j); | ||
115 | } | ||
116 | if (k >= 0) { | ||
117 | bitmap_clear_bit(b, k); | ||
118 | BN_clear_bit(bn, k); | ||
119 | } | ||
120 | for (n = 0; n < NTESTS; n++) { | ||
121 | ASSERT_INT_EQ(BN_is_bit_set(bn, n), | ||
122 | bitmap_test_bit(b, n)); | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | bitmap_free(b); | ||
128 | BN_free(bn); | ||
129 | TEST_DONE(); | ||
130 | } | ||
131 | |||