blob: a98a91c695be366ecd913a549c544e20546c43cc [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
153static void test_array()
154{
155 struct bitvec b;
156 uint8_t d[4096];
157 b.data = d;
158 b.data_len = sizeof(d);
159
160 unsigned int i, n = 64;
161 uint32_t array[n];
162 for (i = 0; i < n; i++) {
163 array[i] = i * i * i + i;
164 printf("0x%x ", array[i]);
165 }
166
167 test_array_item(3, &b, n, array, n);
168 test_array_item(9, &b, n, array, n * 2);
169 test_array_item(17, &b, n, array, n * 3);
170}
171
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100172int main(int argc, char **argv)
173{
Max0a59e982016-02-05 13:55:37 +0100174 struct bitvec bv;
175 uint8_t i = 8, test[i];
176
177 memset(test, 0, i);
178 bv.data_len = i;
179 bv.data = test;
180 bv.cur_bit = 0;
181
182 printf("test shifting...\n");
183
184 bitvec_set_uint(&bv, 0x0E, 7);
185 test_shift(&bv, 3);
186 test_shift(&bv, 17);
187 bitvec_set_uint(&bv, 0, 32);
188 bitvec_set_uint(&bv, 0x0A, 7);
189 test_shift(&bv, 24);
190
191 printf("checking RL functions...\n");
192
193 bitvec_zero(&bv);
194 test_rl(&bv);
195 bitvec_set_uint(&bv, 0x000F, 32);
196 test_rl(&bv);
197 bitvec_shiftl(&bv, 18);
198 test_rl(&bv);
199 bitvec_set_uint(&bv, 0x0F, 8);
200 test_rl(&bv);
201 bitvec_zero(&bv);
202 bitvec_set_uint(&bv, 0xFF, 8);
203 test_rl(&bv);
204 bitvec_set_uint(&bv, 0xFE, 7);
205 test_rl(&bv);
206 bitvec_set_uint(&bv, 0, 17);
207 test_rl(&bv);
208 bitvec_shiftl(&bv, 18);
209 test_rl(&bv);
210
211 printf("probing bit access...\n");
212
213 bitvec_zero(&bv);
214 bitvec_set_uint(&bv, 0x3747817, 32);
215 bitvec_shiftl(&bv, 10);
216
217 test_get(&bv, 2);
218 test_get(&bv, 7);
219 test_get(&bv, 9);
220 test_get(&bv, 13);
221 test_get(&bv, 16);
222 test_get(&bv, 42);
223
224 printf("feeling bit fills...\n");
225
226 test_set(&bv, ONE);
227 test_fill(&bv, 3, ZERO);
228 test_spare(&bv, 38);
229 test_spare(&bv, 43);
230 test_spare(&bv, 1);
231 test_spare(&bv, 7);
232 test_fill(&bv, 5, ONE);
233 test_fill(&bv, 3, L);
234
235 printf("byte me...\n");
236
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100237 test_byte_ops();
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100238 test_unhex("48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b");
239 test_unhex("47240c00400000000000000079eb2ac9402b2b2b2b2b2b");
240 test_unhex("47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b");
241 test_unhex("DEADFACE000000000000000000000000000000BEEFFEED");
242 test_unhex("FFFFFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
Max0a59e982016-02-05 13:55:37 +0100243
Maxd4793212016-03-17 11:51:08 +0100244 printf("arrr...\n");
245
246 test_array();
247
248 printf("\nbitvec ok.\n");
249
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100250 return 0;
251}