unit_str_tokens_remove.c (14174B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2017-2021 Karlson2k (Evgeny Grin) 4 5 This test tool is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 2, or 8 (at your option) any later version. 9 10 This test tool is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /** 21 * @file src/tests/unit/unit_str_tokens_remove.c 22 * @brief Unit tests for mhd_str_remove_tokens_caseless() function 23 * @author Karlson2k (Evgeny Grin) 24 */ 25 26 #include "mhd_sys_options.h" 27 28 #include <string.h> 29 #include <stdio.h> 30 31 #include "mhd_str.h" 32 #include "mhd_str.c" 33 34 #include "mhd_assert.h" 35 36 37 #ifndef MHD_STATICSTR_LEN_ 38 /** 39 * Determine length of static string / macro strings at compile time. 40 */ 41 #define MHD_STATICSTR_LEN_(macro) (sizeof(macro) / sizeof(char) - 1) 42 #endif /* ! MHD_STATICSTR_LEN_ */ 43 44 45 static int 46 expect_result_n (const char *str, size_t str_len, 47 const char *tokens, size_t tokens_len, 48 const char *expected, size_t expected_len, 49 const bool expected_removed) 50 { 51 char buf_in[1024]; 52 char buf_tokens[256]; 53 bool res; 54 size_t result_len; 55 56 mhd_assert (sizeof(buf_in) > str_len + 2); 57 mhd_assert (sizeof(buf_tokens) > tokens_len + 2); 58 59 memset (buf_tokens, '#', sizeof(buf_tokens)); 60 memcpy (buf_tokens, tokens, tokens_len); /* Copy without zero-termination */ 61 memset (buf_in, '$', sizeof(buf_in)); 62 memcpy (buf_in, str, str_len); /* Copy without zero-termination */ 63 64 result_len = str_len; 65 66 res = mhd_str_remove_tokens_caseless (buf_in, &result_len, 67 buf_tokens, tokens_len); 68 69 if ( (expected_removed != res) || 70 (expected_len != result_len) || 71 ((0 != result_len) && (0 != memcmp (expected, buf_in, result_len))) || 72 ('$' != buf_in[str_len])) 73 { 74 fprintf (stderr, 75 "mhd_str_remove_tokens_caseless() FAILED:\n" 76 "\tRESULT: " 77 "\tmhd_str_remove_token_caseless(\"%s\"->\"%.*s\", &(%lu->%lu)," 78 " \"%.*s\", %lu) returned %s\n", 79 str, 80 (int) result_len, buf_in, 81 (unsigned long) str_len, (unsigned long) result_len, 82 (int) tokens_len, buf_tokens, (unsigned long) tokens_len, 83 res ? "true" : "false"); 84 fprintf (stderr, 85 "\tEXPECTED: " 86 "\tmhd_str_remove_token_caseless(\"%s\"->\"%s\", &(%lu->%lu)," 87 " \"%.*s\", %lu) returned %s\n", 88 str, 89 expected, 90 (unsigned long) str_len, (unsigned long) expected_len, 91 (int) tokens_len, buf_tokens, (unsigned long) tokens_len, 92 expected_removed ? "true" : "false"); 93 return 1; 94 } 95 return 0; 96 } 97 98 99 #define expect_result(s,t,e,found) \ 100 expect_result_n ((s),MHD_STATICSTR_LEN_ (s), \ 101 (t),MHD_STATICSTR_LEN_ (t), \ 102 (e),MHD_STATICSTR_LEN_ (e), found) 103 104 static int 105 check_result (void) 106 { 107 int errcount = 0; 108 errcount += expect_result ("string", "string", "", true); 109 errcount += expect_result ("String", "string", "", true); 110 errcount += expect_result ("string", "String", "", true); 111 errcount += expect_result ("strinG", "String", "", true); 112 errcount += expect_result ("strinG", "String\t", "", true); 113 errcount += expect_result ("strinG", "\tString", "", true); 114 errcount += expect_result ("tOkEn", " \t toKEN ", "", true); 115 errcount += expect_result ("not-token, tOkEn", "token", "not-token", 116 true); 117 errcount += expect_result ("not-token1, tOkEn1, token", "token1", 118 "not-token1, token", 119 true); 120 errcount += expect_result ("token, tOkEn1", "token1", "token", 121 true); 122 errcount += expect_result ("not-token, tOkEn", " \t toKEN", "not-token", 123 true); 124 errcount += expect_result ("not-token, tOkEn, more-token", "toKEN\t", 125 "not-token, more-token", true); 126 errcount += expect_result ("not-token, tOkEn, more-token", "\t toKEN,,,,,", 127 "not-token, more-token", true); 128 errcount += expect_result ("a, b, c, d", ",,,,,a", "b, c, d", true); 129 errcount += expect_result ("a, b, c, d", "a,,,,,,", "b, c, d", true); 130 errcount += expect_result ("a, b, c, d", ",,,,a,,,,,,", "b, c, d", true); 131 errcount += expect_result ("a, b, c, d", "\t \t,,,,a,, , ,,,\t", 132 "b, c, d", true); 133 errcount += expect_result ("a, b, c, d", "b, c, d", "a", true); 134 errcount += expect_result ("a, b, c, d", "a, b, c, d", "", true); 135 errcount += expect_result ("a, b, c, d", "d, c, b, a", "", true); 136 errcount += expect_result ("a, b, c, d", "b, d, a, c", "", true); 137 errcount += expect_result ("a, b, c, d, e", "b, d, a, c", "e", true); 138 errcount += expect_result ("e, a, b, c, d", "b, d, a, c", "e", true); 139 errcount += expect_result ("e, a, b, c, d, e", "b, d, a, c", "e, e", true); 140 errcount += expect_result ("a, b, c, d", "b,c,d", "a", true); 141 errcount += expect_result ("a, b, c, d", "a,b,c,d", "", true); 142 errcount += expect_result ("a, b, c, d", "d,c,b,a", "", true); 143 errcount += expect_result ("a, b, c, d", "b,d,a,c", "", true); 144 errcount += expect_result ("a, b, c, d, e", "b,d,a,c", "e", true); 145 errcount += expect_result ("e, a, b, c, d", "b,d,a,c", "e", true); 146 errcount += expect_result ("e, a, b, c, d, e", "b,d,a,c", "e, e", true); 147 errcount += expect_result ("a, b, c, d", "d,,,,,,,,,c,b,a", "", true); 148 errcount += expect_result ("a, b, c, d", "b,d,a,c,,,,,,,,,,", "", true); 149 errcount += expect_result ("a, b, c, d, e", ",,,,\t,,,,b,d,a,c,\t", "e", 150 true); 151 errcount += expect_result ("e, a, b, c, d", "b,d,a,c", "e", true); 152 errcount += expect_result ("token, a, b, c, d", "token", "a, b, c, d", true); 153 errcount += expect_result ("token1, a, b, c, d", "token1", "a, b, c, d", 154 true); 155 errcount += expect_result ("token12, a, b, c, d", "token12", "a, b, c, d", 156 true); 157 errcount += expect_result ("token123, a, b, c, d", "token123", "a, b, c, d", 158 true); 159 errcount += expect_result ("token1234, a, b, c, d", "token1234", "a, b, c, d", 160 true); 161 errcount += expect_result ("token12345, a, b, c, d", "token12345", 162 "a, b, c, d", true); 163 errcount += expect_result ("token123456, a, b, c, d", "token123456", 164 "a, b, c, d", true); 165 errcount += expect_result ("token1234567, a, b, c, d", "token1234567", 166 "a, b, c, d", true); 167 errcount += expect_result ("token12345678, a, b, c, d", "token12345678", 168 "a, b, c, d", true); 169 170 errcount += expect_result ("", "a", "", false); 171 errcount += expect_result ("", "", "", false); 172 errcount += expect_result ("a, b, c, d", "bb, dd, aa, cc", "a, b, c, d", 173 false); 174 errcount += expect_result ("a, b, c, d, e", "bb, dd, aa, cc", "a, b, c, d, e", 175 false); 176 errcount += expect_result ("e, a, b, c, d", "bb, dd, aa, cc", "e, a, b, c, d", 177 false); 178 errcount += expect_result ("e, a, b, c, d, e", "bb, dd, aa, cc", 179 "e, a, b, c, d, e", false); 180 errcount += expect_result ("aa, bb, cc, dd", "b, d, a, c", "aa, bb, cc, dd", 181 false); 182 errcount += expect_result ("aa, bb, cc, dd, ee", "b, d, a, c", 183 "aa, bb, cc, dd, ee", false); 184 errcount += expect_result ("ee, aa, bb, cc, dd", "b, d, a, c", 185 "ee, aa, bb, cc, dd", false); 186 errcount += expect_result ("ee, aa, bb, cc, dd, ee", "b, d, a, c", 187 "ee, aa, bb, cc, dd, ee", false); 188 189 errcount += expect_result ("TESt", ",,,,,,test,,,,", "", true); 190 errcount += expect_result ("TESt", ",,,,,\t,test,,,,", "", true); 191 errcount += expect_result ("TESt", ",,,,,,test, ,,,", "", true); 192 errcount += expect_result ("TESt", ",,,,,, test,,,,", "", true); 193 errcount += expect_result ("TESt", ",,,,,, test-not,test,,", "", 194 true); 195 errcount += expect_result ("TESt", ",,,,,, test-not,,test,,", "", 196 true); 197 errcount += expect_result ("TESt", ",,,,,, test-not ,test,,", "", 198 true); 199 errcount += expect_result ("TESt", ",,,,,, test", "", true); 200 errcount += expect_result ("TESt", ",,,,,, test ", "", true); 201 errcount += expect_result ("TESt", "no-test,,,,,, test ", "", 202 true); 203 204 errcount += expect_result ("the-token, a, the-token, b, the-token, " \ 205 "the-token, c, the-token", "the-token", "a, b, c", 206 true); 207 errcount += expect_result ("aa, the-token, bb, the-token, cc, the-token, " \ 208 "the-token, dd, the-token", "the-token", 209 "aa, bb, cc, dd", true); 210 errcount += expect_result ("the-token, a, the-token, b, the-token, " \ 211 "the-token, c, the-token, e", "the-token", 212 "a, b, c, e", true); 213 errcount += expect_result ("aa, the-token, bb, the-token, cc, the-token, " \ 214 "the-token, dd, the-token, ee", "the-token", 215 "aa, bb, cc, dd, ee", true); 216 errcount += expect_result ("the-token, the-token, the-token, " \ 217 "the-token, the-token", "the-token", "", true); 218 errcount += expect_result ("the-token, a, the-token, the-token, b, " \ 219 "the-token, c, the-token, a", "c,a,b", 220 "the-token, the-token, the-token, the-token, the-token", 221 true); 222 errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \ 223 "the-token, yy, the-token, ww", "ww,zz,yy", 224 "the-token, xx, the-token, the-token, the-token, the-token", 225 true); 226 errcount += expect_result ("the-token, a, the-token, the-token, b, " \ 227 "the-token, c, the-token, a", " c,\t a,b,,,", 228 "the-token, the-token, the-token, the-token, the-token", 229 true); 230 errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \ 231 "the-token, yy, the-token, ww", 232 ",,,,ww,\t zz, yy", 233 "the-token, xx, the-token, the-token, the-token, the-token", 234 true); 235 errcount += expect_result ("the-token, a, the-token, the-token, b, " \ 236 "the-token, c, the-token, a", ",,,,c,\t a,b", 237 "the-token, the-token, the-token, the-token, the-token", 238 true); 239 errcount += expect_result ("the-token, xx, the-token, the-token, zz, " \ 240 "the-token, yy, the-token, ww", " ww,\t zz,yy,,,,", 241 "the-token, xx, the-token, the-token, the-token, the-token", 242 true); 243 errcount += expect_result ("close, 2", "close", 244 "2", true); 245 errcount += expect_result ("close, 22", "close", 246 "22", true); 247 errcount += expect_result ("close, nothing", "close", 248 "nothing", true); 249 errcount += expect_result ("close, 2", "2", 250 "close", true); 251 errcount += expect_result ("close", "close", 252 "", true); 253 errcount += expect_result ("close, nothing", "close, token", 254 "nothing", true); 255 errcount += expect_result ("close, nothing", "nothing, token", 256 "close", true); 257 errcount += expect_result ("close, 2", "close, 10, 12, 22, nothing", 258 "2", true); 259 260 errcount += expect_result ("strin", "string", "strin", false); 261 errcount += expect_result ("Stringer", "string", "Stringer", false); 262 errcount += expect_result ("sstring", "String", "sstring", false); 263 errcount += expect_result ("string", "Strin", "string", false); 264 errcount += expect_result ("String", "\t(-strinG", "String", false); 265 errcount += expect_result ("String", ")strinG\t ", "String", false); 266 errcount += expect_result ("not-token, tOkEner", "toKEN", 267 "not-token, tOkEner", false); 268 errcount += expect_result ("not-token, tOkEns, more-token", "toKEN", 269 "not-token, tOkEns, more-token", false); 270 errcount += expect_result ("tests, quest", "TESt", "tests, quest", 271 false); 272 errcount += expect_result ("test��", "TESt", "test��", false); 273 errcount += expect_result ("test-not, ��test", "TESt", 274 "test-not, ��test", false); 275 errcount += expect_result ("testing, test not, test2", "TESt", 276 "testing, test not, test2", false); 277 errcount += expect_result ("", ",,,,,,,,,,,,,,,,,,,the-token", "", false); 278 errcount += expect_result ("a1, b1, c1, d1, e1, f1, g1", "", 279 "a1, b1, c1, d1, e1, f1, g1", false); 280 281 return errcount; 282 } 283 284 285 int 286 main (int argc, char *argv[]) 287 { 288 int errcount = 0; 289 (void) argc; (void) argv; /* Unused. Silent compiler warning. */ 290 errcount += check_result (); 291 if (0 == errcount) 292 printf ("All tests were passed without errors.\n"); 293 return errcount == 0 ? 0 : 1; 294 }