1 //===---------- llvm/unittest/Support/DJBTest.cpp -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/DJB.h"
10 #include "llvm/ADT/Twine.h"
11 #include "gtest/gtest.h"
15 TEST(DJBTest
, caseFolding
) {
21 static constexpr TestCase Tests
[] = {
24 {{"qqqqqqqqqqqqqqqqqqqq"}, {"QQQQQQQQQQQQQQQQQQQQ"}},
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"}},
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
) {
63 static constexpr TestCase Tests
[] = {
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"));
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"
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"