Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
TestingBase.hh
Go to the documentation of this file.
1
10
11#ifndef TESTING_BASE_HH
12#define TESTING_BASE_HH
13
17
19#include <gtest/gtest.h>
20
22
23
31template<typename T>
32class TestingBase : public ::testing::Test
33{
34 public:
35
41 TestingBase() = default;
42
43
49 virtual ~TestingBase() = default;
50
51
60 inline static void SetCommandLineArgs(int argc, char** argv)
61 {
62 m_argc = argc;
63 m_argv = argv;
64 }
65
66 protected:
67
75 inline void RequiredSetup()
76 {
77 // Parse the command line arguments.
78 ParseArgc();
79
80 // Initialize Loggers if they are not already initialized.
81 if (logging::g_qSharedLogger == nullptr)
82 {
83 logging::InitializeLoggers(constants::LOGGING_OUTPUT_PATH_ABSOLUTE, m_szTimestamp);
84 }
85 }
86
87
95 inline void RequiredTeardown() {}
96
97
104 inline void SetupRoveComm()
105 {
106 // Initialize RoveComm.
107 if (network::g_pRoveCommUDPNode == nullptr)
108 {
109 network::g_pRoveCommUDPNode = std::make_shared<rovecomm::RoveCommUDP>();
110 network::g_bRoveCommUDPStatus = network::g_pRoveCommUDPNode->InitUDPSocket(manifest::General::ETHERNET_UDP_PORT);
111 }
112
113 if (network::g_pRoveCommTCPNode == nullptr)
114 {
115 network::g_pRoveCommTCPNode = std::make_shared<rovecomm::RoveCommTCP>();
116 network::g_bRoveCommTCPStatus =
117 network::g_pRoveCommTCPNode->InitTCPSocket(constants::ROVECOMM_TCP_INTERFACE_IP.c_str(), manifest::General::ETHERNET_TCP_PORT);
118 }
119
120 // Check if RoveComm was successfully initialized.
121 if (!network::g_bRoveCommUDPStatus || !network::g_bRoveCommTCPStatus)
122 {
123 // Submit logger message.
124 LOG_CRITICAL(logging::g_qSharedLogger,
125 "RoveComm did not initialize properly! UDPNode Status: {}, TCPNode Status: {}",
126 network::g_bRoveCommUDPStatus,
127 network::g_bRoveCommTCPStatus);
128
129 // Since RoveComm is crucial, skip the test.
130 GTEST_SKIP();
131 }
132 else
133 {
134 // Submit logger message.
135 LOG_INFO(logging::g_qSharedLogger, "RoveComm UDP and TCP nodes successfully initialized.");
136 }
137 }
138
139
146 inline void TeardownRoveComm()
147 {
148 // Stop RoveComm quill logging or quill will segfault if trying to output logs to RoveComm.
149 network::g_bRoveCommUDPStatus = false;
150 network::g_bRoveCommTCPStatus = false;
151
152 // Stop handlers.
153 if (network::g_pRoveCommUDPNode != nullptr)
154 {
155 network::g_pRoveCommUDPNode->CloseUDPSocket();
156 }
157 if (network::g_pRoveCommTCPNode != nullptr)
158 {
159 network::g_pRoveCommTCPNode->CloseTCPSocket();
160 }
161
162 // Delete dynamically allocated objects.
163 delete network::g_pRoveCommUDPNode;
164 delete network::g_pRoveCommTCPNode;
165
166 // Set dangling pointers to null.
167 network::g_pRoveCommUDPNode = nullptr;
168 network::g_pRoveCommTCPNode = nullptr;
169 }
170
171
178 virtual inline void TestSetup() {}
179
180
187 virtual inline void TestTeardown() {}
188
189 private:
190 // Declare private members.
191 static int m_argc;
192 static char** m_argv;
193 static std::string m_szTimestamp;
194
195
201 inline void SetUp() override
202 {
203 // Call the base setup method. This initializes the loggers and RoveComm instances.
205 // Call the user's setup method.
206 TestSetup();
207 }
208
209
215 inline void TearDown() override
216 {
217 // Call the base teardown method. This stops the RoveComm instances and loggers.
219 // Call the user's teardown method.
220 TestTeardown();
221 }
222
223
229 inline void ParseArgc()
230 {
231 // Set the current timestamp as the default timestamp.
232 std::string szTimestamp = timeops::GetTimestamp();
233
234 // Check if the test being run is in the list of tests to skip setting up logging.
235 for (int i = 1; i < m_argc; ++i)
236 {
237 if (std::string(m_argv[i]).find("--timestamp=") != std::string::npos)
238 {
239 szTimestamp = std::string(m_argv[i]).substr(std::string(m_argv[i]).find("=") + 1);
240 break;
241 }
242 }
243
244 // Set the timestamp.
245 m_szTimestamp = szTimestamp;
246 }
247};
248
249// Initialize static members
250template<typename T>
251int TestingBase<T>::m_argc = 0;
252
253template<typename T>
254char** TestingBase<T>::m_argv = nullptr;
255
256template<typename T>
257std::string TestingBase<T>::m_szTimestamp = "";
258
259#endif // TESTING_BASE_HH
Defines functions and objects used project wide.
Implements Logging for Autonomy.
Defines functions and objects used for Autonomy Networking.
static void SetCommandLineArgs(int argc, char **argv)
Mutator for the Command Line Args private member.
Definition TestingBase.hh:60
virtual void TestSetup()
User's setup method.
Definition TestingBase.hh:178
void TeardownRoveComm()
Teardown RoveComm for testing.
Definition TestingBase.hh:146
void ParseArgc()
Method to parse the command line arguments.
Definition TestingBase.hh:229
void TearDown() override
Method to tear down the test class.
Definition TestingBase.hh:215
void RequiredTeardown()
Required teardown for all tests.
Definition TestingBase.hh:95
void RequiredSetup()
Required setup for all tests.
Definition TestingBase.hh:75
virtual ~TestingBase()=default
Destroy the Testing Base object.
void SetUp() override
Method to set up the test class.
Definition TestingBase.hh:201
TestingBase()=default
Construct a new Testing Base object.
virtual void TestTeardown()
User's teardown method.
Definition TestingBase.hh:187
void SetupRoveComm()
Setup RoveComm for testing.
Definition TestingBase.hh:104
void InitializeLoggers(std::string szLoggingOutputPath, std::string szProgramTimeLogsDir)
Logger Initializer - Sets Up all the logging handlers required for having the above loggers.
Definition AutonomyLogging.cpp:57
std::string GetTimestamp(const std::string &szFormat="%Y%m%d-%H%M%S")
Accessor for getting the current time in a specified format.
Definition TimeOperations.hpp:42