Skip to content

Commit 0cee3a1

Browse files
committed
fix: [tests] Check custom fail msg is already set
Check if custom fail message is already set before converting expected & actual message to strings. This will enable setting custom fail message separately. Add test framework description to README.
1 parent a66940d commit 0cee3a1

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ retained but no new code is pushed due to me using C++ full time so I gain
1111
expertise in that.
1212

1313
For these practice programs I have borrowed working code from various online
14-
sources and relevant credits are given in code comments. So I hope anyone who
14+
sources and relevant credits are given in code comments. So I hope anyone who
1515
finds this repository may use it for their own learning of data structures and
1616
algorithms. After learning from working solutions in this repository you may
1717
practice solving problems on coding contest platforms with time limit.
@@ -29,7 +29,7 @@ Coding guidelines
2929
* Every C++ source file must build & run as a single file target the focus is
3030
on solving the problem correctly.
3131
* Each C++ compiled binary must output test result as a single line in this
32-
format `Executed M implementations with N tests.`. This makes it easy to
32+
format `Executed M implementations with N tests.`. This makes it easy to
3333
get tests summary.
3434
* For debugging each binary must print test name, test case number with input
3535
and output in this format and only when enabled with environment variable
@@ -70,3 +70,33 @@ make cpp/arrays/2sum-01-format # format .cpp source file
7070

7171
SHOW_TEST_OUTPUT=1 make cpp/arrays/2sum-01-check # run test with debug output
7272
```
73+
74+
Test framework
75+
--------------
76+
77+
This repository uses a single header file test "framework" to write test cases
78+
to verify the correctness of the algorithms and various implementations. It
79+
is designed to execute test cases by providing definition to `TEST()` macros
80+
with unique and name description to that test. This step registers the test
81+
with the tests registry.
82+
83+
To check the test conditions with expected and actual values it provides
84+
`CHECK_EQ()` macro. It checks a given condition and increments test case
85+
count on success and reports error and exits on failure. To convert the
86+
expected & actual data into printable strings helper functions such as
87+
`to_string()` have to be provided. Use `SUCCESS()` & `FAIL()` macros to
88+
create custom checks not provided by the framework.
89+
90+
The test output can be printed with `SHOW_OUTPUT()` macro to verify the inputs
91+
passed to an algorithm and the output received from it. The output can be
92+
manipulated by providing the list of IO manipulators to `SET_IO()` and reset
93+
with `RESET_IO()`. Custom test can be provided using `SET_CUSTOM_FAIL_MSG()`
94+
or by providing `to_string()` overloads.
95+
96+
The test code is initialized with `INIT_TEST_MAIN()` macro that provides
97+
`main()` function definition which includes `RUN_ALL_TESTS()` macro function
98+
to run all the registered tests.
99+
100+
This framework removes the need to write test output and test logic code
101+
across the implementations significantly reducing the number of source lines
102+
in a single file and improves readability.

cpp/includes/tests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ static vector<_test *> _tests;
107107
if (e op a) { \
108108
SUCCESS(); \
109109
} else { \
110-
string em = to_string(e), am = to_string(a); \
111-
SET_CUSTOM_FAIL_MSG(em, am); \
110+
if (empty(this->em) && empty(this->am)) \
111+
this->em = to_string(e), this->am = to_string(a); \
112112
FAIL(); \
113113
}
114114

0 commit comments

Comments
 (0)