HashedStringTests.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "../Tests.hpp"
  2. #include "core/data/HashMap.hpp"
  3. #include "core/utils/HashedString.hpp"
  4. template class Core::HashedString<32>;
  5. using HString = Core::HashedString<32>;
  6. static void testComparison() {
  7. HString a("test");
  8. HString b("testA");
  9. HString c("");
  10. HString d("Btest");
  11. CORE_TEST_TRUE(a == a);
  12. CORE_TEST_TRUE(b == b);
  13. CORE_TEST_TRUE(c == c);
  14. CORE_TEST_TRUE(d == d);
  15. CORE_TEST_TRUE(a != b);
  16. CORE_TEST_TRUE(a != c);
  17. CORE_TEST_TRUE(a != d);
  18. CORE_TEST_TRUE(b != a);
  19. CORE_TEST_TRUE(b != c);
  20. CORE_TEST_TRUE(b != d);
  21. CORE_TEST_TRUE(c != a);
  22. CORE_TEST_TRUE(c != b);
  23. CORE_TEST_TRUE(c != d);
  24. CORE_TEST_TRUE(d != a);
  25. CORE_TEST_TRUE(d != b);
  26. CORE_TEST_TRUE(d != c);
  27. }
  28. static void testLength() {
  29. HString s("test");
  30. CORE_TEST_EQUAL(4, s.getLength());
  31. CORE_TEST_EQUAL(31, s.getCapacity());
  32. }
  33. static void testHashCode() {
  34. HString a;
  35. HString b("wusi");
  36. CORE_TEST_EQUAL(a.hashCode(), 0lu);
  37. CORE_TEST_TRUE(b.hashCode() != 0u);
  38. }
  39. static void testAsHashMapKey() {
  40. Core::HashMap<HString, int> map;
  41. map.add("wusi", 3).add("hiThere", 7).add("baum123", 5);
  42. int* a = map.search("wusi");
  43. int* b = map.search("hiThere");
  44. int* c = map.search("baum123");
  45. int* d = map.search("423hifd");
  46. CORE_TEST_NOT_NULL(a);
  47. CORE_TEST_NOT_NULL(b);
  48. CORE_TEST_NOT_NULL(c);
  49. CORE_TEST_NULL(d);
  50. if(a != nullptr && b != nullptr && c != nullptr) {
  51. CORE_TEST_EQUAL(3, *a);
  52. CORE_TEST_EQUAL(7, *b);
  53. CORE_TEST_EQUAL(5, *c);
  54. }
  55. }
  56. void Core::testHashedString() {
  57. testComparison();
  58. testLength();
  59. testHashCode();
  60. testAsHashMapKey();
  61. }