Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 6 | #include <time.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <errno.h> |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 9 | |
| 10 | #include <osmocom/core/utils.h> |
| 11 | #include <osmocom/core/bitvec.h> |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 12 | #include <osmocom/core/bits.h> |
| 13 | |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 14 | static char lol[1024]; // we pollute this with printed vectors |
| 15 | static 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 | |
| 21 | static 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 | |
| 30 | static 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); |
Max | 6a5ef46 | 2016-02-24 16:05:48 +0100 | [diff] [blame] | 37 | printf(" -> %d (%u bit) ["OSMO_BIN_SPEC" "OSMO_BIN_SPEC"]:\n", x, n, OSMO_BIN_PRINT(tmp[0]), OSMO_BIN_PRINT(tmp[1])); |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 38 | bitvec_to_string_r(bv, lol); |
| 39 | printf("%s [%d]\n", lol, bv->cur_bit); |
| 40 | } |
| 41 | |
| 42 | static 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 | |
| 52 | static 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 | |
| 62 | static 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 Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 71 | |
| 72 | static 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 | |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 98 | printf("bitvec: %s\n", osmo_hexdump(bv.data, bv.data_len)); |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 99 | |
| 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 | |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 110 | printf("out: %s\n", osmo_hexdump(out, sizeof(out))); |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 111 | |
| 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 Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 120 | static void test_unhex(const char *hex) |
| 121 | { |
Holger Hans Peter Freyther | 5710804 | 2016-01-30 16:16:28 +0100 | [diff] [blame] | 122 | int rc; |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 123 | 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 Freyther | 5710804 | 2016-01-30 16:16:28 +0100 | [diff] [blame] | 128 | |
| 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 Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Max | d479321 | 2016-03-17 11:51:08 +0100 | [diff] [blame] | 135 | static 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 Kumarvel | 848de8f | 2016-12-02 15:13:03 +0530 | [diff] [blame] | 153 | static 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 | |
Max | d479321 | 2016-03-17 11:51:08 +0100 | [diff] [blame] | 165 | static 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 | |
Harald Welte | ae7966d | 2019-02-03 12:04:46 +0100 | [diff] [blame] | 184 | static void test_used_bytes() |
| 185 | { |
| 186 | struct bitvec b; |
| 187 | uint8_t d[32]; |
| 188 | unsigned int i; |
| 189 | |
| 190 | b.data = d; |
| 191 | b.data_len = sizeof(d); |
| 192 | bitvec_zero(&b); |
| 193 | |
| 194 | OSMO_ASSERT(bitvec_used_bytes(&b) == 0); |
| 195 | |
| 196 | for (i = 0; i < 8; i++) { |
| 197 | bitvec_set_bit(&b, 1); |
| 198 | OSMO_ASSERT(bitvec_used_bytes(&b) == 1); |
| 199 | } |
| 200 | |
| 201 | for (i = 8; i < 16; i++) { |
| 202 | bitvec_set_bit(&b, 1); |
| 203 | OSMO_ASSERT(bitvec_used_bytes(&b) == 2); |
| 204 | } |
| 205 | } |
| 206 | |
Harald Welte | b0708d3 | 2019-02-04 12:09:05 +0100 | [diff] [blame] | 207 | static void test_tailroom() |
| 208 | { |
| 209 | struct bitvec b; |
| 210 | uint8_t d[32]; |
| 211 | unsigned int i; |
| 212 | |
| 213 | b.data = d; |
| 214 | b.data_len = sizeof(d); |
| 215 | bitvec_zero(&b); |
| 216 | |
| 217 | OSMO_ASSERT(bitvec_tailroom_bits(&b) == sizeof(d)*8); |
| 218 | |
| 219 | for (i = 0; i < 8*sizeof(d); i++) { |
| 220 | bitvec_set_bit(&b, 1); |
| 221 | OSMO_ASSERT(bitvec_tailroom_bits(&b) == sizeof(d)*8-(i+1)); |
| 222 | } |
| 223 | } |
| 224 | |
Vadim Yanitskiy | 6e270e2 | 2020-02-19 05:55:49 +0700 | [diff] [blame] | 225 | static void test_bitvec_read_field(void) |
| 226 | { |
| 227 | uint8_t data[8] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xeb, 0xda, 0xed }; |
| 228 | struct bitvec bv = { |
| 229 | .data_len = sizeof(data), |
| 230 | .data = data, |
| 231 | .cur_bit = 0, |
| 232 | }; |
| 233 | |
| 234 | unsigned int readIndex; |
| 235 | uint64_t field; |
| 236 | |
| 237 | #define _bitvec_read_field(idx, len) \ |
| 238 | readIndex = idx; \ |
| 239 | field = bitvec_read_field(&bv, &readIndex, len); \ |
| 240 | printf("bitvec_read_field(idx=%u, len=%u) => %" PRIx64 "\n", idx, len, field); |
| 241 | |
| 242 | _bitvec_read_field(0, 64); |
| 243 | _bitvec_read_field(0, 32); |
| 244 | _bitvec_read_field(0, 16); |
| 245 | _bitvec_read_field(0, 8); |
| 246 | _bitvec_read_field(0, 0); |
| 247 | |
| 248 | _bitvec_read_field(8, 8); |
| 249 | _bitvec_read_field(8, 4); |
| 250 | _bitvec_read_field(8, 0); |
| 251 | |
| 252 | _bitvec_read_field(10, 9); |
| 253 | _bitvec_read_field(10, 7); |
| 254 | _bitvec_read_field(10, 5); |
| 255 | _bitvec_read_field(10, 3); |
| 256 | _bitvec_read_field(10, 1); |
| 257 | |
| 258 | /* Out of bounds (see OS#4388) */ |
| 259 | _bitvec_read_field(8 * 8 * 8, 16); /* index too far */ |
| 260 | _bitvec_read_field(0, 8 * 8 + 1); /* too many bits */ |
| 261 | _bitvec_read_field(8 * 8, 16); /* 16 bits past */ |
| 262 | } |
| 263 | |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 264 | int main(int argc, char **argv) |
| 265 | { |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 266 | struct bitvec bv; |
| 267 | uint8_t i = 8, test[i]; |
| 268 | |
| 269 | memset(test, 0, i); |
| 270 | bv.data_len = i; |
| 271 | bv.data = test; |
| 272 | bv.cur_bit = 0; |
| 273 | |
| 274 | printf("test shifting...\n"); |
| 275 | |
| 276 | bitvec_set_uint(&bv, 0x0E, 7); |
| 277 | test_shift(&bv, 3); |
| 278 | test_shift(&bv, 17); |
| 279 | bitvec_set_uint(&bv, 0, 32); |
| 280 | bitvec_set_uint(&bv, 0x0A, 7); |
| 281 | test_shift(&bv, 24); |
| 282 | |
| 283 | printf("checking RL functions...\n"); |
| 284 | |
| 285 | bitvec_zero(&bv); |
| 286 | test_rl(&bv); |
| 287 | bitvec_set_uint(&bv, 0x000F, 32); |
| 288 | test_rl(&bv); |
| 289 | bitvec_shiftl(&bv, 18); |
| 290 | test_rl(&bv); |
| 291 | bitvec_set_uint(&bv, 0x0F, 8); |
| 292 | test_rl(&bv); |
| 293 | bitvec_zero(&bv); |
| 294 | bitvec_set_uint(&bv, 0xFF, 8); |
| 295 | test_rl(&bv); |
| 296 | bitvec_set_uint(&bv, 0xFE, 7); |
| 297 | test_rl(&bv); |
| 298 | bitvec_set_uint(&bv, 0, 17); |
| 299 | test_rl(&bv); |
| 300 | bitvec_shiftl(&bv, 18); |
| 301 | test_rl(&bv); |
| 302 | |
| 303 | printf("probing bit access...\n"); |
| 304 | |
| 305 | bitvec_zero(&bv); |
| 306 | bitvec_set_uint(&bv, 0x3747817, 32); |
| 307 | bitvec_shiftl(&bv, 10); |
| 308 | |
| 309 | test_get(&bv, 2); |
| 310 | test_get(&bv, 7); |
| 311 | test_get(&bv, 9); |
| 312 | test_get(&bv, 13); |
| 313 | test_get(&bv, 16); |
| 314 | test_get(&bv, 42); |
| 315 | |
| 316 | printf("feeling bit fills...\n"); |
| 317 | |
| 318 | test_set(&bv, ONE); |
| 319 | test_fill(&bv, 3, ZERO); |
| 320 | test_spare(&bv, 38); |
| 321 | test_spare(&bv, 43); |
| 322 | test_spare(&bv, 1); |
| 323 | test_spare(&bv, 7); |
| 324 | test_fill(&bv, 5, ONE); |
| 325 | test_fill(&bv, 3, L); |
| 326 | |
| 327 | printf("byte me...\n"); |
| 328 | |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 329 | test_byte_ops(); |
Holger Hans Peter Freyther | a9301a1 | 2016-01-30 10:54:43 +0100 | [diff] [blame] | 330 | test_unhex("48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b"); |
| 331 | test_unhex("47240c00400000000000000079eb2ac9402b2b2b2b2b2b"); |
| 332 | test_unhex("47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b"); |
| 333 | test_unhex("DEADFACE000000000000000000000000000000BEEFFEED"); |
| 334 | test_unhex("FFFFFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); |
Max | 0a59e98 | 2016-02-05 13:55:37 +0100 | [diff] [blame] | 335 | |
Max | d479321 | 2016-03-17 11:51:08 +0100 | [diff] [blame] | 336 | printf("arrr...\n"); |
| 337 | |
| 338 | test_array(); |
| 339 | |
Pravin Kumarvel | 848de8f | 2016-12-02 15:13:03 +0530 | [diff] [blame] | 340 | printf("\nbitvec_runlength....\n"); |
Max | d479321 | 2016-03-17 11:51:08 +0100 | [diff] [blame] | 341 | |
Pravin Kumarvel | 848de8f | 2016-12-02 15:13:03 +0530 | [diff] [blame] | 342 | bitvec_zero(&bv); |
| 343 | bitvec_set_uint(&bv, 0xff, 8); |
| 344 | bv.cur_bit -= 8; |
| 345 | test_bitvec_rl_curbit(&bv, 1, 64, 8); |
| 346 | |
| 347 | bitvec_zero(&bv); |
| 348 | bitvec_set_uint(&bv, 0xfc, 8); |
| 349 | bv.cur_bit -= 8; |
| 350 | test_bitvec_rl_curbit(&bv, 1, 64, 6); |
| 351 | |
| 352 | bitvec_zero(&bv); |
| 353 | test_bitvec_rl_curbit(&bv, 0, 52, 52); |
| 354 | |
| 355 | bitvec_zero(&bv); |
| 356 | bitvec_set_uint(&bv, 0xfc, 8); |
| 357 | bv.cur_bit -= 2; |
| 358 | test_bitvec_rl_curbit(&bv, 0, 64, 58); |
| 359 | |
| 360 | bitvec_zero(&bv); |
| 361 | bitvec_set_uint(&bv, 0x07, 8); |
| 362 | bitvec_set_uint(&bv, 0xf8, 8); |
| 363 | bv.cur_bit -= 11; |
| 364 | test_bitvec_rl_curbit(&bv, 1, 64, 8); |
| 365 | |
| 366 | bitvec_zero(&bv); |
| 367 | test_bitvec_rl_curbit(&bv, 1, 64, 0); |
| 368 | |
Harald Welte | ae7966d | 2019-02-03 12:04:46 +0100 | [diff] [blame] | 369 | printf("\nbitvec bytes used.\n"); |
| 370 | test_used_bytes(); |
Harald Welte | b0708d3 | 2019-02-04 12:09:05 +0100 | [diff] [blame] | 371 | test_tailroom(); |
Harald Welte | ae7966d | 2019-02-03 12:04:46 +0100 | [diff] [blame] | 372 | |
Vadim Yanitskiy | 6e270e2 | 2020-02-19 05:55:49 +0700 | [diff] [blame] | 373 | printf("\ntest bitvec_read_field():\n"); |
| 374 | test_bitvec_read_field(); |
| 375 | |
Pravin Kumarvel | 848de8f | 2016-12-02 15:13:03 +0530 | [diff] [blame] | 376 | printf("\nbitvec ok.\n"); |
Jacob Erlbeck | 5f349be | 2015-12-21 16:04:03 +0100 | [diff] [blame] | 377 | return 0; |
| 378 | } |