aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-04 18:27:40 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-04 18:27:40 +0200
commit078c6661c24a9460240d9c7c063a44e9bed6f96a (patch)
tree4d19d628210d5e0414228f1330c0d998b44c02e8 /test
parent18d06c79b9f6a625eb218a15c8216556fb99dc02 (diff)
add i2ctcp unit tests
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt27
-rw-r--r--test/ExampleTest.cpp5
-rw-r--r--test/i2ctcp/main.cpp46
-rw-r--r--test/makefile7
4 files changed, 72 insertions, 13 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index a280a86..44191cc 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,23 +1,26 @@
cmake_minimum_required(VERSION 3.29)
-project(puzzlebox_test C CXX ASM)
-
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
-add_executable(tests
- ExampleTest.cpp
-)
+project(pbtest C CXX ASM)
-enable_testing()
+include(../i2ctcp/include.cmake)
+include(../shared/include.cmake)
+
+add_executable(test
+ ExampleTest.cpp
+ i2ctcp/main.cpp
+ )
+# enable_testing()
add_subdirectory(lib/googletest)
-target_include_directories(tests PRIVATE ${CMAKE_CURRENT_LIST_DIR})
-target_link_libraries(tests PRIVATE gtest_main)
+# target_include_directories(tests PRIVATE ${CMAKE_CURRENT_LIST_DIR})
+target_link_libraries(test
+ gtest_main
+ i2ctcp
+ mpack
+ )
-add_test(
- NAME tests
- COMMAND tests
-)
diff --git a/test/ExampleTest.cpp b/test/ExampleTest.cpp
index a3909f5..3da58ff 100644
--- a/test/ExampleTest.cpp
+++ b/test/ExampleTest.cpp
@@ -4,4 +4,7 @@ class ExampleTest : public testing::Test {
protected:
};
-TEST_F(ExampleTest, Test) { EXPECT_EQ(5, 5); } \ No newline at end of file
+TEST_F(ExampleTest, Test) {
+ EXPECT_EQ(5, 5);
+}
+
diff --git a/test/i2ctcp/main.cpp b/test/i2ctcp/main.cpp
new file mode 100644
index 0000000..1f0c3ff
--- /dev/null
+++ b/test/i2ctcp/main.cpp
@@ -0,0 +1,46 @@
+#include <algorithm>
+#include <gtest/gtest.h>
+
+#include "i2ctcpv1.h"
+
+using std::min;
+
+const uint8_t data[] = { 0xff, 0x00, 0xde, 0xad, 0xbe, 0xef, };
+const size_t data_len = sizeof(data);
+const size_t chunk_size = 6;
+
+char * send_data = nullptr;
+size_t send_size = 0;
+
+TEST(i2ctcp, send) {
+ i2ctcp_msg_t send_msg = {
+ .addr = 0x1122,
+ .data = (char *) data,
+ .length = data_len,
+ };
+
+ ASSERT_TRUE(i2ctcp_write(&send_msg, &send_data, &send_size));
+ ASSERT_NE(send_data, nullptr);
+ ASSERT_GE(send_size, 0);
+}
+
+TEST(i2ctcp, recv) {
+ i2ctcp_msg_t recv_msg;
+ i2ctcp_read_reset(&recv_msg);
+ for (size_t i = 0; i < send_size; i += chunk_size) {
+ size_t expected_size = min(send_size, i + chunk_size) - i;
+
+ int parsed = i2ctcp_read(&recv_msg, send_data + i, expected_size);
+ EXPECT_GE(parsed, 0);
+
+ if (i + expected_size == send_size)
+ EXPECT_EQ(parsed, 0);
+ else
+ EXPECT_GT(parsed, 0);
+ }
+
+ ASSERT_NE(recv_msg.data, nullptr);
+ ASSERT_EQ(recv_msg.length, data_len);
+ ASSERT_EQ(0, memcmp(recv_msg.data, data, data_len));
+}
+
diff --git a/test/makefile b/test/makefile
new file mode 100644
index 0000000..7aeee34
--- /dev/null
+++ b/test/makefile
@@ -0,0 +1,7 @@
+TARGET = $(BUILD_DIR)/test
+
+include ../lazy.mk
+
+test: $(TARGET) FORCE
+ $(TARGET)
+