flat_map_tests.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include <catch.hpp>
  8. #include "base/flat_map.h"
  9. #include <string>
  10. struct int_wrap {
  11. int value;
  12. };
  13. struct int_wrap_comparator {
  14. inline bool operator()(const int_wrap &a, const int_wrap &b) const {
  15. return a.value < b.value;
  16. }
  17. };
  18. using namespace std;
  19. TEST_CASE("flat_maps should keep items sorted by key", "[flat_map]") {
  20. base::flat_map<int, string> v;
  21. v.emplace(0, "a");
  22. v.emplace(5, "b");
  23. v.emplace(4, "d");
  24. v.emplace(2, "e");
  25. auto checkSorted = [&] {
  26. auto prev = v.begin();
  27. REQUIRE(prev != v.end());
  28. for (auto i = prev + 1; i != v.end(); prev = i, ++i) {
  29. REQUIRE(prev->first < i->first);
  30. }
  31. };
  32. REQUIRE(v.size() == 4);
  33. checkSorted();
  34. SECTION("adding item puts it in the right position") {
  35. v.emplace(3, "c");
  36. REQUIRE(v.size() == 5);
  37. REQUIRE(v.find(3) != v.end());
  38. checkSorted();
  39. }
  40. }
  41. TEST_CASE("simple flat_maps tests", "[flat_map]") {
  42. SECTION("copy constructor") {
  43. base::flat_map<int, string> v;
  44. v.emplace(0, "a");
  45. v.emplace(2, "b");
  46. auto u = v;
  47. REQUIRE(u.size() == 2);
  48. REQUIRE(u.find(0) == u.begin());
  49. REQUIRE(u.find(2) == u.end() - 1);
  50. }
  51. SECTION("assignment") {
  52. base::flat_map<int, string> v, u;
  53. v.emplace(0, "a");
  54. v.emplace(2, "b");
  55. u = v;
  56. REQUIRE(u.size() == 2);
  57. REQUIRE(u.find(0) == u.begin());
  58. REQUIRE(u.find(2) == u.end() - 1);
  59. }
  60. }
  61. TEST_CASE("flat_maps custom comparator", "[flat_map]") {
  62. base::flat_map<int_wrap, string, int_wrap_comparator> v;
  63. v.emplace(int_wrap{ 0 }, "a");
  64. v.emplace(int_wrap{ 5 }, "b");
  65. v.emplace(int_wrap{ 4 }, "d");
  66. v.emplace(int_wrap{ 2 }, "e");
  67. auto checkSorted = [&] {
  68. auto prev = v.begin();
  69. REQUIRE(prev != v.end());
  70. for (auto i = prev + 1; i != v.end(); prev = i, ++i) {
  71. REQUIRE(int_wrap_comparator()(prev->first, i->first));
  72. }
  73. };
  74. REQUIRE(v.size() == 4);
  75. checkSorted();
  76. SECTION("adding item puts it in the right position") {
  77. v.emplace(int_wrap{ 3 }, "c");
  78. REQUIRE(v.size() == 5);
  79. REQUIRE(v.find({ 3 }) != v.end());
  80. checkSorted();
  81. }
  82. }
  83. TEST_CASE("flat_maps structured bindings", "[flat_map]") {
  84. base::flat_map<int, std::unique_ptr<double>> v;
  85. v.emplace(0, std::make_unique<double>(0.));
  86. v.emplace(1, std::make_unique<double>(1.));
  87. SECTION("structred binded range-based for loop") {
  88. for (const auto &[key, value] : v) {
  89. REQUIRE(key == int(std::round(*value)));
  90. }
  91. }
  92. SECTION("non-const structured binded range-based for loop") {
  93. base::flat_map<int, int> second = {
  94. { 1, 1 },
  95. { 2, 2 },
  96. { 2, 3 },
  97. { 3, 3 },
  98. };
  99. REQUIRE(second.size() == 3);
  100. //for (auto [a, b] : second) { // #MSVC Bug, reported
  101. // REQUIRE(a == b);
  102. //}
  103. for (const auto [a, b] : second) {
  104. REQUIRE(a == b);
  105. }
  106. }
  107. }