Skip to content

Commit 57a4728

Browse files
committed
added IgnoreCase tests for constexpr_hash
1 parent f12e788 commit 57a4728

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/constexpr_hash.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
#include <catch.hpp>
22
#include <ut/constexpr_hash/constexpr_hash.hpp>
33

4+
#include <algorithm>
45
#include <array>
6+
#include <cctype>
7+
#include <iterator>
58
#include <string>
69

710
constexpr auto empty = "";
811
constexpr auto short_str = "hello";
912
constexpr auto long_str =
1013
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce pellentesque justo eu mauris gravida aliquet. Nam et velit tortor. Vestibulum sit amet commodo ante. Fusce eleifend tellus ac euismod porta. Proin at gravida tortor. Suspendisse eget urna vitae dui varius auctor et ac quam. Phasellus sodales sodales dolor, vel aliquet erat posuere hendrerit. Donec consectetur orci eget pulvinar pellentesque. Donec in bibendum orci. Phasellus congue, ligula non tincidunt malesuada, nisl ligula mauris. ";
14+
constexpr auto ascii =
15+
"\a\b\t\n\v\f\r\"\\@!#$%&'()*+,-./0123456789:;<=>?[]^_ `abcdefghijklmnopqrstuvwxyz{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZ";
16+
constexpr auto ascii_lower =
17+
"\a\b\t\n\v\f\r\"\\@!#$%&'()*+,-./0123456789:;<=>?[]^_ `abcdefghijklmnopqrstuvwxyz{|}~abcdefghijklmnopqrstuvwxyz";
1118

1219
constexpr auto len(char const *str) {
1320
return std::char_traits<char>::length(str);
@@ -80,3 +87,36 @@ TEST_CASE("Runtime hashing", "[constexpr_hash]") {
8087
REQUIRE(str3_hash != str_cpy_1_hash);
8188
REQUIRE(str3_hash != str_cpy_2_hash);
8289
}
90+
91+
TEST_CASE("Compile time ignore case", "[constexpr_hash]") {
92+
93+
static constexpr auto ascii_2 = makeCopy<len(ascii)>(ascii);
94+
static constexpr auto ascii_lower_2 = makeCopy<len(ascii_lower)>(ascii_lower);
95+
96+
STATIC_REQUIRE(ut::fnv_1a<std::size_t>(ascii) == ut::fnv_1a<std::size_t>(ascii_2.data()));
97+
STATIC_REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(ascii)
98+
== ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(ascii_2.data()));
99+
STATIC_REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(ascii)
100+
== ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(ascii_2.data()));
101+
STATIC_REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(ascii)
102+
!= ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(ascii_2.data()));
103+
104+
STATIC_REQUIRE(ut::fnv_1a<std::size_t>(ascii) != ut::fnv_1a<std::size_t>(ascii_lower_2.data()));
105+
STATIC_REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(ascii)
106+
!= ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(ascii_lower_2.data()));
107+
STATIC_REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(ascii)
108+
== ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(ascii_lower_2.data()));
109+
STATIC_REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(ascii)
110+
== ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(ascii_lower_2.data()));
111+
}
112+
113+
TEST_CASE("Runtime ignore case", "[constexpr_hash]") {
114+
std::string str1, str2;
115+
std::generate_n(std::back_inserter(str1), 20, []() { return std::rand() % (128 - '!') + '!'; });
116+
std::transform(str1.begin(), str1.end(), std::back_inserter(str2), [](char ch) { return std::tolower(ch); });
117+
118+
REQUIRE(ut::fnv_1a<std::size_t>(str1) != ut::fnv_1a<std::size_t>(str2));
119+
REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(str1) != ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(str2));
120+
REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(str1) == ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(str2));
121+
REQUIRE(ut::fnv_1a<std::size_t, ut::CHIgnoreCase::Yes>(str1) == ut::fnv_1a<std::size_t, ut::CHIgnoreCase::No>(str2));
122+
}

0 commit comments

Comments
 (0)