| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
#include <stdarg.h> |
| 4 |
#include <stdint.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "util.h" |
| 10 |
|
| 11 |
static void hexdump(const uint8_t *buf, size_t len) |
| 12 |
{ |
| 13 |
size_t i = 0; |
| 14 |
|
| 15 |
if (!len) |
| 16 |
return; |
| 17 |
|
| 18 |
for (;;) { |
| 19 |
fprintf(stderr, "%02X", buf[i++]); |
| 20 |
if (i >= len) |
| 21 |
break; |
| 22 |
|
| 23 |
fputc(i % 16 > 0 ? ' ' : '\n', stderr); |
| 24 |
} |
| 25 |
fputc('\n', stderr); |
| 26 |
} |
| 27 |
|
| 28 |
void fail(const char *fmt, ...) |
| 29 |
{ |
| 30 |
va_list args; |
| 31 |
|
| 32 |
va_start(args, fmt); |
| 33 |
vfprintf(stderr, fmt, args); |
| 34 |
va_end(args); |
| 35 |
fputc('\n', stderr); |
| 36 |
abort(); |
| 37 |
} |
| 38 |
|
| 39 |
void cmp(const void *actual, const void *expected, size_t len, const char *msg) |
| 40 |
{ |
| 41 |
if (memcmp(actual, expected, len) == 0) |
| 42 |
return; |
| 43 |
|
| 44 |
fputs(msg, stderr); |
| 45 |
fputs("\nactual:\n", stderr); |
| 46 |
hexdump(actual, len); |
| 47 |
fputs("expected:\n", stderr); |
| 48 |
hexdump(expected, len); |
| 49 |
abort(); |
| 50 |
} |
| 51 |
|
| 52 |
void arbitrary(void *buf_, size_t len) |
| 53 |
{ |
| 54 |
uint8_t *buf = buf_; |
| 55 |
|
| 56 |
while (len--) |
| 57 |
*(buf++) = rand(); |
| 58 |
} |
| 59 |
|
| 60 |
size_t arbitrary_range(size_t max) |
| 61 |
{ |
| 62 |
size_t value; |
| 63 |
arbitrary(&value, sizeof(value)); |
| 64 |
return value % max; |
| 65 |
} |