Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67a74af

Browse files
committed
Added assert_fatal test
1 parent 7ff4fbe commit 67a74af

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

buildcc/lib/env/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ if (${TESTING})
4242
add_executable(test_storage test/test_storage.cpp)
4343
target_link_libraries(test_storage PRIVATE mock_env)
4444

45+
add_executable(test_assert_fatal test/test_assert_fatal.cpp)
46+
target_link_libraries(test_assert_fatal PRIVATE mock_env)
47+
4548
add_test(NAME test_static_project COMMAND test_static_project)
4649
add_test(NAME test_env_util COMMAND test_env_util)
4750
add_test(NAME test_task_state COMMAND test_task_state)
4851
add_test(NAME test_command COMMAND test_command)
4952
add_test(NAME test_storage COMMAND test_storage)
53+
add_test(NAME test_assert_fatal COMMAND test_assert_fatal)
5054
endif()
5155

5256
set(ENV_SRCS
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)