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