From 36cf8a1eb9eaa6d01a434d007f707be972e6e383 Mon Sep 17 00:00:00 2001 From: "Daniele E. Domenichelli" Date: Thu, 30 Oct 2014 12:23:27 +0100 Subject: [PATCH] Tests/Tutorial: Fix when USE_MYMATH is OFF Unit tests for the square root of "-25" currently fail when USE_MYMATH is disabled. The "mysqrt" method in the tutorials, returns "0" for a negative value, while "sqrt" returns "NaN", and therefore the output is not accepted by the test. This patch checks if the number is negative and eventually returns "0" before calling "sqrt" or "mysqrt" to fix this issue. Printing a NaN might cause issues with the string catched by the tests on some platform. Therefore assume that "0" is correct and "fix" the USE_MYMATH=OFF version by checking if the number is negative and eventually returning "0" before calling "sqrt" or "mysqrt". --- Tests/Tutorial/Step2/tutorial.cxx | 8 ++++++-- Tests/Tutorial/Step3/tutorial.cxx | 8 ++++++-- Tests/Tutorial/Step4/tutorial.cxx | 8 ++++++-- Tests/Tutorial/Step5/tutorial.cxx | 8 ++++++-- Tests/Tutorial/Step6/tutorial.cxx | 8 ++++++-- Tests/Tutorial/Step7/tutorial.cxx | 8 ++++++-- 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Tests/Tutorial/Step2/tutorial.cxx b/Tests/Tutorial/Step2/tutorial.cxx index 82b416f11..c27da0b84 100644 --- a/Tests/Tutorial/Step2/tutorial.cxx +++ b/Tests/Tutorial/Step2/tutorial.cxx @@ -21,12 +21,16 @@ int main (int argc, char *argv[]) } double inputValue = atof(argv[1]); + double outputValue = 0; + if(inputValue >= 0) + { #ifdef USE_MYMATH - double outputValue = mysqrt(inputValue); + outputValue = mysqrt(inputValue); #else - double outputValue = sqrt(inputValue); + outputValue = sqrt(inputValue); #endif + } fprintf(stdout,"The square root of %g is %g\n", inputValue, outputValue); diff --git a/Tests/Tutorial/Step3/tutorial.cxx b/Tests/Tutorial/Step3/tutorial.cxx index 82b416f11..c27da0b84 100644 --- a/Tests/Tutorial/Step3/tutorial.cxx +++ b/Tests/Tutorial/Step3/tutorial.cxx @@ -21,12 +21,16 @@ int main (int argc, char *argv[]) } double inputValue = atof(argv[1]); + double outputValue = 0; + if(inputValue >= 0) + { #ifdef USE_MYMATH - double outputValue = mysqrt(inputValue); + outputValue = mysqrt(inputValue); #else - double outputValue = sqrt(inputValue); + outputValue = sqrt(inputValue); #endif + } fprintf(stdout,"The square root of %g is %g\n", inputValue, outputValue); diff --git a/Tests/Tutorial/Step4/tutorial.cxx b/Tests/Tutorial/Step4/tutorial.cxx index 82b416f11..c27da0b84 100644 --- a/Tests/Tutorial/Step4/tutorial.cxx +++ b/Tests/Tutorial/Step4/tutorial.cxx @@ -21,12 +21,16 @@ int main (int argc, char *argv[]) } double inputValue = atof(argv[1]); + double outputValue = 0; + if(inputValue >= 0) + { #ifdef USE_MYMATH - double outputValue = mysqrt(inputValue); + outputValue = mysqrt(inputValue); #else - double outputValue = sqrt(inputValue); + outputValue = sqrt(inputValue); #endif + } fprintf(stdout,"The square root of %g is %g\n", inputValue, outputValue); diff --git a/Tests/Tutorial/Step5/tutorial.cxx b/Tests/Tutorial/Step5/tutorial.cxx index 82b416f11..c27da0b84 100644 --- a/Tests/Tutorial/Step5/tutorial.cxx +++ b/Tests/Tutorial/Step5/tutorial.cxx @@ -21,12 +21,16 @@ int main (int argc, char *argv[]) } double inputValue = atof(argv[1]); + double outputValue = 0; + if(inputValue >= 0) + { #ifdef USE_MYMATH - double outputValue = mysqrt(inputValue); + outputValue = mysqrt(inputValue); #else - double outputValue = sqrt(inputValue); + outputValue = sqrt(inputValue); #endif + } fprintf(stdout,"The square root of %g is %g\n", inputValue, outputValue); diff --git a/Tests/Tutorial/Step6/tutorial.cxx b/Tests/Tutorial/Step6/tutorial.cxx index 82b416f11..c27da0b84 100644 --- a/Tests/Tutorial/Step6/tutorial.cxx +++ b/Tests/Tutorial/Step6/tutorial.cxx @@ -21,12 +21,16 @@ int main (int argc, char *argv[]) } double inputValue = atof(argv[1]); + double outputValue = 0; + if(inputValue >= 0) + { #ifdef USE_MYMATH - double outputValue = mysqrt(inputValue); + outputValue = mysqrt(inputValue); #else - double outputValue = sqrt(inputValue); + outputValue = sqrt(inputValue); #endif + } fprintf(stdout,"The square root of %g is %g\n", inputValue, outputValue); diff --git a/Tests/Tutorial/Step7/tutorial.cxx b/Tests/Tutorial/Step7/tutorial.cxx index 82b416f11..c27da0b84 100644 --- a/Tests/Tutorial/Step7/tutorial.cxx +++ b/Tests/Tutorial/Step7/tutorial.cxx @@ -21,12 +21,16 @@ int main (int argc, char *argv[]) } double inputValue = atof(argv[1]); + double outputValue = 0; + if(inputValue >= 0) + { #ifdef USE_MYMATH - double outputValue = mysqrt(inputValue); + outputValue = mysqrt(inputValue); #else - double outputValue = sqrt(inputValue); + outputValue = sqrt(inputValue); #endif + } fprintf(stdout,"The square root of %g is %g\n", inputValue, outputValue);