aboutsummaryrefslogtreecommitdiffstats
path: root/test_gen.c
blob: 217e6ca7a45aa372f5195c220e737c7a899d55ba (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (C) 2020-2021  Tomasz Kramkowski <tk@the-tk.com>
 * SPDX-License-Identifier: MIT
 */
// @BUILD_CC host
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

struct {
	enum endian { BIG, LITTLE } e;
	char *prefix;
} endian[] = {
	{ BIG, "" },
	{ BIG, ">" },
	{ LITTLE, "<" },
};

struct fmtinfo {
	char fmt;
	char *ptype;
	char *cfmt;
	char *type;
	intmax_t min;
	uintmax_t max;
	size_t size;
} fmtinfo[] = {
	{ 'b', "SCHAR",  "d",   "signed char",         INTMAX_C(               -128),     UINTMAX_C(                 127), 1 },
	{ 'B', "UCHAR",  "u",   "unsigned char",       INTMAX_C(                  0),     UINTMAX_C(                 255), 1 },
	{ 'h', "SHORT",  "d",   "signed short",        INTMAX_C(             -32768),     UINTMAX_C(               32767), 2 },
	{ 'H', "USHORT", "u",   "unsigned short",      INTMAX_C(                  0),     UINTMAX_C(               65535), 2 },
	{ 'i', "INT",    "d",   "signed int",          INTMAX_C(             -32768),     UINTMAX_C(               32767), 2 },
	{ 'I', "UINT",   "u",   "unsigned int",        INTMAX_C(                  0),     UINTMAX_C(               65535), 2 },
	{ 'l', "LONG",   "ld",  "signed long",         INTMAX_C(        -2147483648),     UINTMAX_C(          2147483647), 4 },
	{ 'L', "ULONG",  "lu",  "unsigned long",       INTMAX_C(                  0),     UINTMAX_C(          4294967295), 4 },
	{ 'q', "LLONG",  "lld", "signed long long",   -INTMAX_C(9223372036854775807) - 1, UINTMAX_C( 9223372036854775807), 8 },
	{ 'Q', "ULLONG", "llu", "unsigned long long",  INTMAX_C(                  0),     UINTMAX_C(18446744073709551615), 8 },
};

static char cchar(char c)
{
	if (c == '\0') return c;
	if (isalnum(c)) return c;
	return '_';
}

static const char *cname(const char *s)
{
	static char ret[100];
	for (int i = 0; i < 99; i++) {
		ret[i] = cchar(s[i]);
		if (s[i] == '\0') break;
	}
	return ret;
}

static const char *u2bytes(enum endian e, int n, uintmax_t v)
{
	static char b[100];
	char *p = b;

	assert(n > 0 && n <= 8);

	for (int i = 0; i < n; i++) {
		p += snprintf(p, b + sizeof(b) - p, "%s0x%x", i == 0 ? "" : ", ",
			      (int)((v >> (e == LITTLE ? i : n - i - 1) * 8) & 0xff));
		assert(p < b + sizeof(b));
	}

	return b;
}

static uintmax_t i2u(int n, intmax_t v)
{
	if (v >= 0)
		return v;

	return (UINTMAX_MAX >> (sizeof (uintmax_t) * CHAR_BIT - n * 8)) + v + 1;
}

static const char *i2bytes(enum endian e, int n, intmax_t v)
{
	return u2bytes(e, n, i2u(n, v));
}

static void unpack_signed(FILE *out, const struct fmtinfo *fi, enum endian e, intmax_t testval, int arraysize)
{
	char sizeprefix[11] = "";
	const char *data;

	if (arraysize >= 1)
		snprintf(sizeprefix, sizeof sizeprefix, "%d", arraysize);
	else
		arraysize = 1;

	fprintf(out, "\tCHECK_UNPACK(DATA(");
	data = i2bytes(endian[e].e, fi->size, testval);
	for (int i = 0; i < arraysize; i++)
		fprintf(out, "%s%s", i == 0 ? "" : ", ", data);
	fprintf(out, "), \"%s%s%c\", v);\n", endian[e].prefix, sizeprefix, fi->fmt);
	for (int i = 0; i < arraysize; i++)
		fprintf(out, "\tCHECK_EQUAL(PRIdMAX, (intmax_t)v[%d], -INTMAX_C(%" PRIdMAX ")-1);\n", i, -(testval + 1));
}

static void unpack_unsigned(FILE *out, const struct fmtinfo *fi, enum endian e, uintmax_t testval, int arraysize)
{
	char sizeprefix[11] = "";
	const char *data;

	if (arraysize >= 1)
		snprintf(sizeprefix, sizeof sizeprefix, "%d", arraysize);
	else
		arraysize = 1;

	fprintf(out, "\tCHECK_UNPACK(DATA(");
	data = u2bytes(endian[e].e, fi->size, testval);
	for (int i = 0; i < arraysize; i++)
		fprintf(out, "%s%s", i == 0 ? "" : ", ", data);
	fprintf(out, "), \"%s%s%c\", v);\n", endian[e].prefix, sizeprefix, fi->fmt);
	for (int i = 0; i < arraysize; i++)
		fprintf(out, "\tCHECK_EQUAL(PRIdMAX, (uintmax_t)v[%d], UINTMAX_C(%" PRIuMAX "));\n", i, testval);
}

static void unpack_gen(FILE *out, const struct fmtinfo *fi, int arraysize)
{
	int realsize = arraysize == 0 ? 1 : arraysize;
	bool sign;

	sign = islower(fi->fmt);

	fprintf(out, "TEST(unpack_simple%d_%s, \"unpack simple", arraysize, cname(fi->type));
	if (arraysize != 0)
		fprintf(out, " array[%d]", arraysize);
	fprintf(out, " %s\")\n", fi->type);
	fprintf(out, "{\n");
	fprintf(out, "\t%s v[%d] = { ", fi->type, realsize );
	for (int i = 0; i < realsize; i++)
		fprintf(out, "%s__LINE__ + %d", i == 0 ? "" : ", ", i);
	fprintf(out, " };\n");
	for (size_t e = 0; e < sizeof endian / sizeof endian[0]; e++) {
		for (int i = sign ? -1 : 0; i <= 1; i++)
			unpack_signed(out, fi, e, i, arraysize);
		if (sign)
			unpack_signed(out, fi, e, fi->min, arraysize);
		unpack_unsigned(out, fi, e, fi->max, arraysize);
	}
	fprintf(out, "\treturn true;\n");
	fprintf(out, "}\n");
}

static void unpack_struct_signed(FILE *out, const struct fmtinfo *fi, enum endian e, intmax_t testval, int arraysize)
{
	char sizeprefix[11] = "";
	const char *data;

	if (arraysize >= 1)
		snprintf(sizeprefix, sizeof sizeprefix, "%d", arraysize);
	else
		arraysize = 1;

	fprintf(out, "\tCHECK_UNPACK_STRUCT(DATA(");
	data = i2bytes(e, fi->size, testval);
	for (int i = 0; i < arraysize; i++)
		fprintf(out, "%s%s", i == 0 ? "" : ", ", data);
	fprintf(out, "), &args, &v);\n");
	for (int i = 0; i < arraysize; i++)
		fprintf(out, "\tCHECK_EQUAL(\"%s\", v.field[%d], (%s)-INTMAX_C(%" PRIdMAX ")-1);\n",
			fi->cfmt, i, fi->type, -(testval + 1));
}

static void unpack_struct_unsigned(FILE *out, const struct fmtinfo *fi, enum endian e, uintmax_t testval, int arraysize)
{
	char sizeprefix[11] = "";
	const char *data;

	if (arraysize >= 1)
		snprintf(sizeprefix, sizeof sizeprefix, "%d", arraysize);
	else
		arraysize = 1;

	fprintf(out, "\tCHECK_UNPACK_STRUCT(DATA(");
	data = u2bytes(e, fi->size, testval);
	for (int i = 0; i < arraysize; i++)
		fprintf(out, "%s%s", i == 0 ? "" : ", ", data);
	fprintf(out, "), &args, &v);\n");
	for (int i = 0; i < arraysize; i++)
		fprintf(out, "\tCHECK_EQUAL(\"%s\", v.field[%d], (%s)UINTMAX_C(%" PRIuMAX "));\n", \
		fi->cfmt, i, fi->type, testval);
}

static void unpack_struct_gen(FILE *out, const struct fmtinfo *fi, int arraysize)
{
	int realsize = arraysize == 0 ? 1 : arraysize;
	bool sign;

	sign = islower(fi->fmt);

	fprintf(out, "TEST(unpack_struct_simple%d_%s, \"unpack_struct simple", \
	        arraysize, cname(fi->type));
	if (arraysize != 0)
		fprintf(out, " array[%d]", arraysize);
	fprintf(out, " %s\")\n", fi->type);
	fprintf(out, "{\n");
	fprintf(out, "\tstruct dest {\n");
	fprintf(out, "\t\t%s field[%d];\n", fi->type, realsize);
	fprintf(out, "\t} v = {{ ");
	for (int i = 0; i < realsize; i++)
		fprintf(out, "%s(__LINE__ + %d) %% UINTMAX_C(%" PRIuMAX ")",
		        i == 0 ? "" : ", ", i, fi->max);
	fprintf(out, " }};\n");
	fprintf(out, "\tstruct pack_args args = {\n");
	fprintf(out, "\t\t.fields = (struct pack_field []){\n");
	fprintf(out, "\t\t\t{ PACK_TYPE_%s, %d, offsetof(struct dest, field) }\n",
		fi->ptype, realsize);
	fprintf(out, "\t\t},\n");
	fprintf(out, "\t\t.num_fields = 1,\n");
	fprintf(out, "\t};\n");
	for (enum endian e = BIG; e <= LITTLE; e++) {
		fprintf(out, "\targs.endian = PACK_ENDIAN_%s;\n",
			e == BIG ? "BIG" : "LITTLE");
		for (int i = sign ? -1 : 0; i <= 1; i++)
			unpack_struct_signed(out, fi, e, i, arraysize);
		if (sign)
			unpack_struct_signed(out, fi, e, fi->min, arraysize);
		unpack_struct_unsigned(out, fi, e, fi->max, arraysize);
	}
	fprintf(out, "\treturn true;\n");
	fprintf(out, "}\n");
}

int main(void)
{
	FILE *out = stdout;
	for (size_t i = 0; i < sizeof fmtinfo / sizeof fmtinfo[0]; i++) {
		unpack_gen(out, &fmtinfo[i], 0);
		for (int j = 2; j < 4; j++)
			unpack_gen(out, &fmtinfo[i], j);
	}
	for (size_t i = 0; i < sizeof fmtinfo / sizeof fmtinfo[0]; i++) {
		unpack_struct_gen(out, &fmtinfo[i], 0);
		for (int j = 2; j < 4; j++)
			unpack_struct_gen(out, &fmtinfo[i], j);
	}
}