Added CRC1 (AKA Parity)

Modified test suit names
This commit is contained in:
John Wellbelove 2023-12-07 16:15:06 +00:00
parent 6a48eef934
commit 4b97e82876
15 changed files with 253 additions and 10 deletions

View File

@ -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"

105
include/etl/crc1.h Normal file
View File

@ -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<crc1_policy>
{
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<typename TIterator>
crc1(TIterator begin, const TIterator end)
{
this->reset();
this->add(begin, end);
}
};
}
#endif

128
test/test_crc1.cpp Normal file
View File

@ -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 <iterator>
#include <string>
#include <vector>
#include <stdint.h>
#include "etl/crc1.h"
namespace
{
//***************************************************************************
template <typename TIterator>
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<uint8_t> data1 = { 0x01U, 0x02U, 0x03U, 0x04U, 0x05U, 0x06U, 0x07U, 0x08U };
std::vector<uint32_t> data2 = { 0x04030201UL, 0x08070605UL };
std::vector<uint8_t> 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));
}
};
}

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_ccitt)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc_crc8_cdma2000)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_darc)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_dvbs2)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_ebu)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_icode)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_itu)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_maxim)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_rohc)
{
//*************************************************************************
// Table size 4

View File

@ -41,7 +41,7 @@ SOFTWARE.
namespace
{
SUITE(test_crc_experimental)
SUITE(test_crc8_wcdma)
{
//*************************************************************************
// Table size 4

View File

@ -2951,6 +2951,7 @@
<ClInclude Include="..\..\include\etl\correlation.h" />
<ClInclude Include="..\..\include\etl\covariance.h" />
<ClInclude Include="..\..\include\etl\crc.h" />
<ClInclude Include="..\..\include\etl\crc1.h" />
<ClInclude Include="..\..\include\etl\crc16.h" />
<ClInclude Include="..\..\include\etl\crc16_a.h" />
<ClInclude Include="..\..\include\etl\crc16_arc.h" />
@ -7309,6 +7310,7 @@
<ClCompile Include="..\test_circular_iterator.cpp" />
<ClCompile Include="..\test_correlation.cpp" />
<ClCompile Include="..\test_covariance.cpp" />
<ClCompile Include="..\test_crc1.cpp" />
<ClCompile Include="..\test_crc16.cpp" />
<ClCompile Include="..\test_crc16_a.cpp" />
<ClCompile Include="..\test_crc16_arc.cpp" />

View File

@ -1386,6 +1386,9 @@
<ClInclude Include="..\..\include\etl\to_u8string.h">
<Filter>ETL\Strings</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\crc1.h">
<Filter>ETL\Maths\CRC</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\test_string_char.cpp">
@ -3278,6 +3281,9 @@
<ClCompile Include="..\syntax_check\to_u8string.h.t.cpp">
<Filter>Tests\Syntax Checks\Source</Filter>
</ClCompile>
<ClCompile Include="..\test_crc1.cpp">
<Filter>Tests\CRC</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\library.properties">