1/* ==========================================
2    Unity Project - A Test Framework for C
3    Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4    [Released under MIT License. Please refer to license.txt for details]
5========================================== */
6
7#include "unity.h"
8#define TEST_INSTANCES
9#include "self_assessment_utils.h"
10
11static int SetToOneToFailInTearDown;
12static int SetToOneMeanWeAlreadyCheckedThisGuy;
13
14void setUp(void)
15{
16    SetToOneToFailInTearDown = 0;
17    SetToOneMeanWeAlreadyCheckedThisGuy = 0;
18}
19
20void tearDown(void)
21{
22    endPutcharSpy(); /* Stop suppressing test output */
23    if (SetToOneToFailInTearDown == 1)
24    {
25        /* These will be skipped internally if already failed/ignored */
26        TEST_FAIL_MESSAGE("<= Failed in tearDown");
27        TEST_IGNORE_MESSAGE("<= Ignored in tearDown");
28    }
29    if ((SetToOneMeanWeAlreadyCheckedThisGuy == 0) && (Unity.CurrentTestFailed > 0))
30    {
31        UnityPrint(": [[[[ Test Should Have Passed But Did Not ]]]]");
32        UNITY_OUTPUT_CHAR('\n');
33    }
34}
35
36void testEqualStrings(void)
37{
38    const char *testString = "foo";
39
40    TEST_ASSERT_EQUAL_STRING(testString, testString);
41    TEST_ASSERT_EQUAL_STRING_MESSAGE("foo", "foo", "foo isn't foo");
42    TEST_ASSERT_EQUAL_STRING("foo", testString);
43    TEST_ASSERT_EQUAL_STRING(testString, "foo");
44    TEST_ASSERT_EQUAL_STRING("", "");
45}
46
47void testEqualStringsLen(void)
48{
49    const char *testString = "foobar";
50    TEST_ASSERT_EQUAL_STRING_LEN(testString, testString, strlen(testString));
51    TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE("foobar", "foobaz", 5, "fooba isn't fooba");
52    TEST_ASSERT_EQUAL_STRING_LEN("foo", testString, 3);
53    TEST_ASSERT_EQUAL_STRING_LEN(testString, "foo", 3);
54    TEST_ASSERT_EQUAL_STRING_LEN("", "", 3);
55}
56
57void testEqualStringsWithCarriageReturnsAndLineFeeds(void)
58{
59    const char *testString = "foo\r\nbar";
60
61    TEST_ASSERT_EQUAL_STRING(testString, testString);
62    TEST_ASSERT_EQUAL_STRING("foo\r\nbar", "foo\r\nbar");
63    TEST_ASSERT_EQUAL_STRING("foo\r\nbar", testString);
64    TEST_ASSERT_EQUAL_STRING(testString, "foo\r\nbar");
65    TEST_ASSERT_EQUAL_STRING("", "");
66}
67
68void testNotEqualString1(void)
69{
70    EXPECT_ABORT_BEGIN
71    TEST_ASSERT_EQUAL_STRING("foo", "bar");
72    VERIFY_FAILS_END
73}
74
75void testNotEqualStringLen1(void)
76{
77    EXPECT_ABORT_BEGIN
78    TEST_ASSERT_EQUAL_STRING_LEN("foobar", "foobaz", 6);
79    VERIFY_FAILS_END
80}
81
82void testNotEqualString2(void)
83{
84    EXPECT_ABORT_BEGIN
85    TEST_ASSERT_EQUAL_STRING("foo", "");
86    VERIFY_FAILS_END
87}
88
89void testNotEqualStringLen2(void)
90{
91    EXPECT_ABORT_BEGIN
92    TEST_ASSERT_EQUAL_STRING_LEN("foo", "", 3);
93    VERIFY_FAILS_END
94}
95
96void testNotEqualString3(void)
97{
98    EXPECT_ABORT_BEGIN
99    TEST_ASSERT_EQUAL_STRING("", "bar");
100    VERIFY_FAILS_END
101}
102
103void testNotEqualStringLen3(void)
104{
105    EXPECT_ABORT_BEGIN
106    TEST_ASSERT_EQUAL_STRING_LEN("", "bar", 3);
107    VERIFY_FAILS_END
108}
109
110void testNotEqualString4(void)
111{
112    EXPECT_ABORT_BEGIN
113    TEST_ASSERT_EQUAL_STRING("bar\r", "bar\n");
114    VERIFY_FAILS_END
115}
116
117void testNotEqualStringLen4(void)
118{
119    EXPECT_ABORT_BEGIN
120    TEST_ASSERT_EQUAL_STRING_LEN("ba\r\x16", "ba\r\n", 4);
121    VERIFY_FAILS_END
122}
123
124void testNotEqualString5(void)
125{
126    const char str1[] = { 0x41, 0x42, 0x03, 0x00 };
127    const char str2[] = { 0x41, 0x42, 0x04, 0x00 };
128    EXPECT_ABORT_BEGIN
129    TEST_ASSERT_EQUAL_STRING(str1, str2);
130    VERIFY_FAILS_END
131}
132
133void testNotEqualString_ExpectedStringIsNull(void)
134{
135    EXPECT_ABORT_BEGIN
136    TEST_ASSERT_EQUAL_STRING(NULL, "bar");
137    VERIFY_FAILS_END
138}
139
140void testNotEqualStringLen_ExpectedStringIsNull(void)
141{
142    EXPECT_ABORT_BEGIN
143    TEST_ASSERT_EQUAL_STRING_LEN(NULL, "bar", 1);
144    VERIFY_FAILS_END
145}
146
147void testNotEqualString_ActualStringIsNull(void)
148{
149    EXPECT_ABORT_BEGIN
150    TEST_ASSERT_EQUAL_STRING("foo", NULL);
151    VERIFY_FAILS_END
152}
153
154void testNotEqualStringLen_ActualStringIsNull(void)
155{
156    EXPECT_ABORT_BEGIN
157    TEST_ASSERT_EQUAL_STRING_LEN("foo", NULL, 1);
158    VERIFY_FAILS_END
159}
160
161void testNotEqualString_ExpectedStringIsLonger(void)
162{
163    EXPECT_ABORT_BEGIN
164    TEST_ASSERT_EQUAL_STRING("foo2", "foo");
165    VERIFY_FAILS_END
166}
167
168void testNotEqualString_ActualStringIsLonger(void)
169{
170    EXPECT_ABORT_BEGIN
171    TEST_ASSERT_EQUAL_STRING("foo", "foo2");
172    VERIFY_FAILS_END
173}
174
175void testEqualStringArrays(void)
176{
177    const char *testStrings[] = { "foo", "boo", "woo", "moo" };
178    const char *expStrings[] = { "foo", "boo", "woo", "zoo" };
179
180    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, expStrings, 3);
181    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 3);
182    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 2);
183    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 1);
184}
185
186void testNotEqualStringArray1(void)
187{
188    const char *testStrings[] = { "foo", "boo", "woo", "moo" };
189    const char *expStrings[] = { "foo", "boo", "woo", "zoo" };
190
191    EXPECT_ABORT_BEGIN
192    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
193    VERIFY_FAILS_END
194}
195
196void testNotEqualStringArray2(void)
197{
198    const char *testStrings[] = { "zoo", "boo", "woo", "moo" };
199    const char *expStrings[] = { "foo", "boo", "woo", "moo" };
200
201    EXPECT_ABORT_BEGIN
202    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
203    VERIFY_FAILS_END
204}
205
206void testNotEqualStringArray3(void)
207{
208    const char *testStrings[] = { "foo", "boo", "woo", NULL };
209    const char *expStrings[] = { "foo", "boo", "woo", "zoo" };
210
211    EXPECT_ABORT_BEGIN
212    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
213    VERIFY_FAILS_END
214}
215
216void testNotEqualStringArray4(void)
217{
218    const char *testStrings[] = { "foo", "boo", "woo", "moo" };
219    const char *expStrings[] = { "foo", NULL, "woo", "moo" };
220
221    EXPECT_ABORT_BEGIN
222    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
223    VERIFY_FAILS_END
224}
225
226void testNotEqualStringArray5(void)
227{
228    const char **testStrings = NULL;
229    const char *expStrings[] = { "foo", "boo", "woo", "zoo" };
230
231    EXPECT_ABORT_BEGIN
232    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
233    VERIFY_FAILS_END
234}
235
236void testNotEqualStringArray6(void)
237{
238    const char *testStrings[] = { "foo", "boo", "woo", "zoo" };
239    const char **expStrings = NULL;
240
241    EXPECT_ABORT_BEGIN
242    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
243    VERIFY_FAILS_END
244}
245
246void testEqualStringArrayIfBothNulls(void)
247{
248    const char **testStrings = NULL;
249    const char **expStrings = NULL;
250
251    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
252}
253
254void testNotEqualStringArrayLengthZero(void)
255{
256    const char *testStrings[] = {NULL};
257    const char **expStrings = NULL;
258
259    EXPECT_ABORT_BEGIN
260    TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 0);
261    VERIFY_FAILS_END
262}
263
264void testEqualStringEachEqual(void)
265{
266    const char *testStrings1[] = { "foo", "foo", "foo", "foo" };
267    const char *testStrings2[] = { "boo", "boo", "boo", "zoo" };
268    const char *testStrings3[] = { "", "", "", "" };
269
270    TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings1, 4);
271    TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings1, 1);
272    TEST_ASSERT_EACH_EQUAL_STRING("boo", testStrings2, 3);
273    TEST_ASSERT_EACH_EQUAL_STRING("", testStrings3, 4);
274}
275
276void testNotEqualStringEachEqual1(void)
277{
278    const char *testStrings[] = { "foo", "foo", "foo", "moo" };
279
280    EXPECT_ABORT_BEGIN
281    TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings, 4);
282    VERIFY_FAILS_END
283}
284
285void testNotEqualStringEachEqual2(void)
286{
287    const char *testStrings[] = { "boo", "foo", "foo", "foo" };
288
289    EXPECT_ABORT_BEGIN
290    TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings, 4);
291    VERIFY_FAILS_END
292}
293
294void testNotEqualStringEachEqual3(void)
295{
296    const char *testStrings[] = { "foo", "foo", "foo", NULL };
297
298    EXPECT_ABORT_BEGIN
299    TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings, 4);
300    VERIFY_FAILS_END
301}
302
303void testNotEqualStringEachEqual4(void)
304{
305    const char *testStrings[] = { "foo", "foo", "woo", "foo" };
306
307    EXPECT_ABORT_BEGIN
308    TEST_ASSERT_EACH_EQUAL_STRING("foo", testStrings, 4);
309    VERIFY_FAILS_END
310}
311
312void testNotEqualStringEachEqual5(void)
313{
314    EXPECT_ABORT_BEGIN
315    TEST_ASSERT_EACH_EQUAL_STRING("foo", NULL, 1);
316    VERIFY_FAILS_END
317}
318
319void testCstringsEscapeSequence(void)
320{
321#ifndef USING_OUTPUT_SPY
322    TEST_IGNORE();
323#else
324    startPutcharSpy();
325    UnityPrint("\x16\x10");
326    endPutcharSpy();
327    TEST_ASSERT_EQUAL_STRING("\\x16\\x10", getBufferPutcharSpy());
328#endif
329}
330