Ticket #19035: foo.c

File foo.c, 2.4 KB (added by bhaible, 2 months ago)

test case foo.c

Line 
1#include <locale.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <wchar.h>
6#include <assert.h>
7
8int main ()
9{
10 mbstate_t state;
11 wchar_t wc;
12 size_t ret;
13
14 assert (setlocale (LC_ALL, "en_US.UTF-8") != NULL);
15
16 /* Check a single-byte character. */
17 {
18 memset (&state, '\0', sizeof (mbstate_t));
19
20 wc = (wchar_t) 0xBADFACE;
21 ret = mbrtowc (&wc, "$", 1, &state);
22 assert (ret == 1);
23 assert (wc == (wchar_t) 0x0024);
24 assert (mbsinit (&state));
25 }
26
27 /* Check a 2-bytes character. */
28 {
29 memset (&state, '\0', sizeof (mbstate_t));
30
31 wc = (wchar_t) 0xBADFACE;
32 ret = mbrtowc (&wc, "\302", 1, &state);
33 assert (ret == (size_t)(-2));
34 assert (wc == (wchar_t) 0xBADFACE);
35 assert (!mbsinit (&state));
36
37 wc = (wchar_t) 0xBADFACE;
38 ret = mbrtowc (&wc, "\245", 1, &state);
39 assert (ret == 1);
40 assert (wc == (wchar_t) 0x00A5);
41 assert (mbsinit (&state));
42 }
43
44 /* Check a 3-bytes character. */
45 {
46 memset (&state, '\0', sizeof (mbstate_t));
47
48 wc = (wchar_t) 0xBADFACE;
49 ret = mbrtowc (&wc, "\342", 1, &state);
50 assert (ret == (size_t)(-2));
51 assert (wc == (wchar_t) 0xBADFACE);
52 assert (!mbsinit (&state));
53
54 wc = (wchar_t) 0xBADFACE;
55 ret = mbrtowc (&wc, "\202", 1, &state);
56 assert (ret == (size_t)(-2));
57 assert (wc == (wchar_t) 0xBADFACE);
58 assert (!mbsinit (&state));
59
60 wc = (wchar_t) 0xBADFACE;
61 ret = mbrtowc (&wc, "\254", 1, &state);
62 assert (ret == 1);
63 assert (wc == (wchar_t) 0x20AC);
64 assert (mbsinit (&state));
65 }
66
67 /* Check a 4-bytes character. */
68 {
69 memset (&state, '\0', sizeof (mbstate_t));
70
71 wc = (wchar_t) 0xBADFACE;
72 ret = mbrtowc (&wc, "\360", 1, &state);
73 assert (ret == (size_t)(-2));
74 assert (wc == (wchar_t) 0xBADFACE);
75 assert (!mbsinit (&state));
76
77 wc = (wchar_t) 0xBADFACE;
78 ret = mbrtowc (&wc, "\237", 1, &state);
79 assert (ret == (size_t)(-2));
80 assert (wc == (wchar_t) 0xBADFACE);
81 assert (!mbsinit (&state));
82
83 wc = (wchar_t) 0xBADFACE;
84 ret = mbrtowc (&wc, "\220", 1, &state);
85 assert (ret == (size_t)(-2));
86 assert (wc == (wchar_t) 0xBADFACE);
87 assert (!mbsinit (&state));
88
89 wc = (wchar_t) 0xBADFACE;
90 ret = mbrtowc (&wc, "\203", 1, &state);
91 printf ("ret = %d, wc = 0x%08X\n", (int) ret, (unsigned int) wc);
92 assert (ret == 1);
93 assert (wc == (wchar_t) 0x1F403);
94 assert (mbsinit (&state));
95 }
96}