summaryrefslogtreecommitdiff
path: root/test/number.c
blob: e3c2c8f3c009727b30c7891fe70d77896348dfec (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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <libcss/libcss.h>

#include "utils/utils.h"

#include "testutils.h"

typedef struct line_ctx {
	size_t buflen;
	size_t bufused;
	uint8_t *buf;

	size_t explen;
	char exp[256];

	bool indata;
	bool inexp;
} line_ctx;

static bool handle_line(const char *data, size_t datalen, void *pw);
static void run_test(const uint8_t *data, size_t len,
		const char *exp, size_t explen);
static void print_css_fixed(char *buf, size_t len, css_fixed f);

static void test_logger(
		void *_ctx, nslog_entry_context_t *ctx,
		const char *fmt,
		va_list args)
{
	UNUSED(_ctx);
	fprintf(stderr, "%s: %s: %s:%i: ",
			nslog_level_name(ctx->level),
			ctx->category->description,
			ctx->filename, ctx->lineno);
	vfprintf(stderr, fmt, args);
	fprintf(stderr, "\n");
}

int main(int argc, char **argv)
{
	line_ctx ctx;

	if (argc != 2) {
		printf("Usage: %s <filename>\n", argv[0]);
		return 1;
	}

	if (nslog_set_render_callback(test_logger, NULL) != NSLOG_NO_ERROR) {
		printf("Unable to set log render callback\n");
		return 1;
	}

	if (nslog_uncork() != NSLOG_NO_ERROR) {
		printf("Failed to uncork log.\n");
		return 1;
	}

	ctx.buflen = css__parse_filesize(argv[1]);
	if (ctx.buflen == 0)
		return 1;

	ctx.buf = malloc(ctx.buflen);
	if (ctx.buf == NULL) {
		printf("Failed allocating %u bytes\n",
				(unsigned int) ctx.buflen);
		return 1;
	}

	ctx.buf[0] = '\0';
	ctx.bufused = 0;
	ctx.explen = 0;
	ctx.indata = false;
	ctx.inexp = false;

	assert(css__parse_testfile(argv[1], handle_line, &ctx) == true);

	/* and run final test */
	if (ctx.bufused > 0)
		run_test(ctx.buf, ctx.bufused - 1, ctx.exp, ctx.explen);

	free(ctx.buf);

	nslog_cleanup();

	printf("PASS\n");

	return 0;
}

bool handle_line(const char *data, size_t datalen, void *pw)
{
	line_ctx *ctx = (line_ctx *) pw;

	if (data[0] == '#') {
		if (ctx->inexp) {
			/* This marks end of testcase, so run it */

			run_test(ctx->buf, ctx->bufused - 1,
					ctx->exp, ctx->explen);

			ctx->buf[0] = '\0';
			ctx->bufused = 0;

			ctx->explen = 0;
		}

		if (ctx->indata && strncasecmp(data+1, "expected", 8) == 0) {
			ctx->indata = false;
			ctx->inexp = true;
		} else if (!ctx->indata) {
			ctx->indata = (strncasecmp(data+1, "data", 4) == 0);
			ctx->inexp  = (strncasecmp(data+1, "expected", 8) == 0);
		} else {
			memcpy(ctx->buf + ctx->bufused, data, datalen);
			ctx->bufused += datalen;
		}
	} else {
		if (ctx->indata) {
			memcpy(ctx->buf + ctx->bufused, data, datalen);
			ctx->bufused += datalen;
		}
		if (ctx->inexp) {
			if (data[datalen - 1] == '\n')
				datalen -= 1;

			memcpy(ctx->exp, data, datalen);
			ctx->explen = datalen;
		}
	}

	return true;
}

void run_test(const uint8_t *data, size_t len, const char *exp, size_t explen)
{
        lwc_string *in;
	size_t consumed;
	css_fixed result;
	char buf[256];

	UNUSED(exp);
	UNUSED(explen);

        assert(lwc_intern_string((const char *)data, len, &in) == lwc_error_ok);

	result = css__number_from_lwc_string(in, false, &consumed);

	print_css_fixed(buf, sizeof(buf), result);

	printf("got: %s expected: %.*s\n", buf, (int) explen, exp);

	assert(strncmp(buf, exp, explen) == 0);

	lwc_string_unref(in);
}

void print_css_fixed(char *buf, size_t len, css_fixed f)
{
#define ABS(x) (uint32_t)((x) < 0 ? -(x) : (x))
	uint32_t uintpart = FIXTOINT(ABS(f));
	/* + 500 to ensure round to nearest (division will truncate) */
	uint32_t fracpart = ((ABS(f) & 0x3ff) * 1000 + 500) / (1 << 10);
#undef ABS
	size_t flen = 0;
	char tmp[20];
	size_t tlen = 0;

	if (len == 0)
		return;

	if (f < 0) {
		buf[0] = '-';
		buf++;
		len--;
	}

	do {
		tmp[tlen] = "0123456789"[uintpart % 10];
		tlen++;

		uintpart /= 10;
	} while (tlen < 20 && uintpart != 0);

	while (len > 0 && tlen > 0) {
		buf[0] = tmp[--tlen];
		buf++;
		len--;
	}

	if (len > 0) {
		buf[0] = '.';
		buf++;
		len--;
	}

	do {
		tmp[tlen] = "0123456789"[fracpart % 10];
		tlen++;

		fracpart /= 10;
	} while (tlen < 20 && fracpart != 0);

	while (len > 0 && tlen > 0) {
		buf[0] = tmp[--tlen];
		buf++;
		len--;
		flen++;
	}

	while (len > 0 && flen < 3) {
		buf[0] = '0';
		buf++;
		len--;
		flen++;
	}

	if (len > 0) {
		buf[0] = '\0';
		buf++;
		len--;
	}
}