Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / Posix / vis.cpp
blob0d31082f92b2529e1c083d64117e9440430c5718
1 // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
2 //
3 // UNSUPPORTED: target={{.*(linux|solaris).*}}, darwin
5 #include <ctype.h>
6 #include <err.h>
7 #include <inttypes.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <vis.h>
13 void test_vis() {
14 char visout[5];
15 int ch = toascii(0x1);
16 vis(visout, ch, VIS_SAFE | VIS_NOSLASH, 0);
17 printf("vis: %s\n", visout);
20 void test_nvis() {
21 char visout[5];
22 int ch = toascii(0x2);
23 nvis(visout, sizeof visout, ch, VIS_SAFE | VIS_NOSLASH, 0);
24 printf("nvis: %s\n", visout);
27 void test_strvis() {
28 char visout[5];
29 strvis(visout, "\3", VIS_SAFE | VIS_NOSLASH);
30 printf("strvis: %s\n", visout);
33 void test_stravis() {
34 char *visout;
35 stravis(&visout, "\4", VIS_SAFE | VIS_NOSLASH);
36 printf("stravis: %s\n", visout);
37 free(visout);
40 void test_strnvis() {
41 char visout[5];
42 strnvis(visout, sizeof visout, "\5", VIS_SAFE | VIS_NOSLASH);
43 printf("strnvis: %s\n", visout);
46 void test_strvisx() {
47 char visout[5];
48 char src[] = "\6";
49 strvisx(visout, src, sizeof src - 1 /* skip final \0 */,
50 VIS_SAFE | VIS_NOSLASH);
51 printf("strvisx: %s\n", visout);
54 void test_strnvisx() {
55 char visout[5];
56 char src[] = "\1";
57 strnvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,
58 VIS_SAFE | VIS_NOSLASH);
59 printf("strnvisx: %s\n", visout);
62 void test_strenvisx() {
63 char visout[5];
64 char src[] = "\2";
65 strenvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,
66 VIS_SAFE | VIS_NOSLASH, NULL);
67 printf("strenvisx: %s\n", visout);
70 void test_svis() {
71 char visout[5];
72 int ch = toascii(0x3);
73 svis(visout, ch, VIS_SAFE | VIS_NOSLASH, 0, "x");
74 printf("svis: %s\n", visout);
77 void test_snvis() {
78 char visout[5];
79 int ch = toascii(0x2);
80 snvis(visout, sizeof visout, ch, VIS_SAFE | VIS_NOSLASH, 0, "x");
81 printf("snvis: %s\n", visout);
84 void test_strsvis() {
85 char visout[5];
86 strsvis(visout, "\4", VIS_SAFE | VIS_NOSLASH, "x");
87 printf("strsvis: %s\n", visout);
90 void test_strsnvis() {
91 char visout[5];
92 strsnvis(visout, sizeof visout, "\5", VIS_SAFE | VIS_NOSLASH, "x");
93 printf("strsnvis: %s\n", visout);
96 void test_strsvisx() {
97 char visout[5];
98 char src[] = "\5";
99 strsvisx(visout, src, sizeof src - 1 /* skip final \0 */,
100 VIS_SAFE | VIS_NOSLASH, "x");
101 printf("strsvisx: %s\n", visout);
104 void test_strsnvisx() {
105 char visout[5];
106 char src[] = "\6";
107 strsnvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,
108 VIS_SAFE | VIS_NOSLASH, "x");
109 printf("strsnvisx: %s\n", visout);
112 void test_strsenvisx() {
113 char visout[5];
114 char src[] = "\1";
115 strsenvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,
116 VIS_SAFE | VIS_NOSLASH, "x", NULL);
117 printf("strsenvisx: %s\n", visout);
120 void test_unvis() {
121 char visout[5];
122 int ch = toascii(0x1);
123 vis(visout, ch, VIS_SAFE, 0);
125 int state = 0;
126 char out;
127 char *p = visout;
128 while ((ch = *(p++)) != '\0') {
129 again:
130 switch (unvis(&out, ch, &state, 0)) {
131 case 0:
132 case UNVIS_NOCHAR:
133 break;
134 case UNVIS_VALID:
135 printf("unvis: %" PRIx8 "\n", (unsigned char)out);
136 break;
137 case UNVIS_VALIDPUSH:
138 printf("unvis: %" PRIx8 "\n", (unsigned char)out);
139 goto again;
140 case UNVIS_SYNBAD:
141 errx(1, "Bad character sequence!");
144 if (unvis(&out, '\0', &state, UNVIS_END) == UNVIS_VALID)
145 printf("unvis: %" PRIx8 "\n", (unsigned char)out);
148 void test_strunvis() {
149 char visout[5];
150 int ch = toascii(0x2);
151 vis(visout, ch, VIS_SAFE, 0);
153 char p[5];
154 strunvis(p, visout);
156 char *pp = p;
157 while ((ch = *(pp++)) != '\0')
158 printf("strunvis: %" PRIx8 "\n", (unsigned char)ch);
161 void test_strnunvis() {
162 char visout[5];
163 int ch = toascii(0x3);
164 vis(visout, ch, VIS_SAFE, 0);
166 char p[5];
167 strnunvis(p, sizeof p, visout);
169 char *pp = p;
170 while ((ch = *(pp++)) != '\0')
171 printf("strnunvis: %" PRIx8 "\n", (unsigned char)ch);
174 void test_strunvisx() {
175 char visout[5];
176 int ch = toascii(0x4);
177 vis(visout, ch, VIS_SAFE, 0);
179 char p[5];
180 strunvisx(p, visout, VIS_SAFE);
182 char *pp = p;
183 while ((ch = *(pp++)) != '\0')
184 printf("strunvisx: %" PRIx8 "\n", (unsigned char)ch);
187 void test_strnunvisx() {
188 char visout[5];
189 int ch = toascii(0x5);
190 vis(visout, ch, VIS_SAFE, 0);
192 char p[5];
193 strnunvisx(p, sizeof p, visout, VIS_SAFE);
195 char *pp = p;
196 while ((ch = *(pp++)) != '\0')
197 printf("strnunvisx: %" PRIx8 "\n", (unsigned char)ch);
200 int main(void) {
201 printf("vis\n");
203 test_vis();
204 test_nvis();
205 test_strvis();
206 test_stravis();
207 test_strnvis();
208 test_strvisx();
209 test_strnvisx();
210 test_strenvisx();
211 test_svis();
212 test_snvis();
213 test_strsvis();
214 test_strsnvis();
215 test_strsvisx();
216 test_strsnvisx();
217 test_strsenvisx();
218 test_unvis();
219 test_strunvis();
220 test_strnunvis();
221 test_strunvisx();
222 test_strnunvisx();
224 // CHECK: vis
225 // CHECK: vis: ^A
226 // CHECK: nvis: ^B
227 // CHECK: strvis: ^C
228 // CHECK: stravis: ^D
229 // CHECK: strnvis: ^E
230 // CHECK: strvisx: ^F
231 // CHECK: strnvisx: ^A
232 // CHECK: strenvisx: ^B
233 // CHECK: svis: ^C
234 // CHECK: snvis: ^B
235 // CHECK: strsvis: ^D
236 // CHECK: strsnvis: ^E
237 // CHECK: strsvisx: ^E
238 // CHECK: strsnvisx: ^F
239 // CHECK: strsenvisx: ^A
240 // CHECK: unvis: 1
241 // CHECK: strunvis: 2
242 // CHECK: strnunvis: 3
243 // CHECK: strunvisx: 4
244 // CHECK: strnunvisx: 5
246 return 0;