HashedStringTests.cpp 1.7 KB

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