13b17b84bSPavel Labath //===---------- llvm/unittest/Support/DJBTest.cpp -------------------------===//
23b17b84bSPavel Labath //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63b17b84bSPavel Labath //
73b17b84bSPavel Labath //===----------------------------------------------------------------------===//
83b17b84bSPavel Labath 
93b17b84bSPavel Labath #include "llvm/Support/DJB.h"
103b17b84bSPavel Labath #include "llvm/ADT/Twine.h"
113b17b84bSPavel Labath #include "gtest/gtest.h"
123b17b84bSPavel Labath 
133b17b84bSPavel Labath using namespace llvm;
143b17b84bSPavel Labath 
TEST(DJBTest,caseFolding)153b17b84bSPavel Labath TEST(DJBTest, caseFolding) {
163b17b84bSPavel Labath   struct TestCase {
173b17b84bSPavel Labath     StringLiteral One;
183b17b84bSPavel Labath     StringLiteral Two;
193b17b84bSPavel Labath   };
203b17b84bSPavel Labath 
213b17b84bSPavel Labath   static constexpr TestCase Tests[] = {
223b17b84bSPavel Labath       {{"ASDF"}, {"asdf"}},
233b17b84bSPavel Labath       {{"qWeR"}, {"QwEr"}},
243b17b84bSPavel Labath       {{"qqqqqqqqqqqqqqqqqqqq"}, {"QQQQQQQQQQQQQQQQQQQQ"}},
253b17b84bSPavel Labath 
263b17b84bSPavel Labath       {{"I"}, {"i"}},
273b17b84bSPavel Labath       // Latin Small Letter Dotless I
283b17b84bSPavel Labath       {{u8"\u0130"}, {"i"}},
293b17b84bSPavel Labath       // Latin Capital Letter I With Dot Above
303b17b84bSPavel Labath       {{u8"\u0131"}, {"i"}},
313b17b84bSPavel Labath 
323b17b84bSPavel Labath       // Latin Capital Letter A With Grave
333b17b84bSPavel Labath       {{u8"\u00c0"}, {u8"\u00e0"}},
343b17b84bSPavel Labath       // Latin Capital Letter A With Macron
353b17b84bSPavel Labath       {{u8"\u0100"}, {u8"\u0101"}},
363b17b84bSPavel Labath       // Latin Capital Letter L With Acute
373b17b84bSPavel Labath       {{u8"\u0139"}, {u8"\u013a"}},
383b17b84bSPavel Labath       // Cyrillic Capital Letter Ie
393b17b84bSPavel Labath       {{u8"\u0415"}, {u8"\u0435"}},
403b17b84bSPavel Labath       // Latin Capital Letter A With Circumflex And Grave
413b17b84bSPavel Labath       {{u8"\u1ea6"}, {u8"\u1ea7"}},
423b17b84bSPavel Labath       // Kelvin Sign
433b17b84bSPavel Labath       {{u8"\u212a"}, {u8"\u006b"}},
443b17b84bSPavel Labath       // Glagolitic Capital Letter Chrivi
453b17b84bSPavel Labath       {{u8"\u2c1d"}, {u8"\u2c4d"}},
463b17b84bSPavel Labath       // Fullwidth Latin Capital Letter M
473b17b84bSPavel Labath       {{u8"\uff2d"}, {u8"\uff4d"}},
483b17b84bSPavel Labath       // Old Hungarian Capital Letter Ej
493b17b84bSPavel Labath       {{u8"\U00010c92"}, {u8"\U00010cd2"}},
503b17b84bSPavel Labath   };
513b17b84bSPavel Labath 
523b17b84bSPavel Labath   for (const TestCase &T : Tests) {
533b17b84bSPavel Labath     SCOPED_TRACE("Comparing '" + T.One + "' and '" + T.Two + "'");
543b17b84bSPavel Labath     EXPECT_EQ(caseFoldingDjbHash(T.One), caseFoldingDjbHash(T.Two));
553b17b84bSPavel Labath   }
563b17b84bSPavel Labath }
573b17b84bSPavel Labath 
TEST(DJBTest,knownValuesLowerCase)583b17b84bSPavel Labath TEST(DJBTest, knownValuesLowerCase) {
593b17b84bSPavel Labath   struct TestCase {
603b17b84bSPavel Labath     StringLiteral Text;
613b17b84bSPavel Labath     uint32_t Hash;
623b17b84bSPavel Labath   };
633b17b84bSPavel Labath   static constexpr TestCase Tests[] = {
643b17b84bSPavel Labath       {{""}, 5381u},
653b17b84bSPavel Labath       {{"f"}, 177675u},
663b17b84bSPavel Labath       {{"fo"}, 5863386u},
673b17b84bSPavel Labath       {{"foo"}, 193491849u},
683b17b84bSPavel Labath       {{"foob"}, 2090263819u},
693b17b84bSPavel Labath       {{"fooba"}, 259229388u},
703b17b84bSPavel Labath       {{"foobar"}, 4259602622u},
713b17b84bSPavel Labath       {{"pneumonoultramicroscopicsilicovolcanoconiosis"}, 3999417781u},
723b17b84bSPavel Labath   };
733b17b84bSPavel Labath 
743b17b84bSPavel Labath   for (const TestCase &T : Tests) {
753b17b84bSPavel Labath     SCOPED_TRACE("Text: '" + T.Text + "'");
763b17b84bSPavel Labath     EXPECT_EQ(T.Hash, djbHash(T.Text));
773b17b84bSPavel Labath     EXPECT_EQ(T.Hash, caseFoldingDjbHash(T.Text));
783b17b84bSPavel Labath     EXPECT_EQ(T.Hash, caseFoldingDjbHash(T.Text.upper()));
793b17b84bSPavel Labath   }
803b17b84bSPavel Labath }
813b17b84bSPavel Labath 
TEST(DJBTest,knownValuesUnicode)823b17b84bSPavel Labath TEST(DJBTest, knownValuesUnicode) {
833b17b84bSPavel Labath   EXPECT_EQ(5866553u, djbHash(u8"\u0130"));
843b17b84bSPavel Labath   EXPECT_EQ(177678u, caseFoldingDjbHash(u8"\u0130"));
853b17b84bSPavel Labath   EXPECT_EQ(
863b17b84bSPavel Labath       1302161417u,
873b17b84bSPavel Labath       djbHash(
883b17b84bSPavel Labath           u8"\u0130\u0131\u00c0\u00e0\u0100\u0101\u0139\u013a\u0415\u0435\u1ea6"
893b17b84bSPavel Labath           u8"\u1ea7\u212a\u006b\u2c1d\u2c4d\uff2d\uff4d\U00010c92\U00010cd2"));
903b17b84bSPavel Labath   EXPECT_EQ(
913b17b84bSPavel Labath       1145571043u,
923b17b84bSPavel Labath       caseFoldingDjbHash(
933b17b84bSPavel Labath           u8"\u0130\u0131\u00c0\u00e0\u0100\u0101\u0139\u013a\u0415\u0435\u1ea6"
943b17b84bSPavel Labath           u8"\u1ea7\u212a\u006b\u2c1d\u2c4d\uff2d\uff4d\U00010c92\U00010cd2"));
953b17b84bSPavel Labath }
96