diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-26 18:41:53 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-26 18:41:53 +0200 |
commit | 791249d73b3105d8602de5ffbdbb924bc47d179d (patch) | |
tree | e84f5a970a0e8e8b464b0e77e614452dbd9cc8ac | |
parent | ee5d161f6153e1f9da73e477f63f38dcf52ca10c (diff) |
fix up unit tests
-rw-r--r-- | test/CMakeLists.txt | 6 | ||||
-rw-r--r-- | test/pbdrv/mem.cpp | 11 | ||||
-rw-r--r-- | test/pbdrv/mod.c | 3 | ||||
-rw-r--r-- | test/pbdrv/msg.cpp | 55 | ||||
-rw-r--r-- | test/pbdrv/pb-route.c | 1 | ||||
-rw-r--r-- | test/pbdrv/send.cpp | 76 |
6 files changed, 92 insertions, 60 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 330f497..88ce667 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,10 +9,10 @@ set(CMAKE_BUILD_TYPE Debug) project(pbtest C CXX ASM) add_executable(test - # i2ctcp/main.cpp - pbdrv/pb-route.c - pbdrv/msg.cpp + i2ctcp/main.cpp pbdrv/mod.c + pbdrv/send.cpp + pbdrv/mem.cpp ) add_subdirectory(lib/googletest) diff --git a/test/pbdrv/mem.cpp b/test/pbdrv/mem.cpp new file mode 100644 index 0000000..6836852 --- /dev/null +++ b/test/pbdrv/mem.cpp @@ -0,0 +1,11 @@ +#include <gtest/gtest.h> + +#include "pb-mem.h" + +TEST(pb_mem, all) { + void * chunk = pb_malloc(64); + ASSERT_NE(chunk, nullptr); + + pb_free(chunk); +} + diff --git a/test/pbdrv/mod.c b/test/pbdrv/mod.c index e4c2505..81cd459 100644 --- a/test/pbdrv/mod.c +++ b/test/pbdrv/mod.c @@ -4,8 +4,9 @@ #include "pb-types.h" const char * PB_MOD_NAME = "test"; -const i2c_addr_t PB_MOD_ADDR = 0x08; +const i2c_addr_t PB_MOD_ADDR = 0x00; void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) { printf("[0x%02x]: buf[%lu]\n", addr & 0x7f, sz); } + diff --git a/test/pbdrv/msg.cpp b/test/pbdrv/msg.cpp deleted file mode 100644 index 3847b5f..0000000 --- a/test/pbdrv/msg.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include <gtest/gtest.h> - -#include "pb-mod.h" -#include "pb-msg.h" -#include "pb-send.h" - -TEST(pb_msg_rw, cmd_req_read) { - pb_cmd_prop_t cmd = { - .propid = 2, - }; - pb_msg_t msg_write = { - .type = PB_CMD_PROP, - .action = PB_ACTION_REQ, - .sender = 0xff, - .cmd = &cmd, - }; - pb_buf_t buf = pb_msg_write(&msg_write); - - ASSERT_NE(buf.data, nullptr); - ASSERT_GE(buf.size, 0); - - pb_msg_t * msg_read = pb_msg_read(&buf); - pb_buf_free(&buf); - - ASSERT_EQ(buf.data, nullptr); - - EXPECT_EQ(msg_write.type, msg_read->type); - EXPECT_EQ(msg_write.sender, msg_read->sender); - EXPECT_NE(nullptr, msg_read->cmd); - pb_cmd_prop_t * prop = (pb_cmd_prop_t *) msg_read->cmd; - EXPECT_EQ(prop->propid, cmd.propid); - - pb_msg_free(msg_read); -} - -TEST(pb_msg_rw, cmd_req_magic) { - pb_buf_t buf = pb_send_magic_req(); - - ASSERT_NE(buf.data, nullptr); - ASSERT_GE(buf.size, 0); - - pb_msg_t * msg_read = pb_msg_read(&buf); - pb_buf_free(&buf); - - ASSERT_EQ(buf.data, nullptr); - - EXPECT_EQ(msg_read->type, PB_CMD_MAGIC); - EXPECT_EQ(msg_read->sender, PB_MOD_ADDR); - EXPECT_NE(msg_read->cmd, nullptr); - pb_cmd_magic_t * magic = (pb_cmd_magic_t *) msg_read->cmd; - EXPECT_EQ(magic->_magic_size, sizeof(pb_cmd_magic_req)); - EXPECT_EQ(0, memcmp(pb_cmd_magic_req, magic->magic, magic->_magic_size)); - - pb_msg_free(msg_read); -} diff --git a/test/pbdrv/pb-route.c b/test/pbdrv/pb-route.c deleted file mode 100644 index 9e0fa30..0000000 --- a/test/pbdrv/pb-route.c +++ /dev/null @@ -1 +0,0 @@ -void pb_mod_blocking_delay_ms(unsigned long ms) {} diff --git a/test/pbdrv/send.cpp b/test/pbdrv/send.cpp new file mode 100644 index 0000000..7562ca4 --- /dev/null +++ b/test/pbdrv/send.cpp @@ -0,0 +1,76 @@ +#include <gtest/gtest.h> + +#include "pb-mod.h" +#include "pb-msg.h" +#include "pb-send.h" + +/** + * Only one of each command type is tested due to the (de)serializers being the + * same (i.e. you're not testing more by changing the command action). + */ + +TEST(pb_send_rw, cmd_state_req) { + pb_hook_mod_state_write(PB_GS_NOINIT); + + pb_buf_t buf = pb_send_state_req(); + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); + + pb_msg_t * msg = pb_msg_read(&buf); + EXPECT_EQ(msg->type, PB_CMD_STATE); + EXPECT_EQ(msg->action, PB_ACTION_REQ); + EXPECT_EQ(msg->sender, PB_MOD_ADDR); + EXPECT_NE(nullptr, msg->cmd); + + pb_cmd_state_t * cmd = (pb_cmd_state_t *) msg->cmd; + EXPECT_EQ(cmd->state, pb_hook_mod_state_read()); + + pb_buf_free(&buf); + ASSERT_EQ(buf.data, nullptr); + + pb_msg_free(msg); +} + +TEST(pb_send_rw, cmd_read_req) { + pb_buf_t buf = pb_send_read_req(2); + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); + + pb_msg_t * msg = pb_msg_read(&buf); + + EXPECT_EQ(msg->type, PB_CMD_PROP); + EXPECT_EQ(msg->action, PB_ACTION_REQ); + EXPECT_EQ(msg->sender, PB_MOD_ADDR); + EXPECT_NE(nullptr, msg->cmd); + + pb_cmd_prop_t * cmd = (pb_cmd_prop_t *) msg->cmd; + EXPECT_EQ(cmd->propid, 2); + + pb_buf_free(&buf); + ASSERT_EQ(buf.data, nullptr); + + pb_msg_free(msg); +} + +TEST(pb_send_rw, cmd_magic_req) { + pb_buf_t buf = pb_send_magic_req(); + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); + + pb_msg_t * msg = pb_msg_read(&buf); + + EXPECT_EQ(msg->type, PB_CMD_MAGIC); + EXPECT_EQ(msg->action, PB_ACTION_REQ); + EXPECT_EQ(msg->sender, PB_MOD_ADDR); + EXPECT_NE(msg->cmd, nullptr); + + pb_cmd_magic_t * cmd = (pb_cmd_magic_t *) msg->cmd; + EXPECT_EQ(cmd->_magic_size, sizeof(pb_cmd_magic_req)); + EXPECT_EQ(0, memcmp(pb_cmd_magic_req, cmd->magic, cmd->_magic_size)); + + pb_buf_free(&buf); + ASSERT_EQ(buf.data, nullptr); + + pb_msg_free(msg); +} + |