flat_set_tests.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_set.h"
  9. struct int_wrap {
  10. int value;
  11. };
  12. struct int_wrap_comparator {
  13. using is_transparent = void;
  14. inline bool operator()(const int &a, const int_wrap &b) const {
  15. return a < b.value;
  16. }
  17. inline bool operator()(const int_wrap &a, const int_wrap &b) const {
  18. return a.value < b.value;
  19. }
  20. inline bool operator()(const int_wrap &a, const int &b) const {
  21. return a.value < b;
  22. }
  23. inline bool operator()(const int &a, const int &b) const {
  24. return a < b;
  25. }
  26. };
  27. TEST_CASE("flat_sets should keep items sorted", "[flat_set]") {
  28. base::flat_set<int> v;
  29. v.insert(0);
  30. v.insert(5);
  31. v.insert(4);
  32. v.insert(2);
  33. REQUIRE(v.contains(4));
  34. auto checkSorted = [&] {
  35. auto prev = v.begin();
  36. REQUIRE(prev != v.end());
  37. for (auto i = prev + 1; i != v.end(); prev = i, ++i) {
  38. REQUIRE(*prev < *i);
  39. }
  40. };
  41. REQUIRE(v.size() == 4);
  42. checkSorted();
  43. SECTION("adding item puts it in the right position") {
  44. v.insert(3);
  45. REQUIRE(v.size() == 5);
  46. REQUIRE(v.find(3) != v.end());
  47. checkSorted();
  48. }
  49. }
  50. TEST_CASE("flat_sets with custom comparators", "[flat_set]") {
  51. base::flat_set<int_wrap, int_wrap_comparator> v;
  52. v.insert({ 0 });
  53. v.insert({ 5 });
  54. v.insert({ 4 });
  55. v.insert({ 2 });
  56. REQUIRE(v.find(4) != v.end());
  57. auto checkSorted = [&] {
  58. auto prev = v.begin();
  59. REQUIRE(prev != v.end());
  60. for (auto i = prev + 1; i != v.end(); prev = i, ++i) {
  61. REQUIRE(prev->value < i->value);
  62. }
  63. };
  64. REQUIRE(v.size() == 4);
  65. checkSorted();
  66. SECTION("adding item puts it in the right position") {
  67. v.insert({ 3 });
  68. REQUIRE(v.size() == 5);
  69. REQUIRE(v.find(3) != v.end());
  70. checkSorted();
  71. }
  72. }