diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index f72d58b1..55d5b650 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -38,8 +38,9 @@ jobs: coverage-data-path: ${{ env.COVERAGE_DATA_PATH }} # See: https://github.com/codecov/codecov-action/blob/master/README.md - - name: Upload coverage report to Codecov - uses: codecov/codecov-action@v1 + - name: Code coverage + uses: codecov/codecov-action@v3 with: - file: ${{ env.COVERAGE_DATA_PATH }} + token: ${{ secrets.CODECOV_TOKEN }} + files: ${{ env.COVERAGE_DATA_PATH }} fail_ci_if_error: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..93c700e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +.idea/ diff --git a/api/ArduinoAPI.h b/api/ArduinoAPI.h index 8464396c..c1ce5d2d 100644 --- a/api/ArduinoAPI.h +++ b/api/ArduinoAPI.h @@ -20,8 +20,8 @@ #ifndef ARDUINO_API_H #define ARDUINO_API_H -// version 1.4.1 -#define ARDUINO_API_VERSION 10401 +// version 1.4.2 +#define ARDUINO_API_VERSION 10402 #include "Binary.h" diff --git a/api/CanMsg.cpp b/api/CanMsg.cpp new file mode 100644 index 00000000..1137f066 --- /dev/null +++ b/api/CanMsg.cpp @@ -0,0 +1,34 @@ +/* + * This file is free software; you can redistribute it and/or modify + * it under the terms of either the GNU General Public License version 2 + * or the GNU Lesser General Public License version 2.1, both as + * published by the Free Software Foundation. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include "CanMsg.h" + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +namespace arduino +{ + +/************************************************************************************** + * STATIC CONST DEFINITION + **************************************************************************************/ + +uint8_t const CanMsg::MAX_DATA_LENGTH; +uint32_t const CanMsg::CAN_EFF_FLAG; +uint32_t const CanMsg::CAN_SFF_MASK; +uint32_t const CanMsg::CAN_EFF_MASK; + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +} /* arduino */ diff --git a/api/CanMsg.h b/api/CanMsg.h index 5b120344..dfece0d6 100644 --- a/api/CanMsg.h +++ b/api/CanMsg.h @@ -12,10 +12,12 @@ * INCLUDE **************************************************************************************/ -#include +#include #include -#include +#include "Print.h" +#include "Printable.h" +#include "Common.h" /************************************************************************************** * NAMESPACE @@ -31,14 +33,19 @@ namespace arduino class CanMsg : public Printable { public: - static size_t constexpr MAX_DATA_LENGTH = 8; + static uint8_t constexpr MAX_DATA_LENGTH = 8; + + static uint32_t constexpr CAN_EFF_FLAG = 0x80000000U; + static uint32_t constexpr CAN_SFF_MASK = 0x000007FFU; /* standard frame format (SFF) */ + static uint32_t constexpr CAN_EFF_MASK = 0x1FFFFFFFU; /* extended frame format (EFF) */ + CanMsg(uint32_t const can_id, uint8_t const can_data_len, uint8_t const * can_data_ptr) : id{can_id} - , data_length{can_data_len} + , data_length{min(can_data_len, MAX_DATA_LENGTH)} , data{0} { - memcpy(data, can_data_ptr, min(can_data_len, MAX_DATA_LENGTH)); + memcpy(data, can_data_ptr, data_length); } CanMsg() : CanMsg(0, 0, nullptr) { } @@ -52,14 +59,15 @@ class CanMsg : public Printable virtual ~CanMsg() { } - void operator = (CanMsg const & other) + CanMsg & operator = (CanMsg const & other) { - if (this == &other) - return; - - this->id = other.id; - this->data_length = other.data_length; - memcpy(this->data, other.data, this->data_length); + if (this != &other) + { + this->id = other.id; + this->data_length = other.data_length; + memcpy(this->data, other.data, this->data_length); + } + return (*this); } virtual size_t printTo(Print & p) const override @@ -68,7 +76,10 @@ class CanMsg : public Printable size_t len = 0; /* Print the header. */ - len = snprintf(buf, sizeof(buf), "[%08X] (%d) : ", id, data_length); + if (isStandardId()) + len = snprintf(buf, sizeof(buf), "[%03" PRIX32 "] (%d) : ", getStandardId(), data_length); + else + len = snprintf(buf, sizeof(buf), "[%08" PRIX32 "] (%d) : ", getExtendedId(), data_length); size_t n = p.write(buf, len); /* Print the data. */ @@ -82,11 +93,45 @@ class CanMsg : public Printable return n; } + + uint32_t getStandardId() const { + return (id & CAN_SFF_MASK); + } + uint32_t getExtendedId() const { + return (id & CAN_EFF_MASK); + } + bool isStandardId() const { + return ((id & CAN_EFF_FLAG) == 0); + } + bool isExtendedId() const { + return ((id & CAN_EFF_FLAG) == CAN_EFF_FLAG); + } + + + /* + * CAN ID semantics (mirroring definition by linux/can.h): + * |- Bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit) + * |- Bit 30 : reserved (future remote transmission request flag) + * |- Bit 29 : reserved (future error frame flag) + * |- Bit 0-28 : CAN identifier (11/29 bit) + */ uint32_t id; uint8_t data_length; uint8_t data[MAX_DATA_LENGTH]; }; +/************************************************************************************** + * FREE FUNCTIONS + **************************************************************************************/ + +inline uint32_t CanStandardId(uint32_t const id) { + return (id & CanMsg::CAN_SFF_MASK); +} + +inline uint32_t CanExtendedId(uint32_t const id) { + return (CanMsg::CAN_EFF_FLAG | (id & CanMsg::CAN_EFF_MASK)); +} + /************************************************************************************** * NAMESPACE **************************************************************************************/ diff --git a/api/Print.cpp b/api/Print.cpp index 8c3e1930..f1e82469 100644 --- a/api/Print.cpp +++ b/api/Print.cpp @@ -287,6 +287,12 @@ size_t Print::printULLNumber(unsigned long long n64, uint8_t base) uint8_t i = 0; uint8_t innerLoops = 0; + // Special case workaround https://github.com/arduino/ArduinoCore-API/issues/178 + if (n64 == 0) { + write('0'); + return 1; + } + // prevent crash if called with base == 1 if (base < 2) base = 10; diff --git a/api/String.cpp b/api/String.cpp index 1e4d8418..1b3e1047 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -664,9 +664,9 @@ void String::replace(const String& find, const String& replace) } } else if (diff < 0) { unsigned int size = len; // compute size needed for result + diff = 0 - diff; while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { readFrom = foundAt + find.len; - diff = 0 - diff; size -= diff; } if (size == len) return; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fedf7029..7d9e26ab 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,6 +25,14 @@ set(TEST_TARGET ${CMAKE_PROJECT_NAME}) ########################################################################## set(TEST_SRCS + src/CanMsg/test_CanMsg.cpp + src/CanMsg/test_CanMsg_CopyCtor.cpp + src/CanMsg/test_CanExtendedId.cpp + src/CanMsg/test_CanStandardId.cpp + src/CanMsg/test_isExtendedId.cpp + src/CanMsg/test_isStandardId.cpp + src/CanMsg/test_operator_assignment.cpp + src/CanMsg/test_printTo.cpp src/Common/test_makeWord.cpp src/Common/test_map.cpp src/Common/test_max.cpp @@ -97,6 +105,7 @@ set(TEST_SRCS ) set(TEST_DUT_SRCS + ../api/CanMsg.cpp ../api/Common.cpp ../api/IPAddress.cpp ../api/String.cpp diff --git a/test/src/CanMsg/test_CanExtendedId.cpp b/test/src/CanMsg/test_CanExtendedId.cpp new file mode 100644 index 00000000..72f8ece5 --- /dev/null +++ b/test/src/CanMsg/test_CanExtendedId.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +using namespace arduino; + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Verify correct conversion to 29-Bit CAN ID for lowest valid 29-Bit CAN ID", "[CanMsg-CanExtendedId-01]") +{ + REQUIRE(CanExtendedId(0) == (CanMsg::CAN_EFF_FLAG | 0U)); +} + +TEST_CASE ("Verify correct conversion to 29-Bit CAN ID for highest valid 29-Bit CAN ID", "[CanMsg-CanExtendedId-02]") +{ + REQUIRE(CanExtendedId(0x1FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x1FFFFFFFU)); +} + +TEST_CASE ("Verify capping of CAN IDs exceeding 29-Bit CAN ID range", "[CanMsg-CanExtendedId-03]") +{ + REQUIRE(CanExtendedId(0x2FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x0FFFFFFFU)); + REQUIRE(CanExtendedId(0x3FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x1FFFFFFFU)); + REQUIRE(CanExtendedId(0x4FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x0FFFFFFFU)); + REQUIRE(CanExtendedId(0x5FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x1FFFFFFFU)); + /* ... */ + REQUIRE(CanExtendedId(0xFFFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x1FFFFFFFU)); +} diff --git a/test/src/CanMsg/test_CanMsg.cpp b/test/src/CanMsg/test_CanMsg.cpp new file mode 100644 index 00000000..14b8a76a --- /dev/null +++ b/test/src/CanMsg/test_CanMsg.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +using namespace arduino; + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Test constructor with no data (data length = 0)", "[CanMsg-CanMsg-01]") +{ + CanMsg const msg(CanStandardId(0x20), 0, nullptr); + + REQUIRE(msg.data_length == 0); + for (size_t i = 0; i < CanMsg::MAX_DATA_LENGTH; i++) + REQUIRE(msg.data[i] == 0); +} + +TEST_CASE ("Test constructor with data (data length < CanMsg::MAX_DATA_LENGTH)", "[CanMsg-CanMsg-02]") +{ + uint8_t const msg_data[4] = {0xDE, 0xAD, 0xC0, 0xDE}; + + CanMsg const msg(CanStandardId(0x20), sizeof(msg_data), msg_data); + + REQUIRE(msg.data_length == 4); + for (size_t i = 0; i < msg.data_length; i++) + REQUIRE(msg.data[i] == msg_data[i]); +} + +TEST_CASE ("Test constructor with data (data length > CanMsg::MAX_DATA_LENGTH)", "[CanMsg-CanMsg-03]") +{ + uint8_t const msg_data[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + + CanMsg const msg(CanStandardId(0x20), sizeof(msg_data), msg_data); + + REQUIRE(msg.data_length == 8); + for (size_t i = 0; i < msg.data_length; i++) + REQUIRE(msg.data[i] == msg_data[i]); +} + +TEST_CASE ("Test constructor constructing a CAN frame with standard ID", "[CanMsg-CanMsg-04]") +{ + CanMsg const msg(CanStandardId(0x20), 0, nullptr); + + REQUIRE(msg.id == 0x20); +} + +TEST_CASE ("Test constructor constructing a CAN frame with extended ID", "[CanMsg-CanMsg-05]") +{ + CanMsg const msg(CanExtendedId(0x20), 0, nullptr); + + REQUIRE(msg.id == (CanMsg::CAN_EFF_FLAG | 0x20)); +} diff --git a/test/src/CanMsg/test_CanMsg_CopyCtor.cpp b/test/src/CanMsg/test_CanMsg_CopyCtor.cpp new file mode 100644 index 00000000..b303369a --- /dev/null +++ b/test/src/CanMsg/test_CanMsg_CopyCtor.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +using namespace arduino; + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Test copy constructor", "[CanMsg-CopyCtor-01]") +{ + uint8_t const msg_data[4] = {0xDE, 0xAD, 0xC0, 0xDE}; + + CanMsg const msg_1(CanStandardId(0x20), sizeof(msg_data), msg_data); + CanMsg const msg_2(msg_1); + + REQUIRE(msg_1.data_length == msg_2.data_length); + + for (size_t i = 0; i < msg_1.data_length; i++) + { + REQUIRE(msg_1.data[i] == msg_data[i]); + REQUIRE(msg_2.data[i] == msg_data[i]); + } +} diff --git a/test/src/CanMsg/test_CanStandardId.cpp b/test/src/CanMsg/test_CanStandardId.cpp new file mode 100644 index 00000000..02496241 --- /dev/null +++ b/test/src/CanMsg/test_CanStandardId.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +using namespace arduino; + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Verify correct conversion to 11-Bit CAN ID for lowest valid 11-Bit CAN ID", "[CanMsg-CanStandardId-01]") +{ + REQUIRE(CanStandardId(0) == 0U); +} + +TEST_CASE ("Verify correct conversion to 11-Bit CAN ID for highest valid 11-Bit CAN ID", "[CanMsg-CanStandardId-02]") +{ + REQUIRE(CanStandardId(0x7FF) == 0x7FF); +} + +TEST_CASE ("Verify capping of CAN IDs exceeding 11-Bit CAN ID range", "[CanMsg-CanStandardId-03]") +{ + REQUIRE(CanStandardId(0x800U) == 0x000U); + REQUIRE(CanStandardId(0x801U) == 0x001U); + REQUIRE(CanStandardId(0x8FFU) == 0x0FFU); + REQUIRE(CanStandardId(0x1FFFFFFFU) == 0x7FFU); + REQUIRE(CanStandardId(0xFFFFFFFFU) == 0x7FFU); +} diff --git a/test/src/CanMsg/test_isExtendedId.cpp b/test/src/CanMsg/test_isExtendedId.cpp new file mode 100644 index 00000000..3a12ab8f --- /dev/null +++ b/test/src/CanMsg/test_isExtendedId.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +using namespace arduino; + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("\"isExtendedId\" should return true for extended CAN ID", "[CanMsg-isExtendedId-01]") +{ + CanMsg const msg_ext_id(CanExtendedId(0x020), 0, nullptr); + REQUIRE(msg_ext_id.isExtendedId() == true); +} + +TEST_CASE ("\"isExtendedId\" should return false for standard CAN ID", "[CanMsg-isExtendedId-02]") +{ + CanMsg const msg_std_id(CanStandardId(0x020), 0, nullptr); + REQUIRE(msg_std_id.isExtendedId() == false); +} diff --git a/test/src/CanMsg/test_isStandardId.cpp b/test/src/CanMsg/test_isStandardId.cpp new file mode 100644 index 00000000..718d2535 --- /dev/null +++ b/test/src/CanMsg/test_isStandardId.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +using namespace arduino; + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("\"isStandardId\" should return true for standard CAN ID", "[CanMsg-isStandardId-01]") +{ + CanMsg const msg_std_id(CanStandardId(0x020), 0, nullptr); + REQUIRE(msg_std_id.isStandardId() == true); +} + +TEST_CASE ("\"isStandardId\" should return false for extended CAN ID", "[CanMsg-isStandardId-02]") +{ + CanMsg const msg_ext_id(CanExtendedId(0x020), 0, nullptr); + REQUIRE(msg_ext_id.isStandardId() == false); +} diff --git a/test/src/CanMsg/test_operator_assignment.cpp b/test/src/CanMsg/test_operator_assignment.cpp new file mode 100644 index 00000000..ae2d2ecb --- /dev/null +++ b/test/src/CanMsg/test_operator_assignment.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +using namespace arduino; + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Testing CanMsg::operator = (CanMsg const &)", "[CanMsg-Operator-=-1]") +{ + uint8_t const msg_data[4] = {0xDE, 0xAD, 0xC0, 0xDE}; + + CanMsg const msg_1(CanStandardId(0x20), sizeof(msg_data), msg_data); + CanMsg msg_2(CanStandardId(0x21), 0, nullptr); + + msg_2 = msg_1; + + REQUIRE(msg_1.data_length == msg_2.data_length); + + for (size_t i = 0; i < msg_1.data_length; i++) + { + REQUIRE(msg_1.data[i] == msg_data[i]); + REQUIRE(msg_2.data[i] == msg_data[i]); + } +} diff --git a/test/src/CanMsg/test_printTo.cpp b/test/src/CanMsg/test_printTo.cpp new file mode 100644 index 00000000..dd1fb4b8 --- /dev/null +++ b/test/src/CanMsg/test_printTo.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + */ + +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include +#include + +/************************************************************************************** + * NAMESPACE + **************************************************************************************/ + +using namespace arduino; + +/************************************************************************************** + * TEST CODE + **************************************************************************************/ + +TEST_CASE ("Print CAN frame with standard ID", "[CanMsg-printTo-1]") +{ + uint8_t const std_msg_data[] = {0xBE, 0xEF}; + CanMsg const std_msg(CanStandardId(0x20), sizeof(std_msg_data), std_msg_data); + + PrintMock mock; + mock.print(std_msg); + + REQUIRE(mock._str == "[020] (2) : BEEF"); +} + +TEST_CASE ("Print CAN frame with extended ID", "[CanMsg-printTo-2]") +{ + uint8_t const ext_msg_data[] = {0xDE, 0xAD, 0xC0, 0xDE}; + CanMsg const ext_msg(CanExtendedId(0x20), sizeof(ext_msg_data), ext_msg_data); + + PrintMock mock; + mock.print(ext_msg); + + REQUIRE(mock._str == "[00000020] (4) : DEADC0DE"); +} diff --git a/test/src/Print/test_print.cpp b/test/src/Print/test_print.cpp index 92e9c083..ac731a89 100644 --- a/test/src/Print/test_print.cpp +++ b/test/src/Print/test_print.cpp @@ -110,12 +110,24 @@ TEST_CASE ("Print::print(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-pr { PrintMock mock; - unsigned long long const val = 17; + GIVEN("a value of zero ...") + { + unsigned long long const val = 0; - WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); } - WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); } - WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); } - WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); } + WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "0"); } + WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "0"); } + WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "0"); } + WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "0"); } + } + GIVEN("a non-zero value ...") + { + unsigned long long const val = 17; + + WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); } + WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); } + WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); } + WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); } + } } TEST_CASE ("Print::print(double, int = 2)", "[Print-print-10]") diff --git a/test/src/String/test_replace.cpp b/test/src/String/test_replace.cpp index 5d46fafb..d4f28bf5 100644 --- a/test/src/String/test_replace.cpp +++ b/test/src/String/test_replace.cpp @@ -66,3 +66,24 @@ TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than ' str.replace(arduino::String("ll"), arduino::String("111")); REQUIRE(str == "He111o Arduino!"); } + +TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than 'replace' multiple occurencies", "[String-replace-08]") +{ + arduino::String str("Hello Arduino! Hello, Hello, Hello"); + str.replace(arduino::String("ll"), arduino::String("lll")); + REQUIRE(str == "Helllo Arduino! Helllo, Helllo, Helllo"); +} + +TEST_CASE ("Testing String::replace(String, String) substr 'find' same length as 'replace' multiple occurencies", "[String-replace-09]") +{ + arduino::String str("Hello Arduino! Hello, Hello, Hello"); + str.replace(arduino::String("ll"), arduino::String("11")); + REQUIRE(str == "He11o Arduino! He11o, He11o, He11o"); +} + +TEST_CASE ("Testing String::replace(String, String) substr 'find' larger than 'replace' multiple occurencies", "[String-replace-10]") +{ + arduino::String str("Helllo Arduino! Helllo, Helllo, Helllo"); + str.replace(arduino::String("lll"), arduino::String("ll")); + REQUIRE(str == "Hello Arduino! Hello, Hello, Hello"); +}