aboutsummaryrefslogtreecommitdiff
path: root/test_util.c
blob: c6b9b8363e2bb5750e6e218bfd6c31326ebae255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <string.h>

#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]);
}