| 1 | #include <ndjson.h> |
| 2 | |
| 3 | #include "internal.h" |
| 4 | |
| 5 | NDJSON_DECLARE(selftest_types, NDJSON_SECTION_SELFTEST, "types" , 1, |
| 6 | NDJSON_U64(u), NDJSON_I64(i), NDJSON_I64(i_min), NDJSON_BOOL(b), |
| 7 | NDJSON_STR(s), NDJSON_HEX(h)); |
| 8 | |
| 9 | NDJSON_DECLARE(selftest_omitted, NDJSON_SECTION_SELFTEST, "omitted" , 1, |
| 10 | NDJSON_U64(u), NDJSON_I64(i), NDJSON_BOOL(b), NDJSON_STR(s), |
| 11 | NDJSON_HEX(h)); |
| 12 | |
| 13 | NDJSON_DECLARE(selftest_escapes, NDJSON_SECTION_SELFTEST, "escapes" , 1, |
| 14 | NDJSON_STR(quote), NDJSON_STR(backslash), NDJSON_STR(control), |
| 15 | NDJSON_STR(ansi), NDJSON_STR(high)); |
| 16 | |
| 17 | NDJSON_DECLARE(selftest_truncation, NDJSON_SECTION_SELFTEST, "truncation" , 1, |
| 18 | NDJSON_STR(long_string), NDJSON_U64(sent)); |
| 19 | |
| 20 | static char selftest_long[NDJSON_STR_MAX + 64]; |
| 21 | |
| 22 | void ndjson_selftest(void) { |
| 23 | ndjson_emit(selftest_types, .u = UINT64_MAX, .i = -42, .i_min = INT64_MIN, |
| 24 | .b = true, .s = "hello" , .h = 0xffffffff80051de7); |
| 25 | |
| 26 | ndjson_emit(selftest_omitted); |
| 27 | |
| 28 | ndjson_emit(selftest_escapes, .quote = "a \"quoted\" word" , |
| 29 | .backslash = "C:\\path\\to" , .control = "line\nbreak\ttab" , |
| 30 | .ansi = "\033[31mred\033[0m" , .high = "caf\xc3\xa9" ); |
| 31 | |
| 32 | for (size_t i = 0; i < sizeof(selftest_long) - 1; i++) |
| 33 | selftest_long[i] = (char) ('a' + (i % 26)); |
| 34 | selftest_long[sizeof(selftest_long) - 1] = '\0'; |
| 35 | |
| 36 | ndjson_emit(selftest_truncation, .long_string = selftest_long, |
| 37 | .sent = sizeof(selftest_long) - 1); |
| 38 | } |
| 39 | |