Main.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <iostream>
  2. #include "utils/HashMap.h"
  3. constexpr int MAP_MIN_CAPACITY = 5;
  4. typedef HashMap<int, int, MAP_MIN_CAPACITY> IntMap;
  5. const char* RED = "\033[0;31m";
  6. const char* GREEN = "\033[0;32m";
  7. int tests = 0;
  8. int successTests = 0;
  9. void finalizeTest() {
  10. std::cout << ((successTests == tests) ? GREEN : RED);
  11. std::cout << successTests << " / " << tests << " succeeded\n";
  12. tests = 0;
  13. successTests = 0;
  14. }
  15. template<typename T>
  16. void checkEqual(const T& wanted, const T& actual, const char* text) {
  17. if(wanted == actual) {
  18. tests++;
  19. successTests++;
  20. std::cout << GREEN << tests << ": " << text << "\n";
  21. } else {
  22. tests++;
  23. std::cout << RED << tests << ": " << text << " - ";
  24. std::cout << RED << "expected '" << wanted << "' got '" << actual << "'\n";
  25. }
  26. }
  27. namespace Map {
  28. void testAdd() {
  29. IntMap map;
  30. map.add(5, 4);
  31. checkEqual(true, map.contains(5), "map contains added value");
  32. checkEqual(true, map.search(5, -1) == 4, "map search finds added value");
  33. }
  34. void testMultipleAdd() {
  35. IntMap map;
  36. map.add(5, 4);
  37. map.add(10, 3);
  38. map.add(15, 2);
  39. checkEqual(true, map.contains(5), "map contains added value 1");
  40. checkEqual(true, map.contains(10), "map contains added value 2");
  41. checkEqual(true, map.contains(15), "map contains added value 3");
  42. checkEqual(true, map.search(5, -1) == 4, "map search finds added value 1");
  43. checkEqual(true, map.search(10, -1) == 3, "map search finds added value 2");
  44. checkEqual(true, map.search(15, -1) == 2, "map search finds added value 3");
  45. }
  46. void testAddReplace() {
  47. IntMap map;
  48. map.add(5, 4);
  49. map.add(5, 10);
  50. checkEqual(true, map.contains(5), "map contains replaced value");
  51. checkEqual(true, map.search(5, -1) == 10, "map search finds replaced value");
  52. }
  53. void testClear() {
  54. IntMap map;
  55. map.add(5, 4);
  56. map.add(4, 10);
  57. map.clear();
  58. checkEqual(false, map.contains(5), "map does not contain cleared values");
  59. checkEqual(false, map.contains(4), "map does not contain cleared values");
  60. }
  61. void testOverflow() {
  62. IntMap map;
  63. for(int i = 0; i < 1000000; i++) {
  64. map.add(i, i);
  65. }
  66. for(int i = 0; i < MAP_MIN_CAPACITY; i++) {
  67. checkEqual(true, map.contains(i), "map still contains values after overflow");
  68. }
  69. checkEqual(true, true, "map survives overflow");
  70. }
  71. void test() {
  72. testAdd();
  73. testMultipleAdd();
  74. testAddReplace();
  75. testClear();
  76. testOverflow();
  77. finalizeTest();
  78. }
  79. }
  80. int main() {
  81. Map::test();
  82. return 0;
  83. }