disable warning C5250: _BitScanReverse: intrinsic function not declared
[liba.git] / test / crc.h
blob74e9897485e55495803417895f42521281146ec0
1 #define MAIN_(x) A_CAST_2(x, _crc)
2 #include "test.h"
3 #include "a/crc.h"
4 #if !defined __cplusplus
5 #define WRITE_TABLE(bit, row, fmt) \
6 static void write_table##bit(FILE *out, a_u##bit ctx[0x100], char const *label) \
7 { \
8 (void)fprintf(out, "uint%i_t const %s[0x%X] = {\n", bit, label, 0x100); \
9 (void)fprintf(out, " /* clang-format off */\n"); \
10 for (a_size i = 0; i != 0x100 / (row); ++i) \
11 { \
12 (void)fprintf(out, " "); \
13 for (a_size j = 0; j != (row); ++j) \
14 { \
15 (void)fprintf(out, "0x%0" #fmt PRIX##bit ",", ctx[(row) * i + j]); \
16 if (j != (row)-1) { (void)fputc(' ', out); } \
17 } \
18 (void)fputc('\n', out); \
19 } \
20 (void)fprintf(out, " /* clang-format on */\n"); \
21 (void)fprintf(out, "};\n"); \
23 WRITE_TABLE(8, 8, 2)
24 WRITE_TABLE(16, 8, 4)
25 WRITE_TABLE(32, 8, 8)
26 WRITE_TABLE(64, 4, 16)
27 static void create_table(FILE *out)
29 (void)fprintf(out, "#include <stdint.h>\n");
31 a_u8 table8[0x100];
32 a_crc8m_init(table8, 0x07);
33 write_table8(out, table8, "CRC8M");
34 a_crc8l_init(table8, 0x07); // 0xE0
35 write_table8(out, table8, "CRC8L");
37 a_u16 table16[0x100];
38 a_crc16m_init(table16, 0x8005);
39 write_table16(out, table16, "CRC16M");
40 a_crc16l_init(table16, 0x8005); // 0xA001
41 write_table16(out, table16, "CRC16L");
43 a_u32 table32[0x100];
44 a_crc32m_init(table32, 0x04C11DB7);
45 write_table32(out, table32, "CRC32M");
46 a_crc32l_init(table32, 0x04C11DB7); // 0xEDB88320
47 write_table32(out, table32, "CRC32L");
49 a_u64 table64[0x100];
50 a_crc64m_init(table64, 0x42F0E1EBA9EA3693);
51 write_table64(out, table64, "CRC64M");
52 a_crc64l_init(table64, 0x42F0E1EBA9EA3693); // 0xC96C5795D7870F42
53 write_table64(out, table64, "CRC64L");
55 (void)fflush(out);
57 #endif /* __cplusplus */
59 static void test(void)
61 #define TEXT "0123456789"
62 #define SIZE (sizeof(TEXT) - 1)
64 a_u8 table8[0x100];
65 a_crc8m_init(table8, A_U8_C(0x07));
66 TEST_BUG(a_crc8(table8, TEXT, SIZE, 0) == A_U8_C(0x45));
67 a_crc8l_init(table8, A_U8_C(0x31)); // 0x8C
68 TEST_BUG(a_crc8(table8, TEXT, SIZE, 0) == A_U8_C(0x75));
70 a_u16 table16[0x100];
71 a_crc16m_init(table16, A_U16_C(0x1021));
72 TEST_BUG(a_crc16m(table16, TEXT, SIZE, 0) == A_U16_C(0x9C58));
73 a_crc16l_init(table16, A_U16_C(0x8005)); // 0xA001
74 TEST_BUG(a_crc16l(table16, TEXT, SIZE, 0) == A_U16_C(0x443D));
76 a_u32 table32[0x100];
77 a_crc32m_init(table32, A_U32_C(0x1EDC6F41));
78 TEST_BUG(a_crc32m(table32, TEXT, SIZE, 0) == A_U32_C(0x512B456E));
79 a_crc32l_init(table32, A_U32_C(0x04C11DB7)); // 0xEDB88320
80 TEST_BUG(a_crc32l(table32, TEXT, SIZE, 0) == A_U32_C(0x450EAFB0));
82 a_u64 table64[0x100];
83 a_crc64m_init(table64, A_U64_C(0x000000000000001B));
84 TEST_BUG(a_crc64m(table64, TEXT, SIZE, 0) == A_U64_C(0xE4FFBEA588AFC790));
85 a_crc64l_init(table64, A_U64_C(0x42F0E1EBA9EA3693)); // 0xC96C5795D7870F42
86 TEST_BUG(a_crc64l(table64, TEXT, SIZE, 0) == A_U64_C(0xDA60676A5CDE0008));
88 #undef TEXT
89 #undef SIZE
92 int MAIN(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
94 #if !defined __cplusplus
95 FILE *out = stdout;
96 if (argc > 1)
98 char const *name = argv[argc - 1];
99 int file = a_cast_s(int, *name);
100 if (file)
102 out = fopen(name, "wb");
103 if (!out)
105 perror(name);
106 exit(EXIT_FAILURE);
109 create_table(out);
110 if (file && fclose(out) == EOF)
112 perror(name);
115 #else /* !__cplusplus */
116 (void)argc;
117 (void)argv;
118 #endif /* __cplusplus */
119 test();
120 return 0;