xref: /llvm-project/llvm/unittests/Support/DJBTest.cpp (revision f1440978a114e6da6d44d9583efa914cb57a6019)
1 //===---------- llvm/unittest/Support/DJBTest.cpp -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Support/DJB.h"
11 #include "llvm/ADT/Twine.h"
12 #include "gtest/gtest.h"
13 
14 using namespace llvm;
15 
16 TEST(DJBTest, caseFolding) {
17   struct TestCase {
18     StringLiteral One;
19     StringLiteral Two;
20   };
21 
22   static constexpr TestCase Tests[] = {
23       {"ASDF", "asdf"},
24       {"qWeR", "QwEr"},
25       {"qqqqqqqqqqqqqqqqqqqq", "QQQQQQQQQQQQQQQQQQQQ"},
26 
27       {"I", "i"},
28       // Latin Small Letter Dotless I
29       {u8"\u0130", "i"},
30       // Latin Capital Letter I With Dot Above
31       {u8"\u0131", "i"},
32 
33       // Latin Capital Letter A With Grave
34       {u8"\u00c0", u8"\u00e0"},
35       // Latin Capital Letter A With Macron
36       {u8"\u0100", u8"\u0101"},
37       // Latin Capital Letter L With Acute
38       {u8"\u0139", u8"\u013a"},
39       // Cyrillic Capital Letter Ie
40       {u8"\u0415", u8"\u0435"},
41       // Latin Capital Letter A With Circumflex And Grave
42       {u8"\u1ea6", u8"\u1ea7"},
43       // Kelvin Sign
44       {u8"\u212a", u8"\u006b"},
45       // Glagolitic Capital Letter Chrivi
46       {u8"\u2c1d", u8"\u2c4d"},
47       // Fullwidth Latin Capital Letter M
48       {u8"\uff2d", u8"\uff4d"},
49       // Old Hungarian Capital Letter Ej
50       {u8"\U00010c92", u8"\U00010cd2"},
51   };
52 
53   for (const TestCase &T : Tests) {
54     SCOPED_TRACE("Comparing '" + T.One + "' and '" + T.Two + "'");
55     EXPECT_EQ(caseFoldingDjbHash(T.One), caseFoldingDjbHash(T.Two));
56   }
57 }
58 
59 TEST(DJBTest, knownValuesLowerCase) {
60   struct TestCase {
61     StringLiteral Text;
62     uint32_t Hash;
63   };
64   static constexpr TestCase Tests[] = {
65       {"", 5381u},
66       {"f", 177675u},
67       {"fo", 5863386u},
68       {"foo", 193491849u},
69       {"foob", 2090263819u},
70       {"fooba", 259229388u},
71       {"foobar", 4259602622u},
72       {"pneumonoultramicroscopicsilicovolcanoconiosis", 3999417781u},
73   };
74 
75   for (const TestCase &T : Tests) {
76     SCOPED_TRACE("Text: '" + T.Text + "'");
77     EXPECT_EQ(T.Hash, djbHash(T.Text));
78     EXPECT_EQ(T.Hash, caseFoldingDjbHash(T.Text));
79     EXPECT_EQ(T.Hash, caseFoldingDjbHash(T.Text.upper()));
80   }
81 }
82 
83 TEST(DJBTest, knownValuesUnicode) {
84   EXPECT_EQ(
85       2326183139u,
86       caseFoldingDjbHash(
87           u8"\u0130\u0131\u00c0\u00e0\u0100\u0101\u0139\u013a\u0415\u0435\u1ea6"
88           u8"\u1ea7\u212a\u006b\u2c1d\u2c4d\uff2d\uff4d\U00010c92\U00010cd2"));
89 }
90