etl/test/test_hash.cpp
Roland Reichwein 866c8a315e
Extensions for testing (#1380)
* Extensions for testing

Generalize run-tests.sh

Test all C++ versions at once

Fix combination of big endian and -Wsign-conversion

Failed on s390x (as reference for big endian)

Add github workflow for s390x

Add armhf container files

Devcontainers for i386 and riscv

Add github workflows for armhf, i386 and riscv64

Add run-tests.sh for foreign architectures

Document testing in doc/testing.md

Adjustments from clang-format run

Fix .devcontainer/s390x/Dockerfile for linebreak syntax

Fix exit code of run-test.sh

Previously, "exit $?" was used, actually the return value of
FailedCompilation and FailedTest which are always 0.

Now just using 1.

In run-tests.sh at ctest, use -V for printing number of tests unconditionally

While ctest suppresses individual test list by default, it didn't even
print the number of tests anymore, as run_tests.sh does because
it suppresses it output completely.

Now, by default print number of tests, and in verbose mode, print test list
in addition.

* Support powerpc as foreign architecture

* Add SFINAE constraints to etl::begin/end and reverse iterator free functions

The unconstrained etl::begin(), etl::end(), etl::cbegin(), etl::cend(),
etl::rbegin(), etl::rend(), etl::crbegin(), and etl::crend() templates
in the no-STL code path were matching iterator types during ADL, causing
a hard error with GCC 15's std::ranges::begin. When std::ranges performed
ADL on an etl::*::iterator, it found etl::begin() as a candidate; since
the iterator type has a nested iterator typedef, the return type TContainer::iterator
was valid, but calling .begin() on the iterator failed.

Fix: add etl::void_t<decltype(...)> SFINAE guards to each template,
ensuring they only participate in overload resolution when TContainer
actually has the corresponding member function (.begin(), .end(), etc.).

* - Fix red unit tests on 32 bits big-endian platform.

* Document powerpc architecture for testing

* Use Dockerfiles in cross testing github workflows

Synchronizes environment setup for github workflows to what is
defined in the development Dockerfiles. So they don't need to
be maintained separately.

---------

Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: Sergei Shirokov <sergej.shirokov@gmail.com>
Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
2026-04-15 10:47:52 +02:00

357 lines
9.5 KiB
C++

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2014 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 <stdint.h>
#include <string>
#include <type_traits>
#include <vector>
#include "etl/endianness.h"
#include "etl/hash.h"
// for testing user-defined hash specializations
namespace
{
class CustomType
{
};
} // namespace
namespace etl
{
template <>
struct hash<CustomType>
{
size_t operator()(CustomType)
{
return 0;
}
};
} // namespace etl
namespace
{
SUITE(test_hash)
{
//*************************************************************************
TEST(test_hash_bool)
{
size_t hash = etl::hash<bool>()(false);
CHECK_EQUAL(0U, hash);
hash = etl::hash<bool>()(true);
CHECK_EQUAL(1U, hash);
}
//*************************************************************************
TEST(test_hash_char)
{
size_t hash = etl::hash<char>()(0x5A);
CHECK_EQUAL(0x5AU, hash);
}
//*************************************************************************
TEST(test_hash_signed_char)
{
size_t hash = etl::hash<signed char>()(0x5A);
CHECK_EQUAL(0x5AU, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_char)
{
size_t hash = etl::hash<unsigned char>()(0x5AU);
CHECK_EQUAL(0x5AU, hash);
}
//*************************************************************************
TEST(test_hash_short)
{
size_t hash = etl::hash<short>()(0x5AA5);
CHECK_EQUAL(0x5AA5U, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_short)
{
size_t hash = etl::hash<unsigned short>()(0x5AA5U);
CHECK_EQUAL(0x5AA5U, hash);
}
//*************************************************************************
TEST(test_hash_int)
{
size_t hash = etl::hash<int>()(0x5AA555AA);
CHECK_EQUAL(0x5AA555AAUL, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_int)
{
size_t hash = etl::hash<unsigned int>()(0x5AA555AAU);
CHECK_EQUAL(0x5AA555AAUL, hash);
}
//*************************************************************************
TEST(test_hash_long)
{
size_t hash = etl::hash<long>()(0x5AA555AAL);
CHECK_EQUAL(0x5AA555AAUL, hash);
}
//*************************************************************************
TEST(test_hash_unsigned_long)
{
size_t hash = etl::hash<unsigned long>()(0x5AA555AAUL);
CHECK_EQUAL(0x5AA555AAUL, hash);
}
//*************************************************************************
TEST(test_hash_long_long)
{
size_t hash = etl::hash<long long>()(0x5AA555AA3CC333CCLL);
if (ETL_PLATFORM_32BIT)
{
if (etl::endianness::value() == etl::endian::little)
{
CHECK_EQUAL(0xEC6A8D69UL, hash);
}
else
{
CHECK_EQUAL(0x5F69FD19UL, hash);
}
}
if (ETL_PLATFORM_64BIT)
{
CHECK_EQUAL(0x5AA555AA3CC333CCULL, hash);
}
}
//*************************************************************************
TEST(test_hash_unsigned_long_long)
{
size_t hash = etl::hash<unsigned long long>()(0x5AA555AA3CC333CCULL);
if (ETL_PLATFORM_32BIT)
{
if (etl::endianness::value() == etl::endian::little)
{
CHECK_EQUAL(0xEC6A8D69UL, hash);
}
else
{
CHECK_EQUAL(0x5F69FD19UL, hash);
}
}
if (ETL_PLATFORM_64BIT)
{
CHECK_EQUAL(0x5AA555AA3CC333CCULL, hash);
}
}
//*************************************************************************
TEST(test_hash_float)
{
size_t hash = etl::hash<float>()(1.2345f);
if (ETL_PLATFORM_32BIT)
{
CHECK_EQUAL(0X3F9E0419U, hash);
}
if (ETL_PLATFORM_64BIT)
{
if (etl::endianness::value() == etl::endian::little)
{
CHECK_EQUAL(9821047038287739023U, hash);
}
else
{
CHECK_EQUAL(15239628249615675935U, hash);
}
}
}
//*************************************************************************
TEST(test_hash_double)
{
size_t hash = etl::hash<double>()(1.2345);
if (ETL_PLATFORM_32BIT)
{
if (etl::endianness::value() == etl::endian::little)
{
CHECK_EQUAL(0x86FBF224UL, hash);
}
else
{
CHECK_EQUAL(0xD1F372DEUL, hash);
}
}
if (ETL_PLATFORM_64BIT)
{
CHECK_EQUAL(0x3FF3C083126E978DULL, hash);
}
}
TEST(test_hash_floating_point_negative_zero)
{
if (sizeof(float) == sizeof(size_t))
{
etl::hash<float> hasher{};
size_t hash1 = hasher(0.0);
size_t hash2 = hasher(-0.0);
CHECK_EQUAL(hash1, hash2);
}
if (sizeof(double) == sizeof(size_t))
{
etl::hash<double> hasher{};
size_t hash1 = hasher(0.0);
size_t hash2 = hasher(-0.0);
CHECK_EQUAL(hash1, hash2);
}
if (sizeof(long double) == sizeof(size_t))
{
etl::hash<long double> hasher{};
size_t hash1 = hasher(0.0);
size_t hash2 = hasher(-0.0);
CHECK_EQUAL(hash1, hash2);
}
}
//*************************************************************************
TEST(test_hash_pointer)
{
int i{};
size_t hash = etl::hash<int*>()(&i);
CHECK_EQUAL(size_t(&i), hash);
}
//*************************************************************************
TEST(test_hash_const_pointer)
{
int i{};
size_t hash = etl::hash<const int*>()(&i);
CHECK_EQUAL(size_t(&i), hash);
}
//*************************************************************************
TEST(test_hash_const_pointer_const)
{
int i{};
const int* const pi = &i;
size_t hash = etl::hash<const int*>()(pi);
CHECK_EQUAL(size_t(&i), hash);
}
//*************************************************************************
TEST(test_hash_enums)
{
enum class MyEnumClass : char
{
OneE = 0x1E
};
enum MyEnum : char
{
MyEnum_TwoF = 0x2F
};
size_t hash = etl::hash<MyEnumClass>()(MyEnumClass::OneE);
CHECK_EQUAL(static_cast<size_t>(MyEnumClass::OneE), hash);
hash = etl::hash<MyEnum>()(MyEnum_TwoF);
CHECK_EQUAL(0x2F, hash);
}
//*************************************************************************
TEST(test_hash_big_enums)
{
constexpr unsigned long long big_number = 0x5AA555AA3CC333CCULL;
enum class MyBigEnumClass : unsigned long long
{
Big = big_number
};
size_t hash = etl::hash<MyBigEnumClass>()(MyBigEnumClass::Big);
size_t expectedHash = etl::hash<unsigned long long>()(big_number);
CHECK_EQUAL(expectedHash, hash);
}
//*************************************************************************
TEST(test_hash_poisoned)
{
// Unspecialized hash<> should be disabled (unusable) - see
// https://en.cppreference.com/w/cpp/utility/hash
class A
{
};
typedef etl::hash<A> general_hasher;
CHECK_FALSE(std::is_default_constructible<general_hasher>::value);
CHECK_FALSE(std::is_copy_constructible<general_hasher>::value);
CHECK_FALSE(std::is_move_constructible<general_hasher>::value);
CHECK_FALSE(std::is_copy_assignable<general_hasher>::value);
CHECK_FALSE(std::is_move_assignable<general_hasher>::value);
}
//*************************************************************************
TEST(test_hash_custom)
{
typedef etl::hash<CustomType> custom_hasher;
CHECK_TRUE(std::is_default_constructible<custom_hasher>::value);
CHECK_TRUE(std::is_copy_constructible<custom_hasher>::value);
CHECK_TRUE(std::is_move_constructible<custom_hasher>::value);
CHECK_TRUE(std::is_copy_assignable<custom_hasher>::value);
CHECK_TRUE(std::is_move_assignable<custom_hasher>::value);
}
}
} // namespace