functional: add mntent test
[libc-test.git] / src / functional / clocale_mbfuncs.c
blob2c4935ebbc728f123b11835a41f0f0d0b34d28d9
1 #include <stdio.h>
2 #include <string.h>
3 #include <wchar.h>
4 #include <wctype.h>
5 #include <stdlib.h>
6 #include <locale.h>
7 #include <langinfo.h>
8 #include <limits.h>
9 #include "test.h"
11 int main(void)
13 int i, j;
14 mbstate_t st, st2;
15 wchar_t wc, map[257], wtmp[257];
16 char s[MB_LEN_MAX*256];
17 size_t rv;
18 int c;
19 int ni_errors=0;
21 setlocale(LC_CTYPE, "C");
23 if (MB_CUR_MAX != 1) t_error("MB_CUR_MAX = %d, expected 1\n", (int)MB_CUR_MAX);
25 for (i=0; i<256; i++) {
26 st = (mbstate_t){0};
27 if (mbrtowc(&wc, &(char){i}, 1, &st) != !!i)
28 t_error("mbrtowc failed to convert byte %.2x to wchar_t\n", i);
29 if ((map[i]=btowc(i)) == WEOF) {
30 t_error("btowc failed to convert byte %.2x to wchar_t\n", i);
31 continue;
33 for (j=0; j<i; j++) {
34 if (map[j]==map[i])
35 t_error("bytes %.2x and %.2x map to same wchar_t %.4x\n", j, i, (unsigned)map[i]);
39 for (i=0; i<256; i++) {
40 if (map[i]==WEOF) continue;
41 if (wctob(map[i]) != i)
42 t_error("wctob failed to convert wchar_t %.4x back to byte %.2x\n", (unsigned)map[i], i);
45 /* covering whole 32-bit range would be too slow... maybe add random high tests? */
46 for (i=0; i<0x110000; i++) {
47 if (wcschr(map+1, i)) continue;
48 if ((c=wctob(i)) != WEOF && ni_errors++ < 50)
49 t_error("wctob accepted non-image wchar_t %.4x as byte %.2x\n", i, c);
50 st = (mbstate_t){0};
51 if (wcrtomb(s, i, &st) != -1 && ni_errors++ < 50)
52 t_error("wcrtomb accepted non-image wchar_t %.4x\n", i);
54 if (ni_errors > 50)
55 t_error("additional %d non-image errors (not printed)\n", ni_errors);
57 map[256] = 0;
58 st = (mbstate_t){0};
59 if ((rv=wcsrtombs(s, &(const wchar_t *){map+1}, sizeof s, &st)) != 255)
60 t_error("wcsrtombs returned %zd, expected 255\n", rv);
61 if ((rv=mbsrtowcs(wtmp, &(const char *){s}, 256, &st)) != 255)
62 t_error("mbsrtowcs returned %zd, expected 255\n", rv);
63 if (memcmp(map+1, wtmp, 256*sizeof(*map)))
64 t_error("wcsrtombs/mbsrtowcs round trip failed\n");
66 for (i=128; i<256; i++) {
67 if (iswalnum(map[i])) t_error("iswalnum returned true for %.4x (%.2x)\n", map[i], i);
68 if (iswalpha(map[i])) t_error("iswalpha returned true for %.4x (%.2x)\n", map[i], i);
69 if (iswblank(map[i])) t_error("iswblank returned true for %.4x (%.2x)\n", map[i], i);
70 if (iswcntrl(map[i])) t_error("iswcntrl returned true for %.4x (%.2x)\n", map[i], i);
71 if (iswdigit(map[i])) t_error("iswdigit returned true for %.4x (%.2x)\n", map[i], i);
72 if (iswgraph(map[i])) t_error("iswgraph returned true for %.4x (%.2x)\n", map[i], i);
73 if (iswlower(map[i])) t_error("iswlower returned true for %.4x (%.2x)\n", map[i], i);
74 if (iswprint(map[i])) t_error("iswprint returned true for %.4x (%.2x)\n", map[i], i);
75 if (iswpunct(map[i])) t_error("iswpunct returned true for %.4x (%.2x)\n", map[i], i);
76 if (iswspace(map[i])) t_error("iswspace returned true for %.4x (%.2x)\n", map[i], i);
77 if (iswupper(map[i])) t_error("iswupper returned true for %.4x (%.2x)\n", map[i], i);
78 if (iswxdigit(map[i])) t_error("iswxdigit returned true for %.4x (%.2x)\n", map[i], i);
81 return t_status;