HashedStringTests.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(), 0u);
  37. CORE_TEST_TRUE(b.hashCode() != 0u);
  38. }
  39. static void testAsHashMapKey() {
  40. Core::HashMap<HString, int> map;
  41. CORE_TEST_ERROR(map.add("wusi", 3));
  42. CORE_TEST_ERROR(map.add("hiThere", 7));
  43. CORE_TEST_ERROR(map.add("baum123", 5));
  44. int* a = map.search("wusi");
  45. int* b = map.search("hiThere");
  46. int* c = map.search("baum123");
  47. int* d = map.search("423hifd");
  48. CORE_TEST_NOT_NULL(a);
  49. CORE_TEST_NOT_NULL(b);
  50. CORE_TEST_NOT_NULL(c);
  51. CORE_TEST_NULL(d);
  52. if(a != nullptr && b != nullptr && c != nullptr) {
  53. CORE_TEST_EQUAL(3, *a);
  54. CORE_TEST_EQUAL(7, *b);
  55. CORE_TEST_EQUAL(5, *c);
  56. }
  57. }
  58. void Core::testHashedString() {
  59. testComparison();
  60. testLength();
  61. testHashCode();
  62. testAsHashMapKey();
  63. }