Mon, 20 Feb 2017 16:04:14 +0100
adds new test case for sstrsplit: string ends with delimiter but empty string exceeds list bound
olaf@108 | 1 | /* |
olaf@108 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@108 | 3 | * |
universe@225 | 4 | * Copyright 2016 Olaf Wintermann. All rights reserved. |
olaf@108 | 5 | * |
olaf@108 | 6 | * Redistribution and use in source and binary forms, with or without |
olaf@108 | 7 | * modification, are permitted provided that the following conditions are met: |
olaf@108 | 8 | * |
olaf@108 | 9 | * 1. Redistributions of source code must retain the above copyright |
olaf@108 | 10 | * notice, this list of conditions and the following disclaimer. |
olaf@108 | 11 | * |
olaf@108 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
olaf@108 | 13 | * notice, this list of conditions and the following disclaimer in the |
olaf@108 | 14 | * documentation and/or other materials provided with the distribution. |
olaf@108 | 15 | * |
olaf@108 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
olaf@108 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
olaf@108 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
olaf@108 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
olaf@108 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
olaf@108 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
olaf@108 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
olaf@108 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
olaf@108 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
olaf@108 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
olaf@108 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@108 | 27 | */ |
olaf@108 | 28 | |
olaf@108 | 29 | #include "prop_tests.h" |
olaf@147 | 30 | #include "ucx/mempool.h" |
olaf@108 | 31 | |
universe@134 | 32 | UCX_TEST(test_ucx_properties_new) { |
olaf@110 | 33 | UcxProperties *parser = ucx_properties_new(); |
olaf@108 | 34 | |
olaf@108 | 35 | UCX_TEST_BEGIN |
olaf@108 | 36 | |
olaf@108 | 37 | UCX_TEST_ASSERT(parser != NULL, "failed"); |
olaf@108 | 38 | UCX_TEST_ASSERT(parser->buffer == NULL, "parser has buffer"); |
olaf@108 | 39 | UCX_TEST_ASSERT(parser->tmp == NULL, "parser has tmp buffer"); |
olaf@108 | 40 | |
olaf@108 | 41 | UCX_TEST_END |
olaf@108 | 42 | |
olaf@110 | 43 | ucx_properties_free(parser); |
olaf@108 | 44 | } |
olaf@108 | 45 | |
universe@134 | 46 | UCX_TEST(test_ucx_properties_next) { |
olaf@109 | 47 | const char *tests[] = { |
olaf@108 | 48 | "name = value\n", |
olaf@108 | 49 | "name=value\n", |
olaf@108 | 50 | "n=value\n", |
olaf@108 | 51 | "name=v\n", |
olaf@108 | 52 | "n=v\n", |
olaf@108 | 53 | "name = value # comment\n", |
olaf@108 | 54 | "#comment\nn=v\n", |
olaf@108 | 55 | "# comment1\n# comment2\n\n \n\nname = value\n", |
olaf@108 | 56 | " name = value\n", |
olaf@108 | 57 | "name = value\n\n" |
olaf@108 | 58 | }; |
olaf@108 | 59 | |
olaf@109 | 60 | const char *names[] = { |
olaf@108 | 61 | "name", |
olaf@108 | 62 | "name", |
olaf@108 | 63 | "n", |
olaf@108 | 64 | "name", |
olaf@108 | 65 | "n", |
olaf@108 | 66 | "name", |
olaf@108 | 67 | "n", |
olaf@108 | 68 | "name", |
olaf@108 | 69 | "name", |
olaf@108 | 70 | "name" |
olaf@108 | 71 | }; |
olaf@108 | 72 | |
olaf@109 | 73 | const char *values[] = { |
olaf@108 | 74 | "value", |
olaf@108 | 75 | "value", |
olaf@108 | 76 | "value", |
olaf@108 | 77 | "v", |
olaf@108 | 78 | "v", |
olaf@108 | 79 | "value", |
olaf@108 | 80 | "v", |
olaf@108 | 81 | "value", |
olaf@108 | 82 | "value", |
olaf@108 | 83 | "value" |
olaf@108 | 84 | }; |
olaf@108 | 85 | |
olaf@108 | 86 | UCX_TEST_BEGIN |
olaf@108 | 87 | |
olaf@108 | 88 | sstr_t name; |
olaf@108 | 89 | sstr_t value; |
olaf@108 | 90 | |
olaf@108 | 91 | for(int i=0;i<10;i++) { |
olaf@110 | 92 | UcxProperties *parser = ucx_properties_new(); |
olaf@108 | 93 | |
olaf@110 | 94 | ucx_properties_fill(parser, (char*)tests[i], strlen(tests[i])); |
olaf@108 | 95 | UCX_TEST_ASSERT(parser->buffer == tests[i], "fill failed"); |
olaf@108 | 96 | UCX_TEST_ASSERT(parser->buflen == strlen(tests[i]), "wrong buflen"); |
olaf@108 | 97 | |
olaf@110 | 98 | int r = ucx_properties_next(parser, &name, &value); |
olaf@109 | 99 | sstr_t n = sstr((char*)names[i]); |
olaf@109 | 100 | sstr_t v = sstr((char*)values[i]); |
olaf@110 | 101 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@108 | 102 | UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong property name"); |
olaf@108 | 103 | UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong property value"); |
olaf@108 | 104 | |
olaf@110 | 105 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 106 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 107 | UCX_TEST_ASSERT(parser->tmp == NULL, "tmp not NULL"); |
olaf@108 | 108 | UCX_TEST_ASSERT(parser->tmpcap == 0, "tmpcap not NULL"); |
olaf@108 | 109 | UCX_TEST_ASSERT(parser->tmplen == 0, "tmplen not NULL"); |
olaf@108 | 110 | |
olaf@110 | 111 | ucx_properties_free(parser); |
olaf@108 | 112 | } |
olaf@108 | 113 | |
olaf@108 | 114 | UCX_TEST_END |
olaf@108 | 115 | } |
olaf@108 | 116 | |
universe@134 | 117 | UCX_TEST(test_ucx_properties_next_multi) { |
olaf@109 | 118 | const char *names[] = { |
olaf@108 | 119 | "a", |
olaf@108 | 120 | "b", |
olaf@108 | 121 | "c", |
olaf@108 | 122 | "uap", |
olaf@108 | 123 | "name", |
olaf@108 | 124 | "key1", |
olaf@108 | 125 | "key2", |
olaf@108 | 126 | "key3" |
olaf@108 | 127 | }; |
olaf@108 | 128 | |
olaf@109 | 129 | const char *values[] = { |
olaf@108 | 130 | "a value", |
olaf@108 | 131 | "b value", |
olaf@108 | 132 | "core", |
olaf@108 | 133 | "core", |
olaf@108 | 134 | "ucx", |
olaf@108 | 135 | "value1", |
olaf@108 | 136 | "value2", |
olaf@108 | 137 | "value3" |
olaf@108 | 138 | }; |
olaf@108 | 139 | |
olaf@109 | 140 | const char *str = "#\n" |
olaf@108 | 141 | "# properties\n" |
olaf@108 | 142 | "# contains key/value pairs\n" |
olaf@108 | 143 | "#\n" |
olaf@108 | 144 | "a = a value\n" |
olaf@108 | 145 | "b = b value\n" |
olaf@108 | 146 | "c = core\n" |
olaf@108 | 147 | "\n# test\n" |
olaf@108 | 148 | "uap = core\n" |
olaf@108 | 149 | "name = ucx\n" |
olaf@108 | 150 | "# no = property\n" |
olaf@108 | 151 | "key1 = value1\n" |
olaf@108 | 152 | "#key1 = wrong value\n" |
olaf@108 | 153 | "#key2 = not value 2\n" |
olaf@108 | 154 | "key2 = value2\n" |
olaf@108 | 155 | "\n\n\n \n key3=value3\n"; |
olaf@108 | 156 | |
olaf@110 | 157 | UcxProperties *parser = ucx_properties_new(); |
olaf@108 | 158 | |
olaf@108 | 159 | UCX_TEST_BEGIN |
olaf@108 | 160 | |
olaf@110 | 161 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@108 | 162 | |
olaf@108 | 163 | sstr_t name; |
olaf@108 | 164 | sstr_t value; |
olaf@108 | 165 | for(int i=0;i<8;i++) { |
olaf@110 | 166 | int r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 167 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@109 | 168 | UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)names[i]))), "wrong name"); |
olaf@109 | 169 | UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)values[i]))), |
olaf@109 | 170 | "wrong value"); |
olaf@108 | 171 | } |
olaf@110 | 172 | int r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 173 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 174 | |
olaf@108 | 175 | UCX_TEST_END |
olaf@108 | 176 | |
olaf@110 | 177 | ucx_properties_free(parser); |
olaf@108 | 178 | } |
olaf@108 | 179 | |
universe@134 | 180 | UCX_TEST(test_ucx_properties_next_part) { |
olaf@110 | 181 | UcxProperties *parser = ucx_properties_new(); |
olaf@109 | 182 | const char *str; |
olaf@108 | 183 | int r; |
olaf@108 | 184 | sstr_t name; |
olaf@108 | 185 | sstr_t value; |
olaf@108 | 186 | |
olaf@108 | 187 | UCX_TEST_BEGIN |
olaf@108 | 188 | |
olaf@108 | 189 | str = ""; |
olaf@110 | 190 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 191 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 192 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 193 | |
olaf@108 | 194 | str = " \n"; |
olaf@110 | 195 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 196 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 197 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 198 | |
olaf@108 | 199 | str = "name"; |
olaf@110 | 200 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 201 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 202 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 203 | |
olaf@108 | 204 | str = " "; |
olaf@110 | 205 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 206 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 207 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 208 | |
olaf@108 | 209 | str = "= "; |
olaf@110 | 210 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 211 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 212 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 213 | |
olaf@108 | 214 | str = "value"; |
olaf@110 | 215 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 216 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 217 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 218 | |
olaf@108 | 219 | str = "\n"; |
olaf@110 | 220 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 221 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 222 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@109 | 223 | UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name"); |
olaf@109 | 224 | UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value"); |
olaf@108 | 225 | |
olaf@108 | 226 | // second round |
olaf@108 | 227 | str = "#comment\n"; |
olaf@110 | 228 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 229 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 230 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 231 | |
olaf@108 | 232 | str = "#comment\nname = "; |
olaf@110 | 233 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 234 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 235 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 236 | |
olaf@108 | 237 | str = "value\na = b\n"; |
olaf@110 | 238 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 239 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 240 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@109 | 241 | UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name"); |
olaf@109 | 242 | UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value"); |
olaf@108 | 243 | |
olaf@110 | 244 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 245 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@109 | 246 | UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name"); |
olaf@109 | 247 | UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"b"))), "wrong value"); |
olaf@108 | 248 | |
olaf@108 | 249 | str = "# comment\n#\n#\ntests = "; |
olaf@110 | 250 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 251 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 252 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 253 | |
olaf@108 | 254 | str = "test1 "; |
olaf@110 | 255 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 256 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 257 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 258 | |
olaf@108 | 259 | str = "test2 test3 test4\n"; |
olaf@109 | 260 | sstr_t testv = sstr((char*)"test1 test2 test3 test4"); |
olaf@110 | 261 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 262 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 263 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@109 | 264 | UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"tests"))), "wrong name"); |
olaf@108 | 265 | UCX_TEST_ASSERT((!sstrcmp(value, testv)), "wrong value"); |
olaf@108 | 266 | |
olaf@110 | 267 | // test if ucx_properties_next finds a name/value after a tmp comment |
olaf@108 | 268 | str = "# just a comment"; |
olaf@110 | 269 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 270 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 271 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 272 | |
olaf@108 | 273 | str = " in 3"; |
olaf@110 | 274 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 275 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 276 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 277 | |
olaf@108 | 278 | str = " parts\na = 1\n"; |
olaf@110 | 279 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 280 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 281 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@109 | 282 | UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name"); |
olaf@109 | 283 | UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"1"))), "wrong value"); |
olaf@108 | 284 | |
olaf@108 | 285 | UCX_TEST_END |
olaf@108 | 286 | |
olaf@110 | 287 | ucx_properties_free(parser); |
olaf@108 | 288 | } |
olaf@108 | 289 | |
universe@134 | 290 | UCX_TEST(test_ucx_properties_next_long) { |
olaf@110 | 291 | UcxProperties *parser = ucx_properties_new(); |
olaf@108 | 292 | int r; |
olaf@108 | 293 | size_t name_len = 512; |
olaf@108 | 294 | char *long_name = (char*)malloc(name_len); |
olaf@108 | 295 | memset(long_name, 'a', 70); |
olaf@108 | 296 | memset(long_name+70, 'b', 242); |
olaf@108 | 297 | memset(long_name+312, 'c', 200); |
olaf@108 | 298 | |
olaf@108 | 299 | size_t value_len = 2048; |
olaf@108 | 300 | char *long_value = (char*)malloc(value_len); |
olaf@108 | 301 | memset(long_value, 'x', 1024); |
olaf@108 | 302 | memset(long_value+1024, 'y', 1024); |
olaf@108 | 303 | |
olaf@108 | 304 | UCX_TEST_BEGIN |
olaf@108 | 305 | |
olaf@108 | 306 | sstr_t name; |
olaf@108 | 307 | sstr_t value; |
olaf@108 | 308 | |
olaf@110 | 309 | ucx_properties_fill(parser, long_name, 10); |
olaf@110 | 310 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 311 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 312 | |
olaf@110 | 313 | ucx_properties_fill(parser, long_name+10, 202); |
olaf@110 | 314 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 315 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 316 | |
olaf@110 | 317 | ucx_properties_fill(parser, long_name+212, 200); |
olaf@110 | 318 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 319 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 320 | |
olaf@110 | 321 | ucx_properties_fill(parser, long_name+412, 100); |
olaf@110 | 322 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 323 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 324 | |
olaf@109 | 325 | const char *str = " = "; |
olaf@110 | 326 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 327 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 328 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 329 | |
olaf@110 | 330 | ucx_properties_fill(parser, long_value, 512); |
olaf@110 | 331 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 332 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 333 | |
olaf@110 | 334 | ucx_properties_fill(parser, long_value+512, 1024); |
olaf@110 | 335 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 336 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 337 | |
olaf@110 | 338 | ucx_properties_fill(parser, long_value+1536, 512); |
olaf@110 | 339 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 340 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 341 | |
olaf@108 | 342 | str = "\n#comment\nkey = value\n"; |
olaf@110 | 343 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@110 | 344 | r = ucx_properties_next(parser, &name, &value); |
olaf@108 | 345 | sstr_t n = sstrn(long_name, name_len); |
olaf@108 | 346 | sstr_t v = sstrn(long_value, value_len); |
olaf@110 | 347 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@108 | 348 | UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong name"); |
olaf@108 | 349 | UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong value"); |
olaf@108 | 350 | |
olaf@110 | 351 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 352 | UCX_TEST_ASSERT(r == 1, "next returned 0"); |
olaf@109 | 353 | UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"key"))), "wrong name"); |
olaf@109 | 354 | UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value"); |
olaf@108 | 355 | |
olaf@110 | 356 | r = ucx_properties_next(parser, &name, &value); |
olaf@110 | 357 | UCX_TEST_ASSERT(r == 0, "next returned 1"); |
olaf@108 | 358 | |
olaf@108 | 359 | UCX_TEST_END |
olaf@108 | 360 | |
olaf@108 | 361 | free(long_name); |
olaf@108 | 362 | free(long_value); |
olaf@110 | 363 | ucx_properties_free(parser); |
olaf@108 | 364 | } |
olaf@109 | 365 | |
universe@134 | 366 | UCX_TEST(test_ucx_properties2map) { |
olaf@147 | 367 | UcxMempool *mp = ucx_mempool_new(64); |
olaf@158 | 368 | UcxMap *map = ucx_map_new_a(mp->allocator, 16); |
olaf@110 | 369 | UcxProperties *parser = ucx_properties_new(); |
olaf@109 | 370 | |
olaf@109 | 371 | UCX_TEST_BEGIN |
olaf@109 | 372 | |
olaf@109 | 373 | const char *str = "key1 = value1\nkey2 = value2\n\n#comment\n\nkey3 = value3\n"; |
olaf@110 | 374 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@109 | 375 | |
olaf@110 | 376 | int r = ucx_properties2map(parser, map); |
olaf@109 | 377 | |
olaf@110 | 378 | UCX_TEST_ASSERT(r == 0, "properties2map failed"); |
olaf@109 | 379 | UCX_TEST_ASSERT(map->count == 3, "wrong number of properties"); |
olaf@109 | 380 | |
olaf@109 | 381 | char *v1 = (char*)ucx_map_cstr_get(map, "key1"); |
olaf@109 | 382 | char *v2 = (char*)ucx_map_cstr_get(map, "key2"); |
olaf@109 | 383 | char *v3 = (char*)ucx_map_cstr_get(map, "key3"); |
olaf@109 | 384 | |
olaf@109 | 385 | UCX_TEST_ASSERT(v1, "value for key1 not found"); |
olaf@109 | 386 | UCX_TEST_ASSERT(v2, "value for key2 not found"); |
olaf@109 | 387 | UCX_TEST_ASSERT(v3, "value for key3 not found"); |
olaf@109 | 388 | |
olaf@109 | 389 | UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1"); |
olaf@109 | 390 | UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2"); |
olaf@109 | 391 | UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3"); |
olaf@109 | 392 | |
olaf@109 | 393 | // second test |
olaf@109 | 394 | ucx_map_free(map); |
olaf@158 | 395 | map = ucx_map_new_a(mp->allocator, 16); |
olaf@109 | 396 | |
olaf@109 | 397 | str = "\n#comment\n"; |
olaf@110 | 398 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@109 | 399 | |
olaf@110 | 400 | r = ucx_properties2map(parser, map); |
olaf@110 | 401 | UCX_TEST_ASSERT(r == 0, "properties2map failed"); |
olaf@109 | 402 | UCX_TEST_ASSERT(map->count == 0, "wrong number of properties"); |
olaf@109 | 403 | |
olaf@109 | 404 | str = "key1 = value1\nsyntax error line\n"; |
olaf@110 | 405 | ucx_properties_fill(parser, (char*)str, strlen(str)); |
olaf@109 | 406 | |
olaf@110 | 407 | r = ucx_properties2map(parser, map); |
olaf@110 | 408 | UCX_TEST_ASSERT(r == 1, "properties2map should return 1"); |
olaf@109 | 409 | UCX_TEST_ASSERT(map->count == 1, "wrong number of properties"); |
olaf@109 | 410 | |
olaf@109 | 411 | char *v = (char*)ucx_map_cstr_get(map, "key1"); |
olaf@109 | 412 | UCX_TEST_ASSERT((!strcmp(v, "value1")), "wrong value"); |
olaf@109 | 413 | |
olaf@109 | 414 | UCX_TEST_END |
olaf@109 | 415 | |
olaf@147 | 416 | ucx_mempool_destroy(mp); |
olaf@110 | 417 | ucx_properties_free(parser); |
olaf@109 | 418 | } |
olaf@109 | 419 | |
universe@134 | 420 | UCX_TEST(test_ucx_properties_load) { |
olaf@109 | 421 | UCX_TEST_BEGIN |
olaf@109 | 422 | FILE *f = tmpfile(); |
olaf@109 | 423 | UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted"); |
olaf@109 | 424 | |
olaf@109 | 425 | fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n"); |
olaf@109 | 426 | fprintf(f, "\n\nkey3 = value3\n\n"); |
olaf@109 | 427 | |
olaf@109 | 428 | size_t name_len = 512; |
olaf@109 | 429 | char *long_name = (char*)malloc(name_len); |
olaf@109 | 430 | memset(long_name, 'k', 512); |
olaf@109 | 431 | |
olaf@109 | 432 | size_t value_len = 2048; |
olaf@109 | 433 | char *long_value = (char*)malloc(value_len); |
olaf@109 | 434 | memset(long_value, 'v', 2048); |
olaf@109 | 435 | |
olaf@109 | 436 | fwrite(long_name, 1, name_len, f); |
olaf@109 | 437 | fprintf(f, " = "); |
olaf@109 | 438 | fwrite(long_value, 1, value_len, f); |
olaf@109 | 439 | fprintf(f, " \n"); |
olaf@109 | 440 | |
olaf@109 | 441 | fprintf(f, "\n\n\n\nlast_key = property value\n"); |
olaf@109 | 442 | |
olaf@109 | 443 | fflush(f); |
olaf@109 | 444 | fseek(f, 0, SEEK_SET); |
olaf@109 | 445 | |
olaf@109 | 446 | UcxMap *map = ucx_map_new(8); |
olaf@109 | 447 | int r = ucx_properties_load(map, f); |
olaf@109 | 448 | |
olaf@109 | 449 | UCX_TEST_ASSERT(r == 0, "ucx_properties_load failed"); |
olaf@109 | 450 | UCX_TEST_ASSERT(map->count == 5, "wrong number of properties"); |
olaf@109 | 451 | |
olaf@109 | 452 | char *v1 = (char*)ucx_map_cstr_get(map, "key1"); |
olaf@109 | 453 | char *v2 = (char*)ucx_map_cstr_get(map, "key2"); |
olaf@109 | 454 | char *v3 = (char*)ucx_map_cstr_get(map, "key3"); |
olaf@109 | 455 | char *lv = (char*)ucx_map_sstr_get(map, sstrn(long_name, name_len)); |
olaf@109 | 456 | char *lk = (char*)ucx_map_cstr_get(map, "last_key"); |
olaf@109 | 457 | |
olaf@109 | 458 | UCX_TEST_ASSERT(v1, "value for key1 not found"); |
olaf@109 | 459 | UCX_TEST_ASSERT(v2, "value for key2 not found"); |
olaf@109 | 460 | UCX_TEST_ASSERT(v3, "value for key3 not found"); |
olaf@109 | 461 | UCX_TEST_ASSERT(lv, "value for long key not found"); |
olaf@109 | 462 | UCX_TEST_ASSERT(lk, "value for last_key not found"); |
olaf@109 | 463 | |
olaf@109 | 464 | UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1"); |
olaf@109 | 465 | UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2"); |
olaf@109 | 466 | UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3"); |
olaf@109 | 467 | sstr_t long1 = sstrn(long_value, value_len); |
olaf@109 | 468 | sstr_t long2 = sstr(lv); |
olaf@109 | 469 | UCX_TEST_ASSERT((!sstrcmp(long1, long2)), "wrong value for long key"); |
olaf@109 | 470 | UCX_TEST_ASSERT(!strcmp(lk, "property value"), "wrong value for last_key"); |
olaf@109 | 471 | |
olaf@109 | 472 | free(v1); |
olaf@109 | 473 | free(v2); |
olaf@109 | 474 | free(v3); |
olaf@109 | 475 | free(lv); |
olaf@109 | 476 | free(lk); |
olaf@109 | 477 | ucx_map_free(map); |
olaf@109 | 478 | fclose(f); |
olaf@109 | 479 | |
olaf@147 | 480 | free(long_name); |
olaf@147 | 481 | free(long_value); |
olaf@147 | 482 | |
olaf@109 | 483 | UCX_TEST_END |
olaf@109 | 484 | } |
olaf@109 | 485 | |
universe@134 | 486 | UCX_TEST(test_ucx_properties_store) { |
olaf@109 | 487 | UcxMap *map1 = ucx_map_new(16); |
olaf@109 | 488 | ucx_map_cstr_put(map1, "key1", "value1"); |
olaf@109 | 489 | ucx_map_cstr_put(map1, "key2", "value2"); |
olaf@109 | 490 | ucx_map_cstr_put(map1, "key3", "value3"); |
olaf@109 | 491 | ucx_map_cstr_put(map1, "key4", "value4"); |
olaf@109 | 492 | ucx_map_cstr_put(map1, "property.key1", "some value 1"); |
olaf@109 | 493 | ucx_map_cstr_put(map1, "property.key2", "some value 2"); |
olaf@109 | 494 | ucx_map_cstr_put(map1, "property.key3", "some value 3"); |
olaf@109 | 495 | ucx_map_cstr_put(map1, "property.key4", "some value 4"); |
olaf@109 | 496 | |
olaf@109 | 497 | UCX_TEST_BEGIN |
olaf@109 | 498 | |
olaf@109 | 499 | FILE *f = tmpfile(); |
olaf@109 | 500 | fprintf(f, "#\n# test property file\n#\n#\n"); |
olaf@109 | 501 | ucx_properties_store(map1, f); |
olaf@109 | 502 | |
olaf@109 | 503 | fflush(f); |
olaf@109 | 504 | fseek(f, 0, SEEK_SET); |
olaf@109 | 505 | UcxMap *map2 = ucx_map_new(16); |
olaf@109 | 506 | ucx_properties_load(map2, f); |
olaf@109 | 507 | |
olaf@109 | 508 | UCX_TEST_ASSERT(map2->count == 8, "wrong number of properties in map2"); |
olaf@109 | 509 | |
olaf@109 | 510 | char *v1 = (char*)ucx_map_cstr_get(map2, "key1"); |
olaf@109 | 511 | char *v2 = (char*)ucx_map_cstr_get(map2, "key2"); |
olaf@109 | 512 | char *v3 = (char*)ucx_map_cstr_get(map2, "key3"); |
olaf@109 | 513 | char *v4 = (char*)ucx_map_cstr_get(map2, "key4"); |
olaf@109 | 514 | char *v5 = (char*)ucx_map_cstr_get(map2, "property.key1"); |
olaf@109 | 515 | char *v6 = (char*)ucx_map_cstr_get(map2, "property.key2"); |
olaf@109 | 516 | char *v7 = (char*)ucx_map_cstr_get(map2, "property.key3"); |
olaf@109 | 517 | char *v8 = (char*)ucx_map_cstr_get(map2, "property.key4"); |
olaf@109 | 518 | |
olaf@109 | 519 | UCX_TEST_ASSERT(v1 && v2 && v3 && v4 && v5 && v6 && v7 && v8, |
olaf@109 | 520 | "missing values"); |
olaf@109 | 521 | |
olaf@109 | 522 | UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value 1"); |
olaf@109 | 523 | UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value 2"); |
olaf@109 | 524 | UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value 3"); |
olaf@109 | 525 | UCX_TEST_ASSERT((!strcmp(v4, "value4")), "wrong value 4"); |
olaf@109 | 526 | UCX_TEST_ASSERT((!strcmp(v5, "some value 1")), "wrong value 5"); |
olaf@109 | 527 | UCX_TEST_ASSERT((!strcmp(v6, "some value 2")), "wrong value 6"); |
olaf@109 | 528 | UCX_TEST_ASSERT((!strcmp(v7, "some value 3")), "wrong value 7"); |
olaf@109 | 529 | UCX_TEST_ASSERT((!strcmp(v8, "some value 4")), "wrong value 8"); |
olaf@109 | 530 | |
olaf@109 | 531 | free(v1); |
olaf@109 | 532 | free(v2); |
olaf@109 | 533 | free(v3); |
olaf@109 | 534 | free(v4); |
olaf@109 | 535 | free(v5); |
olaf@109 | 536 | free(v6); |
olaf@109 | 537 | free(v7); |
olaf@109 | 538 | free(v8); |
olaf@109 | 539 | ucx_map_free(map2); |
olaf@109 | 540 | fclose(f); |
olaf@109 | 541 | |
olaf@109 | 542 | UCX_TEST_END |
olaf@109 | 543 | |
olaf@109 | 544 | ucx_map_free(map1); |
olaf@109 | 545 | } |