DJBTest.cpp 2.6 KB

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