-
Notifications
You must be signed in to change notification settings - Fork 25
/
encode2-test.cc
51 lines (41 loc) · 1.44 KB
/
encode2-test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <array>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <fadec-enc2.h>
using Buffer = std::array<uint8_t, 16>;
static
void print_hex(const uint8_t* buf, size_t len) {
for (size_t i = 0; i < len; i++)
std::printf("%02x", buf[i]);
}
static int
check(const Buffer& buf, const char* exp, size_t exp_len, unsigned res, const char* name) {
if (res == exp_len && !std::memcmp(buf.data(), exp, exp_len))
return 0;
std::printf("Failed case (new) %s:\n", name);
std::printf(" Exp (%2zu): ", exp_len);
print_hex(reinterpret_cast<const uint8_t*>(exp), exp_len);
std::printf("\n Got (%2u): ", res);
print_hex(buf.data(), res);
std::printf("\n");
return -1;
}
#define TEST1(str, exp, name, ...) do { \
buf.fill(0); \
unsigned res = fe64_ ## name(buf.data(), __VA_ARGS__); \
failed |= check(buf, exp, sizeof(exp) - 1, res, str); \
} while (0)
#define TEST(exp, ...) TEST1(#__VA_ARGS__, exp, __VA_ARGS__)
int main() {
int failed = 0;
Buffer buf{};
// This API is type safe and prohibits compilation of reg-type mismatches
#define ENC_TEST_TYPESAFE
// Silence -Warray-bounds with double cast
#define FE_PTR(off) (const void*) ((uintptr_t) buf.data() + (off))
#define FLAGMASK(flags, mask) flags, mask
#include "encode-test.inc"
std::puts(failed ? "Some tests FAILED" : "All tests PASSED");
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}