From 746d0fad6f0969006d69c6dbf9607d55968ed5c5 Mon Sep 17 00:00:00 2001 From: Matt Way Date: Tue, 9 Apr 2024 00:31:46 +1000 Subject: [PATCH] Create initial tests Tests are run via a PlatformIO project --- platformio.ini | 23 +++++ test/test_crc/test_crc.cpp | 85 +++++++++++++++++++ .../test_packet_parser/test_packet_parser.cpp | 68 +++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 platformio.ini create mode 100644 test/test_crc/test_crc.cpp create mode 100644 test/test_packet_parser/test_packet_parser.cpp diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..d3c52cf --- /dev/null +++ b/platformio.ini @@ -0,0 +1,23 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:nodemcuv2] +platform = espressif8266 +board = nodemcuv2 +framework = arduino + +[env] +monitor_speed = 115200 +test_build_src = true +test_framework = unity +lib_deps = + robtillaart/CRC @ 1.0.2 + nrf24/RF24 @ 1.4.8 + rlogiacco/CircularBuffer @ 1.3.3 diff --git a/test/test_crc/test_crc.cpp b/test/test_crc/test_crc.cpp new file mode 100644 index 0000000..a530f10 --- /dev/null +++ b/test/test_crc/test_crc.cpp @@ -0,0 +1,85 @@ +#include +#include +#include "PacketCRC.h" + +void run_calculate_test(const uint8_t *packet_data, const uint16_t expected_checksum) +{ + PacketCRC packetCRC; + + uint16_t actual_checksum = packetCRC.calculate(packet_data); + + TEST_ASSERT_EQUAL_UINT16(expected_checksum, actual_checksum); +} + +void test_calculate_withDataLengthOf15() +{ + const uint8_t packet_data[] = {0xC0, 0x0F, 0x00, 0x05, 0xA1, 0xFF, 0xFF, 0x00, 0x00, 0x86, 0x04, 0xFF, 0x00, 0x00, 0x53, 0x47, 0x1B}; + const uint16_t expected_checksum = 0x446B; + + run_calculate_test(packet_data, expected_checksum); +} + +void test_calculate_withDataLengthOf17() +{ + const uint8_t packet_data[] = {0xC0, 0x11, 0x00, 0x05, 0xD7, 0xFF, 0xFF, 0x36, 0x9E, 0x86, 0x06, 0xB3, 0x04, 0x00, 0x36, 0x9E, 0x52, 0x53, 0x00}; + const uint16_t expected_checksum = 0xE04C; + + run_calculate_test(packet_data, expected_checksum); +} + +void test_calculate_withDataLengthOf19() +{ + const uint8_t packet_data[] = {0xC0, 0x13, 0x00, 0x05, 0xDD, 0xFF, 0xFF, 0x00, 0x00, 0x86, 0x05, 0x36, 0x4E, 0xF1, 0x00, 0x00, 0x3F, 0x5A, 0x02, 0x3F, 0x42}; + const uint16_t expected_checksum = 0x9BD3; + + run_calculate_test(packet_data, expected_checksum); +} + +void test_calculate_withDataLengthOf20() +{ + const uint8_t packet_data[] = {0xC0, 0x14, 0x10, 0x05, 0x59, 0xFF, 0xFF, 0x4E, 0xF1, 0x86, 0x05, 0xC2, 0x00, 0x00, 0x4E, 0xF1, 0x21, 0x5A, 0x03, 0x21, 0x42, 0x9C}; + const uint16_t expected_checksum = 0x4A8B; + + run_calculate_test(packet_data, expected_checksum); +} + +void test_calculate_withDataLengthOf21() +{ + const uint8_t packet_data[] = {0xC0, 0x15, 0x10, 0x05, 0xEA, 0xFF, 0xFF, 0x4E, 0xF1, 0x86, 0x05, 0x24, 0x00, 0x00, 0x4E, 0xF1, 0x21, 0x46, 0x4E, 0x98, 0x07, 0x08, 0x01}; + const uint16_t expected_checksum = 0x9887; + + run_calculate_test(packet_data, expected_checksum); +} + +void test_calculate_withDataLengthOf25() +{ + const uint8_t packet_data[] = {0xC0, 0x19, 0x00, 0x05, 0x47, 0xFF, 0xFF, 0x00, 0x00, 0x86, 0x05, 0x3D, 0x4E, 0xF1, 0x00, 0x00, 0x3F, 0x5A, 0x02, 0x3F, 0x50, 0x02, 0x3F, 0x4D, 0x02, 0x3F, 0x54}; + const uint16_t expected_checksum = 0x8318; + + run_calculate_test(packet_data, expected_checksum); +} + +int runUnityTests(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_calculate_withDataLengthOf15); + RUN_TEST(test_calculate_withDataLengthOf17); + RUN_TEST(test_calculate_withDataLengthOf19); + RUN_TEST(test_calculate_withDataLengthOf20); + RUN_TEST(test_calculate_withDataLengthOf21); + RUN_TEST(test_calculate_withDataLengthOf25); + return UNITY_END(); +} + +/** + * For Arduino framework + */ +void setup() +{ + // Wait ~2 seconds before the Unity test runner + // establishes connection with a board Serial interface + delay(2000); + + runUnityTests(); +} +void loop() {} \ No newline at end of file diff --git a/test/test_packet_parser/test_packet_parser.cpp b/test/test_packet_parser/test_packet_parser.cpp new file mode 100644 index 0000000..00b319e --- /dev/null +++ b/test/test_packet_parser/test_packet_parser.cpp @@ -0,0 +1,68 @@ +#include +#include +#include "PacketParser.h" + +void run_parse_test(const uint8_t *packet_data, Packet& packet) +{ + PacketParser packetParser; + + bool result = packetParser.parsePacket(packet_data, packet); + + TEST_ASSERT_TRUE(result); +} + +void test_parse_stop() +{ + const uint8_t packet_data[] = {0xC0, 0x11, 0x00, 0x05, 0x6C, 0xFF, 0xFF, 0x36, 0x9E, 0x86, 0x06, 0x3C, 0x04, 0x00, 0x36, 0x9E, 0x52, 0x53, 0x00}; + + Packet packet; + + run_parse_test(packet_data, packet); + + TEST_ASSERT_TRUE(packet.type == PacketType::STOP); +} + +void test_parse_close() +{ + const uint8_t packet_data[] = {0xC0, 0x11, 0x00, 0x05, 0x6C, 0xFF, 0xFF, 0x36, 0x9E, 0x86, 0x06, 0x3C, 0x04, 0x00, 0x36, 0x9E, 0x52, 0x44, 0x00}; + + Packet packet; + + run_parse_test(packet_data, packet); + + TEST_ASSERT_TRUE(packet.type == PacketType::CLOSE); +} + +void test_parse_open() +{ + const uint8_t packet_data[] = {0xC0, 0x11, 0x00, 0x05, 0x6C, 0xFF, 0xFF, 0x36, 0x9E, 0x86, 0x06, 0x3C, 0x04, 0x00, 0x36, 0x9E, 0x52, 0x55, 0x00}; + + Packet packet; + + run_parse_test(packet_data, packet); + + TEST_ASSERT_TRUE(packet.type == PacketType::OPEN); +} + +int runParseUnityTests(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_parse_stop); + RUN_TEST(test_parse_close); + RUN_TEST(test_parse_open); + return UNITY_END(); +} + +/** + * For Arduino framework + */ + +void setup() +{ + // Wait ~2 seconds before the Unity test runner + // establishes connection with a board Serial interface + delay(2000); + + runParseUnityTests(); +} +void loop() {}