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 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/DJB.h"
10 #include "llvm/ADT/Twine.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 
TEST(DJBTest,caseFolding)15 TEST(DJBTest, caseFolding) {
16   struct TestCase {
17     StringLiteral One;
18     StringLiteral Two;
19   };
20 
21   static constexpr TestCase Tests[] = {
22       {{"ASDF"}, {"asdf"}},
23       {{"qWeR"}, {"QwEr"}},
24       {{"qqqqqqqqqqqqqqqqqqqq"}, {"QQQQQQQQQQQQQQQQQQQQ"}},
25 
26       {{"I"}, {"i"}},
27       // Latin Small Letter Dotless I
28       {{u8"\u0130"}, {"i"}},
29       // Latin Capital Letter I With Dot Above
30       {{u8"\u0131"}, {"i"}},
31 
32       // Latin Capital Letter A With Grave
33       {{u8"\u00c0"}, {u8"\u00e0"}},
34       // Latin Capital Letter A With Macron
35       {{u8"\u0100"}, {u8"\u0101"}},
36       // Latin Capital Letter L With Acute
37       {{u8"\u0139"}, {u8"\u013a"}},
38       // Cyrillic Capital Letter Ie
39       {{u8"\u0415"}, {u8"\u0435"}},
40       // Latin Capital Letter A With Circumflex And Grave
41       {{u8"\u1ea6"}, {u8"\u1ea7"}},
42       // Kelvin Sign
43       {{u8"\u212a"}, {u8"\u006b"}},
44       // Glagolitic Capital Letter Chrivi
45       {{u8"\u2c1d"}, {u8"\u2c4d"}},
46       // Fullwidth Latin Capital Letter M
47       {{u8"\uff2d"}, {u8"\uff4d"}},
48       // Old Hungarian Capital Letter Ej
49       {{u8"\U00010c92"}, {u8"\U00010cd2"}},
50   };
51 
52   for (const TestCase &T : Tests) {
53     SCOPED_TRACE("Comparing '" + T.One + "' and '" + T.Two + "'");
54     EXPECT_EQ(caseFoldingDjbHash(T.One), caseFoldingDjbHash(T.Two));
55   }
56 }
57 
TEST(DJBTest,knownValuesLowerCase)58 TEST(DJBTest, knownValuesLowerCase) {
59   struct TestCase {
60     StringLiteral Text;
61     uint32_t Hash;
62   };
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},
72   };
73 
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()));
79   }
80 }
81 
TEST(DJBTest,knownValuesUnicode)82 TEST(DJBTest, knownValuesUnicode) {
83   EXPECT_EQ(5866553u, djbHash(u8"\u0130"));
84   EXPECT_EQ(177678u, caseFoldingDjbHash(u8"\u0130"));
85   EXPECT_EQ(
86       1302161417u,
87       djbHash(
88           u8"\u0130\u0131\u00c0\u00e0\u0100\u0101\u0139\u013a\u0415\u0435\u1ea6"
89           u8"\u1ea7\u212a\u006b\u2c1d\u2c4d\uff2d\uff4d\U00010c92\U00010cd2"));
90   EXPECT_EQ(
91       1145571043u,
92       caseFoldingDjbHash(
93           u8"\u0130\u0131\u00c0\u00e0\u0100\u0101\u0139\u013a\u0415\u0435\u1ea6"
94           u8"\u1ea7\u212a\u006b\u2c1d\u2c4d\uff2d\uff4d\U00010c92\U00010cd2"));
95 }
96