Skip to content

Commit

Permalink
Implement first actual unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanLF6768 committed May 11, 2024
1 parent e697506 commit 9f41cb6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ set(CMAKE_C_STANDARD 23)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_SCAN_FOR_MODULES OFF CACHE INTERNAL "")

add_compile_options(-g3)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
Expand Down
3 changes: 3 additions & 0 deletions Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Start testing: May 11 23:20 AEST
----------------------------------------------------------
End testing: May 11 23:20 AEST
4 changes: 2 additions & 2 deletions common/Inc/obc/bus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class ListenBusMixin {
* @param msg The message to be forwarded.
*/
void FeedListeners(const M& msg) {
for (const auto& callback : m_listeners) callback(msg);
for (auto& callback : m_listeners) callback(msg);
}

private:
Expand Down Expand Up @@ -407,7 +407,7 @@ class ProcessBusMixin {
* @param req The request message to forward.
*/
void FeedProcessors(const Req& req) {
for (const auto& processor : m_processors)
for (auto& processor : m_processors)
if (auto res = processor(req)) Derived().IssueResponse(req, *res);
}

Expand Down
2 changes: 0 additions & 2 deletions common/Inc/obc/ipc/callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ class AsyncValue {
: std::optional<std::reference_wrapper<T>>(std::nullopt);
}

void Foo();

private:
std::optional<T> m_data {};
L m_lock {};
Expand Down
6 changes: 3 additions & 3 deletions common/Inc/obc/mock/bus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MockListenBus : public ListenBusMixin<M> {
*
* @param msg The message to be forwarded.
*/
void PushListenMessage(const M& msg) { FeedListeners(msg); }
void PushListenMessage(const M& msg) { this->FeedListeners(msg); }
};

/**
Expand All @@ -81,7 +81,7 @@ class MockRequestBus : public RequestBusMixin<
*
* @param res The response message to forward.
*/
void PushRequestResponse(const Res& res) { FeedRequesters(res); }
void PushRequestResponse(const Res& res) { this->FeedRequesters(res); }

MOCK_METHOD(Flt, IssueRequest, (const Req& req));
};
Expand All @@ -101,7 +101,7 @@ class MockProcessBus
*
* @param req The request message to forward.
*/
void PushProcessResponse(const Res& res) { FeedProcessors(res); }
void PushProcessResponse(const Res& res) { this->FeedProcessors(res); }

MOCK_METHOD(void, IssueResponse, (const Req& req, const Res& res));
};
Expand Down
17 changes: 10 additions & 7 deletions common/Inc/obc/utils/handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class Handle;
template<typename T, typename L = ipc::SpinLock>
class HandleChainRoot {
public:
class Iter;
friend class Iter;

/**
* @brief Initializes a handle chain with no elements.
*/
Expand Down Expand Up @@ -138,10 +135,15 @@ class HandleChainRoot {
* @param root Reference to the chain root.
*/
Iter(HandleChainRoot& root)
: m_lock(m_curr->m_next.lock), m_curr(root.m_next.ptr) {
// After the pointer to the first real node has safely been
// acquired, lock the pointer to the next node.
m_lock.swap(m_curr->m_next.lock);
: m_lock(root.m_next.lock), m_curr(root.m_next.ptr) {
if (m_curr) {
// After the pointer to the first real node has safely been
// acquired, lock the pointer to the next node.
std::unique_lock<L> next_lock(m_curr->m_next.lock);
m_lock.swap(next_lock);
} else {
m_lock.unlock();
}
}
};

Expand Down Expand Up @@ -198,6 +200,7 @@ class HandleChainRoot {
*/
template<typename T, typename L>
class Handle : private HandleChainRoot<T, L> {
friend HandleChainRoot<T, L>::Iter;
/*
* Inherits from roots as the root is *just* a node with no payload or
* previous element. This allows the pointer to the previous node to be
Expand Down
68 changes: 67 additions & 1 deletion common/Tests/mock/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,70 @@

#include <gtest/gtest.h>

TEST(MockBus, Listen) { ASSERT_EQ(1 + 1, 2); }
using namespace obc::bus::mock;

using obc::bus::BasicMessage;
using obc::ipc::AsyncValue;

using testing::ElementsAre;

std::byte operator""_b(unsigned long long val) {
return std::byte {static_cast<unsigned char>(val)};
}

TEST(MockBus, Listen) {
MockListenBus bus;

AsyncValue<BasicMessage> listener_0;
AsyncValue<BasicMessage> listener_1;
AsyncValue<BasicMessage> listener_2;
AsyncValue<BasicMessage> listener_3;

std::array<std::byte, 4> msg_buf {0x42_b, 0xB0_b, 0xF2_b, 0x41_b};

{
bus.PushListenMessage({.address = 0x00, .data = msg_buf});
auto h0 = bus.Listen(listener_0);
ASSERT_EQ(listener_1(), std::nullopt);
}

{
auto h0 = bus.Listen(listener_0);
bus.PushListenMessage({.address = 0x11, .data = msg_buf});

ASSERT_EQ(listener_0()->get().address, 0x11);
ASSERT_THAT(
listener_0()->get().data,
ElementsAre(0x42_b, 0xB0_b, 0xF2_b, 0x41_b)
);
}

{
auto h0 = bus.Listen(listener_1);
bus.Listen(listener_2); // Fail to save the handle
auto h3 = bus.Listen(listener_3);

std::array<std::byte, 4> msg_buf {0x13_b, 0x37_b, 0xBE_b, 0xEF_b};
bus.PushListenMessage({.address = 0xE4, .data = msg_buf});

ASSERT_EQ(listener_0()->get().address, 0x11);
ASSERT_THAT(
listener_0()->get().data,
ElementsAre(0x42_b, 0xB0_b, 0xF2_b, 0x41_b)
);

ASSERT_EQ(listener_1()->get().address, 0xE4);
ASSERT_THAT(
listener_1()->get().data,
ElementsAre(0x13_b, 0x37_b, 0xBE_b, 0xEF_b)
);

ASSERT_EQ(listener_2(), std::nullopt);

ASSERT_EQ(listener_1()->get().address, 0xE4);
ASSERT_THAT(
listener_1()->get().data,
ElementsAre(0x13_b, 0x37_b, 0xBE_b, 0xEF_b)
);
}
}

0 comments on commit 9f41cb6

Please sign in to comment.