Debian Codegraph @

src/tests/textprint.c

raw
1 /*
2 textprint: Test filtering of text before terminal printout.
3
4 copyright 2019-2020 by the mpg123 project
5 free software under the terms of the LGPL 2.1
6
7 see COPYING and AUTHORS files in distribution or http://mpg123.org
8 initially written by Thomas Orgis
9 */
10
11 #include "../mpg123app.h"
12 #include "../local.h"
13
14 // A number of UTF-8 strings to test.
15
16 struct test_string
17 {
18 const char* in; // UTF-8 input including control chars etc.
19 const char* ascii; // output reduced to ASCII
20 const char* utf8hack; // output in UTF-8 without controls, hacked
21 const char* utf8full; // output in UTF-8 only including printing chars
22 size_t chars; // number of apparent characters (for utf8hack)
23 size_t width; // proper printing width
24 };
25
26 // const char *bla = "..." is not constant enoug to server as initializer.
27 #define simple_string "Just a plain string without trouble."
28 #define simple_width 36
29 // o-Umlaut and a random Chinese character
30 #define utf_string "St\xc3\xb6rt mein \xe7\x8c\x95?"
31 #define utf_string_ascii "St?rt mein ??"
32 #define utf_width 14
33 #define utf_chars 13
34
35 const struct test_string test_input[] =
36 {
37 {
38 .in=simple_string
39 , .ascii=simple_string
40 , .utf8hack=simple_string
41 , .utf8full=simple_string
42 , .chars=simple_width
43 , .width=simple_width
44 }
45 , {
46 .in="U\xcc\x88""bel\nist mir."
47 , .ascii="U?bel ist mir."
48 , .utf8hack="U\xcc\x88""bel ist mir."
49 , .utf8full="U\xcc\x88""bel ist mir."
50 , .chars=14
51 , .width=13
52 }
53 , {
54 .in=utf_string
55 , .ascii=utf_string_ascii
56 , .utf8hack=utf_string
57 , .utf8full=utf_string
58 , .chars=utf_chars
59 , .width=utf_width
60 }
61 , {
62 .in="Gimme\x0a some\x0a\x0d""con\302\237tr\xc3\xb6l!\x7f\x7f""b \033]0;xtermed\x07"
63 , .ascii="Gimme some contr?l!b ]0;xtermed"
64 , .utf8hack="Gimme some contr\xc3\xb6l!b ]0;xtermed"
65 , .utf8full="Gimme some contr\xc3\xb6l!b ]0;xtermed"
66 , .chars=33
67 , .width=33
68 }
69 };
70
71
72 int main()
73 {
74 char *dst = NULL;
75 const size_t test_count = sizeof(test_input)/sizeof(*test_input);
76 int err = 0;
77
78 // First, without any locale check, we should work like in C locale.
79 // Only ASCII is safe.
80 fprintf(stderr, "Testing plain ASCII world.\n");
81 for(size_t t=0; t<test_count; ++t)
82 {
83 int lerr = 0;
84 fprintf(stderr, "string %zu: ", t);
85 size_t w = utf8outstr(&dst, test_input[t].in, 1);
86 size_t l = strlen(test_input[t].ascii);
87 if(!dst)
88 {
89 ++lerr;
90 fprintf(stderr, "(conversion failed) ");
91 }
92 else if(w == l)
93 {
94 if(strcmp(test_input[t].ascii, dst))
95 {
96 ++lerr;
97 fprintf(stderr, "(mismatch) ");
98 }
99 }
100 else
101 {
102 ++lerr;
103 fprintf(stderr, "(bad length %zu != %zu) ", w, l);
104 }
105 err += lerr;
106 fprintf(stderr, "%s\n", lerr ? "FAIL" : "PASS");
107 }
108
109 // Now, the internal UTF-8 sanitation is employed, assuming no working
110 // locale to help us.
111 utf8env = 1;
112 utf8loc = 0;
113 fprintf(stderr, "Testing hacked UTF-8 world.\n");
114 for(size_t t=0; t<test_count; ++t)
115 {
116 int lerr = 0;
117 fprintf(stderr, "string %zu: ", t);
118 size_t w = utf8outstr(&dst, test_input[t].in, 1);
119 size_t l = test_input[t].chars;
120 if(!dst)
121 {
122 ++lerr;
123 fprintf(stderr, "(conversion failed) ");
124 }
125 else if(w == l)
126 {
127 if(strcmp(test_input[t].utf8hack, dst))
128 {
129 ++lerr;
130 fprintf(stderr, "(mismatch) ");
131 }
132 }
133 else
134 {
135 ++lerr;
136 fprintf(stderr, "(bad length %zu != %zu) ", w, l);
137 }
138 err += lerr;
139 fprintf(stderr, "%s\n", lerr ? "FAIL" : "PASS");
140 }
141
142 #if defined(HAVE_MBSTOWCS) && defined(HAVE_WCSWIDTH) && \
143 defined(HAVE_ISWPRINT) && defined(HAVE_WCSTOMBS)
144 // Finally, proper UTF-8 handling including iswprint() shall be undertaken.
145 // The test is skipped if there is no UTF-8 locale to work with.
146 utf8env = 0;
147 utf8loc = 0;
148 utf8force = 1;
149 check_locale();
150 if(utf8env && utf8loc)
151 {
152 fprintf(stderr, "Got locale, testing proper workings.\n");
153 for(size_t t=0; t<test_count; ++t)
154 {
155 int lerr = 0;
156 fprintf(stderr, "string %zu: ", t);
157 size_t w = utf8outstr(&dst, test_input[t].in, 1);
158 size_t l = test_input[t].width;
159 if(!dst)
160 {
161 ++lerr;
162 fprintf(stderr, "(conversion failed) ");
163 }
164 else if(w == l)
165 {
166 if(strcmp(test_input[t].utf8full, dst))
167 {
168 ++lerr;
169 fprintf(stderr, "(mismatch) ");
170 }
171 }
172 else
173 {
174 ++lerr;
175 fprintf(stderr, "(bad length %zu != %zu) ", w, l);
176 }
177 err += lerr;
178 fprintf(stderr, "%s\n", lerr ? "FAIL" : "PASS");
179 fprintf(stderr, "non-terminal non-filtering: ");
180 lerr = 0;
181 w = utf8outstr(&dst, test_input[t].in, 0);
182 if(!dst || strcmp(test_input[t].in, dst))
183 ++lerr;
184 err += lerr;
185 fprintf(stderr, "%s\n", lerr ? "FAIL" : "PASS");
186 }
187 }
188 else
189 #endif
190 fprintf(stderr, "Skipped locale-based conversion\n");
191
192 free(dst);
193
194 printf("%s\n", err ? "FAIL" : "PASS");
195 return err ? 1 : 0;
196 }