aboutsummaryrefslogtreecommitdiff
path: root/test_util.c
blob: 81c3db73ef697259ba63c5e91f2682181b5aac6e (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
52
53
54
55
56
57
58
59
60
61
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();
}