#include "adaptor.h" #include "yexception.h" #include struct TOnCopy: yexception { }; struct TOnMove: yexception { }; struct TState { explicit TState() { } TState(const TState&) { ythrow TOnCopy(); } TState(TState&&) { ythrow TOnMove(); } void operator=(const TState&) { ythrow TOnCopy(); } void rbegin() const { } void rend() const { } }; Y_UNIT_TEST_SUITE(TReverseAdaptor) { Y_UNIT_TEST(ReadTest) { TVector cont = {1, 2, 3}; TVector etalon = {3, 2, 1}; size_t idx = 0; for (const auto& x : Reversed(cont)) { UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x); } idx = 0; for (const auto& x : Reversed(std::move(cont))) { UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x); } } Y_UNIT_TEST(WriteTest) { TVector cont = {1, 2, 3}; TVector etalon = {3, 6, 9}; size_t idx = 0; for (auto& x : Reversed(cont)) { x *= x + idx++; } idx = 0; for (auto& x : cont) { UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x); } } Y_UNIT_TEST(InnerTypeTest) { using TStub = TVector; TStub stub; const TStub cstub; using namespace NPrivate; UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(stub)), TReverseRange); UNIT_ASSERT_TYPES_EQUAL(decltype(Reversed(cstub)), TReverseRange); } Y_UNIT_TEST(CopyMoveTest) { TState lvalue; const TState clvalue; UNIT_ASSERT_NO_EXCEPTION(Reversed(lvalue)); UNIT_ASSERT_NO_EXCEPTION(Reversed(clvalue)); } Y_UNIT_TEST(ReverseX2Test) { TVector cont = {1, 2, 3}; size_t idx = 0; for (const auto& x : Reversed(Reversed(cont))) { UNIT_ASSERT_VALUES_EQUAL(cont[idx++], x); } } Y_UNIT_TEST(ReverseX3Test) { TVector cont = {1, 2, 3}; TVector etalon = {3, 2, 1}; size_t idx = 0; for (const auto& x : Reversed(Reversed(Reversed(cont)))) { UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x); } } Y_UNIT_TEST(ReverseTemporaryTest) { TVector etalon = {3, 2, 1}; TVector etalon2 = {1, 2, 3}; size_t idx = 0; for (const auto& x : Reversed(TVector{1, 2, 3})) { UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x); } idx = 0; for (const auto& x : Reversed(Reversed(TVector{1, 2, 3}))) { UNIT_ASSERT_VALUES_EQUAL(etalon2[idx++], x); } } Y_UNIT_TEST(ReverseInitializerListTest) { // initializer_list has no rbegin and rend auto cont = {1, 2, 3}; TVector etalon = {3, 2, 1}; TVector etalon2 = {1, 2, 3}; size_t idx = 0; for (const auto& x : Reversed(cont)) { UNIT_ASSERT_VALUES_EQUAL(etalon[idx++], x); } idx = 0; for (const auto& x : Reversed(Reversed(cont))) { UNIT_ASSERT_VALUES_EQUAL(etalon2[idx++], x); } } } // Y_UNIT_TEST_SUITE(TReverseAdaptor)