mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-21 03:16:36 +08:00
Added shift version of XOR checksum
XOR: sum = sum ^ value XOR-shift sum = etl::rotate_left(sum) ^ value
This commit is contained in:
parent
1b61672eb3
commit
4cb02cb6df
@ -110,6 +110,30 @@ namespace etl
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// XOR-shift checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_xor_shift
|
||||
{
|
||||
typedef T value_type;
|
||||
|
||||
inline T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline T add(T sum, uint8_t value) const
|
||||
{
|
||||
return etl::rotate_left(sum) ^ value;
|
||||
}
|
||||
|
||||
inline T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Standard Checksum.
|
||||
//*************************************************************************
|
||||
@ -196,6 +220,35 @@ namespace etl
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// XOR-shift Checksum.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
class xor_shift_checksum : public etl::frame_check_sequence<etl::checksum_policy_xor_shift<T> >
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
xor_shift_checksum()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
xor_shift_checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
149
test/test_xor_shift_checksum.cpp
Normal file
149
test/test_xor_shift_checksum.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 jwellbelove
|
||||
|
||||
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 "UnitTest++.h"
|
||||
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "checksum.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename TSum, typename TIterator>
|
||||
TSum reference_checksum(TIterator begin, TIterator end)
|
||||
{
|
||||
typedef typename std::iterator_traits<TIterator>::value_type value_type;
|
||||
TSum checksum = 0;
|
||||
|
||||
while (begin != end)
|
||||
{
|
||||
value_type value = *begin++;
|
||||
checksum = etl::rotate_left(checksum) ^ value;
|
||||
}
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
SUITE(test_checksum)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_constructor)
|
||||
{
|
||||
std::string data("123456789");
|
||||
|
||||
uint8_t sum = etl::xor_shift_checksum<uint8_t>(data.begin(), data.end());
|
||||
uint8_t compare = reference_checksum<uint8_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(int(compare), int(sum));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_values8_add)
|
||||
{
|
||||
std::string data("123456789");
|
||||
|
||||
etl::xor_shift_checksum<uint8_t> checksum_calculator;
|
||||
|
||||
for (size_t i = 0; i < data.size(); ++i)
|
||||
{
|
||||
checksum_calculator.add(data[i]);
|
||||
}
|
||||
|
||||
uint8_t sum = checksum_calculator;
|
||||
uint8_t compare = reference_checksum<uint8_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(int(compare), int(sum));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_values8_operator_plus_equals)
|
||||
{
|
||||
std::string data("123456789");
|
||||
|
||||
etl::xor_shift_checksum<uint8_t> checksum_calculator;
|
||||
|
||||
for (size_t i = 0; i < data.size(); ++i)
|
||||
{
|
||||
checksum_calculator.add(data[i]);
|
||||
}
|
||||
|
||||
uint8_t sum = checksum_calculator;
|
||||
uint8_t compare = reference_checksum<uint8_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(int(compare), int(sum));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_range)
|
||||
{
|
||||
std::string data("123456789");
|
||||
|
||||
etl::xor_shift_checksum<uint8_t> checksum_calculator;
|
||||
|
||||
checksum_calculator.add(data.begin(), data.end());
|
||||
|
||||
uint8_t sum = checksum_calculator.value();
|
||||
uint8_t compare = reference_checksum<uint8_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(int(compare), int(sum));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_range_sum32)
|
||||
{
|
||||
std::string data("1");
|
||||
|
||||
etl::xor_shift_checksum<uint32_t> checksum_calculator;
|
||||
|
||||
checksum_calculator.add(data.begin(), data.end());
|
||||
|
||||
uint32_t sum = checksum_calculator.value();
|
||||
uint32_t compare = reference_checksum<uint32_t>(data.begin(), data.end());
|
||||
|
||||
CHECK_EQUAL(compare, sum);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_checksum_add_range_endian)
|
||||
{
|
||||
std::vector<uint8_t> data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
|
||||
std::vector<uint32_t> data2 = { 0x04030201, 0x08070605 };
|
||||
std::vector<uint8_t> data3 = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
|
||||
|
||||
uint64_t hash1 = etl::xor_shift_checksum<uint8_t>(data1.begin(), data1.end());
|
||||
uint64_t hash2 = etl::xor_shift_checksum<uint8_t>((uint8_t*)&data2[0], (uint8_t*)&data2[0] + (data2.size() * sizeof(uint32_t)));
|
||||
uint64_t hash3 = etl::xor_shift_checksum<uint8_t>(data3.rbegin(), data3.rend());
|
||||
CHECK_EQUAL(hash1, hash2);
|
||||
CHECK_EQUAL(hash1, hash3);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -467,6 +467,7 @@
|
||||
<ClCompile Include="..\test_vector_pointer.cpp" />
|
||||
<ClCompile Include="..\test_visitor.cpp" />
|
||||
<ClCompile Include="..\test_xor_checksum.cpp" />
|
||||
<ClCompile Include="..\test_xor_shift_checksum.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\Doxyfile" />
|
||||
|
||||
@ -953,6 +953,9 @@
|
||||
<ClCompile Include="..\test_array_wrapper.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\test_xor_shift_checksum.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\Doxyfile">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user