blob: d0bc30c436fedb976ff9f290e56a6f7d233afb17 [file] [log] [blame]
Jacob Erlbeck5f349be2015-12-21 16:04:03 +01001#include <inttypes.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <string.h>
Max0a59e982016-02-05 13:55:37 +01006#include <time.h>
7#include <stdbool.h>
8#include <errno.h>
Jacob Erlbeck5f349be2015-12-21 16:04:03 +01009
10#include <osmocom/core/utils.h>
11#include <osmocom/core/bitvec.h>
Max0a59e982016-02-05 13:55:37 +010012#include <osmocom/core/bits.h>
13
Max0a59e982016-02-05 13:55:37 +010014static char lol[1024]; // we pollute this with printed vectors
15static inline void test_rl(const struct bitvec *bv)
16{
17 bitvec_to_string_r(bv, lol);
18 printf("%s [%d] RL0=%d, RL1=%d\n", lol, bv->cur_bit, bitvec_rl(bv, false), bitvec_rl(bv, true));
19}
20
21static inline void test_shift(struct bitvec *bv, unsigned n)
22{
23 bitvec_to_string_r(bv, lol);
24 printf("%s << %d:\n", lol, n);
25 bitvec_shiftl(bv, n);
26 bitvec_to_string_r(bv, lol);
27 printf("%s\n", lol);
28}
29
30static inline void test_get(struct bitvec *bv, unsigned n)
31{
32 bitvec_to_string_r(bv, lol);
33 printf("%s [%d]", lol, bv->cur_bit);
34 int16_t x = bitvec_get_int16_msb(bv, n);
35 uint8_t tmp[2];
36 osmo_store16be(x, &tmp);
Max6a5ef462016-02-24 16:05:48 +010037 printf(" -> %d (%u bit) ["OSMO_BIN_SPEC" "OSMO_BIN_SPEC"]:\n", x, n, OSMO_BIN_PRINT(tmp[0]), OSMO_BIN_PRINT(tmp[1]));
Max0a59e982016-02-05 13:55:37 +010038 bitvec_to_string_r(bv, lol);
39 printf("%s [%d]\n", lol, bv->cur_bit);
40}
41
42static inline void test_fill(struct bitvec *bv, unsigned n, enum bit_value val)
43{
44 bitvec_to_string_r(bv, lol);
45 unsigned bvlen = bv->cur_bit;
46 int fi = bitvec_fill(bv, n, val);
47 printf("%c> FILL %s [%d] -%d-> [%d]:\n", bit_value_to_char(val), lol, bvlen, n, fi);
48 bitvec_to_string_r(bv, lol);
49 printf(" %s [%d]\n\n", lol, bv->cur_bit);
50}
51
52static inline void test_spare(struct bitvec *bv, unsigned n)
53{
54 bitvec_to_string_r(bv, lol);
55 unsigned bvlen = bv->cur_bit;
56 int sp = bitvec_spare_padding(bv, n);
57 printf("%c> SPARE %s [%d] -%d-> [%d]:\n", bit_value_to_char(L), lol, bvlen, n, sp);
58 bitvec_to_string_r(bv, lol);
59 printf(" %s [%d]\n\n", lol, bv->cur_bit);
60}
61
62static inline void test_set(struct bitvec *bv, enum bit_value bit)
63{
64 bitvec_to_string_r(bv, lol);
65 unsigned bvlen = bv->cur_bit;
66 int set = bitvec_set_bit(bv, bit);
67 printf("%c> SET %s [%d] ++> [%d]:\n", bit_value_to_char(bit), lol, bvlen, set);
68 bitvec_to_string_r(bv, lol);
69 printf(" %s [%d]\n\n", lol, bv->cur_bit);
70}
Jacob Erlbeck5f349be2015-12-21 16:04:03 +010071
72static void test_byte_ops()
73{
74 struct bitvec bv;
75 const uint8_t *in = (const uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
76 uint8_t out[26 + 2];
77 uint8_t data[64];
78 int i;
79 int rc;
80 int in_size = strlen((const char *)in);
81
82 printf("=== start %s ===\n", __func__);
83
84 bv.data = data;
85 bv.data_len = sizeof(data);
86
87 for (i = 0; i < 32; i++) {
88 /* Write to bitvec */
89 memset(data, 0x00, sizeof(data));
90 bv.cur_bit = i;
91 rc = bitvec_set_uint(&bv, 0x7e, 8);
92 OSMO_ASSERT(rc >= 0);
93 rc = bitvec_set_bytes(&bv, in, in_size);
94 OSMO_ASSERT(rc >= 0);
95 rc = bitvec_set_uint(&bv, 0x7e, 8);
96 OSMO_ASSERT(rc >= 0);
97
Max0a59e982016-02-05 13:55:37 +010098 printf("bitvec: %s\n", osmo_hexdump(bv.data, bv.data_len));
Jacob Erlbeck5f349be2015-12-21 16:04:03 +010099
100 /* Read from bitvec */
101 memset(out, 0xff, sizeof(out));
102 bv.cur_bit = i;
103 rc = bitvec_get_uint(&bv, 8);
104 OSMO_ASSERT(rc == 0x7e);
105 rc = bitvec_get_bytes(&bv, out + 1, in_size);
106 OSMO_ASSERT(rc >= 0);
107 rc = bitvec_get_uint(&bv, 8);
108 OSMO_ASSERT(rc == 0x7e);
109
Max0a59e982016-02-05 13:55:37 +0100110 printf("out: %s\n", osmo_hexdump(out, sizeof(out)));
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100111
112 OSMO_ASSERT(out[0] == 0xff);
113 OSMO_ASSERT(out[in_size+1] == 0xff);
114 OSMO_ASSERT(memcmp(in, out + 1, in_size) == 0);
115 }
116
117 printf("=== end %s ===\n", __func__);
118}
119
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100120static void test_unhex(const char *hex)
121{
Holger Hans Peter Freyther57108042016-01-30 16:16:28 +0100122 int rc;
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100123 struct bitvec b;
124 uint8_t d[64] = {0};
125 b.data = d;
126 b.data_len = sizeof(d);
127 b.cur_bit = 0;
Holger Hans Peter Freyther57108042016-01-30 16:16:28 +0100128
129 rc = bitvec_unhex(&b, hex);
130 printf("%d -=> cur_bit=%u\n", rc, b.cur_bit);
131 printf("%s\n", osmo_hexdump_nospc(d, 64));
132 printf("%s\n", hex);
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100133}
134
Maxd4793212016-03-17 11:51:08 +0100135static inline void test_array_item(unsigned t, struct bitvec *b, unsigned int n,
136 uint32_t *array, unsigned int p)
137{
138 unsigned int i, x, y;
139 bitvec_zero(b);
140 x = b->cur_bit;
141 i = bitvec_add_array(b, array, n, true, t);
142 y = b->cur_bit;
143 bitvec_add_array(b, array, n, false, t);
144 printf("\nbits: %u, est: %u, real: %u, x: %u, y: %u\n",
145 t, i, b->cur_bit, x, y);
146 for (i = 0; i < p; i++) {
147 printf(OSMO_BIT_SPEC " ", OSMO_BIT_PRINT(b->data[i]));
148 if (0 == (i + 1) % 15)
149 printf("\n");
150 }
151}
152
Pravin Kumarvel848de8f2016-12-02 15:13:03 +0530153static inline void test_bitvec_rl_curbit(struct bitvec *bv, bool b, int max_bits,
154 int result )
155{
156 int num = 0;
157 int readIndex = bv->cur_bit;
158 OSMO_ASSERT(bv->cur_bit < max_bits);
159 num = bitvec_rl_curbit(bv, b, max_bits);
160 readIndex += num;
161 OSMO_ASSERT(bv->cur_bit == readIndex);
162 OSMO_ASSERT(num == result);
163}
164
Maxd4793212016-03-17 11:51:08 +0100165static void test_array()
166{
167 struct bitvec b;
168 uint8_t d[4096];
169 b.data = d;
170 b.data_len = sizeof(d);
171
172 unsigned int i, n = 64;
173 uint32_t array[n];
174 for (i = 0; i < n; i++) {
175 array[i] = i * i * i + i;
176 printf("0x%x ", array[i]);
177 }
178
179 test_array_item(3, &b, n, array, n);
180 test_array_item(9, &b, n, array, n * 2);
181 test_array_item(17, &b, n, array, n * 3);
182}
183
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100184int main(int argc, char **argv)
185{
Max0a59e982016-02-05 13:55:37 +0100186 struct bitvec bv;
187 uint8_t i = 8, test[i];
188
189 memset(test, 0, i);
190 bv.data_len = i;
191 bv.data = test;
192 bv.cur_bit = 0;
193
194 printf("test shifting...\n");
195
196 bitvec_set_uint(&bv, 0x0E, 7);
197 test_shift(&bv, 3);
198 test_shift(&bv, 17);
199 bitvec_set_uint(&bv, 0, 32);
200 bitvec_set_uint(&bv, 0x0A, 7);
201 test_shift(&bv, 24);
202
203 printf("checking RL functions...\n");
204
205 bitvec_zero(&bv);
206 test_rl(&bv);
207 bitvec_set_uint(&bv, 0x000F, 32);
208 test_rl(&bv);
209 bitvec_shiftl(&bv, 18);
210 test_rl(&bv);
211 bitvec_set_uint(&bv, 0x0F, 8);
212 test_rl(&bv);
213 bitvec_zero(&bv);
214 bitvec_set_uint(&bv, 0xFF, 8);
215 test_rl(&bv);
216 bitvec_set_uint(&bv, 0xFE, 7);
217 test_rl(&bv);
218 bitvec_set_uint(&bv, 0, 17);
219 test_rl(&bv);
220 bitvec_shiftl(&bv, 18);
221 test_rl(&bv);
222
223 printf("probing bit access...\n");
224
225 bitvec_zero(&bv);
226 bitvec_set_uint(&bv, 0x3747817, 32);
227 bitvec_shiftl(&bv, 10);
228
229 test_get(&bv, 2);
230 test_get(&bv, 7);
231 test_get(&bv, 9);
232 test_get(&bv, 13);
233 test_get(&bv, 16);
234 test_get(&bv, 42);
235
236 printf("feeling bit fills...\n");
237
238 test_set(&bv, ONE);
239 test_fill(&bv, 3, ZERO);
240 test_spare(&bv, 38);
241 test_spare(&bv, 43);
242 test_spare(&bv, 1);
243 test_spare(&bv, 7);
244 test_fill(&bv, 5, ONE);
245 test_fill(&bv, 3, L);
246
247 printf("byte me...\n");
248
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100249 test_byte_ops();
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100250 test_unhex("48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b");
251 test_unhex("47240c00400000000000000079eb2ac9402b2b2b2b2b2b");
252 test_unhex("47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b");
253 test_unhex("DEADFACE000000000000000000000000000000BEEFFEED");
254 test_unhex("FFFFFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
Max0a59e982016-02-05 13:55:37 +0100255
Maxd4793212016-03-17 11:51:08 +0100256 printf("arrr...\n");
257
258 test_array();
259
Pravin Kumarvel848de8f2016-12-02 15:13:03 +0530260 printf("\nbitvec_runlength....\n");
Maxd4793212016-03-17 11:51:08 +0100261
Pravin Kumarvel848de8f2016-12-02 15:13:03 +0530262 bitvec_zero(&bv);
263 bitvec_set_uint(&bv, 0xff, 8);
264 bv.cur_bit -= 8;
265 test_bitvec_rl_curbit(&bv, 1, 64, 8);
266
267 bitvec_zero(&bv);
268 bitvec_set_uint(&bv, 0xfc, 8);
269 bv.cur_bit -= 8;
270 test_bitvec_rl_curbit(&bv, 1, 64, 6);
271
272 bitvec_zero(&bv);
273 test_bitvec_rl_curbit(&bv, 0, 52, 52);
274
275 bitvec_zero(&bv);
276 bitvec_set_uint(&bv, 0xfc, 8);
277 bv.cur_bit -= 2;
278 test_bitvec_rl_curbit(&bv, 0, 64, 58);
279
280 bitvec_zero(&bv);
281 bitvec_set_uint(&bv, 0x07, 8);
282 bitvec_set_uint(&bv, 0xf8, 8);
283 bv.cur_bit -= 11;
284 test_bitvec_rl_curbit(&bv, 1, 64, 8);
285
286 bitvec_zero(&bv);
287 test_bitvec_rl_curbit(&bv, 1, 64, 0);
288
289 printf("\nbitvec ok.\n");
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100290 return 0;
291}