diff --git a/include/etl/crc.h b/include/etl/crc.h index da8dd0a43..eab6d7bc2 100644 --- a/include/etl/crc.h +++ b/include/etl/crc.h @@ -31,6 +31,8 @@ SOFTWARE. #ifndef ETL_CRC_INCLUDED #define ETL_CRC_INCLUDED +#include "crc1.h" + #include "crc8_ccitt.h" #include "crc8_cdma2000.h" #include "crc8_darc.h" diff --git a/include/etl/crc1.h b/include/etl/crc1.h new file mode 100644 index 000000000..2eaf923e8 --- /dev/null +++ b/include/etl/crc1.h @@ -0,0 +1,105 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2023 John Wellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_CRC1_INCLUDED +#define ETL_CRC1_INCLUDED + +#include "platform.h" +#include "frame_check_sequence.h" +#include "binary.h" + +namespace etl +{ + //*************************************************************************** + /// fnv_1 policy. + /// Calculates FNV1. + //*************************************************************************** + struct crc1_policy + { + typedef uint8_t value_type; + + enum + { + Odd_Parity = 1, + Even_Parity = 0 + }; + + //********************************* + value_type initial() const + { + return Even_Parity; + } + + //********************************* + uint8_t add(int parity, uint8_t value) const + { + return parity ^ etl::parity(value); + } + + //********************************* + uint8_t final(uint8_t parity) const + { + return parity; + } + }; + + class crc1 : public etl::frame_check_sequence + { + public: + + enum + { + Odd_Parity = crc1_policy::Odd_Parity, + Even_Parity = crc1_policy::Even_Parity + }; + + //************************************************************************* + /// Default constructor. + //************************************************************************* + crc1() + { + this->reset(); + } + + //************************************************************************* + /// Constructor from range. + /// \param begin Start of the range. + /// \param end End of the range. + //************************************************************************* + template + crc1(TIterator begin, const TIterator end) + { + this->reset(); + this->add(begin, end); + } + }; +} + +#endif diff --git a/test/test_crc1.cpp b/test/test_crc1.cpp new file mode 100644 index 000000000..3b87c9044 --- /dev/null +++ b/test/test_crc1.cpp @@ -0,0 +1,128 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2023 John Wellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include "unit_test_framework.h" + +#include +#include +#include +#include + +#include "etl/crc1.h" + +namespace +{ + //*************************************************************************** + template + int calculate_parity(TIterator b, TIterator e) + { + size_t count = 0; + + while (b != e) + { + count += etl::count_bits(*b); + ++b; + } + + return ((count &= 1) == 0) ? etl::crc1::Even_Parity : etl::crc1::Odd_Parity; + } + + SUITE(test_crc1) + { + //************************************************************************* + TEST(test_crc1_constructor) + { + std::string data("123456789"); + + uint8_t crc = etl::crc1(data.begin(), data.end()); + + CHECK_EQUAL(calculate_parity(data.begin(), data.end()), int(crc)); + } + + //************************************************************************* + TEST(test_crc1_add_values) + { + std::string data("123456789"); + + etl::crc1 crc_calculator; + + for (size_t i = 0UL; i < data.size(); ++i) + { + crc_calculator.add(data[i]); + } + + uint8_t crc = crc_calculator; + + CHECK_EQUAL(calculate_parity(data.begin(), data.end()), int(crc)); + } + + //************************************************************************* + TEST(test_crc1_add_range) + { + std::string data("123456789"); + + etl::crc1 crc_calculator; + + crc_calculator.add(data.begin(), data.end()); + + uint8_t crc = crc_calculator.value(); + + CHECK_EQUAL(calculate_parity(data.begin(), data.end()), int(crc)); + } + + //************************************************************************* + TEST(test_crc1_add_range_via_iterator) + { + std::string data("123456789"); + + etl::crc1 crc_calculator; + + std::copy(data.begin(), data.end(), crc_calculator.input()); + + uint8_t crc = crc_calculator.value(); + + CHECK_EQUAL(calculate_parity(data.begin(), data.end()), int(crc)); + } + + //************************************************************************* + TEST(test_crc1_add_range_endian) + { + std::vector data1 = { 0x01U, 0x02U, 0x03U, 0x04U, 0x05U, 0x06U, 0x07U, 0x08U }; + std::vector data2 = { 0x04030201UL, 0x08070605UL }; + std::vector data3 = { 0x08U, 0x07U, 0x06U, 0x05U, 0x04U, 0x03U, 0x02U, 0x01U }; + + uint8_t crc1 = etl::crc1(data1.begin(), data1.end()); + uint8_t crc2 = etl::crc1((uint8_t*)&data2[0], (uint8_t*)(&data2[0] + data2.size())); + CHECK_EQUAL(int(crc1), int(crc2)); + + uint8_t crc3 = etl::crc1(data3.rbegin(), data3.rend()); + CHECK_EQUAL(int(crc1), int(crc3)); + } + }; +} + diff --git a/test/test_crc8_ccitt.cpp b/test/test_crc8_ccitt.cpp index 9c042943a..bb531a681 100644 --- a/test/test_crc8_ccitt.cpp +++ b/test/test_crc8_ccitt.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_ccitt) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_cdma2000.cpp b/test/test_crc8_cdma2000.cpp index 629d17a72..bf43d73e5 100644 --- a/test/test_crc8_cdma2000.cpp +++ b/test/test_crc8_cdma2000.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc_crc8_cdma2000) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_darc.cpp b/test/test_crc8_darc.cpp index 0e97b8ab0..098ca4ce9 100644 --- a/test/test_crc8_darc.cpp +++ b/test/test_crc8_darc.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_darc) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_dvbs2.cpp b/test/test_crc8_dvbs2.cpp index 8f80413db..54667146e 100644 --- a/test/test_crc8_dvbs2.cpp +++ b/test/test_crc8_dvbs2.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_dvbs2) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_ebu.cpp b/test/test_crc8_ebu.cpp index 91ac9f578..0e378e934 100644 --- a/test/test_crc8_ebu.cpp +++ b/test/test_crc8_ebu.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_ebu) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_icode.cpp b/test/test_crc8_icode.cpp index e01225c12..32fd5468b 100644 --- a/test/test_crc8_icode.cpp +++ b/test/test_crc8_icode.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_icode) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_itu.cpp b/test/test_crc8_itu.cpp index aa9bbdd0d..6a14613fa 100644 --- a/test/test_crc8_itu.cpp +++ b/test/test_crc8_itu.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_itu) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_maxim.cpp b/test/test_crc8_maxim.cpp index 035c80682..8c68d85cf 100644 --- a/test/test_crc8_maxim.cpp +++ b/test/test_crc8_maxim.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_maxim) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_rohc.cpp b/test/test_crc8_rohc.cpp index a03b964d0..366474fce 100644 --- a/test/test_crc8_rohc.cpp +++ b/test/test_crc8_rohc.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_rohc) { //************************************************************************* // Table size 4 diff --git a/test/test_crc8_wcdma.cpp b/test/test_crc8_wcdma.cpp index 14faaafe8..7dfc5215d 100644 --- a/test/test_crc8_wcdma.cpp +++ b/test/test_crc8_wcdma.cpp @@ -41,7 +41,7 @@ SOFTWARE. namespace { - SUITE(test_crc_experimental) + SUITE(test_crc8_wcdma) { //************************************************************************* // Table size 4 diff --git a/test/vs2022/etl.vcxproj b/test/vs2022/etl.vcxproj index 50d71f988..4c18142c7 100644 --- a/test/vs2022/etl.vcxproj +++ b/test/vs2022/etl.vcxproj @@ -2951,6 +2951,7 @@ + @@ -7309,6 +7310,7 @@ + diff --git a/test/vs2022/etl.vcxproj.filters b/test/vs2022/etl.vcxproj.filters index d4dfc25b7..2adfa8622 100644 --- a/test/vs2022/etl.vcxproj.filters +++ b/test/vs2022/etl.vcxproj.filters @@ -1386,6 +1386,9 @@ ETL\Strings + + ETL\Maths\CRC + @@ -3278,6 +3281,9 @@ Tests\Syntax Checks\Source + + Tests\CRC +