aboutsummaryrefslogtreecommitdiff
path: root/test/_case-test.h
blob: 24a18f16625077950f4487f5c62f1af6ddc2f232 (plain) (blame)
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
#if !defined(CASETYPE) || !defined(CASETYPE_VERB)
#	error "CASETYPE and CASETYPE_VERB must be defined"
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <alloc.h>
#include <errors.h>
#include <macros.h>
#include <mbstring.h>
#include <unicode/string.h>

#define TESTFILE STR(CASETYPE) ".in"
#define FUNC     CONCAT(ucs, CASETYPE)

static bool test(const char8_t *, int);

int
main(int, char **argv)
{
	int rv;
	size_t n;
	ssize_t nr;
	char *line;
	FILE *fp;

	rv = EXIT_SUCCESS;
	line = nullptr;
	mlib_setprogname(argv[0]);

	if ((fp = fopen(TESTFILE, "r")) == nullptr)
		err("fopen: %s:", TESTFILE);

	for (int id = 1; (nr = getline(&line, &n, fp)) > 0; id++) {
		if (line[nr - 1] == '\n')
			line[--nr] = '\0';

		if (!test(line, id))
			rv = EXIT_FAILURE;
	}
	if (ferror(fp))
		err("getline: %s:", TESTFILE);

	free(line);
	fclose(fp);
	return rv;
}

bool
test(const char8_t *line, int id)
{
	struct u8view mapped, sv = {line, strlen(line)};
	struct u8view before, after, flags;
	ucscut(&before, &sv, U";", 1);
	ucscut(&after,  &sv, U";", 1);
	ucscut(&flags,  &sv, U";", 1);

	enum caseflags cf = ucseq(flags, U8("ẞ"))  ? CF_ẞ
	                  : ucseq(flags, U8("AZ")) ? CF_LANG_AZ
	                  : ucseq(flags, U8("LT")) ? CF_LANG_LT
	                  : ucseq(flags, U8("NL")) ? CF_LANG_NL
	                                          : 0;

	arena a = mkarena(0);
	mapped.p = FUNC(&mapped.len, before, cf, alloc_arena, &((struct arena_ctx){
		.a = &a,
	}));

	if (!ucseq(mapped, after)) {
		warn("case %d: expected ‘%.*s’ but got ‘%.*s’", id, SV_PRI_ARGS(after),
		     SV_PRI_ARGS(mapped));
		arena_free(&a);
		return false;
	}

	arena_free(&a);
	return true;
}