Unit Test Framework Modern C++ API Reference
Tester.hpp
1/*
2 * (c) Copyright, Real-Time Innovations, 2014-2022.
3 *
4 * All rights reserved.
5 * No duplications, whole or partial, manual or electronic, may be made
6 * without express written permission. Any such copies, or
7 * revisions thereof, must display this notice unaltered.
8 * This code contains trade secrets of Real-Time Innovations, Inc.
9 */
10
11#ifndef RTI_TEST_TESTER_HPP_
12#define RTI_TEST_TESTER_HPP_
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <stdexcept>
17#include <string>
18#include <functional> // for std::mem_fun
19#include <vector>
20
21#include "rtitest/test_setting.h"
22#include "rtitest/TestAssertion.hpp"
23
36namespace rti { namespace test {
37
38// This adapter function takes a member function of a C++ Tester and runs it as
39// the C test framework expects: it catches exceptions, reports errors as
40// return codes and prints log info.
41template <typename Tester, typename TestFunc>
42int run_test_function(
43 const struct RTITestSetting *setting,
44 const std::string& function_name,
45 Tester& tester,
46 TestFunc test_function)
47{
48 // Log the test that is running
49 RTITestLog_printTestNameExtWithFunctionName(function_name.c_str(), NULL);
50
51 // Set the current test name in the global variable
52 if (RTITest_g_currentTestNameAllocated != NULL) {
53 size_t pos = function_name.find("::");
54 if (pos == std::string::npos) {
55 if (function_name.size()
56 < RTI_TEST_MAX_CURRENT_TEST_NAME_ALLOCATED_SIZE - 1) {
57 RTITest_g_currentTestNameAllocated =
58 strncpy(RTITest_g_currentTestNameAllocated,
59 function_name.c_str(),
60 function_name.size());
61 }
62 } else {
63 size_t method_name_size = pos + strlen("::");
64 if (function_name.size() - method_name_size
65 < RTI_TEST_MAX_CURRENT_TEST_NAME_ALLOCATED_SIZE - 1) {
66 RTITest_g_currentTestNameAllocated =
67 strncpy(RTITest_g_currentTestNameAllocated,
68 function_name.c_str() + method_name_size,
69 function_name.size() - method_name_size);
70 }
71 }
72 }
73
74 try {
75 tester.set_settings(setting);
76 if (tester.file_system_is_required()) {
77 tester.assert_file_system_support();
78 }
79
80 // Run the actual test
81 test_function(&tester);
82 } catch (const TestFailure& ex) {
83 // Test fails because an assertion failed
84 RTITest_assertLogWithFunctionName(
85 function_name.c_str(),
86 &RTI_TEST_LOG_EMPTY_TEMPLATE,
87 "%s",
88 ex.what());
89 if (RTITestConnext_isAbortOnErrorEnabled()) {
90 throw;
91 } else {
92 return 0;
93 }
94 } catch (const TestIncomplete& ex) {
95 // Test fails because an assertion failed
96 return ex.retcode();
97 } catch (const std::exception& ex) {
98 // Test fails because some operation failed (not an assertion)
99 RTITest_assertLogWithFunctionName(
100 function_name.c_str(),
101 &RTI_TEST_LOG_EMPTY_TEMPLATE,
102 "Unhandled exception in test: %s",
103 ex.what());
104 if (RTITestConnext_isAbortOnErrorEnabled()) {
105 throw;
106 } else {
107 return 0;
108 }
109 } catch (...) {
110 // Test fails because of an unknown exception
111 RTITest_assertLogWithFunctionName(
112 function_name.c_str(),
113 &RTI_TEST_LOG_EMPTY_TEMPLATE,
114 "Unknown exception in test");
115 if (RTITestConnext_isAbortOnErrorEnabled()) {
116 throw;
117 } else {
118 return 0;
119 }
120 }
121
122 return 1; // test passes
123}
124
130#define RTI_TEST_FUNCTION_ADD(class_name__, test_function__) \
131 struct runner_##test_function__ { \
132 static int run(const struct RTITestSetting *setting) \
133 { \
134 return run_test_function( \
135 setting, \
136 class_name__::get_instance().get_name() + "::" #test_function__, \
137 class_name__::get_instance(), \
138 std::mem_fn(&class_name__::test_function__)); \
139 } \
140 }; \
141\
142 add_test_function((RTITestFunction) runner_##test_function__::run)
143
144// The RTI_TEST_FUNCTION_ADD_ADDITIONAL macro was created to support splitting
145// tests as basic and additional. Tests added with this macro will belong to
146// the additional test set
147#define RTI_TEST_FUNCTION_ADD_ADDITIONAL(class_name__, test_function__) \
148 struct runner_##test_function__ { \
149 static int run(const struct RTITestSetting *setting) \
150 { \
151 return run_test_function( \
152 setting, \
153 class_name__::get_instance().get_name() + "::" #test_function__, \
154 class_name__::get_instance(), \
155 std::mem_fn(&class_name__::test_function__)); \
156 } \
157 }; \
158\
159 add_test_function_additional((RTITestFunction) runner_##test_function__::run)
160
161// The RTI_TEST_FUNCTION_ADD_BASICPUBSUB macro was created to support running a
162// basic publish subscribe test as part of
163// CORE_BASICPUBSUB_VALIDATION_ARCHITECTURES. Tests added with this macro will
164// belong to _test_functions_basic_pubsub test set
165#define RTI_TEST_FUNCTION_ADD_BASICPUBSUB(class_name__, test_function__) \
166 struct runner_basicpubsub_##test_function__ { \
167 static int run(const struct RTITestSetting *setting) \
168 { \
169 return run_test_function( \
170 setting, \
171 class_name__::get_instance().get_name() + "::" #test_function__, \
172 class_name__::get_instance(), \
173 std::mem_fn(&class_name__::test_function__)); \
174 } \
175 }; \
176\
177 add_test_function_basic_pubsub( \
178 (RTITestFunction) runner_basicpubsub_##test_function__::run)
179
180#define RTI_TEST_FUNCTION_ADD_FROM_BASE_CLASS( \
181 CLASS_NAME, BASE_CLASS, TEST_FUNCTION) \
182 struct runner_ ## TEST_FUNCTION { \
183 static int run(const struct RTITestSetting* setting) \
184 { \
185 return rti::test::run_test_function( \
186 setting, \
187 CLASS_NAME::get_instance().get_name()+"::"#TEST_FUNCTION, \
188 static_cast<BASE_CLASS&>(CLASS_NAME::get_instance()), \
189 std::mem_fn(&BASE_CLASS::TEST_FUNCTION)); \
190 } \
191 }; \
192 \
193 add_test_function((RTITestFunction) runner_ ## TEST_FUNCTION::run);
194
195/*
196 Inherit from this class to get a singleton behavior:
197
198 class MyClass : public Singleton<MyClass> {
199 private:
200 MyClass();
201 friend Singleton<MyClass>;
202 };
203 */
204template <class T>
205class Singleton {
206public:
207 static T& get_instance()
208 {
209 static T instance;
210 return instance;
211 }
212};
213
228template <
229 typename T,
230 typename R1,
231 R1 InitializeFn(T *),
232 typename R2,
233 R2 FinalizeFn(T *)>
234struct RaiiWrapper : public T {
236 {
237 (void) InitializeFn(this);
238 }
239
241 {
242 (void) FinalizeFn(this);
243 }
244
245 RaiiWrapper(RaiiWrapper const&) = delete;
246 RaiiWrapper(RaiiWrapper&&) = delete;
247
248 RaiiWrapper& operator=(RaiiWrapper const&) = delete;
249 RaiiWrapper& operator=(RaiiWrapper&&) = delete;
250};
251
252/* Provides basic functionality all Testers can benefit from:
253 * Registration of test functions
254 * Running those test functions using the underlying test framework
255 * Accessing the test settings
256 * Indicating that a test requires file system--that will ignore all tests
257 if the current platform doesn't have one.
258 */
259class Tester {
260public:
261 Tester(const std::string& name,
262 RTITestLevel test_level = RTI_TEST_LEVEL_UNIT,
263 bool tests_require_file_system = false)
264 : _settings(NULL),
265 _name(name),
266 _test_level(test_level),
267 _tests_require_file_system(tests_require_file_system)
268 { }
269
270 virtual ~Tester()
271 { }
272
273 const std::string& get_name() const
274 {
275 return _name;
276 }
277
278 // Methods to register and run tests:
279 int get_test_count() const
280 {
281 return static_cast<int>(_test_functions.size());
282 }
283
284 int get_test_count_additional() const
285 {
286 return static_cast<int>(_test_functions_additional.size());
287 }
288
289 int get_test_count_basic_pubsub() const
290 {
291 return static_cast<int>(_test_functions_basic_pubsub.size());
292 }
293
294 const RTITestFunction *get_test_functions() const
295 {
296 return (_test_functions.empty() ? NULL : &_test_functions[0]);
297 }
298
299 const RTITestFunction *get_test_functions_additional() const
300 {
301 return (_test_functions_additional.empty()
302 ? NULL
303 : &_test_functions_additional[0]);
304 }
305
306 const RTITestFunction *get_test_functions_basic_pubsub() const
307 {
308 return (_test_functions_basic_pubsub.empty()
309 ? NULL
310 : &_test_functions_basic_pubsub[0]);
311 }
312
313 void add_test_function(RTITestFunction fnc)
314 {
315 _test_functions.push_back(fnc);
316 }
317
318 void add_test_function_additional(RTITestFunction fnc)
319 {
320 _test_functions_additional.push_back(fnc);
321 }
322
323 void add_test_function_basic_pubsub(RTITestFunction fnc)
324 {
325 _test_functions_basic_pubsub.push_back(fnc);
326 }
327
328 template <typename SingletonTester>
329 void merge_tester()
330 {
331 merge_tester(SingletonTester::get_instance());
332 }
333
334 void set_settings(const RTITestSetting *settings)
335 {
336 _settings = settings;
337 }
338
339 const RTITestSetting *get_settings() const
340 {
341 if (_settings == NULL) {
342 throw std::logic_error("test settings not specified");
343 }
344 return _settings;
345 }
346
347 std::string get_resource_path(const std::string& file) const
348 {
349 char resourcePath[RTI_TEST_MAX_FILE_NAME_LENGTH] = { 0 };
350 RTI_TEST_ASSERT(RTITestSetting_getFullResourcePathFileName(
351 get_settings(),
352 resourcePath,
353 file.c_str()));
354
355 return std::string(resourcePath);
356 }
357
358 int32_t domain_id() const
359 {
360 return get_settings()->id;
361 }
362
363 bool file_system_is_required() const
364 {
365 return _tests_require_file_system;
366 }
367
368 bool file_system_support() const
369 {
370 return (RTITestSetting_fileTestIsSupport(get_settings()) != 0);
371 }
372
373 void assert_file_system_support() const
374 {
375 if (!file_system_support()) {
376 RTI_TEST_THROW_UNSUPPORTED("File system not supported");
377 }
378 }
379
380 virtual RTIBool run_tests(const RTITestSetting *settings)
381 {
382 int begin, end;
383 RTITestFunction *testSelectionBasic = NULL,
384 *testSelectionAdditional = NULL;
385 int testSelectionBasicSize = 0, testSelectionAdditionalSize = 0;
386
387 RTITestSetting_getRange(settings, &begin, &end, _test_level);
388 if (RTITestSetting_isCoreBasicPubSubValidationArchitecture()
389 && !RTITestSetting_isRangeSpecified(begin, end)) {
390 testSelectionBasic =
391 (RTITestFunction *) get_test_functions_basic_pubsub();
392 testSelectionBasicSize = get_test_count_basic_pubsub();
393 testSelectionAdditional = NULL;
394 testSelectionAdditionalSize = 0;
395 } else {
396 testSelectionBasic = (RTITestFunction *) get_test_functions();
397 testSelectionBasicSize = get_test_count();
398 testSelectionAdditional =
399 (RTITestFunction *) get_test_functions_additional();
400 testSelectionAdditionalSize = get_test_count_additional();
401 }
402
403 return RTITestSetting_runTestsExt(
404 settings,
405 _name.c_str(),
406 testSelectionBasic,
407 testSelectionBasicSize,
408 testSelectionAdditional,
409 testSelectionAdditionalSize,
410 begin,
411 end);
412 }
413
414
415private:
416 // Adds the test functions from src_tester into dest_tester.
417 // Useful to combine smaller Testers into a larger one
418 void merge_tester(Tester& src_tester)
419 {
420 const RTITestFunction *src_functions = src_tester.get_test_functions();
421 const int test_count = src_tester.get_test_count();
422 const RTITestFunction *src_functions_additional =
423 src_tester.get_test_functions_additional();
424 const int test_count_additional =
425 src_tester.get_test_count_additional();
426 const RTITestFunction *src_functions_basic_pubsub =
427 src_tester.get_test_functions_basic_pubsub();
428 const int test_count_basicpubsub =
429 src_tester.get_test_count_basic_pubsub();
430
431 if (src_functions != NULL) {
432 for (int i = 0; i < test_count; i++) {
433 add_test_function(src_functions[i]);
434 }
435 }
436 if (src_functions_additional != NULL) {
437 for (int i = 0; i < test_count_additional; i++) {
438 add_test_function_additional(src_functions_additional[i]);
439 }
440 }
441 if (src_functions_basic_pubsub != NULL) {
442 for (int i = 0; i < test_count_basicpubsub; i++) {
443 add_test_function_basic_pubsub(src_functions_basic_pubsub[i]);
444 }
445 }
446 }
447
448 const RTITestSetting *_settings;
449
450 enum { MAX_TEST_COUNT = 64 };
451
452 std::vector<RTITestFunction> _test_functions;
453 std::vector<RTITestFunction> _test_functions_additional;
454 std::vector<RTITestFunction> _test_functions_basic_pubsub;
455
456 const std::string _name;
457 RTITestLevel _test_level;
458 bool _tests_require_file_system;
459};
460
461// Generic function to run all the test functions in a Tester.
462// When instantiated for a concrete Tester it has the
463// signature that the C test framework expects.
464template <typename SingletonTester>
465int run_tester(const struct RTITestSetting *setting)
466{
467 return SingletonTester::get_instance().run_tests(setting); // does not
468 // throw
469}
470
477public:
478 TesterContainer(const std::string& name)
479 : tester_(name, RTI_TEST_LEVEL_SUBMODULE)
480 { }
481
482 virtual ~TesterContainer()
483 {
484 // Empty virtual destructor added because this class includes virtual
485 // functions
486 }
487
488 template <typename Tester>
489 void add_tester()
490 {
491 tester_.add_test_function(run_tester<Tester>);
492 }
493
494 template <typename Tester>
495 void add_tester_basic_pubsub()
496 {
497 tester_.add_test_function_basic_pubsub(run_tester<Tester>);
498 }
499
506 int run_tests(const RTITestSetting& setting)
507 {
508 if (!on_tests_begin(setting)) {
509 return 1;
510 }
511
512 int ok = tester_.run_tests(&setting);
513 if (!on_tests_end(setting)) {
514 ok = 0;
515 }
516
517 RTITestSetting_printComplete(tester_.get_name().c_str());
518 return ok ? 0 : 1; // return 0 on success as expected by main()
519 }
520
528 int run_tests(int argc, char **argv)
529 {
530 RTITestSetting setting;
531 RTITestSetting_init(&setting);
532 if (!RTITestSetting_parseArgument(&setting, argc, argv)) {
533 return RTI_FALSE;
534 }
535
536 return run_tests(setting);
537 }
538
539protected:
540 // These function can be overridden to add additional behavior before and
541 // after running all tests.
542 virtual bool on_tests_begin(const RTITestSetting&)
543 {
544 return true;
545 }
546
547 virtual bool on_tests_end(const RTITestSetting&)
548 {
549 return true;
550 }
551
552private:
553 Tester tester_; // does most of the work
554};
555
556}} // namespace rti::test
557
558#endif // RTI_TEST_TESTER_HPP_
A container of Testers. This is the top level tester, so tester classes should inherits from this cla...
Definition: Tester.hpp:476
int run_tests(int argc, char **argv)
Parses the command-line arguments and runs the tests.
Definition: Tester.hpp:528
int run_tests(const RTITestSetting &setting)
Runs the tests.
Definition: Tester.hpp:506
#define RTI_TEST_ASSERT(expression__)
Asserts that the given expression is true. Otherwise, throw a TestFailure exception.
Definition: TestAssertion.hpp:188
#define RTI_TEST_THROW_UNSUPPORTED(reason__)
Throw a TestIncomplete exception with the unsupported return code.
Definition: TestAssertion.hpp:209
Auxiliary class that augments C structs with a destructor.
Definition: Tester.hpp:234