File size: 4,370 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "../../unity/unity.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

/* setUp and tearDown required by Unity */
void setUp(void) {
  /* Nothing to set up */
}

void tearDown(void) {
  /* Nothing to tear down */
}

/* Tests */

void test_set_prefix_no_spaces(void) {
  char buf[] = "abc";

  set_prefix(buf);

  TEST_ASSERT_EQUAL_INT(0, prefix_lead_space);
  TEST_ASSERT_EQUAL_PTR(&buf[0], prefix);
  TEST_ASSERT_EQUAL_INT(3, prefix_full_length); /* strlen("abc") */
  TEST_ASSERT_EQUAL_INT(3, prefix_length);
  TEST_ASSERT_EQUAL_STRING("abc", prefix);
}

void test_set_prefix_only_spaces(void) {
  char buf[] = "    "; /* four spaces */

  set_prefix(buf);

  TEST_ASSERT_EQUAL_INT(4, prefix_lead_space);
  /* prefix should point at the terminating NUL within buf */
  TEST_ASSERT_EQUAL_PTR(&buf[4], prefix);
  TEST_ASSERT_EQUAL_INT(0, prefix_full_length);
  TEST_ASSERT_EQUAL_INT(0, prefix_length);
  TEST_ASSERT_EQUAL_STRING("", prefix);
}

void test_set_prefix_leading_spaces_only(void) {
  char buf[] = "   abc";

  set_prefix(buf);

  TEST_ASSERT_EQUAL_INT(3, prefix_lead_space);
  TEST_ASSERT_EQUAL_PTR(&buf[3], prefix);
  TEST_ASSERT_EQUAL_INT(3, prefix_full_length); /* "abc" + no trailing spaces yet considered */
  TEST_ASSERT_EQUAL_INT(3, prefix_length);
  TEST_ASSERT_EQUAL_STRING("abc", prefix);
}

void test_set_prefix_trailing_spaces_only(void) {
  char buf[] = "abc   "; /* three trailing spaces */

  set_prefix(buf);

  TEST_ASSERT_EQUAL_INT(0, prefix_lead_space);
  TEST_ASSERT_EQUAL_PTR(&buf[0], prefix);
  TEST_ASSERT_EQUAL_INT(6, prefix_full_length); /* strlen("abc   ") */
  TEST_ASSERT_EQUAL_INT(3, prefix_length);
  /* Ensure trailing spaces were nulled just after 'c' */
  TEST_ASSERT_EQUAL_CHAR('a', buf[0]);
  TEST_ASSERT_EQUAL_CHAR('b', buf[1]);
  TEST_ASSERT_EQUAL_CHAR('c', buf[2]);
  TEST_ASSERT_EQUAL_INT('\0', buf[3]);
  /* The bytes after the new NUL remain whatever they were; commonly spaces */
  TEST_ASSERT_EQUAL_STRING("abc", prefix);
}

void test_set_prefix_leading_and_trailing_spaces(void) {
  char buf[] = "  abc  ";

  set_prefix(buf);

  TEST_ASSERT_EQUAL_INT(2, prefix_lead_space);
  TEST_ASSERT_EQUAL_PTR(&buf[2], prefix);
  TEST_ASSERT_EQUAL_INT(5, prefix_full_length); /* strlen("abc  ") */
  TEST_ASSERT_EQUAL_INT(3, prefix_length);
  /* After trimming trailing spaces, NUL should be placed at buf[5] */
  TEST_ASSERT_EQUAL_CHAR('a', buf[2]);
  TEST_ASSERT_EQUAL_CHAR('b', buf[3]);
  TEST_ASSERT_EQUAL_CHAR('c', buf[4]);
  TEST_ASSERT_EQUAL_INT('\0', buf[5]);
  TEST_ASSERT_EQUAL_STRING("abc", prefix);
}

void test_set_prefix_empty_string(void) {
  char buf[] = "";

  set_prefix(buf);

  TEST_ASSERT_EQUAL_INT(0, prefix_lead_space);
  TEST_ASSERT_EQUAL_PTR(&buf[0], prefix);
  TEST_ASSERT_EQUAL_INT(0, prefix_full_length);
  TEST_ASSERT_EQUAL_INT(0, prefix_length);
  TEST_ASSERT_EQUAL_STRING("", prefix);
}

void test_set_prefix_tabs_not_trimmed(void) {
  /* Leading tab and an internal tab; only the trailing space should be trimmed */
  char buf[] = "\tabc\t ";

  set_prefix(buf);

  TEST_ASSERT_EQUAL_INT(0, prefix_lead_space);      /* tabs are not counted as spaces */
  TEST_ASSERT_EQUAL_PTR(&buf[0], prefix);
  TEST_ASSERT_EQUAL_INT(6, prefix_full_length);     /* strlen("\tabc\t ") */
  TEST_ASSERT_EQUAL_INT(5, prefix_length);          /* after trimming the final ' ' only */
  /* After trimming, the string should be "\tabc\t" */
  TEST_ASSERT_EQUAL_STRING("\tabc\t", prefix);
  /* Ensure the NUL was written over the final space */
  TEST_ASSERT_EQUAL_INT('\0', buf[5]);
}

void test_set_prefix_internal_spaces_preserved(void) {
  char buf[] = "  ab c  ";

  set_prefix(buf);

  TEST_ASSERT_EQUAL_INT(2, prefix_lead_space);
  TEST_ASSERT_EQUAL_PTR(&buf[2], prefix);
  TEST_ASSERT_EQUAL_INT(6, prefix_full_length); /* strlen("ab c  ") */
  TEST_ASSERT_EQUAL_INT(4, prefix_length);      /* "ab c" */
  TEST_ASSERT_EQUAL_STRING("ab c", prefix);
}

int main(void) {
  UNITY_BEGIN();

  RUN_TEST(test_set_prefix_no_spaces);
  RUN_TEST(test_set_prefix_only_spaces);
  RUN_TEST(test_set_prefix_leading_spaces_only);
  RUN_TEST(test_set_prefix_trailing_spaces_only);
  RUN_TEST(test_set_prefix_leading_and_trailing_spaces);
  RUN_TEST(test_set_prefix_empty_string);
  RUN_TEST(test_set_prefix_tabs_not_trimmed);
  RUN_TEST(test_set_prefix_internal_spaces_preserved);

  return UNITY_END();
}