/****************************************************************************** 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 #include #include #include #include #include "etl/hash.h" // for testing user-defined hash specializations namespace { class CustomType{}; } namespace etl { template <> struct hash { size_t operator()(CustomType) {return 0;} }; } namespace { SUITE(test_hash) { //************************************************************************* TEST(test_hash_bool) { size_t hash = etl::hash()(false); CHECK_EQUAL(0U, hash); hash = etl::hash()(true); CHECK_EQUAL(1U, hash); } //************************************************************************* TEST(test_hash_char) { size_t hash = etl::hash()(0x5A); CHECK_EQUAL(0x5AU, hash); } //************************************************************************* TEST(test_hash_signed_char) { size_t hash = etl::hash()(0x5A); CHECK_EQUAL(0x5AU, hash); } //************************************************************************* TEST(test_hash_unsigned_char) { size_t hash = etl::hash()(0x5AU); CHECK_EQUAL(0x5AU, hash); } //************************************************************************* TEST(test_hash_short) { size_t hash = etl::hash()(0x5AA5); CHECK_EQUAL(0x5AA5U, hash); } //************************************************************************* TEST(test_hash_unsigned_short) { size_t hash = etl::hash()(0x5AA5U); CHECK_EQUAL(0x5AA5U, hash); } //************************************************************************* TEST(test_hash_int) { size_t hash = etl::hash()(0x5AA555AA); CHECK_EQUAL(0x5AA555AAUL, hash); } //************************************************************************* TEST(test_hash_unsigned_int) { size_t hash = etl::hash()(0x5AA555AAU); CHECK_EQUAL(0x5AA555AAUL, hash); } //************************************************************************* TEST(test_hash_long) { size_t hash = etl::hash()(0x5AA555AAL); CHECK_EQUAL(0x5AA555AAUL, hash); } //************************************************************************* TEST(test_hash_unsigned_long) { size_t hash = etl::hash()(0x5AA555AAUL); CHECK_EQUAL(0x5AA555AAUL, hash); } //************************************************************************* TEST(test_hash_long_long) { size_t hash = etl::hash()(0x5AA555AA3CC333CCLL); if (ETL_PLATFORM_32BIT) { CHECK_EQUAL(0xEC6A8D69UL, hash); } if (ETL_PLATFORM_64BIT) { CHECK_EQUAL(0x5AA555AA3CC333CCULL, hash); } } //************************************************************************* TEST(test_hash_unsigned_long_long) { size_t hash = etl::hash()(0x5AA555AA3CC333CCULL); if (ETL_PLATFORM_32BIT) { CHECK_EQUAL(0xEC6A8D69UL, hash); } if (ETL_PLATFORM_64BIT) { CHECK_EQUAL(0x5AA555AA3CC333CCULL, hash); } } //************************************************************************* TEST(test_hash_float) { size_t hash = etl::hash()((float)(1.2345)); if (ETL_PLATFORM_32BIT) { CHECK_EQUAL(0X3F9E0419U, hash); } if (ETL_PLATFORM_64BIT) { CHECK_EQUAL(9821047038287739023U, hash); } } //************************************************************************* TEST(test_hash_double) { size_t hash = etl::hash()(1.2345); if (ETL_PLATFORM_32BIT) { CHECK_EQUAL(0x86FBF224UL, hash); } if (ETL_PLATFORM_64BIT) { CHECK_EQUAL(0x3FF3C083126E978DULL, hash); } } TEST(test_hash_floating_point_negative_zero) { if (sizeof(float) == sizeof(size_t)) { etl::hash 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 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 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()(&i); CHECK_EQUAL(size_t(&i), hash); } //************************************************************************* TEST(test_hash_const_pointer) { int i{}; size_t hash = etl::hash()(&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()(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::OneE); CHECK_EQUAL(static_cast(MyEnumClass::OneE), hash); hash = etl::hash()(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::Big); size_t expectedHash = etl::hash()(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 general_hasher; CHECK_FALSE(std::is_default_constructible::value); CHECK_FALSE(std::is_copy_constructible::value); CHECK_FALSE(std::is_move_constructible::value); CHECK_FALSE(std::is_copy_assignable::value); CHECK_FALSE(std::is_move_assignable::value); } //************************************************************************* TEST(test_hash_custom) { typedef etl::hash custom_hasher; CHECK_TRUE(std::is_default_constructible::value); CHECK_TRUE(std::is_copy_constructible::value); CHECK_TRUE(std::is_move_constructible::value); CHECK_TRUE(std::is_copy_assignable::value); CHECK_TRUE(std::is_move_assignable::value); } }; }