Skip to content

Commit

Permalink
Better error reporting in unit tests
Browse files Browse the repository at this point in the history
Now  floating point failures look as follows:
[x] Assertion at line 10 (/cygdrive/c/Users/Admin/pdev/rpn/HIP35/test/test.cpp) failed:
    hp->EvalString("1 ENTER") = 1.0000 and 2 = 2.0000 are not close
  • Loading branch information
leonmavr committed Dec 5, 2023
1 parent a73937f commit baf14e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
30 changes: 25 additions & 5 deletions test/nanotest.h
Expand Up @@ -2,7 +2,27 @@
* This is a tiny header-only unit testing framework for
* C and C++. It is compatible with GNU-based compilers
* and has not been tested in anything else.
*
* Features:
* -- Macros for float comparison check and condition
* truth/false check.
* -- Report of passed assertions.
* -- Error report of failed assertion with valiable names
* and their values.
*
* Examples:
* float engineering_pi = 3;
* float actual_pi = 3.14159;
* NTEST_ASSERT_FLOAT_CLOSE(engineering_pi, actual_pi);
* float tolerance = 0.1;
* NTEST_ASSERT_FLOAT_CLOSE(3.1, actual_pi. tolerance);
* NTEST_ASSERT(3 == 3);
* // ...you can return the status of the tests; != 0
* // if at least one fails
* return ntest_result;
*
* Author: L Mavropalias 2023
* License: MIT
*/
extern "C" {

Expand Down Expand Up @@ -52,10 +72,10 @@ int ntest_result = NTEST_PASS;
printf("[v] Assertion at line %d of file %s passed.\n", \
__LINE__, __FILENAME__); \
else { \
fprintf(stderr, "[x] Assertion at line %d of file %s failed:\n" \
" Values of %s and %s are not close\n", \
__LINE__, __FILE__, STR_VAL(actual), \
STR_VAL(expected)); \
fprintf(stderr, "[x] Assertion at line %d (%s) failed:\n" \
" %s = %.4f and %s = %.4f are not close\n", \
__LINE__, __FILE__, STR_NAME(actual), actual_value, \
STR_NAME(expected), expected_value); \
ntest_result = NTEST_FAIL; \
} \
} while(0)
Expand All @@ -72,7 +92,7 @@ int ntest_result = NTEST_PASS;
printf("[v] Assertion at line %d of file %s passed.\n", \
__LINE__, __FILENAME__); \
else { \
fprintf(stderr, "[x] Assertion at line %d of file %s failed:\n" \
fprintf(stderr, "[x] Assertion at line %d (%s) failed:\n" \
" %s\n", __LINE__, __FILE__, #condition); \
ntest_result = NTEST_FAIL; \
} \
Expand Down
7 changes: 4 additions & 3 deletions test/test.cpp
Expand Up @@ -49,7 +49,7 @@ int main() {
// sin(6) * tan(60/2)
NTEST_ASSERT_FLOAT_CLOSE(hp->EvalString(""
"60 SIN LASTX 2 / TAN *"), 0.5);
// --56 (three below)
// --56 (two below)
NTEST_ASSERT_FLOAT_CLOSE(hp->EvalString(""
"16 ENTER 19 - LASTX + LASTX *"), 16*19);
NTEST_ASSERT_FLOAT_CLOSE(hp->EvalString(""
Expand All @@ -58,8 +58,9 @@ int main() {
// store/recall //
//------------------------------------------------------------------//
// NOTE:
// store/recall do not work in EvalString yet. To see why,
// read TODO comment in method's definition
// store/recall do not work in EvalString yet however it
// works in the UI. To see why, read TODO comment in
// method's definition

//------------------------------------------------------------------//
// postfix exponent (EEX) //
Expand Down

0 comments on commit baf14e6

Please sign in to comment.