diff options
-rw-r--r-- | bitmap.c | 175 |
1 files changed, 0 insertions, 175 deletions
@@ -210,178 +210,3 @@ bitmap_from_string(struct bitmap *b, const void *p, size_t l) | |||
210 | retop(b); | 210 | retop(b); |
211 | return 0; | 211 | return 0; |
212 | } | 212 | } |
213 | |||
214 | #ifdef BITMAP_TEST | ||
215 | |||
216 | /* main() test against OpenSSL BN */ | ||
217 | #include <err.h> | ||
218 | #include <openssl/bn.h> | ||
219 | #include <stdio.h> | ||
220 | #include <stdarg.h> | ||
221 | |||
222 | #define LIM 131 | ||
223 | #define FAIL(...) fail(__FILE__, __LINE__, __VA_ARGS__) | ||
224 | |||
225 | void | ||
226 | bitmap_dump(struct bitmap *b, FILE *f) | ||
227 | { | ||
228 | size_t i; | ||
229 | |||
230 | fprintf(f, "bitmap %p len=%zu top=%zu d =", b, b->len, b->top); | ||
231 | for (i = 0; i < b->len; i++) { | ||
232 | fprintf(f, " %0*llx", (int)BITMAP_BITS / 4, | ||
233 | (unsigned long long)b->d[i]); | ||
234 | } | ||
235 | fputc('\n', f); | ||
236 | } | ||
237 | |||
238 | static void | ||
239 | fail(char *file, int line, char *fmt, ...) | ||
240 | { | ||
241 | va_list ap; | ||
242 | |||
243 | fprintf(stderr, "%s:%d ", file, line); | ||
244 | va_start(ap, fmt); | ||
245 | vfprintf(stderr, fmt, ap); | ||
246 | va_end(ap); | ||
247 | fputc('\n', stdout); | ||
248 | /* abort(); */ | ||
249 | exit(1); | ||
250 | } | ||
251 | |||
252 | static void | ||
253 | dump(const char *s, const u_char *buf, size_t l) | ||
254 | { | ||
255 | size_t i; | ||
256 | |||
257 | fprintf(stderr, "%s len %zu = ", s, l); | ||
258 | for (i = 0; i < l; i++) | ||
259 | fprintf(stderr, "%02x ", buf[i]); | ||
260 | fputc('\n', stderr); | ||
261 | } | ||
262 | |||
263 | int main(void) { | ||
264 | struct bitmap *b = bitmap_new(); | ||
265 | BIGNUM *bn = BN_new(); | ||
266 | size_t len; | ||
267 | int i, j, k, n; | ||
268 | u_char bbuf[1024], bnbuf[1024]; | ||
269 | int r; | ||
270 | |||
271 | for (i = -1; i < LIM; i++) { | ||
272 | fputc('i', stdout); | ||
273 | fflush(stdout); | ||
274 | for (j = -1; j < LIM; j++) { | ||
275 | for (k = -1; k < LIM; k++) { | ||
276 | bitmap_zero(b); | ||
277 | BN_clear(bn); | ||
278 | /* Test setting bits */ | ||
279 | if (i > 0 && bitmap_set_bit(b, i) != 0) | ||
280 | FAIL("bitmap_set_bit %d fail", i); | ||
281 | if (j > 0 && bitmap_set_bit(b, j) != 0) | ||
282 | FAIL("bitmap_set_bit %d fail", j); | ||
283 | if (k > 0 && bitmap_set_bit(b, k) != 0) | ||
284 | FAIL("bitmap_set_bit %d fail", k); | ||
285 | if ((i > 0 && BN_set_bit(bn, i) != 1) || | ||
286 | (j > 0 && BN_set_bit(bn, j) != 1) || | ||
287 | (k > 0 && BN_set_bit(bn, k) != 1)) | ||
288 | FAIL("BN_set_bit fail"); | ||
289 | for (n = 0; n < LIM; n++) { | ||
290 | if (BN_is_bit_set(bn, n) != | ||
291 | bitmap_test_bit(b, n)) { | ||
292 | FAIL("miss %d/%d/%d %d " | ||
293 | "%d %d", i, j, k, n, | ||
294 | BN_is_bit_set(bn, n), | ||
295 | bitmap_test_bit(b, n)); | ||
296 | } | ||
297 | } | ||
298 | /* Test length calculations */ | ||
299 | if (BN_num_bytes(bn) != (int)bitmap_nbytes(b)) { | ||
300 | FAIL("bytes %d/%d/%d %d != %zu", | ||
301 | i, j, k, BN_num_bytes(bn), | ||
302 | bitmap_nbytes(b)); | ||
303 | } | ||
304 | if (BN_num_bits(bn) != (int)bitmap_nbits(b)) { | ||
305 | FAIL("bits %d/%d/%d %d != %zu", | ||
306 | i, j, k, BN_num_bits(bn), | ||
307 | bitmap_nbits(b)); | ||
308 | } | ||
309 | /* Test serialisation */ | ||
310 | len = bitmap_nbytes(b); | ||
311 | memset(bbuf, 0xfc, sizeof(bbuf)); | ||
312 | if (bitmap_to_string(b, bbuf, | ||
313 | sizeof(bbuf)) != 0) | ||
314 | FAIL("bitmap_to_string %d/%d/%d", | ||
315 | i, j, k); | ||
316 | for (n = len; n < (int)sizeof(bbuf); n++) { | ||
317 | if (bbuf[n] != 0xfc) | ||
318 | FAIL("bad string " | ||
319 | "%d/%d/%d %d 0x%02x", | ||
320 | i, j, k, n, bbuf[n]); | ||
321 | } | ||
322 | if ((r = BN_bn2bin(bn, bnbuf)) < 0) | ||
323 | FAIL("BN_bn2bin %d/%d/%d", | ||
324 | i, j, k); | ||
325 | if ((size_t)r != len) | ||
326 | FAIL("len bad %d/%d/%d", i, j, k); | ||
327 | if (memcmp(bbuf, bnbuf, len) != 0) { | ||
328 | dump("bbuf", bbuf, sizeof(bbuf)); | ||
329 | dump("bnbuf", bnbuf, sizeof(bnbuf)); | ||
330 | FAIL("buf bad %d/%d/%d", i, j, k); | ||
331 | } | ||
332 | /* Test deserialisation */ | ||
333 | bitmap_zero(b); | ||
334 | if (bitmap_from_string(b, bnbuf, len) != 0) | ||
335 | FAIL("bitmap_to_string %d/%d/%d", | ||
336 | i, j, k); | ||
337 | for (n = 0; n < LIM; n++) { | ||
338 | if (BN_is_bit_set(bn, n) != | ||
339 | bitmap_test_bit(b, n)) { | ||
340 | FAIL("miss %d/%d/%d %d " | ||
341 | "%d %d", i, j, k, n, | ||
342 | BN_is_bit_set(bn, n), | ||
343 | bitmap_test_bit(b, n)); | ||
344 | } | ||
345 | } | ||
346 | /* Test clearing bits */ | ||
347 | for (n = 0; n < LIM; n++) { | ||
348 | if (bitmap_set_bit(b, n) != 0) { | ||
349 | bitmap_dump(b, stderr); | ||
350 | FAIL("bitmap_set_bit %d " | ||
351 | "fail", n); | ||
352 | } | ||
353 | if (BN_set_bit(bn, n) != 1) | ||
354 | FAIL("BN_set_bit fail"); | ||
355 | } | ||
356 | if (i > 0) { | ||
357 | bitmap_clear_bit(b, i); | ||
358 | BN_clear_bit(bn, i); | ||
359 | } | ||
360 | if (j > 0) { | ||
361 | bitmap_clear_bit(b, j); | ||
362 | BN_clear_bit(bn, j); | ||
363 | } | ||
364 | if (k > 0) { | ||
365 | bitmap_clear_bit(b, k); | ||
366 | BN_clear_bit(bn, k); | ||
367 | } | ||
368 | for (n = 0; n < LIM; n++) { | ||
369 | if (BN_is_bit_set(bn, n) != | ||
370 | bitmap_test_bit(b, n)) { | ||
371 | bitmap_dump(b, stderr); | ||
372 | FAIL("cmiss %d/%d/%d %d " | ||
373 | "%d %d", i, j, k, n, | ||
374 | BN_is_bit_set(bn, n), | ||
375 | bitmap_test_bit(b, n)); | ||
376 | } | ||
377 | } | ||
378 | } | ||
379 | } | ||
380 | } | ||
381 | fputc('\n', stdout); | ||
382 | bitmap_free(b); | ||
383 | BN_free(bn); | ||
384 | |||
385 | return 0; | ||
386 | } | ||
387 | #endif | ||