Create userdata without user values in Lua 5.4
[liba.git] / test / crc.h
blobefcfb8a414c5c7be2c330933c01052d443c10ea1
1 #define MAIN_(x) A_CAST_2(x, _crc)
2 #include "test.h"
3 #include "a/crc.h"
4 #if !defined __cplusplus
5 #include <stdlib.h>
6 #define WRITE_TABLE(bit, row, fmt) \
7 static void write_table##bit(FILE *out, a_u##bit##_t ctx[A_CRC_SIZ], char const *const label) \
8 { \
9 (void)fprintf(out, "uint%i_t const %s[0x%X] = {\n", bit, label, A_CRC_SIZ); \
10 (void)fprintf(out, " /* clang-format off */\n"); \
11 for (a_size_t i = 0; i != A_CRC_SIZ / (row); ++i) \
12 { \
13 (void)fprintf(out, " "); \
14 for (a_size_t j = 0; j != (row); ++j) \
15 { \
16 (void)fprintf(out, "0x%0" #fmt PRIX##bit ",", ctx[(row) * i + j]); \
17 if (j != (row)-1) { (void)fputc(' ', out); } \
18 } \
19 (void)fputc('\n', out); \
20 } \
21 (void)fprintf(out, " /* clang-format on */\n"); \
22 (void)fprintf(out, "};\n"); \
24 WRITE_TABLE(8, 8, 2)
25 WRITE_TABLE(16, 8, 4)
26 WRITE_TABLE(32, 8, 8)
27 WRITE_TABLE(64, 4, 16)
28 #undef WRITE_TABLE
29 #endif /* __cplusplus */
31 static void create_table(FILE *out)
33 #if !defined __cplusplus
34 (void)fprintf(out, "#include <stdint.h>\n");
36 a_u8_t table8[A_CRC_SIZ];
37 a_crc8le_init(table8, A_CRC8_POLY);
38 write_table8(out, table8, "CRC8LE");
39 a_crc8be_init(table8, A_CRC8_POLY);
40 write_table8(out, table8, "CRC8BE");
42 a_u16_t table16[A_CRC_SIZ];
43 a_crc16le_init(table16, A_CRC16_POLY);
44 write_table16(out, table16, "CRC16LE");
45 a_crc16be_init(table16, A_CRC16_POLY);
46 write_table16(out, table16, "CRC16BE");
48 a_u32_t table32[A_CRC_SIZ];
49 a_crc32le_init(table32, A_CRC32_POLY);
50 write_table32(out, table32, "CRC32LE");
51 a_crc32be_init(table32, A_CRC32_POLY);
52 write_table32(out, table32, "CRC32BE");
54 a_u64_t table64[A_CRC_SIZ];
55 a_crc64le_init(table64, A_CRC64_POLY);
56 write_table64(out, table64, "CRC64LE");
57 a_crc64be_init(table64, A_CRC64_POLY);
58 write_table64(out, table64, "CRC64BE");
60 #else /* !__cplusplus */
61 (void)out;
62 #endif /* __cplusplus */
65 static void test(void)
67 #define TEXT "123456789"
68 #define SIZE (sizeof(TEXT) - 1)
70 a_u8_t table8[A_CRC_SIZ];
71 debug("POLY: 0x%02u\n", A_CRC8_POLY);
72 debug("INIT: 0x%02u\n", A_CRC8_INIT);
73 a_crc8le_init(table8, A_CRC8_POLY);
74 debug("LSB: 0x%02" PRIX8 "\n", a_crc8(table8, TEXT, SIZE, A_CRC8_INIT));
75 a_crc8be_init(table8, A_CRC8_POLY);
76 debug("MSB: 0x%02" PRIX8 "\n", a_crc8(table8, TEXT, SIZE, A_CRC8_INIT));
78 a_u16_t table16[A_CRC_SIZ];
79 debug("POLY: 0x%04u\n", A_CRC16_POLY);
80 debug("INIT: 0x%04u\n", A_CRC16_INIT);
81 a_crc16le_init(table16, A_CRC16_POLY);
82 debug("LSB: 0x%04" PRIX16 "(LE) 0x%04" PRIX16 "(BE)\n",
83 a_crc16le(table16, TEXT, SIZE, A_CRC16_INIT),
84 a_crc16be(table16, TEXT, SIZE, A_CRC16_INIT));
85 a_crc16be_init(table16, A_CRC16_POLY);
86 debug("MSB: 0x%04" PRIX16 "(LE) 0x%04" PRIX16 "(BE)\n",
87 a_crc16le(table16, TEXT, SIZE, A_CRC16_INIT),
88 a_crc16be(table16, TEXT, SIZE, A_CRC16_INIT));
90 a_u32_t table32[A_CRC_SIZ];
91 debug("POLY: 0x%08" PRIX32 "\n", A_CRC32_POLY);
92 debug("INIT: 0x%08" PRIX32 "\n", A_CRC32_INIT);
93 a_crc32le_init(table32, A_CRC32_POLY);
94 debug("LSB: 0x%08" PRIX32 "(LE) 0x%08" PRIX32 "(BE)\n",
95 a_crc32le(table32, TEXT, SIZE, A_CRC32_INIT),
96 a_crc32be(table32, TEXT, SIZE, A_CRC32_INIT));
97 a_crc32be_init(table32, A_CRC32_POLY);
98 debug("MSB: 0x%08" PRIX32 "(LE) 0x%08" PRIX32 "(BE)\n",
99 a_crc32le(table32, TEXT, SIZE, A_CRC32_INIT),
100 a_crc32be(table32, TEXT, SIZE, A_CRC32_INIT));
102 a_u64_t table64[A_CRC_SIZ];
103 debug("POLY: 0x%016" PRIX64 "\n", A_CRC64_POLY);
104 debug("INIT: 0x%016" PRIX64 "\n", A_CRC64_INIT);
105 a_crc64le_init(table64, A_CRC64_POLY);
106 debug("LSB: 0x%016" PRIX64 "(LE) 0x%016" PRIX64 "(BE)\n",
107 a_crc64le(table64, TEXT, SIZE, A_CRC64_INIT),
108 a_crc64be(table64, TEXT, SIZE, A_CRC64_INIT));
109 a_crc64be_init(table64, A_CRC64_POLY);
110 debug("MSB: 0x%016" PRIX64 "(LE) 0x%016" PRIX64 "(BE)\n",
111 a_crc64le(table64, TEXT, SIZE, A_CRC64_INIT),
112 a_crc64be(table64, TEXT, SIZE, A_CRC64_INIT));
114 #undef TEXT
115 #undef SIZE
118 int MAIN(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
120 FILE *out = stdout;
121 if (argc > 1)
123 char const *name = argv[argc - 1];
124 int file = a_cast_s(int, *name);
125 if (file)
127 out = fopen(name, "wb");
128 if (!out)
130 perror(name);
131 exit(EXIT_FAILURE);
134 create_table(out);
135 if (file && fclose(out) == EOF)
137 perror(name);
140 test();
141 return 0;