chiark / gitweb /
More tests for mime.c
[disorder] / lib / t-utf8.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20#include "test.h"
21
22void test_utf8(void) {
23 /* Test validutf8, convert to UCS-4, check the answer is right,
24 * convert back to UTF-8, check we got to where we started */
25#define U8(CHARS, WORDS) do { \
26 uint32_t *w = ucs4parse(WORDS); \
27 uint32_t *ucs; \
28 char *u8; \
29 \
30 insist(validutf8(CHARS)); \
31 ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \
32 insist(ucs != 0); \
33 insist(!utf32_cmp(w, ucs)); \
34 u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \
35 insist(u8 != 0); \
36 check_string(u8, CHARS); \
37} while(0)
38
39 fprintf(stderr, "test_utf8\n");
40#define validutf8(S) utf8_valid((S), strlen(S))
41
42 /* empty string */
43
44 U8("", "");
45
46 /* ASCII characters */
47
48 U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
49 "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
50 "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
51 "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
52 "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
53 "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
54 "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
55 "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
56 "0x7c 0x7d 0x7e");
57 U8("\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\177",
58 "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
59 "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
60 "0x1e 0x1f 0x7f");
61
62 /* from RFC3629 */
63
64 /* UTF8-2 = %xC2-DF UTF8-tail */
65 insist(!validutf8("\xC0\x80"));
66 insist(!validutf8("\xC1\x80"));
67 insist(!validutf8("\xC2\x7F"));
68 U8("\xC2\x80", "0x80");
69 U8("\xDF\xBF", "0x7FF");
70 insist(!validutf8("\xDF\xC0"));
71
72 /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
73 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
74 */
75 insist(!validutf8("\xE0\x9F\x80"));
76 U8("\xE0\xA0\x80", "0x800");
77 U8("\xE0\xBF\xBF", "0xFFF");
78 insist(!validutf8("\xE0\xC0\xBF"));
79
80 insist(!validutf8("\xE1\x80\x7F"));
81 U8("\xE1\x80\x80", "0x1000");
82 U8("\xEC\xBF\xBF", "0xCFFF");
83 insist(!validutf8("\xEC\xC0\xBF"));
84
85 U8("\xED\x80\x80", "0xD000");
86 U8("\xED\x9F\xBF", "0xD7FF");
87 insist(!validutf8("\xED\xA0\xBF"));
88
89 insist(!validutf8("\xEE\x7f\x80"));
90 U8("\xEE\x80\x80", "0xE000");
91 U8("\xEF\xBF\xBF", "0xFFFF");
92 insist(!validutf8("\xEF\xC0\xBF"));
93
94 /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
95 * %xF4 %x80-8F 2( UTF8-tail )
96 */
97 insist(!validutf8("\xF0\x8F\x80\x80"));
98 U8("\xF0\x90\x80\x80", "0x10000");
99 U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
100 insist(!validutf8("\xF0\xC0\x80\x80"));
101
102 insist(!validutf8("\xF1\x80\x80\x7F"));
103 U8("\xF1\x80\x80\x80", "0x40000");
104 U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
105 insist(!validutf8("\xF3\xC0\x80\x80"));
106
107 insist(!validutf8("\xF4\x80\x80\x7F"));
108 U8("\xF4\x80\x80\x80", "0x100000");
109 U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
110 insist(!validutf8("\xF4\x90\x80\x80"));
111 insist(!validutf8("\xF4\x80\xFF\x80"));
112
113 /* miscellaneous non-UTF-8 rubbish */
114 insist(!validutf8("\x80"));
115 insist(!validutf8("\xBF"));
116 insist(!validutf8("\xC0"));
117 insist(!validutf8("\xC0\x7F"));
118 insist(!validutf8("\xC0\xC0"));
119 insist(!validutf8("\xE0"));
120 insist(!validutf8("\xE0\x7F"));
121 insist(!validutf8("\xE0\xC0"));
122 insist(!validutf8("\xE0\x80"));
123 insist(!validutf8("\xE0\x80\x7f"));
124 insist(!validutf8("\xE0\x80\xC0"));
125 insist(!validutf8("\xF0"));
126 insist(!validutf8("\xF0\x7F"));
127 insist(!validutf8("\xF0\xC0"));
128 insist(!validutf8("\xF0\x80"));
129 insist(!validutf8("\xF0\x80\x7f"));
130 insist(!validutf8("\xF0\x80\xC0"));
131 insist(!validutf8("\xF0\x80\x80\x7f"));
132 insist(!validutf8("\xF0\x80\x80\xC0"));
133 insist(!validutf8("\xF5\x80\x80\x80"));
134 insist(!validutf8("\xF8"));
135}
136
137/*
138Local Variables:
139c-basic-offset:2
140comment-column:40
141fill-column:79
142indent-tabs-mode:nil
143End:
144*/