]> git.saurik.com Git - apple/libc.git/blame - tests/rpmatch.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / rpmatch.c
CommitLineData
507116e3
A
1#include <stdio.h>
2#include <ctype.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <locale.h>
6
7#include <darwintest.h>
8
9static char *expectations[] = {
10 "inclonclusive", /* -1 */
11 "negative", /* 0 */
12 "affirmative" /* 1 */
13};
14
15static size_t
16dumb_strescape(char * dst, const char * in, size_t len) {
17 size_t count = 0;
18
19 while (*in && count + 3 < len) {
20 switch (*in) {
21 case '\"':
22 *dst++ = '\\';
23 *dst++ = '\"';
24 count += 2;
25 break;
26 case '\'':
27 *dst++ = '\\';
28 *dst++ = '\"';
29 count += 2;
30 break;
31 case '\\':
32 *dst++ = '\\';
33 *dst++ = '\\';
34 count += 2;
35 break;
36 case '\a':
37 *dst++ = '\\';
38 *dst++ = 'a';
39 count += 2;
40 break;
41 case '\b':
42 *dst++ = '\\';
43 *dst++ = 'b';
44 count += 2;
45 break;
46 case '\n':
47 *dst++ = '\\';
48 *dst++ = 'n';
49 count += 2;
50 break;
51 case '\t':
52 *dst++ = '\\';
53 *dst++ = 't';
54 count += 2;
55 break;
56 /* There are many more special cases */
57 default:
58 if (iscntrl(*in)) {
59 count += (size_t)snprintf(dst, len - count, "\\%03o", *in);
60 }
61 else {
62 *dst++ = *in;
63 count++;
64 }
65 }
66 in++;
67 }
68 *dst++ = '\0';
69 return count;
70}
71
72static void
73rpmatch_testcase(const char * response, int expectation) {
74 const char * expect_msg = expectations[expectation+1];
75
76 char escaped_response[16];
77 dumb_strescape(escaped_response, response, sizeof(escaped_response)/sizeof(char));
78
79 /* darwintest should escape special characters in message strings */
80 T_EXPECT_EQ(rpmatch(response), expectation, "'%s' is %s in the %s locale", escaped_response, expect_msg, setlocale(LC_ALL, NULL));
81}
82
83T_DECL(rpmatch, "Ensure rpmatch responds to locales")
84{
85 setlocale(LC_ALL, "C");
86
87 /* Check several single character variants */
88 rpmatch_testcase("y", 1);
89 rpmatch_testcase("Y", 1);
90 rpmatch_testcase("j", -1); /* becomes afirmative in german */
91 rpmatch_testcase("J", -1);
92 rpmatch_testcase("x", -1);
93 rpmatch_testcase(" ", -1);
94 rpmatch_testcase("", -1);
95 rpmatch_testcase("n", 0);
96 rpmatch_testcase("N", 0);
97
98 /* A few full words */
99 rpmatch_testcase("yes", 1);
100 rpmatch_testcase("ja", -1);
101 rpmatch_testcase("no", 0);
102
103 /* Check each variant with a newline */
104 rpmatch_testcase("y\n", 1);
105 rpmatch_testcase("Y\n", 1);
106 rpmatch_testcase("j\n", -1);
107 rpmatch_testcase("J\n", -1);
108 rpmatch_testcase("x\n", -1);
109 rpmatch_testcase(" \n", -1);
110 rpmatch_testcase("\n", -1);
111 rpmatch_testcase("n\n", 0);
112 rpmatch_testcase("N\n", 0);
113
114 rpmatch_testcase("yes\n", 1);
115 rpmatch_testcase("ja\n", -1);
116 rpmatch_testcase("no\n", 0);
117
118 /* Do it all again in a german locale */
119 setlocale(LC_ALL, "de_DE.ISO8859-1");
120
121 if (strcmp(setlocale(LC_ALL, NULL), "de_DE.ISO8859-1") != 0) {
122 T_LOG("This system does not have a de_DE.ISO8859-1 locale");
123 return;
124 }
125
126 /* Check several single character variants */
127 rpmatch_testcase("y", 1);
128 rpmatch_testcase("Y", 1);
129 rpmatch_testcase("j", 1); /* now afirmative */
130 rpmatch_testcase("J", 1);
131 rpmatch_testcase("x", -1);
132 rpmatch_testcase(" ", -1);
133 rpmatch_testcase("", -1);
134 rpmatch_testcase("n", 0);
135 rpmatch_testcase("N", 0);
136
137 /* A few full words */
138 rpmatch_testcase("yes", 1);
139 rpmatch_testcase("ja", 1);
140 rpmatch_testcase("xx", -1);
141 rpmatch_testcase("no", 0);
142
143 /* Check each variant with a newline */
144 rpmatch_testcase("y\n", 1);
145 rpmatch_testcase("Y\n", 1);
146 rpmatch_testcase("j\n", 1);
147 rpmatch_testcase("J\n", 1);
148 rpmatch_testcase("x\n", -1);
149 rpmatch_testcase(" \n", -1);
150 rpmatch_testcase("\n", -1);
151 rpmatch_testcase("n\n", 0);
152 rpmatch_testcase("N\n", 0);
153
154 rpmatch_testcase("yes\n", 1);
155 rpmatch_testcase("ja\n", 1);
156 rpmatch_testcase("xx\n", -1);
157 rpmatch_testcase("no\n", 0);
158}