Debian Codegraph @

tests/CppUTest/TestFailureNaNTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/TestOutput.h"
30 #include "CppUTest/PlatformSpecificFunctions.h"
31
32 namespace
33 {
34 const int failLineNumber = 2;
35 const char* failFileName = "fail.cpp";
36 }
37
38 static double zero = 0.0;
39 static double one = 1.0;
40 static double not_a_number = zero / zero;
41 static double infinity = one / zero;
42
43 extern "C" {
44 static int IsNanForSystemsWithoutNan(double d) { return ((long)not_a_number == (long)d); }
45 static int IsInfForSystemsWithoutInf(double d) { return ((long)infinity == (long)d); }
46 }
47
48 TEST_GROUP(TestFailureNanAndInf)
49 {
50 UtestShell* test;
51
52 void setup() _override
53 {
54 test = new UtestShell("groupname", "testname", failFileName, failLineNumber-1);
55 if(PlatformSpecificIsNan(not_a_number) == false)
56 {
57 not_a_number = -1.0;
58 UT_PTR_SET(PlatformSpecificIsNan, IsNanForSystemsWithoutNan);
59 }
60 if(PlatformSpecificIsInf(infinity) == false)
61 {
62 infinity = -2.0;
63 UT_PTR_SET(PlatformSpecificIsInf, IsInfForSystemsWithoutInf);
64 }
65 }
66 void teardown() _override
67 {
68 delete test;
69 }
70 };
71 #define FAILURE_EQUAL(a, b) STRCMP_EQUAL_LOCATION(a, b.getMessage().asCharString(), "", __FILE__, __LINE__)
72
73 TEST(TestFailureNanAndInf, DoublesEqualExpectedIsNaN)
74 {
75 DoublesEqualFailure f(test, failFileName, failLineNumber, not_a_number, 2.0, 3.0, "");
76 FAILURE_EQUAL("expected <Nan - Not a number>\n"
77 "\tbut was <2> threshold used was <3>\n"
78 "\tCannot make comparisons with Nan", f);
79 }
80
81 TEST(TestFailureNanAndInf, DoublesEqualActualIsNaN)
82 {
83 DoublesEqualFailure f(test, failFileName, failLineNumber, 1.0, not_a_number, 3.0, "");
84 FAILURE_EQUAL("expected <1>\n"
85 "\tbut was <Nan - Not a number> threshold used was <3>\n"
86 "\tCannot make comparisons with Nan", f);
87 }
88
89 TEST(TestFailureNanAndInf, DoublesEqualThresholdIsNaN)
90 {
91 DoublesEqualFailure f(test, failFileName, failLineNumber, 1.0, 2.0, not_a_number, "");
92 FAILURE_EQUAL("expected <1>\n"
93 "\tbut was <2> threshold used was <Nan - Not a number>\n"
94 "\tCannot make comparisons with Nan", f);
95 }
96
97 TEST(TestFailureNanAndInf, DoublesEqualExpectedIsInf)
98 {
99 DoublesEqualFailure f(test, failFileName, failLineNumber, infinity, 2.0, 3.0, "");
100 FAILURE_EQUAL("expected <Inf - Infinity>\n"
101 "\tbut was <2> threshold used was <3>", f);
102 }
103
104 TEST(TestFailureNanAndInf, DoublesEqualActualIsInf)
105 {
106 DoublesEqualFailure f(test, failFileName, failLineNumber, 1.0, infinity, 3.0, "");
107 FAILURE_EQUAL("expected <1>\n"
108 "\tbut was <Inf - Infinity> threshold used was <3>", f);
109 }
110
111 TEST(TestFailureNanAndInf, DoublesEqualThresholdIsInf)
112 {
113 DoublesEqualFailure f(test, failFileName, failLineNumber, 1.0, not_a_number, infinity, "");
114 FAILURE_EQUAL("expected <1>\n"
115 "\tbut was <Nan - Not a number> threshold used was <Inf - Infinity>\n"
116 "\tCannot make comparisons with Nan", f);
117 }