Main.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "data/HashMap.h"
  2. #include "data/ProbingHashMap.h"
  3. #include "test/Test.h"
  4. #include "utils/Clock.h"
  5. #include "utils/Random.h"
  6. template<typename Map>
  7. static void test(const Map& m) {
  8. Core::Clock::Nanos nanos = 0;
  9. CORE_TEST_ERROR(Core::Clock::getNanos(nanos));
  10. int sum = 0;
  11. for(int i = 0; i < 10000; i++) {
  12. for(int k = -5000; k < 5000; k++) {
  13. const int* s = m.search(i + k);
  14. if(s != nullptr) {
  15. sum += *s;
  16. }
  17. }
  18. }
  19. Core::Clock::Nanos nanos2 = 0;
  20. CORE_TEST_ERROR(Core::Clock::getNanos(nanos2));
  21. CORE_LOG_INFO("# | # ns", sum, nanos2 - nanos);
  22. }
  23. static void order() {
  24. Core::HashMap<int, int> m;
  25. Core::ProbingHashMap<int, int> m2;
  26. for(int i = 0; i < 10000; i++) {
  27. CORE_TEST_ERROR(m.add(i, i * i));
  28. CORE_TEST_ERROR(m2.add(i, i * i));
  29. }
  30. test(m);
  31. test(m);
  32. test(m);
  33. test(m2);
  34. test(m2);
  35. test(m2);
  36. }
  37. static void chaos() {
  38. Core::Random random(0);
  39. Core::HashMap<int, int> m;
  40. Core::ProbingHashMap<int, int> m2;
  41. for(int i = 0; i < 10000; i++) {
  42. int r = random.next(0, 9999);
  43. CORE_TEST_ERROR(m.add(r, r * r));
  44. CORE_TEST_ERROR(m2.add(r, r * r));
  45. }
  46. test(m);
  47. test(m);
  48. test(m);
  49. test(m2);
  50. test(m2);
  51. test(m2);
  52. }
  53. int main() {
  54. order();
  55. chaos();
  56. return 0;
  57. }