Repository: cpputest/cpputest (cpp)

Issue:
Borland v5.4 throws a runtime error on division by zero: tests\CppUTest\UtestTest.cpp
This is similar to #1544 
The Borland v5.4 compiler does not have a NaN or Inf .
if one wants the program to catch the division by zero  error, and not crash, it has to contain an signal handler that catches the exception and deal with it itself. 
From the help file:
>For / and %, op2 must be nonzero op2 = 0 results in an error. (You can't divide by zero.)

For  tests/CppUTests/UtestTest.cpp this means that the following test fails with a NUMERICAL error. 
```
static volatile double zero = 0.0;

TEST(UtestShell, compareDoubles)
{
    double not_a_number = zero / zero;
    double infinity = 1 / zero;
    CHECK(doubles_equal(1.0, 1.001, 0.01));
    CHECK(!doubles_equal(not_a_number, 1.001, 0.01));
    CHECK(!doubles_equal(1.0, not_a_number, 0.01));
    CHECK(!doubles_equal(1.0, 1.001, not_a_number));
    CHECK(!doubles_equal(1.0, 1.1, 0.05));
    CHECK(!doubles_equal(infinity, 1.0, 0.01));
    CHECK(!doubles_equal(1.0, infinity, 0.01));
    CHECK(doubles_equal(1.0, -1.0, infinity));
    CHECK(doubles_equal(infinity, infinity, 0.01));
    CHECK(doubles_equal(infinity, infinity, infinity));
    double a = 1.2345678;
    CHECK(doubles_equal(a, a, 0.000000001));
}
```
How should we remove this one test when compiling with Borland v5.4?

This is part of #1493


Discussion:
There are in fact only three of those CHECKs that have any chance of passing.
```
    CHECK(doubles_equal(1.0, 1.001, 0.01));
    CHECK(!doubles_equal(1.0, 1.1, 0.05));
    double a = 1.2345678;
    CHECK(doubles_equal(a, a, 0.000000001));
```
Perhaps split the test in NaN, infinity and others ? Then have a check for the support for NaN and the support for infinity and turn these off in BC ?
Yep.  
Question. Not sure how to test for NaN and Inf support.  Will have to do some homework.
You can just leave it on by default and then turn it off for the BC. Just would like to avoid compiler specific switches in any file and prefer capability specific switches (well... prefer no switches at all but that is not an option)

Patch:
diff --git a/cmake/Modules/CppUTestConfigurationOptions.cmake b/cmake/Modules/CppUTestConfigurationOptions.cmake
--- a/cmake/Modules/CppUTestConfigurationOptions.cmake
+++ b/cmake/Modules/CppUTestConfigurationOptions.cmake
@@ -63,13 +63,13 @@ if (LONGLONG)
     set(CPPUTEST_USE_LONG_LONG 1)
 endif (LONGLONG)
 
-if (HAS_INF)
-    set(CPPUTEST_HAS_INF 1)
-endif (HAS_INF)
+if (NOT HAS_INF)
+    set(CPPUTEST_NO_INF 1)
+endif (NOT HAS_INF)
 
-if (HAS_NAN)
-    set(CPPUTEST_HAS_NAN 1)
-endif (HAS_NAN)
+if (NOT HAS_NAN)
+    set(CPPUTEST_NO_NAN 1)
+endif (NOT HAS_NAN)
 
 if (MAP_FILE AND NOT MSVC)
     set(CPPUTEST_LD_FLAGS "${CPPUTEST_LD_FLAGS} -Wl,-Map,$<.map.txt")
diff --git a/config.h.cmake b/config.h.cmake
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -4,8 +4,8 @@
 #cmakedefine CPPUTEST_MEM_LEAK_DETECTION_DISABLED
 #cmakedefine CPPUTEST_USE_LONG_LONG
 
-#cmakedefine CPPUTEST_HAS_INF
-#cmakedefine CPPUTEST_HAS_NAN
+#cmakedefine CPPUTEST_NO_INF
+#cmakedefine CPPUTEST_NO_NAN
 
 #cmakedefine CPPUTEST_STD_C_LIB_DISABLED
 #cmakedefine CPPUTEST_STD_CPP_LIB_DISABLED
diff --git a/include/CppUTest/CppUTestConfig.h b/include/CppUTest/CppUTestConfig.h
--- a/include/CppUTest/CppUTestConfig.h
+++ b/include/CppUTest/CppUTestConfig.h
@@ -334,5 +334,17 @@ typedef struct cpputest_ulonglong cpputest_ulonglong;
  #pragma clang diagnostic pop
 #endif
 
+/* Borland v5.4 does not have a NaN or Inf value */
+#if defined(CPPUTEST_NO_INF)
+#define CPPUTEST_HAS_INF 0
+#else
+#define CPPUTEST_HAS_INF 1
+#endif
+#if defined(CPPUTEST_NO_NAN)
+#define CPPUTEST_HAS_NAN 0
+#else
+#define CPPUTEST_HAS_NAN 1
+#endif
+
 
 #endif


Test patch:
diff --git a/tests/CppUTest/UtestTest.cpp b/tests/CppUTest/UtestTest.cpp
--- a/tests/CppUTest/UtestTest.cpp
+++ b/tests/CppUTest/UtestTest.cpp
@@ -60,21 +60,33 @@ static volatile double zero = 0.0;
 
 TEST(UtestShell, compareDoubles)
 {
-    double not_a_number = zero / zero;
-    double infinity = 1 / zero;
     CHECK(doubles_equal(1.0, 1.001, 0.01));
+    CHECK(!doubles_equal(1.0, 1.1, 0.05));
+    double a = 1.2345678;
+    CHECK(doubles_equal(a, a, 0.000000001));
+}
+
+#if CPPUTEST_HAS_NAN == 1
+TEST(UtestShell, compareDoublesNaN)
+{
+    double not_a_number = zero / zero;
     CHECK(!doubles_equal(not_a_number, 1.001, 0.01));
     CHECK(!doubles_equal(1.0, not_a_number, 0.01));
     CHECK(!doubles_equal(1.0, 1.001, not_a_number));
-    CHECK(!doubles_equal(1.0, 1.1, 0.05));
+}
+#endif
+
+#if CPPUTEST_HAS_INF == 1
+TEST(UtestShell, compareDoublesInf)
+{
+    double infinity = 1 / zero;
     CHECK(!doubles_equal(infinity, 1.0, 0.01));
     CHECK(!doubles_equal(1.0, infinity, 0.01));
     CHECK(doubles_equal(1.0, -1.0, infinity));
     CHECK(doubles_equal(infinity, infinity, 0.01));
     CHECK(doubles_equal(infinity, infinity, infinity));
-    double a = 1.2345678;
-    CHECK(doubles_equal(a, a, 0.000000001));
 }
+#endif
 
 TEST(UtestShell, FailWillIncreaseTheAmountOfChecks)
 {