Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / Support / DJBTest.cpp
blobcc8a3d000009026a66cbd71475f9f4331bb815f8
1 //===---------- llvm/unittest/Support/DJBTest.cpp -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/DJB.h"
10 #include "llvm/ADT/Twine.h"
11 #include "gtest/gtest.h"
13 using namespace llvm;
15 TEST(DJBTest, caseFolding) {
16 struct TestCase {
17 StringLiteral One;
18 StringLiteral Two;
21 static constexpr TestCase Tests[] = {
22 {{"ASDF"}, {"asdf"}},
23 {{"qWeR"}, {"QwEr"}},
24 {{"qqqqqqqqqqqqqqqqqqqq"}, {"QQQQQQQQQQQQQQQQQQQQ"}},
26 {{"I"}, {"i"}},
27 // Latin Small Letter Dotless I
28 {{/*U+130*/ "\xc4\xb0"}, {"i"}},
29 // Latin Capital Letter I With Dot Above
30 {{/*U+131*/ "\xc4\xb1"}, {"i"}},
32 // Latin Capital Letter A With Grave
33 {{/*U+c0*/ "\xc3\x80"}, {/*U+e0*/ "\xc3\xa0"}},
34 // Latin Capital Letter A With Macron
35 {{/*U+100*/ "\xc4\x80"}, {/*U+101*/ "\xc4\x81"}},
36 // Latin Capital Letter L With Acute
37 {{/*U+139*/ "\xc4\xb9"}, {/*U+13a*/ "\xc4\xba"}},
38 // Cyrillic Capital Letter Ie
39 {{/*U+415*/ "\xd0\x95"}, {/*U+435*/ "\xd0\xb5"}},
40 // Latin Capital Letter A With Circumflex And Grave
41 {{/*U+1ea6*/ "\xe1\xba\xa6"}, {/*U+1ea7*/ "\xe1\xba\xa7"}},
42 // Kelvin Sign
43 {{/*U+212a*/ "\xe2\x84\xaa"}, {"k"}},
44 // Glagolitic Capital Letter Chrivi
45 {{/*U+2c1d*/ "\xe2\xb0\x9d"}, {/*U+2c4d*/ "\xe2\xb1\x8d"}},
46 // Fullwidth Latin Capital Letter M
47 {{/*U+ff2d*/ "\xef\xbc\xad"}, {/*U+ff4d*/ "\xef\xbd\x8d"}},
48 // Old Hungarian Capital Letter Ej
49 {{/*U+10c92*/ "\xf0\x90\xb2\x92"}, {/*U+10cd2*/ "\xf0\x90\xb3\x92"}},
52 for (const TestCase &T : Tests) {
53 SCOPED_TRACE("Comparing '" + T.One + "' and '" + T.Two + "'");
54 EXPECT_EQ(caseFoldingDjbHash(T.One), caseFoldingDjbHash(T.Two));
58 TEST(DJBTest, knownValuesLowerCase) {
59 struct TestCase {
60 StringLiteral Text;
61 uint32_t Hash;
63 static constexpr TestCase Tests[] = {
64 {{""}, 5381u},
65 {{"f"}, 177675u},
66 {{"fo"}, 5863386u},
67 {{"foo"}, 193491849u},
68 {{"foob"}, 2090263819u},
69 {{"fooba"}, 259229388u},
70 {{"foobar"}, 4259602622u},
71 {{"pneumonoultramicroscopicsilicovolcanoconiosis"}, 3999417781u},
74 for (const TestCase &T : Tests) {
75 SCOPED_TRACE("Text: '" + T.Text + "'");
76 EXPECT_EQ(T.Hash, djbHash(T.Text));
77 EXPECT_EQ(T.Hash, caseFoldingDjbHash(T.Text));
78 EXPECT_EQ(T.Hash, caseFoldingDjbHash(T.Text.upper()));
82 TEST(DJBTest, knownValuesUnicode) {
83 EXPECT_EQ(5866553u, djbHash(/*U+130*/ "\xc4\xb0"));
84 EXPECT_EQ(177678u, caseFoldingDjbHash(/*U+130*/ "\xc4\xb0"));
85 EXPECT_EQ(
86 1302161417u,
87 djbHash("\xc4\xb0\xc4\xb1\xc3\x80\xc3\xa0\xc4\x80\xc4\x81\xc4\xb9\xc4\xba"
88 "\xd0\x95\xd0\xb5\xe1\xba\xa6\xe1\xba\xa7\xe2\x84\xaa\x6b\xe2\xb0"
89 "\x9d\xe2\xb1\x8d\xef\xbc\xad\xef\xbd\x8d\xf0\x90\xb2\x92\xf0\x90"
90 "\xb3\x92"));
91 EXPECT_EQ(
92 1145571043u,
93 caseFoldingDjbHash(
94 "\xc4\xb0\xc4\xb1\xc3\x80\xc3\xa0\xc4\x80\xc4\x81\xc4\xb9\xc4\xba"
95 "\xd0\x95\xd0\xb5\xe1\xba\xa6\xe1\xba\xa7\xe2\x84\xaa\x6b\xe2\xb0"
96 "\x9d\xe2\xb1\x8d\xef\xbc\xad\xef\xbd\x8d\xf0\x90\xb2\x92\xf0\x90"
97 "\xb3\x92"));