|
| 1 | +#include <thread> |
| 2 | + |
| 3 | +#include "taskflow/taskflow.hpp" |
| 4 | + |
| 5 | +// NOTE, Make sure all these includes are AFTER the system and header includes |
| 6 | +#include "CppUTest/CommandLineTestRunner.h" |
| 7 | +#include "CppUTest/MemoryLeakDetectorNewMacros.h" |
| 8 | +#include "CppUTest/TestHarness.h" |
| 9 | +#include "CppUTest/Utest.h" |
| 10 | +#include "CppUTestExt/MockSupport.h" |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +std::thread::id my_main_thread = std::this_thread::get_id(); |
| 15 | + |
| 16 | +bool IsRunningInThread() { |
| 17 | + bool threaded = true; |
| 18 | + if (std::this_thread::get_id() == my_main_thread) { |
| 19 | + threaded = false; |
| 20 | + } |
| 21 | + return threaded; |
| 22 | +} |
| 23 | + |
| 24 | +void assert_handle_fatal() { |
| 25 | + if (IsRunningInThread()) { |
| 26 | + mock().actualCall("assert_handle_fatal_threaded"); |
| 27 | + } else { |
| 28 | + mock().actualCall("assert_handle_fatal_main"); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +} // namespace |
| 33 | + |
| 34 | +// clang-format off |
| 35 | +TEST_GROUP(AssertFatalTestGroup) |
| 36 | +{ |
| 37 | + void teardown() { |
| 38 | + mock().clear(); |
| 39 | + } |
| 40 | +}; |
| 41 | +// clang-format on |
| 42 | + |
| 43 | +TEST(AssertFatalTestGroup, AssertFatal_IsThreadedCheck) { |
| 44 | + CHECK_FALSE(IsRunningInThread()); |
| 45 | + |
| 46 | + tf::Taskflow tf; |
| 47 | + tf.emplace([]() { CHECK_TRUE(IsRunningInThread()); }); |
| 48 | + |
| 49 | + tf::Executor ex(1); |
| 50 | + ex.run(tf); |
| 51 | + ex.wait_for_all(); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(AssertFatalTestGroup, AssertFatal_Threaded) { |
| 55 | + mock().expectOneCall("assert_handle_fatal_threaded"); |
| 56 | + |
| 57 | + tf::Taskflow tf; |
| 58 | + tf.emplace([]() { assert_handle_fatal(); }); |
| 59 | + |
| 60 | + tf::Executor ex(1); |
| 61 | + ex.run(tf); |
| 62 | + ex.wait_for_all(); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(AssertFatalTestGroup, AssertFatal_NotThreaded) { |
| 66 | + mock().expectOneCall("assert_handle_fatal_main"); |
| 67 | + assert_handle_fatal(); |
| 68 | +} |
| 69 | + |
| 70 | +int main(int ac, char **av) { |
| 71 | + return CommandLineTestRunner::RunAllTests(ac, av); |
| 72 | +} |
0 commit comments