adaptor_ut.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "adaptor.h"
  2. #include "yexception.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. struct TOnCopy: yexception {
  5. };
  6. struct TOnMove: yexception {
  7. };
  8. struct TState {
  9. explicit TState() {
  10. }
  11. TState(const TState&) {
  12. ythrow TOnCopy();
  13. }
  14. TState(TState&&) {
  15. ythrow TOnMove();
  16. }
  17. void operator=(const TState&) {
  18. ythrow TOnCopy();
  19. }
  20. void rbegin() const {
  21. }
  22. void rend() const {
  23. }
  24. };
  25. Y_UNIT_TEST_SUITE(TReverseAdaptor) {
  26. Y_UNIT_TEST(ReadTest) {
  27. TVector<int> cont = {1, 2, 3};
  28. TVector<int> etalon = {3, 2, 1};
  29. size_t idx = 0;
  30. for (const auto& x : Reversed(cont)) {
  31. UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
  32. }
  33. idx = 0;
  34. for (const auto& x : Reversed(std::move(cont))) {
  35. UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
  36. }
  37. }
  38. Y_UNIT_TEST(WriteTest) {
  39. TVector<int> cont = {1, 2, 3};
  40. TVector<int> etalon = {3, 6, 9};
  41. size_t idx = 0;
  42. for (auto& x : Reversed(cont)) {
  43. x *= x + idx++;
  44. }
  45. idx = 0;
  46. for (auto& x : cont) {
  47. UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
  48. }
  49. }
  50. Y_UNIT_TEST(InnerTypeTest) {
  51. using TStub = TVector<int>;
  52. TStub stub;
  53. const TStub cstub;
  54. using namespace NPrivate;
  55. UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(stub)), TReverseRange<TStub&>);
  56. UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(cstub)), TReverseRange<const TStub&>);
  57. }
  58. Y_UNIT_TEST(CopyMoveTest) {
  59. TState lvalue;
  60. const TState clvalue;
  61. UNIT_ASSERT_NO_EXCEPTION(Reversed(lvalue));
  62. UNIT_ASSERT_NO_EXCEPTION(Reversed(clvalue));
  63. }
  64. Y_UNIT_TEST(ReverseX2Test) {
  65. TVector<int> cont = {1, 2, 3};
  66. size_t idx = 0;
  67. for (const auto& x : Reversed(Reversed(cont))) {
  68. UNIT_ASSERT_VALUES_EQUAL(cont[idx++], x);
  69. }
  70. }
  71. Y_UNIT_TEST(ReverseX3Test) {
  72. TVector<int> cont = {1, 2, 3};
  73. TVector<int> etalon = {3, 2, 1};
  74. size_t idx = 0;
  75. for (const auto& x : Reversed(Reversed(Reversed(cont)))) {
  76. UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
  77. }
  78. }
  79. Y_UNIT_TEST(ReverseTemporaryTest) {
  80. TVector<int> etalon = {3, 2, 1};
  81. TVector<int> etalon2 = {1, 2, 3};
  82. size_t idx = 0;
  83. for (const auto& x : Reversed(TVector<int>{1, 2, 3})) {
  84. UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
  85. }
  86. idx = 0;
  87. for (const auto& x : Reversed(Reversed(TVector<int>{1, 2, 3}))) {
  88. UNIT_ASSERT_VALUES_EQUAL(etalon2[idx++], x);
  89. }
  90. }
  91. Y_UNIT_TEST(ReverseInitializerListTest) {
  92. // initializer_list has no rbegin and rend
  93. auto cont = {1, 2, 3};
  94. TVector<int> etalon = {3, 2, 1};
  95. TVector<int> etalon2 = {1, 2, 3};
  96. size_t idx = 0;
  97. for (const auto& x : Reversed(cont)) {
  98. UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x);
  99. }
  100. idx = 0;
  101. for (const auto& x : Reversed(Reversed(cont))) {
  102. UNIT_ASSERT_VALUES_EQUAL(etalon2[idx++], x);
  103. }
  104. }
  105. } // Y_UNIT_TEST_SUITE(TReverseAdaptor)