tests/test_properties.c

changeset 1031
8a90552bba29
parent 993
b642eca4b956
equal deleted inserted replaced
1030:06091e067bee 1031:8a90552bba29
39 39
40 CX_TEST_ASSERT(prop.config.delimiter == '='); 40 CX_TEST_ASSERT(prop.config.delimiter == '=');
41 CX_TEST_ASSERT(prop.config.comment1 == '#'); 41 CX_TEST_ASSERT(prop.config.comment1 == '#');
42 CX_TEST_ASSERT(prop.config.comment2 == 0); 42 CX_TEST_ASSERT(prop.config.comment2 == 0);
43 CX_TEST_ASSERT(prop.config.comment3 == 0); 43 CX_TEST_ASSERT(prop.config.comment3 == 0);
44 CX_TEST_ASSERT(prop.flags == 0); 44 CX_TEST_ASSERT(prop.input.space == NULL);
45 CX_TEST_ASSERT(prop.text == NULL); 45 CX_TEST_ASSERT(prop.buffer.space == NULL);
46 CX_TEST_ASSERT(prop.buf == NULL);
47 46
48 cxPropertiesDestroy(&prop); 47 cxPropertiesDestroy(&prop);
49 } 48 }
50 } 49 }
51 50
95 cxstring key; 94 cxstring key;
96 cxstring value; 95 cxstring value;
97 CX_TEST_DO { 96 CX_TEST_DO {
98 for (int i = 0; i < 10; i++) { 97 for (int i = 0; i < 10; i++) {
99 cxPropertiesFill(&prop, tests[i]); 98 cxPropertiesFill(&prop, tests[i]);
100 CX_TEST_ASSERT(prop.text == tests[i]); 99 CX_TEST_ASSERT(prop.input.space == tests[i]);
101 CX_TEST_ASSERT(prop.text_size == strlen(tests[i])); 100 CX_TEST_ASSERT(prop.input.size == strlen(tests[i]));
102 CX_TEST_ASSERT(prop.text_pos == 0); 101 CX_TEST_ASSERT(prop.input.pos == 0);
103 102
104 result = cxPropertiesNext(&prop, &key, &value); 103 result = cxPropertiesNext(&prop, &key, &value);
105 cxstring k = cx_str(keys[i]); 104 cxstring k = cx_str(keys[i]);
106 cxstring v = cx_str(values[i]); 105 cxstring v = cx_str(values[i]);
107 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR); 106 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
201 cxstring value; 200 cxstring value;
202 const char *str; 201 const char *str;
203 202
204 CX_TEST_DO { 203 CX_TEST_DO {
205 str = ""; 204 str = "";
206 cxPropertiesFill(&prop, str); 205 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
207 result = cxPropertiesNext(&prop, &key, &value); 206 result = cxPropertiesNext(&prop, &key, &value);
208 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA); 207 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA);
209 208
210 str = " \n"; 209 str = " \n";
211 cxPropertiesFill(&prop, str); 210 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
212 result = cxPropertiesNext(&prop, &key, &value); 211 result = cxPropertiesNext(&prop, &key, &value);
213 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA); 212 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA);
214 213
215 str = "name"; 214 str = "name";
216 cxPropertiesFill(&prop, str); 215 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
217 result = cxPropertiesNext(&prop, &key, &value); 216 result = cxPropertiesNext(&prop, &key, &value);
218 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA); 217 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
219 218
220 str = " "; 219 str = " ";
221 cxPropertiesFill(&prop, str); 220 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
222 result = cxPropertiesNext(&prop, &key, &value); 221 result = cxPropertiesNext(&prop, &key, &value);
223 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA); 222 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
224 223
225 // call fill twice in a row 224 // call fill twice in a row
226 str = "= "; 225 str = "= ";
227 cxPropertiesFill(&prop, str); 226 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
228 str = "value"; 227 str = "value";
229 cxPropertiesFill(&prop, str); 228 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
230 result = cxPropertiesNext(&prop, &key, &value); 229 result = cxPropertiesNext(&prop, &key, &value);
231 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA); 230 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
232 231
233 str = "\n"; 232 str = "\n";
234 cxPropertiesFill(&prop, str); 233 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
235 result = cxPropertiesNext(&prop, &key, &value); 234 result = cxPropertiesNext(&prop, &key, &value);
236 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR); 235 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
237 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("name"))); 236 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("name")));
238 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value"))); 237 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value")));
239 238
240 // second round 239 // second round
241 str = "#comment\n"; 240 str = "#comment\n";
242 cxPropertiesFill(&prop, str); 241 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
243 result = cxPropertiesNext(&prop, &key, &value); 242 result = cxPropertiesNext(&prop, &key, &value);
244 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA); 243 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA);
245 244
246 str = "#comment\nname2 = "; 245 str = "#comment\nname2 = ";
247 cxPropertiesFill(&prop, str); 246 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
248 result = cxPropertiesNext(&prop, &key, &value); 247 result = cxPropertiesNext(&prop, &key, &value);
249 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA); 248 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
250 249
251 str = "value2\na = b\n"; 250 str = "value2\na = b\n";
252 cxPropertiesFill(&prop, str); 251 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
253 result = cxPropertiesNext(&prop, &key, &value); 252 result = cxPropertiesNext(&prop, &key, &value);
254 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR); 253 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
255 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("name2"))); 254 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("name2")));
256 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value2"))); 255 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value2")));
257 256
259 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR); 258 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
260 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("a"))); 259 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("a")));
261 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("b"))); 260 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("b")));
262 261
263 str = "# comment\n#\n#\ntests = "; 262 str = "# comment\n#\n#\ntests = ";
264 cxPropertiesFill(&prop, str); 263 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
265 result = cxPropertiesNext(&prop, &key, &value); 264 result = cxPropertiesNext(&prop, &key, &value);
266 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA); 265 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
267 266
268 str = "test1 "; 267 str = "test1 ";
269 cxPropertiesFill(&prop, str); 268 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
270 result = cxPropertiesNext(&prop, &key, &value); 269 result = cxPropertiesNext(&prop, &key, &value);
271 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA); 270 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
272 271
273 str = "test2 test3 test4\n"; 272 str = "test2 test3 test4\n";
274 cxPropertiesFill(&prop, str); 273 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
275 result = cxPropertiesNext(&prop, &key, &value); 274 result = cxPropertiesNext(&prop, &key, &value);
276 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR); 275 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
277 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("tests"))); 276 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("tests")));
278 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("test1 test2 test3 test4"))); 277 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("test1 test2 test3 test4")));
279 278
280 // test if cxPropertiesNext finds a name/value after a comment 279 // test if cxPropertiesNext finds a name/value after a comment
281 str = "# just a comment"; 280 str = "# just a comment";
282 cxPropertiesFill(&prop, str); 281 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
283 result = cxPropertiesNext(&prop, &key, &value); 282 result = cxPropertiesNext(&prop, &key, &value);
284 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA); 283 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
285 284
286 str = " in 3"; 285 str = " in 3";
287 cxPropertiesFill(&prop, str); 286 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
288 result = cxPropertiesNext(&prop, &key, &value); 287 result = cxPropertiesNext(&prop, &key, &value);
289 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA); 288 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
290 289
291 str = " parts\nx = 1\n"; 290 str = " parts\nx = 1\n";
292 cxPropertiesFill(&prop, str); 291 CX_TEST_ASSERT(0 == cxPropertiesFill(&prop, str));
293 result = cxPropertiesNext(&prop, &key, &value); 292 result = cxPropertiesNext(&prop, &key, &value);
294 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR); 293 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
295 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("x"))); 294 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("x")));
296 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("1"))); 295 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("1")));
297 296
298 // finally we are done 297 // finally we are done
299 result = cxPropertiesNext(&prop, &key, &value); 298 result = cxPropertiesNext(&prop, &key, &value);
300 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA); 299 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA);
301 } 300 }
302 cxPropertiesDestroy(&prop); 301 cxPropertiesDestroy(&prop);
306 CxProperties prop; 305 CxProperties prop;
307 cxPropertiesInitDefault(&prop); 306 cxPropertiesInitDefault(&prop);
308 CxPropertiesStatus result; 307 CxPropertiesStatus result;
309 cxstring key; 308 cxstring key;
310 cxstring value; 309 cxstring value;
311 310
312 size_t key_len = 512; 311 size_t key_len = 512;
313 char *long_key = (char*)malloc(key_len); 312 char *long_key = (char*)malloc(key_len);
314 memset(long_key, 'a', 70); 313 memset(long_key, 'a', 70);
315 memset(long_key + 70, 'b', 242); 314 memset(long_key + 70, 'b', 242);
316 memset(long_key + 312, 'c', 200); 315 memset(long_key + 312, 'c', 200);
369 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value"))); 368 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value")));
370 369
371 result = cxPropertiesNext(&prop, &key, &value); 370 result = cxPropertiesNext(&prop, &key, &value);
372 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA); 371 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA);
373 372
374 CX_TEST_ASSERT(prop.buf != NULL); 373 CX_TEST_ASSERT(prop.buffer.capacity > 0);
375 CX_TEST_ASSERT(prop.buf_capacity > 0); 374 CX_TEST_ASSERT(cxBufferEof(&prop.buffer));
376 CX_TEST_ASSERT(prop.buf_size == 0); 375 CX_TEST_ASSERT(cxBufferEof(&prop.input));
377 cxPropertiesDestroy(&prop); 376 cxPropertiesDestroy(&prop);
378 377 CX_TEST_ASSERT(prop.buffer.capacity == 0);
379 CX_TEST_ASSERT(prop.buf == NULL); 378 CX_TEST_ASSERT(prop.buffer.size == 0);
380 CX_TEST_ASSERT(prop.buf_capacity == 0); 379 CX_TEST_ASSERT(prop.buffer.pos == 0);
381 CX_TEST_ASSERT(prop.buf_size == 0);
382 } 380 }
383 381
384 free(long_key); 382 free(long_key);
385 free(long_value); 383 free(long_value);
386 } 384 }
519 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 517 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
520 } 518 }
521 cx_testing_allocator_destroy(&talloc); 519 cx_testing_allocator_destroy(&talloc);
522 } 520 }
523 521
522 CX_TEST(test_properties_multiple_fill) {
523 const char *props1 = "key1 = value1\n";
524 const char *props2 = "key2 = value2\n";
525 const char *props3 = "key3 = value3\n";
526
527 CxProperties prop;
528 cxPropertiesInitDefault(&prop);
529 CxPropertiesStatus result;
530 cxstring key;
531 cxstring value;
532 CX_TEST_DO {
533 cxPropertiesFill(&prop, props1);
534 cxPropertiesFill(&prop, props2);
535 cxPropertiesFill(&prop, props3);
536 result = cxPropertiesNext(&prop, &key, &value);
537 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
538 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("key1")));
539 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value1")));
540 result = cxPropertiesNext(&prop, &key, &value);
541 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
542 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("key2")));
543 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value2")));
544 result = cxPropertiesNext(&prop, &key, &value);
545 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
546 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("key3")));
547 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value3")));
548
549 result = cxPropertiesNext(&prop, &key, &value);
550 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA);
551 }
552 cxPropertiesDestroy(&prop);
553 }
554
555 CX_TEST(test_properties_use_stack) {
556 const char *props1 = "key1 = val";
557 const char *props2 = "ue1\nkey2 = value2";
558 const char *props3 = "\nkey3 = value3\n";
559 char stackmem[16];
560
561 CxProperties prop;
562 cxPropertiesInitDefault(&prop);
563 cxPropertiesUseStack(&prop, stackmem, 16);
564 CxPropertiesStatus result;
565 cxstring key;
566 cxstring value;
567 CX_TEST_DO {
568 cxPropertiesFill(&prop, props1);
569 result = cxPropertiesNext(&prop, &key, &value);
570 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
571 cxPropertiesFill(&prop, props2);
572 result = cxPropertiesNext(&prop, &key, &value);
573 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
574 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("key1")));
575 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value1")));
576 result = cxPropertiesNext(&prop, &key, &value);
577 CX_TEST_ASSERT(result == CX_PROPERTIES_INCOMPLETE_DATA);
578 cxPropertiesFill(&prop, props3);
579 result = cxPropertiesNext(&prop, &key, &value);
580 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
581 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("key2")));
582 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value2")));
583 result = cxPropertiesNext(&prop, &key, &value);
584 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
585 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("key3")));
586 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("value3")));
587 result = cxPropertiesNext(&prop, &key, &value);
588 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_DATA);
589 }
590 cxPropertiesDestroy(&prop);
591 }
592
593 CX_TEST(test_properties_empty_key) {
594 const char *fail1 = "= val\n";
595 const char *fail2 = " = val\n";
596 const char *good = " key = val\n";
597
598 CxProperties prop;
599 CxPropertiesStatus result;
600 cxstring key;
601 cxstring value;
602 CX_TEST_DO {
603 cxPropertiesInitDefault(&prop);
604 cxPropertiesFill(&prop, fail1);
605 result = cxPropertiesNext(&prop, &key, &value);
606 CX_TEST_ASSERT(result == CX_PROPERTIES_INVALID_EMPTY_KEY);
607 cxPropertiesReset(&prop);
608 cxPropertiesFill(&prop, fail2);
609 result = cxPropertiesNext(&prop, &key, &value);
610 CX_TEST_ASSERT(result == CX_PROPERTIES_INVALID_EMPTY_KEY);
611 cxPropertiesReset(&prop);
612 cxPropertiesFill(&prop, good);
613 result = cxPropertiesNext(&prop, &key, &value);
614 CX_TEST_ASSERT(result == CX_PROPERTIES_NO_ERROR);
615 CX_TEST_ASSERT(0 == cx_strcmp(key, cx_str("key")));
616 CX_TEST_ASSERT(0 == cx_strcmp(value, cx_str("val")));
617 cxPropertiesDestroy(&prop);
618 }
619 }
620
524 CxTestSuite *cx_test_suite_properties(void) { 621 CxTestSuite *cx_test_suite_properties(void) {
525 CxTestSuite *suite = cx_test_suite_new("properties"); 622 CxTestSuite *suite = cx_test_suite_new("properties");
526 623
527 cx_test_register(suite, test_properties_init); 624 cx_test_register(suite, test_properties_init);
528 cx_test_register(suite, test_properties_next); 625 cx_test_register(suite, test_properties_next);
529 cx_test_register(suite, test_properties_next_multi); 626 cx_test_register(suite, test_properties_next_multi);
530 cx_test_register(suite, test_properties_next_part); 627 cx_test_register(suite, test_properties_next_part);
531 cx_test_register(suite, test_properties_next_long_lines); 628 cx_test_register(suite, test_properties_next_long_lines);
532 cx_test_register(suite, test_properties_load_string_to_map); 629 cx_test_register(suite, test_properties_load_string_to_map);
533 cx_test_register(suite, test_properties_load_file_to_map); 630 cx_test_register(suite, test_properties_load_file_to_map);
631 cx_test_register(suite, test_properties_multiple_fill);
632 cx_test_register(suite, test_properties_use_stack);
633 cx_test_register(suite, test_properties_empty_key);
534 634
535 return suite; 635 return suite;
536 } 636 }

mercurial