#include #include "test_runner.h" #include "unity.h" #include "util.h" /* blank_to_eol should pad a buffer with spaces as far as it can, after an * existing string therein, without overrunning, while still terminating */ RUNNER_DECLARE_TEST(test_blank_to_eol_happy) { 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 */ RUNNER_DECLARE_TEST(test_blank_to_eol_empty) { char buf[5] = ""; blank_to_eol(buf, sizeof(buf)); TEST_ASSERT_EQUAL_STRING(" ", buf); } /* blanking should not overrun the specified buffer size at all */ RUNNER_DECLARE_TEST(test_blank_to_eol_no_overrun) { 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' */ RUNNER_DECLARE_TEST(test_blank_to_eol_overrun) { 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]); }