From 8ab11293cdd2a47e7950fa9c488cbc504325cad3 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 9 Feb 2021 21:10:06 +1300 Subject: Add missing test_util.c Accidentally forgot to add this previously. --- test_util.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test_util.c diff --git a/test_util.c b/test_util.c new file mode 100644 index 0000000..81c3db7 --- /dev/null +++ b/test_util.c @@ -0,0 +1,62 @@ +#include + +#include "unity.h" +#include "util.h" + +void setUp(){}; +void tearDown(){}; + +/* blank_to_eol should pad a buffer with spaces as far as it can, after an + * existing string therein, without overrunning, while still terminating */ +void test_blank_to_eol_happy(void) +{ + char buf[11] = "hello"; + blank_to_eol(buf, sizeof(buf)); + TEST_ASSERT_EQUAL_STRING("hello ", buf); +} + +/* blanking an empty string to end of buffer should fill entire buffer with + * spaces until the last byte, which should be null terminator */ +void test_blank_to_eol_empty(void) +{ + char buf[5] = ""; + blank_to_eol(buf, sizeof(buf)); + TEST_ASSERT_EQUAL_STRING(" ", buf); +} + +/* blanking should not overrun the specified buffer size at all */ +void test_blank_to_eol_no_overrun(void) +{ + char buf[20] = { + 'h', 'e', 'l', 'l', 'o', '\0', + 'b', 'l', 'a', 'h', + 'm', 'a', 'g', 'i', 'c', '\0' + }; + blank_to_eol(buf, 10); + const char *expect = "hello "; + TEST_ASSERT_EQUAL_STRING(expect, buf); + TEST_ASSERT_EQUAL_STRING("magic", &buf[strlen(expect)+1]); +} + +/* blanking a buffer which already has no '\0' within it should result in the + * buffer being terminated, truncating contents minimally as necessary to fit + * a '\0' */ +void test_blank_to_eol_overrun(void) +{ + char buf[20] = "hello, world!"; + blank_to_eol(buf, 6); + TEST_ASSERT_EQUAL_STRING("hello", buf); + /* also check that the buffer wasn't clobbered beyond its end. Note this + * doesn't test OOB reads, only writes */ + TEST_ASSERT_EQUAL_STRING(" world!", &buf[6]); +} + +int main(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_blank_to_eol_happy); + RUN_TEST(test_blank_to_eol_empty); + RUN_TEST(test_blank_to_eol_no_overrun); + RUN_TEST(test_blank_to_eol_overrun); + return UNITY_END(); +} -- cgit v1.1