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
11 static int SetToOneToFailInTearDown;
12 static int SetToOneMeanWeAlreadyCheckedThisGuy;
13
setUp(void)14 void setUp(void)
15 {
16 SetToOneToFailInTearDown = 0;
17 SetToOneMeanWeAlreadyCheckedThisGuy = 0;
18 }
19
tearDown(void)20 void 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
testFloatsWithinDelta(void)36 void testFloatsWithinDelta(void)
37 {
38 #ifdef UNITY_EXCLUDE_FLOAT
39 TEST_IGNORE();
40 #else
41 TEST_ASSERT_FLOAT_WITHIN(0.00003f, 187245.03485f, 187245.03488f);
42 TEST_ASSERT_FLOAT_WITHIN(1.0f, 187245.0f, 187246.0f);
43 TEST_ASSERT_FLOAT_WITHIN(0.05f, 9273.2549f, 9273.2049f);
44 TEST_ASSERT_FLOAT_WITHIN(0.007f, -726.93724f, -726.94424f);
45 #endif
46 }
47
testFloatsNotWithinDelta(void)48 void testFloatsNotWithinDelta(void)
49 {
50 #ifdef UNITY_EXCLUDE_FLOAT
51 TEST_IGNORE();
52 #else
53 EXPECT_ABORT_BEGIN
54 TEST_ASSERT_FLOAT_WITHIN(0.05f, 9273.2649f, 9273.2049f);
55 VERIFY_FAILS_END
56 #endif
57 }
58
testFloatsEqual(void)59 void testFloatsEqual(void)
60 {
61 #ifdef UNITY_EXCLUDE_FLOAT
62 TEST_IGNORE();
63 #else
64 TEST_ASSERT_EQUAL_FLOAT(187245.0f, 187246.0f);
65 TEST_ASSERT_EQUAL_FLOAT(18724.5f, 18724.6f);
66 TEST_ASSERT_EQUAL_FLOAT(9273.2549f, 9273.2599f);
67 TEST_ASSERT_EQUAL_FLOAT(-726.93724f, -726.9374f);
68 #endif
69 }
70
testFloatsNotEqual(void)71 void testFloatsNotEqual(void)
72 {
73 #ifdef UNITY_EXCLUDE_FLOAT
74 TEST_IGNORE();
75 #else
76 EXPECT_ABORT_BEGIN
77 TEST_ASSERT_EQUAL_FLOAT(9273.9649f, 9273.0049f);
78 VERIFY_FAILS_END
79 #endif
80 }
81
testFloatsNotEqualNegative1(void)82 void testFloatsNotEqualNegative1(void)
83 {
84 #ifdef UNITY_EXCLUDE_FLOAT
85 TEST_IGNORE();
86 #else
87 EXPECT_ABORT_BEGIN
88 TEST_ASSERT_EQUAL_FLOAT(-9273.9649f, -9273.0049f);
89 VERIFY_FAILS_END
90 #endif
91 }
92
testFloatsNotEqualNegative2(void)93 void testFloatsNotEqualNegative2(void)
94 {
95 #ifdef UNITY_EXCLUDE_FLOAT
96 TEST_IGNORE();
97 #else
98 EXPECT_ABORT_BEGIN
99 TEST_ASSERT_EQUAL_FLOAT(-9273.0049f, -9273.9649f);
100 VERIFY_FAILS_END
101 #endif
102 }
103
testFloatsNotEqualActualNaN(void)104 void testFloatsNotEqualActualNaN(void)
105 {
106 #ifdef UNITY_EXCLUDE_FLOAT
107 TEST_IGNORE();
108 #else
109 EXPECT_ABORT_BEGIN
110 TEST_ASSERT_EQUAL_FLOAT(85.963f, 0.0f / f_zero);
111 VERIFY_FAILS_END
112 #endif
113 }
114
testFloatsNotEqualExpectedNaN(void)115 void testFloatsNotEqualExpectedNaN(void)
116 {
117 #ifdef UNITY_EXCLUDE_FLOAT
118 TEST_IGNORE();
119 #else
120 EXPECT_ABORT_BEGIN
121 TEST_ASSERT_EQUAL_FLOAT(0.0f / f_zero, 85.963f);
122 VERIFY_FAILS_END
123 #endif
124 }
125
testFloatsEqualBothNaN(void)126 void testFloatsEqualBothNaN(void)
127 {
128 #ifdef UNITY_EXCLUDE_FLOAT
129 TEST_IGNORE();
130 #else
131 TEST_ASSERT_EQUAL_FLOAT(0.0f / f_zero, 0.0f / f_zero);
132 #endif
133 }
134
testFloatsNotEqualInfNaN(void)135 void testFloatsNotEqualInfNaN(void)
136 {
137 #ifdef UNITY_EXCLUDE_FLOAT
138 TEST_IGNORE();
139 #else
140 EXPECT_ABORT_BEGIN
141 TEST_ASSERT_EQUAL_FLOAT(1.0f / f_zero, 0.0f / f_zero);
142 VERIFY_FAILS_END
143 #endif
144 }
145
testFloatsNotEqualNaNInf(void)146 void testFloatsNotEqualNaNInf(void)
147 {
148 #ifdef UNITY_EXCLUDE_FLOAT
149 TEST_IGNORE();
150 #else
151 EXPECT_ABORT_BEGIN
152 TEST_ASSERT_EQUAL_FLOAT(0.0f / f_zero, 1.0f / f_zero);
153 VERIFY_FAILS_END
154 #endif
155 }
156
testFloatsNotEqualActualInf(void)157 void testFloatsNotEqualActualInf(void)
158 {
159 #ifdef UNITY_EXCLUDE_FLOAT
160 TEST_IGNORE();
161 #else
162 EXPECT_ABORT_BEGIN
163 TEST_ASSERT_EQUAL_FLOAT(321.642f, 1.0f / f_zero);
164 VERIFY_FAILS_END
165 #endif
166 }
167
testFloatsNotEqualExpectedInf(void)168 void testFloatsNotEqualExpectedInf(void)
169 {
170 #ifdef UNITY_EXCLUDE_FLOAT
171 TEST_IGNORE();
172 #else
173 EXPECT_ABORT_BEGIN
174 TEST_ASSERT_EQUAL_FLOAT(1.0f / f_zero, 321.642f);
175 VERIFY_FAILS_END
176 #endif
177 }
178
testFloatsEqualBothInf(void)179 void testFloatsEqualBothInf(void)
180 {
181 #ifdef UNITY_EXCLUDE_FLOAT
182 TEST_IGNORE();
183 #else
184 TEST_ASSERT_EQUAL_FLOAT(1.0f / f_zero, 1.0f / f_zero);
185 #endif
186 }
187
testFloatsNotEqualPlusMinusInf(void)188 void testFloatsNotEqualPlusMinusInf(void)
189 {
190 #ifdef UNITY_EXCLUDE_FLOAT
191 TEST_IGNORE();
192 #else
193 EXPECT_ABORT_BEGIN
194 TEST_ASSERT_EQUAL_FLOAT(1.0f / f_zero, -1.0f / f_zero);
195 VERIFY_FAILS_END
196 #endif
197 }
198
testFloatIsPosInf1(void)199 void testFloatIsPosInf1(void)
200 {
201 #ifdef UNITY_EXCLUDE_FLOAT
202 TEST_IGNORE();
203 #else
204 TEST_ASSERT_FLOAT_IS_INF(2.0f / f_zero);
205 #endif
206 }
207
testFloatIsPosInf2(void)208 void testFloatIsPosInf2(void)
209 {
210 #ifdef UNITY_EXCLUDE_FLOAT
211 TEST_IGNORE();
212 #else
213 EXPECT_ABORT_BEGIN
214 TEST_ASSERT_FLOAT_IS_NOT_INF(2.0f / f_zero);
215 VERIFY_FAILS_END
216 #endif
217 }
218
testFloatIsNegInf1(void)219 void testFloatIsNegInf1(void)
220 {
221 #ifdef UNITY_EXCLUDE_FLOAT
222 TEST_IGNORE();
223 #else
224 TEST_ASSERT_FLOAT_IS_NEG_INF(-3.0f / f_zero);
225 #endif
226 }
227
testFloatIsNegInf2(void)228 void testFloatIsNegInf2(void)
229 {
230 #ifdef UNITY_EXCLUDE_FLOAT
231 TEST_IGNORE();
232 #else
233 EXPECT_ABORT_BEGIN
234 TEST_ASSERT_FLOAT_IS_NOT_NEG_INF(-3.0f / f_zero);
235 VERIFY_FAILS_END
236 #endif
237 }
238
testFloatIsNotPosInf1(void)239 void testFloatIsNotPosInf1(void)
240 {
241 #ifdef UNITY_EXCLUDE_FLOAT
242 TEST_IGNORE();
243 #else
244 EXPECT_ABORT_BEGIN
245 TEST_ASSERT_FLOAT_IS_INF(2.0f);
246 VERIFY_FAILS_END
247 #endif
248 }
249
testFloatIsNotPosInf2(void)250 void testFloatIsNotPosInf2(void)
251 {
252 #ifdef UNITY_EXCLUDE_FLOAT
253 TEST_IGNORE();
254 #else
255 TEST_ASSERT_FLOAT_IS_NOT_INF(2.0f);
256 #endif
257 }
258
testFloatIsNotNegInf(void)259 void testFloatIsNotNegInf(void)
260 {
261 #ifdef UNITY_EXCLUDE_FLOAT
262 TEST_IGNORE();
263 #else
264 EXPECT_ABORT_BEGIN
265 TEST_ASSERT_FLOAT_IS_NEG_INF(-999.876f);
266 VERIFY_FAILS_END
267 #endif
268 }
269
testFloatIsNan1(void)270 void testFloatIsNan1(void)
271 {
272 #ifdef UNITY_EXCLUDE_FLOAT
273 TEST_IGNORE();
274 #else
275 TEST_ASSERT_FLOAT_IS_NAN(0.0f / f_zero);
276 #endif
277 }
278
testFloatIsNan2(void)279 void testFloatIsNan2(void)
280 {
281 #ifdef UNITY_EXCLUDE_FLOAT
282 TEST_IGNORE();
283 #else
284 EXPECT_ABORT_BEGIN
285 TEST_ASSERT_FLOAT_IS_NOT_NAN(0.0f / f_zero);
286 VERIFY_FAILS_END
287 #endif
288 }
289
testFloatIsNotNan1(void)290 void testFloatIsNotNan1(void)
291 {
292 #ifdef UNITY_EXCLUDE_FLOAT
293 TEST_IGNORE();
294 #else
295 EXPECT_ABORT_BEGIN
296 TEST_ASSERT_FLOAT_IS_NAN(234.9f);
297 VERIFY_FAILS_END
298 #endif
299 }
300
testFloatIsNotNan2(void)301 void testFloatIsNotNan2(void)
302 {
303 #ifdef UNITY_EXCLUDE_FLOAT
304 TEST_IGNORE();
305 #else
306 TEST_ASSERT_FLOAT_IS_NOT_NAN(234.9f);
307 #endif
308 }
309
testFloatInfIsNotNan(void)310 void testFloatInfIsNotNan(void)
311 {
312 #ifdef UNITY_EXCLUDE_FLOAT
313 TEST_IGNORE();
314 #else
315 EXPECT_ABORT_BEGIN
316 TEST_ASSERT_FLOAT_IS_NAN(1.0f / f_zero);
317 VERIFY_FAILS_END
318 #endif
319 }
320
testFloatNanIsNotInf(void)321 void testFloatNanIsNotInf(void)
322 {
323 #ifdef UNITY_EXCLUDE_FLOAT
324 TEST_IGNORE();
325 #else
326 EXPECT_ABORT_BEGIN
327 TEST_ASSERT_FLOAT_IS_INF(0.0f / f_zero);
328 VERIFY_FAILS_END
329 #endif
330 }
331
testFloatIsDeterminate1(void)332 void testFloatIsDeterminate1(void)
333 {
334 #ifdef UNITY_EXCLUDE_FLOAT
335 TEST_IGNORE();
336 #else
337 TEST_ASSERT_FLOAT_IS_DETERMINATE(0.0f);
338 TEST_ASSERT_FLOAT_IS_DETERMINATE(123.3f);
339 TEST_ASSERT_FLOAT_IS_DETERMINATE(-88.3f);
340 #endif
341 }
342
testFloatIsDeterminate2(void)343 void testFloatIsDeterminate2(void)
344 {
345 #ifdef UNITY_EXCLUDE_FLOAT
346 TEST_IGNORE();
347 #else
348 EXPECT_ABORT_BEGIN
349 TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE(-88.3f);
350 VERIFY_FAILS_END
351 #endif
352 }
353
testFloatIsNotDeterminate1(void)354 void testFloatIsNotDeterminate1(void)
355 {
356 #ifdef UNITY_EXCLUDE_FLOAT
357 TEST_IGNORE();
358 #else
359 TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE(1.0f / f_zero);
360 TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE(-1.0f / f_zero);
361 TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE(0.0f / f_zero);
362 #endif
363 }
364
testFloatIsNotDeterminate2(void)365 void testFloatIsNotDeterminate2(void)
366 {
367 #ifdef UNITY_EXCLUDE_FLOAT
368 TEST_IGNORE();
369 #else
370 EXPECT_ABORT_BEGIN
371 TEST_ASSERT_FLOAT_IS_DETERMINATE(-1.0f / f_zero);
372 VERIFY_FAILS_END
373 #endif
374 }
375
testFloatTraitFailsOnInvalidTrait(void)376 void testFloatTraitFailsOnInvalidTrait(void)
377 {
378 #ifdef UNITY_EXCLUDE_FLOAT
379 TEST_IGNORE();
380 #else
381 EXPECT_ABORT_BEGIN
382 UnityAssertFloatSpecial(1.0f, NULL, __LINE__, UNITY_FLOAT_INVALID_TRAIT);
383 VERIFY_FAILS_END
384 #endif
385 }
386
testEqualFloatArrays(void)387 void testEqualFloatArrays(void)
388 {
389 #ifdef UNITY_EXCLUDE_FLOAT
390 TEST_IGNORE();
391 #else
392 float p0[] = {1.0f, -8.0f, 25.4f, -0.123f};
393 float p1[] = {1.0f, -8.0f, 25.4f, -0.123f};
394 float p2[] = {1.0f, -8.0f, 25.4f, -0.2f};
395 float p3[] = {1.0f, -23.0f, 25.0f, -0.26f};
396
397 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p0, 1);
398 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p0, 4);
399 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
400 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p2, 3);
401 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p3, 1);
402 TEST_ASSERT_EQUAL_FLOAT_ARRAY(NULL, NULL, 1);
403 #endif
404 }
405
testNotEqualFloatArraysExpectedNull(void)406 void testNotEqualFloatArraysExpectedNull(void)
407 {
408 #ifdef UNITY_EXCLUDE_FLOAT
409 TEST_IGNORE();
410 #else
411 float* p0 = NULL;
412 float p1[] = {1.0f, 8.0f, 25.4f, 0.252f};
413
414 EXPECT_ABORT_BEGIN
415 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
416 VERIFY_FAILS_END
417 #endif
418 }
419
testNotEqualFloatArraysActualNull(void)420 void testNotEqualFloatArraysActualNull(void)
421 {
422 #ifdef UNITY_EXCLUDE_FLOAT
423 TEST_IGNORE();
424 #else
425 float p0[] = {1.0f, 8.0f, 25.4f, 0.253f};
426 float* p1 = NULL;
427
428 EXPECT_ABORT_BEGIN
429 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
430 VERIFY_FAILS_END
431 #endif
432 }
433
testNotEqualFloatArrays1(void)434 void testNotEqualFloatArrays1(void)
435 {
436 #ifdef UNITY_EXCLUDE_FLOAT
437 TEST_IGNORE();
438 #else
439 float p0[] = {1.0f, 8.0f, 25.4f, 0.253f};
440 float p1[] = {1.0f, 8.0f, 25.4f, 0.252f};
441
442 EXPECT_ABORT_BEGIN
443 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
444 VERIFY_FAILS_END
445 #endif
446 }
447
testNotEqualFloatArrays2(void)448 void testNotEqualFloatArrays2(void)
449 {
450 #ifdef UNITY_EXCLUDE_FLOAT
451 TEST_IGNORE();
452 #else
453 float p0[] = {1.0f, 8.0f, 25.4f, 0.253f};
454 float p1[] = {2.0f, 8.0f, 25.4f, 0.253f};
455
456 EXPECT_ABORT_BEGIN
457 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
458 VERIFY_FAILS_END
459 #endif
460 }
461
testNotEqualFloatArrays3(void)462 void testNotEqualFloatArrays3(void)
463 {
464 #ifdef UNITY_EXCLUDE_FLOAT
465 TEST_IGNORE();
466 #else
467 float p0[] = {1.0f, 8.0f, 25.4f, 0.253f};
468 float p1[] = {1.0f, 8.0f, 25.5f, 0.253f};
469
470 EXPECT_ABORT_BEGIN
471 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
472 VERIFY_FAILS_END
473 #endif
474 }
475
testNotEqualFloatArraysNegative1(void)476 void testNotEqualFloatArraysNegative1(void)
477 {
478 #ifdef UNITY_EXCLUDE_FLOAT
479 TEST_IGNORE();
480 #else
481 float p0[] = {-1.0f, -8.0f, -25.4f, -0.253f};
482 float p1[] = {-1.0f, -8.0f, -25.4f, -0.252f};
483
484 EXPECT_ABORT_BEGIN
485 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
486 VERIFY_FAILS_END
487 #endif
488 }
489
testNotEqualFloatArraysNegative2(void)490 void testNotEqualFloatArraysNegative2(void)
491 {
492 #ifdef UNITY_EXCLUDE_FLOAT
493 TEST_IGNORE();
494 #else
495 float p0[] = {-1.0f, -8.0f, -25.4f, -0.253f};
496 float p1[] = {-2.0f, -8.0f, -25.4f, -0.253f};
497
498 EXPECT_ABORT_BEGIN
499 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
500 VERIFY_FAILS_END
501 #endif
502 }
503
testNotEqualFloatArraysNegative3(void)504 void testNotEqualFloatArraysNegative3(void)
505 {
506 #ifdef UNITY_EXCLUDE_FLOAT
507 TEST_IGNORE();
508 #else
509 float p0[] = {-1.0f, -8.0f, -25.4f, -0.253f};
510 float p1[] = {-1.0f, -8.0f, -25.5f, -0.253f};
511
512 EXPECT_ABORT_BEGIN
513 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
514 VERIFY_FAILS_END
515 #endif
516 }
517
testEqualFloatArraysNaN(void)518 void testEqualFloatArraysNaN(void)
519 {
520 #ifdef UNITY_EXCLUDE_FLOAT
521 TEST_IGNORE();
522 #else
523 float p0[] = {1.0f, 0.0f / f_zero, 25.4f, 0.253f};
524 float p1[] = {1.0f, 0.0f / f_zero, 25.4f, 0.253f};
525
526 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
527 #endif
528 }
529
testEqualFloatArraysInf(void)530 void testEqualFloatArraysInf(void)
531 {
532 #ifdef UNITY_EXCLUDE_FLOAT
533 TEST_IGNORE();
534 #else
535 float p0[] = {1.0f, 1.0f / f_zero, 25.4f, 0.253f};
536 float p1[] = {1.0f, 1.0f / f_zero, 25.4f, 0.253f};
537
538 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 4);
539 #endif
540 }
541
testNotEqualFloatArraysLengthZero(void)542 void testNotEqualFloatArraysLengthZero(void)
543 {
544 #ifdef UNITY_EXCLUDE_FLOAT
545 TEST_IGNORE();
546 #else
547 float p0[1] = {0.0f};
548 float p1[1] = {0.0f};
549
550 EXPECT_ABORT_BEGIN
551 TEST_ASSERT_EQUAL_FLOAT_ARRAY(p0, p1, 0);
552 VERIFY_FAILS_END
553 #endif
554 }
555
testEqualFloatEachEqual(void)556 void testEqualFloatEachEqual(void)
557 {
558 #ifdef UNITY_EXCLUDE_FLOAT
559 TEST_IGNORE();
560 #else
561 float p0[] = {1.0f, 1.0f, 1.0f, 1.0f};
562 float p1[] = {-0.123f, -0.123f, -0.123f, -0.123f};
563 float p2[] = {25.4f, 25.4f, 25.4f, -0.2f};
564 float p3[] = {1.0f, -23.0f, 25.0f, -0.26f};
565
566 TEST_ASSERT_EACH_EQUAL_FLOAT(1.0f, p0, 1);
567 TEST_ASSERT_EACH_EQUAL_FLOAT(1.0f, p0, 4);
568 TEST_ASSERT_EACH_EQUAL_FLOAT(-0.123f, p1, 4);
569 TEST_ASSERT_EACH_EQUAL_FLOAT(25.4f, p2, 3);
570 TEST_ASSERT_EACH_EQUAL_FLOAT(1.0f, p3, 1);
571 #endif
572 }
573
testNotEqualFloatEachEqualActualNull(void)574 void testNotEqualFloatEachEqualActualNull(void)
575 {
576 #ifdef UNITY_EXCLUDE_FLOAT
577 TEST_IGNORE();
578 #else
579 float* p0 = NULL;
580
581 EXPECT_ABORT_BEGIN
582 TEST_ASSERT_EACH_EQUAL_FLOAT(5, p0, 4);
583 VERIFY_FAILS_END
584 #endif
585 }
586
testNotEqualFloatEachEqual1(void)587 void testNotEqualFloatEachEqual1(void)
588 {
589 #ifdef UNITY_EXCLUDE_FLOAT
590 TEST_IGNORE();
591 #else
592 float p0[] = {0.253f, 8.0f, 0.253f, 0.253f};
593
594 EXPECT_ABORT_BEGIN
595 TEST_ASSERT_EACH_EQUAL_FLOAT(0.253f, p0, 4);
596 VERIFY_FAILS_END
597 #endif
598 }
599
testNotEqualFloatEachEqual2(void)600 void testNotEqualFloatEachEqual2(void)
601 {
602 #ifdef UNITY_EXCLUDE_FLOAT
603 TEST_IGNORE();
604 #else
605 float p0[] = {8.0f, 8.0f, 8.0f, 0.253f};
606
607 EXPECT_ABORT_BEGIN
608 TEST_ASSERT_EACH_EQUAL_FLOAT(8.0f, p0, 4);
609 VERIFY_FAILS_END
610 #endif
611 }
612
testNotEqualFloatEachEqual3(void)613 void testNotEqualFloatEachEqual3(void)
614 {
615 #ifdef UNITY_EXCLUDE_FLOAT
616 TEST_IGNORE();
617 #else
618 float p0[] = {1.0f, 1.0f, 1.0f, 0.253f};
619
620 EXPECT_ABORT_BEGIN
621 TEST_ASSERT_EACH_EQUAL_FLOAT(1.0f, p0, 4);
622 VERIFY_FAILS_END
623 #endif
624 }
625
testNotEqualFloatEachEqualNegative1(void)626 void testNotEqualFloatEachEqualNegative1(void)
627 {
628 #ifdef UNITY_EXCLUDE_FLOAT
629 TEST_IGNORE();
630 #else
631 float p0[] = {-1.0f, -0.253f, -0.253f, -0.253f};
632
633 EXPECT_ABORT_BEGIN
634 TEST_ASSERT_EACH_EQUAL_FLOAT(-0.253f, p0, 4);
635 VERIFY_FAILS_END
636 #endif
637 }
638
testNotEqualFloatEachEqualNegative2(void)639 void testNotEqualFloatEachEqualNegative2(void)
640 {
641 #ifdef UNITY_EXCLUDE_FLOAT
642 TEST_IGNORE();
643 #else
644 float p0[] = {-25.4f, -8.0f, -25.4f, -25.4f};
645
646 EXPECT_ABORT_BEGIN
647 TEST_ASSERT_EACH_EQUAL_FLOAT(-25.4f, p0, 4);
648 VERIFY_FAILS_END
649 #endif
650 }
651
testNotEqualFloatEachEqualNegative3(void)652 void testNotEqualFloatEachEqualNegative3(void)
653 {
654 #ifdef UNITY_EXCLUDE_FLOAT
655 TEST_IGNORE();
656 #else
657 float p0[] = {-8.0f, -8.0f, -8.0f, -0.253f};
658
659 EXPECT_ABORT_BEGIN
660 TEST_ASSERT_EACH_EQUAL_FLOAT(-8.0f, p0, 4);
661 VERIFY_FAILS_END
662 #endif
663 }
664
testEqualFloatEachEqualNaN(void)665 void testEqualFloatEachEqualNaN(void)
666 {
667 #ifdef UNITY_EXCLUDE_FLOAT
668 TEST_IGNORE();
669 #else
670 float p0[] = {0.0f / f_zero, 0.0f / f_zero, 0.0f / f_zero, 0.0f / f_zero};
671
672 TEST_ASSERT_EACH_EQUAL_FLOAT(0.0f / f_zero, p0, 4);
673 #endif
674 }
675
testEqualFloatEachEqualInf(void)676 void testEqualFloatEachEqualInf(void)
677 {
678 #ifdef UNITY_EXCLUDE_FLOAT
679 TEST_IGNORE();
680 #else
681 float p0[] = {1.0f / f_zero, 1.0f / f_zero, 25.4f, 0.253f};
682
683 TEST_ASSERT_EACH_EQUAL_FLOAT(1.0f / f_zero, p0, 2);
684 #endif
685 }
686
testNotEqualFloatEachEqualLengthZero(void)687 void testNotEqualFloatEachEqualLengthZero(void)
688 {
689 #ifdef UNITY_EXCLUDE_FLOAT
690 TEST_IGNORE();
691 #else
692 float p0[1] = {0.0f};
693
694 EXPECT_ABORT_BEGIN
695 TEST_ASSERT_EACH_EQUAL_FLOAT(0.0f, p0, 0);
696 VERIFY_FAILS_END
697 #endif
698 }
699
testFloatPrinting(void)700 void testFloatPrinting(void)
701 {
702 #if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_INCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY)
703 TEST_IGNORE();
704 #else
705 TEST_ASSERT_EQUAL_PRINT_FLOATING("0", 0.0f);
706 TEST_ASSERT_EQUAL_PRINT_FLOATING("4.99e-07", 0.000000499f);
707 TEST_ASSERT_EQUAL_PRINT_FLOATING("0.1004695", 0.100469499f);
708 TEST_ASSERT_EQUAL_PRINT_FLOATING("2", 1.9999995f); /*Rounding to int place*/
709 TEST_ASSERT_EQUAL_PRINT_FLOATING("1", 1.0f);
710 TEST_ASSERT_EQUAL_PRINT_FLOATING("1.25", 1.25f);
711 TEST_ASSERT_EQUAL_PRINT_FLOATING("7.999999", 7.999999f); /*Not rounding*/
712 TEST_ASSERT_EQUAL_PRINT_FLOATING("16.00002", 16.00002f);
713 TEST_ASSERT_EQUAL_PRINT_FLOATING("16.00004", 16.00004f);
714 TEST_ASSERT_EQUAL_PRINT_FLOATING("16.00006", 16.00006f);
715 TEST_ASSERT_EQUAL_PRINT_FLOATING("9999999", 9999999.0f); /*Last full print integer*/
716
717 TEST_ASSERT_EQUAL_PRINT_FLOATING("-0", -0.0f);
718 TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.99e-07", -0.000000499f);
719 TEST_ASSERT_EQUAL_PRINT_FLOATING("-0.1004695", -0.100469499f);
720 TEST_ASSERT_EQUAL_PRINT_FLOATING("-2", -1.9999995f); /*Rounding to int place*/
721 TEST_ASSERT_EQUAL_PRINT_FLOATING("-1", -1.0f);
722 TEST_ASSERT_EQUAL_PRINT_FLOATING("-1.25", -1.25f);
723 TEST_ASSERT_EQUAL_PRINT_FLOATING("-7.999999", -7.999999f); /*Not rounding*/
724 TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.00002", -16.00002f);
725 TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.00004", -16.00004f);
726 TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.00006", -16.00006f);
727 TEST_ASSERT_EQUAL_PRINT_FLOATING("-9999999", -9999999.0f); /*Last full print integer*/
728
729 /* Fails, prints "4.294968e+09" due to FP math imprecision
730 * TEST_ASSERT_EQUAL_PRINT_FLOATING("4.294967e+09", 4294967296.0f); */
731 TEST_ASSERT_EQUAL_PRINT_FLOATING("5e+09", 5000000000.0f);
732 TEST_ASSERT_EQUAL_PRINT_FLOATING("8e+09", 8.0e+09f);
733 TEST_ASSERT_EQUAL_PRINT_FLOATING("8.309999e+09", 8309999104.0f);
734 TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 1.0e+10f);
735 TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 10000000000.0f);
736 /* Some compilers have trouble with inexact float constants, a float cast works generally */
737 TEST_ASSERT_EQUAL_PRINT_FLOATING("1.000055e+10", (float)1.000055e+10f);
738 TEST_ASSERT_EQUAL_PRINT_FLOATING("1.1e+38", (float)1.10000005e+38f);
739 TEST_ASSERT_EQUAL_PRINT_FLOATING("1.635299e+10", 1.63529943e+10f);
740 /* Fails, prints "3.402824e+38" due to FP math imprecision
741 * TEST_ASSERT_EQUAL_PRINT_FLOATING("3.402823e+38", 3.40282346638e38f); */
742
743 TEST_ASSERT_EQUAL_PRINT_FLOATING("-1e+10", -1.0e+10f);
744 /* Fails, prints "-3.402824e+38" due to FP math imprecision
745 * TEST_ASSERT_EQUAL_PRINT_FLOATING("-3.402823e+38", -3.40282346638e38f); */
746 #endif
747 }
748
testFloatPrintingRoundTiesToEven(void)749 void testFloatPrintingRoundTiesToEven(void)
750 {
751 #if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_INCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY)
752 TEST_IGNORE();
753 #else
754 #ifdef UNITY_ROUND_TIES_AWAY_FROM_ZERO
755 TEST_ASSERT_EQUAL_PRINT_FLOATING("0.0004882813", 0.00048828125f);
756 TEST_ASSERT_EQUAL_PRINT_FLOATING("488281.3", 488281.25f);
757 TEST_ASSERT_EQUAL_PRINT_FLOATING("5.000001e-07", 0.00000050000005f);
758 TEST_ASSERT_EQUAL_PRINT_FLOATING("-5.000001e-07", -0.00000050000005f);
759 #else /* Default to Round ties to even */
760 TEST_ASSERT_EQUAL_PRINT_FLOATING("0.0004882812", 0.00048828125f);
761 TEST_ASSERT_EQUAL_PRINT_FLOATING("488281.2", 488281.25f);
762 TEST_ASSERT_EQUAL_PRINT_FLOATING("5e-07", 0.00000050000005f);
763 TEST_ASSERT_EQUAL_PRINT_FLOATING("-5e-07", -0.00000050000005f);
764 #endif
765 #endif
766 }
767
testFloatPrintingInfinityAndNaN(void)768 void testFloatPrintingInfinityAndNaN(void)
769 {
770 #if defined(UNITY_EXCLUDE_FLOAT_PRINT) || !defined(USING_OUTPUT_SPY)
771 TEST_IGNORE();
772 #else
773 TEST_ASSERT_EQUAL_PRINT_FLOATING("inf", 1.0f / f_zero);
774 TEST_ASSERT_EQUAL_PRINT_FLOATING("-inf", -1.0f / f_zero);
775
776 TEST_ASSERT_EQUAL_PRINT_FLOATING("nan", 0.0f / f_zero);
777 #endif
778 }
779
780 #if defined(UNITY_TEST_ALL_FLOATS_PRINT_OK) && defined(USING_OUTPUT_SPY)
781 #ifdef UNITY_INCLUDE_DOUBLE
printFloatValue(float f)782 static void printFloatValue(float f)
783 {
784 char expected[18];
785
786 startPutcharSpy();
787 UnityPrintFloat(f);
788
789 sprintf(expected, "%.9g", f);
790 /* We print all NaN's as "nan", not "-nan" */
791 if (strcmp(expected, "-nan") == 0) strcpy(expected, "nan");
792
793 if (strcmp(expected, getBufferPutcharSpy()))
794 {
795 /* Fail with diagnostic printing */
796 TEST_ASSERT_EQUAL_PRINT_FLOATING(expected, f);
797 }
798 }
799 #else
printFloatValue(float f)800 static void printFloatValue(float f)
801 {
802 char expected[18];
803 char expected_lower[18];
804 char expected_lower2[18];
805 char expected_lower3[18];
806 char expected_higher[18];
807 char expected_higher2[18];
808 char expected_higher3[18];
809
810 startPutcharSpy();
811 UnityPrintFloat(f);
812
813 sprintf(expected, "%.7g", f);
814 /* We print all NaN's as "nan", not "-nan" */
815 if (strcmp(expected, "-nan") == 0) strcpy(expected, "nan");
816
817 strcpy(expected_lower, expected);
818 strcpy(expected_lower2, expected);
819 strcpy(expected_lower3, expected);
820 strcpy(expected_higher, expected);
821 strcpy(expected_higher2, expected);
822 strcpy(expected_higher3, expected);
823
824 /* Allow for rounding differences in the last digit */
825 double lower = (double)f * 0.99999995;
826 double higher = (double)f * 1.00000005;
827
828 if(isfinite(lower)) sprintf(expected_lower, "%.7g", lower);
829 if(isfinite(higher)) sprintf(expected_higher, "%.7g", higher);
830
831 /* Outside [1,10000000] allow for relative error of +/-2.5e-7 */
832 if (f < 1.0 || f > 10000000)
833 {
834 double lower2 = (double)f * 0.99999985;
835 double lower3 = (double)f * 0.99999975;
836 double higher2 = (double)f * 1.00000015;
837 double higher3 = (double)f * 1.00000025;
838
839 if (isfinite(lower2)) sprintf(expected_lower2, "%.7g", lower2);
840 if (isfinite(lower3)) sprintf(expected_lower3, "%.7g", lower3);
841 if (isfinite(higher2)) sprintf(expected_higher2, "%.7g", higher2);
842 if (isfinite(higher3)) sprintf(expected_higher3, "%.7g", higher3);
843 }
844
845 if (strcmp(expected, getBufferPutcharSpy()) != 0 &&
846 strcmp(expected_lower, getBufferPutcharSpy()) != 0 &&
847 strcmp(expected_lower2, getBufferPutcharSpy()) != 0 &&
848 strcmp(expected_lower3, getBufferPutcharSpy()) != 0 &&
849 strcmp(expected_higher, getBufferPutcharSpy()) != 0 &&
850 strcmp(expected_higher2, getBufferPutcharSpy()) != 0 &&
851 strcmp(expected_higher3, getBufferPutcharSpy()) != 0)
852 {
853 /* Fail with diagnostic printing */
854 TEST_ASSERT_EQUAL_PRINT_FLOATING(expected, f);
855 }
856 }
857 #endif
858 #endif
859
testFloatPrintingRandomSamples(void)860 void testFloatPrintingRandomSamples(void)
861 {
862 #if !defined(UNITY_TEST_ALL_FLOATS_PRINT_OK) || !defined(USING_OUTPUT_SPY)
863 TEST_IGNORE();
864 #else
865 union { float f_value; uint32_t int_value; } u;
866
867 /* These values are not covered by the MINSTD generator */
868 u.int_value = 0x00000000; printFloatValue(u.f_value);
869 u.int_value = 0x80000000; printFloatValue(u.f_value);
870 u.int_value = 0x7fffffff; printFloatValue(u.f_value);
871 u.int_value = 0xffffffff; printFloatValue(u.f_value);
872
873 uint32_t a = 1;
874 for(int num_tested = 0; num_tested < 1000000; num_tested++)
875 {
876 /* MINSTD pseudo-random number generator */
877 a = (uint32_t)(((uint64_t)a * 48271u) % 2147483647u);
878
879 /* MINSTD does not set the highest bit; test both possibilities */
880 u.int_value = a; printFloatValue(u.f_value);
881 u.int_value = a | 0x80000000; printFloatValue(u.f_value);
882 }
883 #endif
884 }
885