From a032d0e2c0adffdcaaf5d3ab3eae7100888af118 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste CAYROU Date: Wed, 29 Aug 2012 10:20:58 +0200 Subject: [PATCH] Add test_value method in GTestSuite class. --- include/GTestSuite.hpp | 7 ++++++- pyext/GTestSuite.i | 5 +++++ src/test/GTestSuite.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- test/test_GObservations.cpp | 2 +- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/include/GTestSuite.hpp b/include/GTestSuite.hpp index d9354b6..08fa902 100644 --- a/include/GTestSuite.hpp +++ b/include/GTestSuite.hpp @@ -72,10 +72,15 @@ class GTestSuite{ void test_assert(bool result, const std::string& name, const std::string& message=""); + void test_value(double value, + double value_expected, + double eps = 0, + const std::string& name="", + const std::string& message=""); void test_try(const std::string& name); void test_try_success(); void test_try_failure(const std::string& message="", - const std::string& type=""); + const std::string& type=""); void test_try_failure(const std::exception& e); GException::test_failure& exception_failure(const std::string& message); GException::test_error& exception_error(const std::string& message); diff --git a/pyext/GTestSuite.i b/pyext/GTestSuite.i index cbd63f9..39916e9 100644 --- a/pyext/GTestSuite.i +++ b/pyext/GTestSuite.i @@ -56,6 +56,11 @@ class GTestSuite{ void test_assert(bool result, const std::string& name, const std::string& message=""); + void test_value( double value, + double value_expected, + double eps = 0, + const std::string& name="", + const std::string& message=""); void test_try(const std::string& name); void test_try_success(); void test_try_failure(const std::string& message="", diff --git a/src/test/GTestSuite.cpp b/src/test/GTestSuite.cpp index 49a8410..71e7c19 100644 --- a/src/test/GTestSuite.cpp +++ b/src/test/GTestSuite.cpp @@ -30,6 +30,7 @@ #include #endif #include "GTestSuite.hpp" +#include "GTools.hpp" #include "GLog.hpp" /* __ Method name definitions ____________________________________________ */ @@ -306,10 +307,52 @@ void GTestSuite::test_assert(bool assert, const std::string& name,const std::str // Return return; - + } /***********************************************************************//** + * @brief Test a value + * @param[in] value Value to test + * @param[in] value_expected Value expected + * @param[in] eps Precision of the test (default 0) + * @param[in] name Test case name. (defaults to "") + * @param[in] message Test case message. (defaults to "") + * Test if the value is between [ value_excepted-eps, value_excepted+eps ] + ***************************************************************************/ +void GTestSuite::test_value(double value, double value_expected, double eps, const std::string& name, const std::string& message){ + + std::string formated_name; + if(name!=""){ + formated_name = format_name(name); + } + else{ + formated_name = format_name("Test "+str(value)+" is between ["+str(value_expected-eps)+","+str(value_expected+eps)+"]"); + } + // Create a test case of failure type + GTestCase* testcase = new GTestCase(GTestCase::FAIL_TEST,formated_name); + + // If value is not between [ value_excepted-eps, value_excepted+eps ] + if(value > value_expected + eps || value < value_expected - eps) + { + // test case is not ok + testcase->m_passed=false; + + //increment number of failures + m_failures++; + } + //Set message + testcase->message(message); + + // Show the result ( ".","F" or, "E") + m_log<print_result(); + + // Add the test in the container + m_tests.push_back(testcase); + + // Return + return; +} +/***********************************************************************//** * @brief Test an try block * @param[in] name Test case name. (defaults to "") * @see test_try_sucess() diff --git a/test/test_GObservations.cpp b/test/test_GObservations.cpp index f21f0aa..6e73c8a 100644 --- a/test/test_GObservations.cpp +++ b/test/test_GObservations.cpp @@ -139,7 +139,7 @@ GModelPar& TestGObservation::test_observations_optimizer(int mode) test_assert(opt.status()==0,"Check if converged","Optimizer did not convered"); //check if value is correct - test_assert(fabs(result.value()-RATE) < result.error()*3,"check if value is correct","Value is not precise enough."); + test_value(result.value(),RATE,result.error()*3); return (((obs.models())[0])[0]); } -- libgit2 0.21.4