unit-linear-map.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <catch2/catch.hpp>
  2. #include <iostream>
  3. #include <gul/LinearMap.h>
  4. SCENARIO("Test LinearMap")
  5. {
  6. struct D
  7. {
  8. int x;
  9. int y;
  10. };
  11. gul::LinearMap<std::string, D> M;
  12. THEN("Inserting and removing")
  13. {
  14. auto index = M.insert("Gavin", D{1,2});
  15. REQUIRE(index == 0);
  16. REQUIRE(M.size() == 1);
  17. REQUIRE(M.capacity() == 1);
  18. THEN("We can access the data")
  19. {
  20. REQUIRE(M["Gavin"].x == 1);
  21. REQUIRE(M["Gavin"].y == 2);
  22. }
  23. M.erase("Gavin");
  24. REQUIRE(M.size() == 0);
  25. REQUIRE(M.capacity() == 1);
  26. }
  27. THEN("Key lookup")
  28. {
  29. M["Gavin"] = D{1,2};
  30. REQUIRE(M.size() == 1);
  31. REQUIRE(M.capacity() == 1);
  32. THEN("We can access the data")
  33. {
  34. REQUIRE(M["Gavin"].x == 1);
  35. REQUIRE(M["Gavin"].y == 2);
  36. }
  37. M.erase("Gavin");
  38. REQUIRE(M.size() == 0);
  39. REQUIRE(M.capacity() == 1);
  40. }
  41. THEN("Index lookup")
  42. {
  43. M["Batman"] = D{1,7};
  44. M["Superman"] = D{2,8};
  45. M["WonderWoman"] = D{3,8};
  46. M["GreenLantern"] = D{4,10};
  47. M["Flash"] = D{5,11};
  48. M["MartianManhunter"] = D{6,12};
  49. REQUIRE(M.size() == 6);
  50. REQUIRE(M.capacity() == 6);
  51. REQUIRE(M.array().size() == M.capacity());
  52. WHEN("We access a non-existant key using at() ")
  53. {
  54. REQUIRE_THROWS( M.at("Cyborg"));
  55. }
  56. WHEN("We erase a key")
  57. {
  58. auto sI = M.findIndex("Superman");
  59. M.erase("Superman");
  60. REQUIRE(M.size() == 5);
  61. REQUIRE(M.capacity() == 6);
  62. THEN("The index can be reused for the next value")
  63. {
  64. auto sC = M.insert("Cyborg", D{5,5});
  65. REQUIRE( sC == sI);
  66. REQUIRE(M.size() == 6);
  67. REQUIRE(M.capacity() == 6);
  68. }
  69. }
  70. }
  71. THEN("Defragment")
  72. {
  73. M["Batman"] = D{1,7};
  74. M["Superman"] = D{2,8};
  75. M["WonderWoman"] = D{3,8};
  76. M["GreenLantern"] = D{4,10};
  77. M["Flash"] = D{5,11};
  78. M["MartianManhunter"] = D{6,12};
  79. REQUIRE(M.size() == 6);
  80. REQUIRE(M.capacity() == 6);
  81. M.erase("GreenLantern");
  82. REQUIRE(M.size() == 5);
  83. REQUIRE(M.capacity() == 6);
  84. M.defragment();
  85. REQUIRE(M.size() == 5);
  86. REQUIRE(M.capacity() == 5);
  87. }
  88. }