blob: 800a040647798ce14cf014397500fbb095003b23 [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
14#define BIN_PATTERN "%d%d%d%d%d%d%d%d"
15#define BIN(byte) \
16 (byte & 0x80 ? 1 : 0), \
17 (byte & 0x40 ? 1 : 0), \
18 (byte & 0x20 ? 1 : 0), \
19 (byte & 0x10 ? 1 : 0), \
20 (byte & 0x08 ? 1 : 0), \
21 (byte & 0x04 ? 1 : 0), \
22 (byte & 0x02 ? 1 : 0), \
23 (byte & 0x01 ? 1 : 0)
24
25static char lol[1024]; // we pollute this with printed vectors
26static inline void test_rl(const struct bitvec *bv)
27{
28 bitvec_to_string_r(bv, lol);
29 printf("%s [%d] RL0=%d, RL1=%d\n", lol, bv->cur_bit, bitvec_rl(bv, false), bitvec_rl(bv, true));
30}
31
32static inline void test_shift(struct bitvec *bv, unsigned n)
33{
34 bitvec_to_string_r(bv, lol);
35 printf("%s << %d:\n", lol, n);
36 bitvec_shiftl(bv, n);
37 bitvec_to_string_r(bv, lol);
38 printf("%s\n", lol);
39}
40
41static inline void test_get(struct bitvec *bv, unsigned n)
42{
43 bitvec_to_string_r(bv, lol);
44 printf("%s [%d]", lol, bv->cur_bit);
45 int16_t x = bitvec_get_int16_msb(bv, n);
46 uint8_t tmp[2];
47 osmo_store16be(x, &tmp);
48 printf(" -> %d (%u bit) ["BIN_PATTERN" "BIN_PATTERN"]:\n", x, n, BIN(tmp[0]), BIN(tmp[1]));
49 bitvec_to_string_r(bv, lol);
50 printf("%s [%d]\n", lol, bv->cur_bit);
51}
52
53static inline void test_fill(struct bitvec *bv, unsigned n, enum bit_value val)
54{
55 bitvec_to_string_r(bv, lol);
56 unsigned bvlen = bv->cur_bit;
57 int fi = bitvec_fill(bv, n, val);
58 printf("%c> FILL %s [%d] -%d-> [%d]:\n", bit_value_to_char(val), lol, bvlen, n, fi);
59 bitvec_to_string_r(bv, lol);
60 printf(" %s [%d]\n\n", lol, bv->cur_bit);
61}
62
63static inline void test_spare(struct bitvec *bv, unsigned n)
64{
65 bitvec_to_string_r(bv, lol);
66 unsigned bvlen = bv->cur_bit;
67 int sp = bitvec_spare_padding(bv, n);
68 printf("%c> SPARE %s [%d] -%d-> [%d]:\n", bit_value_to_char(L), lol, bvlen, n, sp);
69 bitvec_to_string_r(bv, lol);
70 printf(" %s [%d]\n\n", lol, bv->cur_bit);
71}
72
73static inline void test_set(struct bitvec *bv, enum bit_value bit)
74{
75 bitvec_to_string_r(bv, lol);
76 unsigned bvlen = bv->cur_bit;
77 int set = bitvec_set_bit(bv, bit);
78 printf("%c> SET %s [%d] ++> [%d]:\n", bit_value_to_char(bit), lol, bvlen, set);
79 bitvec_to_string_r(bv, lol);
80 printf(" %s [%d]\n\n", lol, bv->cur_bit);
81}
Jacob Erlbeck5f349be2015-12-21 16:04:03 +010082
83static void test_byte_ops()
84{
85 struct bitvec bv;
86 const uint8_t *in = (const uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
87 uint8_t out[26 + 2];
88 uint8_t data[64];
89 int i;
90 int rc;
91 int in_size = strlen((const char *)in);
92
93 printf("=== start %s ===\n", __func__);
94
95 bv.data = data;
96 bv.data_len = sizeof(data);
97
98 for (i = 0; i < 32; i++) {
99 /* Write to bitvec */
100 memset(data, 0x00, sizeof(data));
101 bv.cur_bit = i;
102 rc = bitvec_set_uint(&bv, 0x7e, 8);
103 OSMO_ASSERT(rc >= 0);
104 rc = bitvec_set_bytes(&bv, in, in_size);
105 OSMO_ASSERT(rc >= 0);
106 rc = bitvec_set_uint(&bv, 0x7e, 8);
107 OSMO_ASSERT(rc >= 0);
108
Max0a59e982016-02-05 13:55:37 +0100109 printf("bitvec: %s\n", osmo_hexdump(bv.data, bv.data_len));
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100110
111 /* Read from bitvec */
112 memset(out, 0xff, sizeof(out));
113 bv.cur_bit = i;
114 rc = bitvec_get_uint(&bv, 8);
115 OSMO_ASSERT(rc == 0x7e);
116 rc = bitvec_get_bytes(&bv, out + 1, in_size);
117 OSMO_ASSERT(rc >= 0);
118 rc = bitvec_get_uint(&bv, 8);
119 OSMO_ASSERT(rc == 0x7e);
120
Max0a59e982016-02-05 13:55:37 +0100121 printf("out: %s\n", osmo_hexdump(out, sizeof(out)));
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100122
123 OSMO_ASSERT(out[0] == 0xff);
124 OSMO_ASSERT(out[in_size+1] == 0xff);
125 OSMO_ASSERT(memcmp(in, out + 1, in_size) == 0);
126 }
127
128 printf("=== end %s ===\n", __func__);
129}
130
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100131static void test_unhex(const char *hex)
132{
Holger Hans Peter Freyther57108042016-01-30 16:16:28 +0100133 int rc;
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100134 struct bitvec b;
135 uint8_t d[64] = {0};
136 b.data = d;
137 b.data_len = sizeof(d);
138 b.cur_bit = 0;
Holger Hans Peter Freyther57108042016-01-30 16:16:28 +0100139
140 rc = bitvec_unhex(&b, hex);
141 printf("%d -=> cur_bit=%u\n", rc, b.cur_bit);
142 printf("%s\n", osmo_hexdump_nospc(d, 64));
143 printf("%s\n", hex);
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100144}
145
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100146int main(int argc, char **argv)
147{
Max0a59e982016-02-05 13:55:37 +0100148 struct bitvec bv;
149 uint8_t i = 8, test[i];
150
151 memset(test, 0, i);
152 bv.data_len = i;
153 bv.data = test;
154 bv.cur_bit = 0;
155
156 printf("test shifting...\n");
157
158 bitvec_set_uint(&bv, 0x0E, 7);
159 test_shift(&bv, 3);
160 test_shift(&bv, 17);
161 bitvec_set_uint(&bv, 0, 32);
162 bitvec_set_uint(&bv, 0x0A, 7);
163 test_shift(&bv, 24);
164
165 printf("checking RL functions...\n");
166
167 bitvec_zero(&bv);
168 test_rl(&bv);
169 bitvec_set_uint(&bv, 0x000F, 32);
170 test_rl(&bv);
171 bitvec_shiftl(&bv, 18);
172 test_rl(&bv);
173 bitvec_set_uint(&bv, 0x0F, 8);
174 test_rl(&bv);
175 bitvec_zero(&bv);
176 bitvec_set_uint(&bv, 0xFF, 8);
177 test_rl(&bv);
178 bitvec_set_uint(&bv, 0xFE, 7);
179 test_rl(&bv);
180 bitvec_set_uint(&bv, 0, 17);
181 test_rl(&bv);
182 bitvec_shiftl(&bv, 18);
183 test_rl(&bv);
184
185 printf("probing bit access...\n");
186
187 bitvec_zero(&bv);
188 bitvec_set_uint(&bv, 0x3747817, 32);
189 bitvec_shiftl(&bv, 10);
190
191 test_get(&bv, 2);
192 test_get(&bv, 7);
193 test_get(&bv, 9);
194 test_get(&bv, 13);
195 test_get(&bv, 16);
196 test_get(&bv, 42);
197
198 printf("feeling bit fills...\n");
199
200 test_set(&bv, ONE);
201 test_fill(&bv, 3, ZERO);
202 test_spare(&bv, 38);
203 test_spare(&bv, 43);
204 test_spare(&bv, 1);
205 test_spare(&bv, 7);
206 test_fill(&bv, 5, ONE);
207 test_fill(&bv, 3, L);
208
209 printf("byte me...\n");
210
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100211 test_byte_ops();
Holger Hans Peter Freythera9301a12016-01-30 10:54:43 +0100212 test_unhex("48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b");
213 test_unhex("47240c00400000000000000079eb2ac9402b2b2b2b2b2b");
214 test_unhex("47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b");
215 test_unhex("DEADFACE000000000000000000000000000000BEEFFEED");
216 test_unhex("FFFFFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
Max0a59e982016-02-05 13:55:37 +0100217
Jacob Erlbeck5f349be2015-12-21 16:04:03 +0100218 return 0;
219}