aboutsummaryrefslogtreecommitdiff
path: root/test_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'test_util.c')
-rw-r--r--test_util.c62
1 files changed, 62 insertions, 0 deletions
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 <string.h>
+
+#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();
+}