diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/runner.c | 1 | ||||
| -rw-r--r-- | tests/t_fmt.c | 108 | ||||
| -rw-r--r-- | tests/test.h | 1 |
3 files changed, 110 insertions, 0 deletions
diff --git a/tests/runner.c b/tests/runner.c index c2350c3..a92315e 100644 --- a/tests/runner.c +++ b/tests/runner.c | |||
| @@ -27,6 +27,7 @@ static const struct module modules[] = {{"ot_urlencoded", test_urlencoded}, | |||
| 27 | {"ot_batch", test_batch}, | 27 | {"ot_batch", test_batch}, |
| 28 | {"ot_fdstate", test_fdstate}, | 28 | {"ot_fdstate", test_fdstate}, |
| 29 | {"ot_event", test_event}, | 29 | {"ot_event", test_event}, |
| 30 | {"ot_fmt", test_fmt}, | ||
| 30 | {"ot_accesslist", test_accesslist}, | 31 | {"ot_accesslist", test_accesslist}, |
| 31 | {NULL, NULL}}; | 32 | {NULL, NULL}}; |
| 32 | 33 | ||
diff --git a/tests/t_fmt.c b/tests/t_fmt.c new file mode 100644 index 0000000..bc23ed5 --- /dev/null +++ b/tests/t_fmt.c | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <string.h> | ||
| 8 | |||
| 9 | /* Opentracker */ | ||
| 10 | #include "ot_fmt.h" | ||
| 11 | #include "test.h" | ||
| 12 | |||
| 13 | static void case_appends_in_sequence(void) { | ||
| 14 | char buffer[64]; | ||
| 15 | char *r = buffer, *re = buffer + sizeof(buffer); | ||
| 16 | |||
| 17 | test_case("appends follow each other and advance the cursor"); | ||
| 18 | fmt_append(&r, re, "d8:completei%zde", (size_t)7); | ||
| 19 | fmt_append(&r, re, "ee"); | ||
| 20 | *r = 0; | ||
| 21 | CHECK_STR(buffer, "d8:completei7eee"); | ||
| 22 | CHECK_INT(r - buffer, 16); | ||
| 23 | } | ||
| 24 | |||
| 25 | static void case_truncation_stops_at_the_end(void) { | ||
| 26 | char buffer[16]; | ||
| 27 | char *r = buffer, *re = buffer + 8; /* pretend the buffer ends early */ | ||
| 28 | |||
| 29 | memset(buffer, '#', sizeof(buffer)); | ||
| 30 | test_case("a value too long for the room left stops at the end"); | ||
| 31 | fmt_append(&r, re, "%s", "0123456789"); | ||
| 32 | CHECK_PTR(r, re); | ||
| 33 | |||
| 34 | test_case("and writes nothing past it"); | ||
| 35 | CHECK_INT(buffer[8], '#'); | ||
| 36 | CHECK_INT(buffer[15], '#'); | ||
| 37 | } | ||
| 38 | |||
| 39 | static void case_a_full_buffer_takes_nothing_more(void) { | ||
| 40 | char buffer[16]; | ||
| 41 | char *r = buffer + 8, *re = buffer + 8; | ||
| 42 | |||
| 43 | memset(buffer, '#', sizeof(buffer)); | ||
| 44 | test_case("appending to a cursor already at the end changes nothing"); | ||
| 45 | fmt_append(&r, re, "more"); | ||
| 46 | CHECK_PTR(r, re); | ||
| 47 | CHECK_INT(buffer[8], '#'); | ||
| 48 | } | ||
| 49 | |||
| 50 | static void case_an_append_that_just_fits(void) { | ||
| 51 | char buffer[8]; | ||
| 52 | char *r = buffer, *re = buffer + 3; | ||
| 53 | |||
| 54 | memset(buffer, '#', sizeof(buffer)); | ||
| 55 | test_case("three characters into three bytes of room still truncate"); | ||
| 56 | fmt_append(&r, re, "abc"); | ||
| 57 | CHECK_PTR(r, re); | ||
| 58 | CHECK_INT(buffer[0], 'a'); | ||
| 59 | CHECK_INT(buffer[1], 'b'); | ||
| 60 | |||
| 61 | test_case("because snprintf spends the last byte on its terminator"); | ||
| 62 | CHECK_INT(buffer[2], 0); | ||
| 63 | CHECK_INT(buffer[3], '#'); | ||
| 64 | } | ||
| 65 | |||
| 66 | static void case_bytes_are_all_or_nothing(void) { | ||
| 67 | char buffer[16]; | ||
| 68 | char *r = buffer, *re = buffer + 8; | ||
| 69 | |||
| 70 | memset(buffer, '#', sizeof(buffer)); | ||
| 71 | test_case("a run of bytes is written when it fits"); | ||
| 72 | fmt_bytes(&r, re, "\0\1\2\3", 4); | ||
| 73 | CHECK_PTR(r, buffer + 4); | ||
| 74 | CHECK_MEM(buffer, "\0\1\2\3", 4); | ||
| 75 | |||
| 76 | test_case("a run that would not fit is refused whole"); | ||
| 77 | fmt_bytes(&r, re, "abcde", 5); | ||
| 78 | CHECK_PTR(r, re); | ||
| 79 | CHECK_INT(buffer[4], '#'); | ||
| 80 | |||
| 81 | test_case("a run of exactly the room left is written"); | ||
| 82 | r = buffer + 4; | ||
| 83 | fmt_bytes(&r, re, "wxyz", 4); | ||
| 84 | CHECK_PTR(r, re); | ||
| 85 | CHECK_MEM(buffer + 4, "wxyz", 4); | ||
| 86 | CHECK_INT(buffer[8], '#'); | ||
| 87 | } | ||
| 88 | |||
| 89 | static void case_bytes_do_not_terminate(void) { | ||
| 90 | char buffer[8]; | ||
| 91 | char *r = buffer, *re = buffer + 4; | ||
| 92 | |||
| 93 | memset(buffer, '#', sizeof(buffer)); | ||
| 94 | test_case("unlike an append, a run of bytes may fill the room exactly"); | ||
| 95 | fmt_bytes(&r, re, "abcd", 4); | ||
| 96 | CHECK_PTR(r, re); | ||
| 97 | CHECK_MEM(buffer, "abcd", 4); | ||
| 98 | CHECK_INT(buffer[4], '#'); | ||
| 99 | } | ||
| 100 | |||
| 101 | void test_fmt(void) { | ||
| 102 | case_appends_in_sequence(); | ||
| 103 | case_truncation_stops_at_the_end(); | ||
| 104 | case_a_full_buffer_takes_nothing_more(); | ||
| 105 | case_an_append_that_just_fits(); | ||
| 106 | case_bytes_are_all_or_nothing(); | ||
| 107 | case_bytes_do_not_terminate(); | ||
| 108 | } | ||
diff --git a/tests/test.h b/tests/test.h index aa4eb00..0eddc82 100644 --- a/tests/test.h +++ b/tests/test.h | |||
| @@ -90,5 +90,6 @@ void test_mutex(void); | |||
| 90 | void test_batch(void); | 90 | void test_batch(void); |
| 91 | void test_fdstate(void); | 91 | void test_fdstate(void); |
| 92 | void test_event(void); | 92 | void test_event(void); |
| 93 | void test_fmt(void); | ||
| 93 | 94 | ||
| 94 | #endif | 95 | #endif |
