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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# include <inttypes.h>
# include <setjmp.h>
# include <stdarg.h>
# include <stdint.h>
# include <stdio.h>
# include <stdlib.h>
# include <u8c/SIZE_C.h>
# include <u8c/err.h>
# include <u8c/fmt.h>
# include <u8c/is.h>
# include <u8c/main.h>
# include <u8c/u32.h>
# include <u8c/u8.h>
static void testmsg(char const * fmt,...) {
va_list args;
va_start(args,fmt);
/* printf("\n+->\n| \x1b[38:2:169:225:61mTesting\x1b[0m \""); */ /* This command works in all of the terminals I tested, except Konsole (whic is funny, because it's xterm-based, and xterm supports it). */
printf("\n+->\n| \x1b[38;2;169;225;61mTesting\x1b[0m \"");
vprintf(fmt,args);
printf("\"...\n+->\n\n");
va_end(args);
}
static void testmsgdone() {
/* printf("\n+->\n| \x1b[38:2::61:225:169mDone\x1b[0m!\n+->\n"); */
printf("\n+->\n| \x1b[38;2;61;225;169mDone\x1b[0m!\n+->\n");
}
static void errhandl(enum u8c_errtyp errtyp) {
printf(":: Error handler called with type %d.\n",(int)errtyp);
}
int main(void) {
if(u8c_init()) {
printf("Unable to initialise u8c!\n");
exit(EXIT_FAILURE);
}
u8c_regerrhandl(u8c_errtyp_all,errhandl);
u8c_setfmt(UINT8_C(0xC),UINT8_C(0x1));
printf("u8c version: %" PRIXLEAST64 ".\n",u8c_ver);
printf("Debug build: %" PRIXLEAST8 ".\n",u8c_dbg);
printf("Thread safe: %" PRIXLEAST8 ".\n",u8c_thrdsafe);
testmsg("Error messages");
{
char32_t const * err = NULL;
u8c_geterr(NULL,&err);
printf("default error message: ");
u8c_println(stdout,err);
u8c_seterr(U"Gluchwein!",u8c_errtyp_deferr);
u8c_geterr(NULL,&err);
printf("set error message: ");
u8c_println(stdout,err);
u8c_u32free(&err);
}
testmsgdone();
testmsg("UTF-8 encoding/decoding");
{
char32_t const * msg0 = U"¢,ह,𐍈,€,↊,👋";
unsigned char const * msg1 = NULL;
u8c_u8enc(NULL,&msg1,msg0);
msg0 = NULL;
printf("Encoded: %s\n",msg1);
u8c_u8dec(NULL,&msg0,msg1);
u8c_u8enc(NULL,&msg1,msg0);
printf("Encoded -> Decoded -> Encoded: %s\n",msg1);
u8c_u32free(&msg0);
u8c_u8free(&msg1);
}
testmsgdone();
testmsg("Printing (u8c_print)");
{
u8c_print(stdout,U"Hello");
u8c_print(stdout,U" ðere!");
}
testmsgdone();
testmsg("Printing (u8c_println)");
{
u8c_println(stdout,U"Hello");
u8c_println(stdout,U" ðere!");
}
testmsgdone();
testmsg("Text formatting");
{
u8c_println(stdout,U"The \uFFFCnumber\uFFFC is \uFFFC.",u8c_fmttyp_fgcol,u8c_col_mint,u8c_fmttyp_fgcol0,u8c_fmttyp_int,(int_least64_t){-0x10});
}
testmsgdone();
testmsg("Colour text");
{
u8c_println(stdout,U"\uFFFCred\uFFFCorange\uFFFCyellow\uFFFCchartreuse\uFFFCgreen\uFFFCmint\uFFFCcyan\uFFFCazure\uFFFCblue\uFFFCviolet\uFFFCmagenta\uFFFCrose\uFFFC",u8c_fmttyp_fgcol,u8c_col_red,u8c_fmttyp_fgcol,u8c_col_orange,u8c_fmttyp_fgcol,u8c_col_yellow,u8c_fmttyp_fgcol,u8c_col_chartreuse,u8c_fmttyp_fgcol,u8c_col_green,u8c_fmttyp_fgcol,u8c_col_mint,u8c_fmttyp_fgcol,u8c_col_cyan,u8c_fmttyp_fgcol,u8c_col_azure,u8c_fmttyp_fgcol,u8c_col_blue,u8c_fmttyp_fgcol,u8c_col_violet,u8c_fmttyp_fgcol,u8c_col_magenta,u8c_fmttyp_fgcol,u8c_col_rose,u8c_fmttyp_fgcol0);
}
testmsgdone();
testmsg("Combining characters");
{
for(register uint_least32_t n = UINT32_C(0x300);n <= UINT32_C(0x36F);n += UINT32_C(0x1)) {
u8c_print(stdout,(uint_least32_t[]){UINT32_C(0x61),n,UINT32_C(0x20),UINT32_C(0x0),});
fflush(stdout);
}
u8c_print(stdout,U"\n");
}
testmsgdone();
testmsg("String comparison");
{
char32_t const * str0 = U"Hello";
char32_t const * str1 = U"Hello";
char32_t const * str2 = U"Goodbye";
printf("str0: ");
u8c_println(stdout,str0);
printf("str1: ");
u8c_println(stdout,str1);
printf("str2: ");
u8c_println(stdout,str2);
uint_least8_t res = UINT8_C(0x0);
u8c_u32cmp(&res,str0,str1);
printf("str0,str1: %" PRIXLEAST8 ".\n",res);
u8c_u32cmp(&res,str1,str2);
printf("str0,str2: %" PRIXLEAST8 ".\n",res);
u8c_u32cmp(&res,str2,str1);
printf("str2,str1: %" PRIXLEAST8 ".\n",res);
}
testmsgdone();
testmsg("u8c_isalnum");
{
for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) {
uint_least8_t res = UINT8_C(0x0);
u8c_isalnum(&res,n);
if(res) {
u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),});
}
}
u8c_println(stdout,U"");
}
testmsgdone();
testmsg("u8c_isalpha");
{
for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) {
uint_least8_t res = UINT8_C(0x0);
u8c_isalpha(&res,n);
if(res) {
u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),});
}
}
u8c_println(stdout,U"");
}
testmsgdone();
testmsg("u8c_isdigit");
{
for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) {
uint_least8_t res = UINT8_C(0x0);
u8c_isdigit(&res,n);
if(res) {
u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),});
}
}
u8c_println(stdout,U"");
}
testmsgdone();
testmsg("u8c_ispunct"); /* This test appears broken on some incomplete fonts, altough it is not. */
{
for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) {
uint_least8_t res = UINT8_C(0x0);
u8c_ispunct(&res,n);
if(res) {
u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),});
}
}
u8c_println(stdout,U"");
}
testmsgdone();
testmsg("UTF-32 string literals");
{
u8c_println(stdout,U"Can you see ðis?");
}
testmsgdone();
testmsg("string concatenation");
{
char32_t const * str0 = U"Free_as_in";
char32_t const * str1 = U"_freedom!";
char32_t const * str2 = NULL;
u8c_u32cat(NULL,&str2,str0,str1);
printf("string #0: ");
u8c_println(stdout,str0);
printf("string #1: ");
u8c_println(stdout,str1);
printf("string #2: ");
u8c_println(stdout,str2);
u8c_u32free(&str2);
}
testmsgdone();
testmsg("sub-strings");
{
char32_t const * str0 = U"I_wish_to_suck_big_duck.";
char32_t const * str1 = NULL;
u8c_u32substr(&str1,SIZE_C(0x0),SIZE_C(0xE),str0);
char32_t const * str2 = NULL;
u8c_u32substr(&str2,SIZE_C(0xF),SIZE_C(0x0),str0);
printf("string #0: ");
u8c_println(stdout,str0);
printf("string #1: ");
u8c_println(stdout,str1);
printf("string #2: ");
u8c_println(stdout,str2);
u8c_u32free(&str1);
u8c_u32free(&str2);
}
testmsgdone();
testmsg("u8c_isxdigit");
{
for(register uint_least32_t n = UINT32_C(0x1);n <= UINT32_C(0x10FFFF);n += UINT32_C(0x1)) {
uint_least8_t res = UINT8_C(0x0);
u8c_isxdigit(&res,n);
if(res) {
u8c_print(stdout,(uint_least32_t[]){n,UINT32_C(0x20),UINT32_C(0x0),});
}
}
u8c_println(stdout,(uint_least32_t[]){UINT32_C(0x0),});
}
testmsgdone();
testmsg("Printing (u8c_dbgprint)");
{
u8c_dbgprint(U"Hello");
u8c_dbgprint(U" ðere!");
}
testmsgdone();
testmsg("u8c_u32fndchr");
{
char32_t const * str = U"Proprietary as in Micro$oft.";
size_t pos0 = SIZE_C(0x0);
size_t pos1 = SIZE_C(0x0);
u8c_u32fndchr(&pos0,str,U'M');
u8c_u32fndchr(&pos1,str,U'ŋ');
printf("string: ");
u8c_println(stdout,str);
printf("Position of 'M': %zu\n",pos0);
printf("Position of 'ŋ': %zu\n",pos1);
}
testmsgdone();
testmsg("u8c_u32fndpat");
{
char32_t const * str = U"Proprietary as in Micro$oft.";
size_t pos0 = SIZE_C(0x0);
size_t pos1 = SIZE_C(0x0);
u8c_u32fndpat(&pos0,str,U"as in");
u8c_u32fndpat(&pos1,str,U"forever");
printf("string: ");
u8c_println(stdout,str);
printf("Position of \"as in\": %zu\n",pos0);
printf("Position of \"forever\": %zu\n",pos1);
}
testmsgdone();
testmsg("string insertion");
{
char32_t const * str0 = U"There_is_I_love.";
char32_t const * str1 = U"just_somebody_that_";
char32_t const * str2 = NULL;
u8c_u32ins(NULL,&str2,SIZE_C(0x9),str0,str1);
printf("String #0: ");
u8c_println(stdout,str0);
printf("String #1: ");
u8c_println(stdout,str1);
printf("String #2: ");
u8c_println(stdout,str2);
u8c_u32free(&str2);
}
testmsgdone();
u8c_end();
exit(EXIT_SUCCESS);
}
|