Skip to content

Commit d6205fa

Browse files
PostAndProcess(...) and ChangeMemPoolTeardownOption(...) pulled in from the 'qpc' version of this library.
And other improvements from the qpc version.
1 parent 6eb8964 commit d6205fa

9 files changed

+161
-15
lines changed

cpputest-for-qpcpp-lib/cmake/cpputestCMake.cmake

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ else()
99
message(STATUS "Found CppUTest version ${CPPUTEST_VERSION}")
1010
endif()
1111

12+
if(CPPUTEST_VERSION_MAJOR LESS 4)
13+
# likely 3.8
14+
add_compile_definitions(CMS_CPPUTEST_LEGACY)
15+
else()
16+
# 4.0 version or newer
17+
add_compile_definitions(CMS_CPPUTEST_V4)
18+
endif()
19+
1220
include_directories(${CPPUTEST_INCLUDE_DIRS})
1321
link_directories(${CPPUTEST_LIBRARIES})
1422

1523
add_executable(${TEST_APP_NAME} ${TEST_SOURCES})
1624
target_link_libraries(${TEST_APP_NAME} ${APP_LIB_NAME} ${CPPUTEST_LDFLAGS})
1725

1826
# (5) Run the test once the build is done
19-
add_custom_command(TARGET ${TEST_APP_NAME} COMMAND ./${TEST_APP_NAME} POST_BUILD)
27+
add_custom_command(TARGET ${TEST_APP_NAME} COMMAND ./${TEST_APP_NAME} POST_BUILD)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Created by eshlemanm on 5/18/24, providing a minor porting layer
3+
// to handle CppUTest 3.8 versus 4.0 changes.
4+
//
5+
6+
#ifndef CPPUTEST_FOR_QPC_CMS_CPPUTEST_HPP
7+
#define CPPUTEST_FOR_QPC_CMS_CPPUTEST_HPP
8+
9+
#include "CppUTest/TestHarness.h"
10+
11+
#ifdef CMS_CPPUTEST_LEGACY
12+
#define CMS_TEST_EXIT do { TEST_EXIT } while(0)
13+
#else
14+
#define CMS_TEST_EXIT do { TEST_EXIT; } while(0)
15+
#endif
16+
17+
#endif // CPPUTEST_FOR_QPC_CMS_CPPUTEST_HPP

cpputest-for-qpcpp-lib/include/cms_cpputest_qf_ctrl.hpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/// @brief Supporting methods for setup, teardown, of a fake cpputest
2-
/// compatible port of the qpcpp QF framework.
1+
/// @brief Supporting methods for setup, teardown, and control of a fake
2+
/// cpputest compatible port of the QP/C++ (qpcpp) framework.
33
/// @ingroup
44
/// @cond
55
///***************************************************************************
@@ -22,8 +22,8 @@
2222
///***************************************************************************
2323
/// @endcond
2424

25-
#ifndef CPPUTEST_FOR_QPCPP_CMS_CPPUTEST_QF_CTRL_HPP
26-
#define CPPUTEST_FOR_QPCPP_CMS_CPPUTEST_QF_CTRL_HPP
25+
#ifndef CMS_CPPUTEST_QF_CTRL_HPP
26+
#define CMS_CPPUTEST_QF_CTRL_HPP
2727

2828
#include <chrono>
2929
#include "qpcpp.hpp"
@@ -62,6 +62,8 @@ void Setup(enum_t maxPubSubSignalValue, uint32_t ticksPerSecond,
6262
const MemPoolConfigs& pubSubEventMemPoolConfigs = {},
6363
MemPoolTeardownOption memPoolOpt = MemPoolTeardownOption::CHECK_FOR_LEAKS);
6464

65+
void ChangeMemPoolTeardownOption(MemPoolTeardownOption memPoolOpt);
66+
6567
/// Teardown the QP/QF subsystem after completing a unit test.
6668
void Teardown();
6769

@@ -95,6 +97,27 @@ void PublishAndProcess(enum_t sig,
9597
void PublishAndProcess(QP::QEvt const * e,
9698
PublishedEventRecorder* recorder = nullptr);
9799

100+
/// Helper method to Post an event to an active object
101+
/// followed internally by ProcessEvents().
102+
/// \param e
103+
/// \param dest
104+
inline void PostAndProcess(QP::QEvt const * e, QP::QActive* dest)
105+
{
106+
dest->POST(e, nullptr);
107+
ProcessEvents();
108+
}
109+
110+
/// Helper method to Post a static const QEvt to an active object
111+
/// followed internally by ProcessEvents().
112+
/// \param sig (template param): the signal value of the const event to post
113+
/// \param dest : the active object to post to.
114+
template <enum_t sig>
115+
inline void PostAndProcess(QP::QActive* dest)
116+
{
117+
static const QP::QEvt constEvent = QP::QEvt(sig);
118+
PostAndProcess(&constEvent, dest);
119+
}
120+
98121
/// Get the internal library version string.
99122
/// Uses semantic versioning.
100123
const char * GetVersion();
@@ -103,4 +126,4 @@ const char * GetVersion();
103126
} //namespace test
104127
} //namespace cms
105128

106-
#endif //CPPUTEST_FOR_QPCPP_CMS_CPPUTEST_QF_CTRL_HPP
129+
#endif //CMS_CPPUTEST_QF_CTRL_HPP

cpputest-for-qpcpp-lib/include/qassertMockSupport.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// @brief Support methods to help with unit test mocks for QASSERT.
1+
/// @brief Support methods to help with unit testing for QASSERT.
22
/// @ingroup
33
/// @cond
44
///***************************************************************************
@@ -21,8 +21,8 @@
2121
///***************************************************************************
2222
/// @endcond
2323

24-
#ifndef CPPUTEST_FOR_QPCPP_QASSERTMOCKSUPPORT_HPP
25-
#define CPPUTEST_FOR_QPCPP_QASSERTMOCKSUPPORT_HPP
24+
#ifndef QASSERT_MOCK_SUPPORT_HPP
25+
#define QASSERT_MOCK_SUPPORT_HPP
2626

2727
#include "CppUTestExt/MockSupport.h"
2828
#include "qsafe.h"
@@ -31,7 +31,7 @@ namespace cms {
3131
namespace test {
3232

3333
static constexpr const char* QASSERT_MOCK_NAME = "QASSERT";
34-
static constexpr const char* ONERROR_FUNC_NAME = "Q_onError";
34+
static constexpr const char* ONERROR_FUNC_NAME = "Q_onError";
3535

3636
inline void MockExpectQAssert()
3737
{
@@ -51,4 +51,4 @@ inline void MockExpectQAssert(const char* module, int id)
5151
} // namespace test
5252
} // namespace cms
5353

54-
#endif // CPPUTEST_FOR_QPCPP_QASSERTMOCKSUPPORT_HPP
54+
#endif // QASSERT_MOCK_SUPPORT_HPP

cpputest-for-qpcpp-lib/src/cms_cpputest_q_onAssert.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
///***************************************************************************
2727
/// @endcond
2828

29-
#include "CppUTest/TestHarness.h"
29+
#include "cms_cpputest.hpp"
3030
#include "qp_port.hpp"
3131
#include "qsafe.h"
3232
#include "qassertMockSupport.hpp"
@@ -51,5 +51,5 @@ void Q_onError(char const* const module, int_t const id)
5151
.withParameter("module", module)
5252
.withParameter("id", id);
5353

54-
TEST_EXIT;
54+
CMS_TEST_EXIT;
5555
}

cpputest-for-qpcpp-lib/src/cms_cpputest_qf_ctrl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ void Teardown()
127127
}
128128
}
129129

130+
void ChangeMemPoolTeardownOption(MemPoolTeardownOption memPoolOpt)
131+
{
132+
l_memPoolOption = memPoolOpt;
133+
}
134+
130135
void ProcessEvents()
131136
{
132137
QP::RunUntilNoReadyActiveObjects();

cpputest-for-qpcpp-lib/tests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(TEST_SOURCES
55
qassertTests.cpp
66
cms_cpputest_qf_ctrlTests.cpp
77
cms_cpputest_qf_ctrlPublishTests.cpp
8+
cms_cpputest_qf_ctrl_post_tests.cpp
89
publishedEventRecorderTests.cpp
910
backedQueueTests.cpp
1011
orthogonalComponentTests.cpp
@@ -15,4 +16,4 @@ set(TEST_SOURCES
1516
# defined, and creates the cpputest based test executable target
1617
include(${CMS_CMAKE_DIR}/cpputestCMake.cmake)
1718

18-
target_link_libraries(${TEST_APP_NAME} cpputest-for-qpcpp-lib ${CPPUTEST_LDFLAGS})
19+
target_link_libraries(${TEST_APP_NAME} cpputest-for-qpcpp-lib ${CPPUTEST_LDFLAGS})

cpputest-for-qpcpp-lib/tests/cms_cpputest_qf_ctrlTests.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ TEST(qf_ctrlTests, setup_provides_option_to_skip_memory_pool_leak_detection)
9898
// teardown.
9999
}
100100

101+
TEST(qf_ctrlTests, provides_modify_memory_pool_leak_detection)
102+
{
103+
//Do a setup with leak detection.
104+
qf_ctrl::Setup(10, 1000, qf_ctrl::MemPoolConfigs {},
105+
qf_ctrl::MemPoolTeardownOption::CHECK_FOR_LEAKS);
106+
107+
//but, this test only wants to disable leak detection, likely because of ASSERT
108+
//testing which may interrupt normal event processing, resulting in false leaks.
109+
qf_ctrl::ChangeMemPoolTeardownOption(qf_ctrl::MemPoolTeardownOption::IGNORE);
110+
111+
// purposeful leak of an allocated QEvt
112+
QP::QEvt* volatile e = Q_NEW(QP::QEvt, 5);
113+
(void)e;
114+
115+
// test should pass, as we disabled the memory pool leak detection during
116+
// teardown.
117+
}
118+
101119
TEST(qf_ctrlTests,
102120
qf_ctrl_provides_for_ability_to_move_time_forward_firing_active_object_timers_as_expected)
103121
{
@@ -147,4 +165,4 @@ TEST(qf_ctrlTests, qf_ctrl_provides_cpputest_for_qpcpp_lib_version)
147165

148166
std::string version_str(version);
149167
CHECK_FALSE(version_str.empty());
150-
}
168+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//// @brief Tests for the qf_ctrl post related functions for the QF cpputest port.
2+
/// @ingroup
3+
/// @cond
4+
///***************************************************************************
5+
///
6+
/// Copyright (C) 2024 Matthew Eshleman. All rights reserved.
7+
///
8+
/// This program is open source software: you can redistribute it and/or
9+
/// modify it under the terms of the GNU General Public License as published
10+
/// by the Free Software Foundation, either version 3 of the License, or
11+
/// (at your option) any later version.
12+
///
13+
/// Alternatively, upon written permission from Matthew Eshleman, this program
14+
/// may be distributed and modified under the terms of a Commercial
15+
/// License. For further details, see the Contact Information below.
16+
///
17+
/// Contact Information:
18+
/// Matthew Eshleman
19+
/// https://covemountainsoftware.com
20+
21+
///***************************************************************************
22+
/// @endcond
23+
24+
#include "cmsDummyActiveObject.hpp"
25+
#include "cms_cpputest_qf_ctrl.hpp"
26+
#include "qpcpp.hpp"
27+
28+
//cpputest header include must always be last
29+
#include "CppUTest/TestHarness.h"
30+
31+
using namespace cms::test;
32+
33+
TEST_GROUP(qf_ctrl_post_tests)
34+
{
35+
cms::DefaultDummyActiveObject* mDummy = nullptr;
36+
37+
void setup() final
38+
{
39+
qf_ctrl::Setup(QP::Q_USER_SIG, 100);
40+
mDummy = new cms::DefaultDummyActiveObject();
41+
mDummy->dummyStart();
42+
}
43+
44+
void teardown() final
45+
{
46+
delete mDummy;
47+
cms::test::qf_ctrl::Teardown();
48+
}
49+
};
50+
51+
TEST(qf_ctrl_post_tests,
52+
provides_a_post_and_process_helper_func_with_trivial_signal_enum)
53+
{
54+
static constexpr enum_t TEST1_SIG = QP::Q_USER_SIG + 1;
55+
enum_t capturedSig = -1;
56+
mDummy->SetPostedEventHandler([&](const QP::QEvt* e){
57+
capturedSig = e->sig;
58+
});
59+
qf_ctrl::PostAndProcess<TEST1_SIG>(mDummy);
60+
CHECK_EQUAL(TEST1_SIG, capturedSig);
61+
}
62+
63+
TEST(qf_ctrl_post_tests,
64+
provides_a_post_and_process_helper_func)
65+
{
66+
static constexpr enum_t TEST2_SIG = QP::Q_USER_SIG + 111;
67+
static const QP::QEvt testEvent = QP::QEvt(TEST2_SIG);
68+
enum_t capturedSig = -1;
69+
mDummy->SetPostedEventHandler([&](const QP::QEvt* e){
70+
capturedSig = e->sig;
71+
});
72+
qf_ctrl::PostAndProcess(&testEvent, mDummy);
73+
CHECK_EQUAL(TEST2_SIG, capturedSig);
74+
}

0 commit comments

Comments
 (0)