summaryrefslogtreecommitdiff
path: root/tests/aplwc_utf8.c
blob: 7d1477874d120bb480bcb6836ea38af0c2a7b696 (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
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
/* 
 * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
 *
 * This file is part of aplwc.
 *
 * Aplwc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Aplwc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aplwc.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>
#include <aplwc.h>

uint8_t bad1[] = {0xff,0};
uint8_t bad2[] = {0x80,0};
uint8_t bad3[] = {0xc0,0x80,0};
uint8_t bad4[] = {0xc0,0x40,0};
uint8_t bad5[] = {0xe0,0x80,0x80,0};
uint8_t bad6[] = {0xc0,0};
uint8_t bad7[] = {0xF0,0x80,0x80,0x80,0};
uint8_t bad8[] = {0xF7,0xBF,0xBF,0xBF,0};

uint8_t good1[] = {0x61,0x70,0x6c,0x77,0x63,0};
uint8_t good2[] = {0x41,0xe2,0x89,0xa2,0xce,0x91,0x2e,0};
uint8_t good3[] = {0xed,0x95,0x9c,0xea,0xb5,0xad,0xec,0x96,0xb4,0};
uint8_t good4[] = {0xe6,0x97,0xa5,0xe6,0x9c,0xac,0xe8,0xaa,0x9e,0};
uint8_t good5[] = {0xef,0xbb,0xbf,0xf0,0xa3,0x8e,0xb4,0};

static void *
my_alloc(size_t size, size_t *actual)
{
	if(actual)
		*actual = size;
	return malloc(size);
}

static void
my_dealloc(void *p)
{
	free(p);
}

int
main(void)
{
	struct aplwc *aplwc;
	AplwcLine line;
	struct aplwc_fns fns;
	enum aplwc_rc ret;
	size_t i;

	uint8_t *bad_tests[] = {
		bad1,
		bad2,
		bad3,
		bad4,
		bad5,
		bad6,
		bad7,
		bad8
	};

	uint8_t *good_tests[] = {
		good1,
		good2,
		good3,
		good4,
		good5
	};

	memset(&fns, 0, sizeof(fns));
	fns.alloc = my_alloc;
	fns.dealloc = my_dealloc;

	aplwc = aplwc_new(fns, &ret);
	if(ret)
		return 1;

	for(i = 0; i < (sizeof(bad_tests)/sizeof(*bad_tests)); i++){
		aplwc_read(aplwc, (char *)bad_tests[i], &line);
		if(aplwc_valid_line(&line))
			return 1;
		if(aplwc_last_error(aplwc) != APLWC_INVALID_UTF8)
			return 1;
		aplwc_free_line(&line);
	}

	for(i = 0; i < (sizeof(good_tests)/sizeof(*good_tests)); i++){
		aplwc_read(aplwc, (char *)good_tests[i], &line);
		if((!aplwc_valid_line(&line)) && (aplwc_last_error(aplwc) == APLWC_INVALID_UTF8))
			return 1;
		aplwc_free_line(&line);
	}

	aplwc_destroy(aplwc);

	return 0;
}