DJBTest.cpp 2.9 KB

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