Created
November 22, 2019 15:47
-
-
Save commander-trashdin/0cfb25ab0d1c7361ec95f3396a9332b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Int { | |
| int x; | |
| Int() = default; | |
| Int(int x) : x(x) { | |
| } | |
| Int(const Int& rhs) { | |
| UpdateCounter(); | |
| x = rhs.x; | |
| } | |
| Int& operator=(const Int& rhs) { | |
| UpdateCounter(); | |
| x = rhs.x; | |
| return *this; | |
| } | |
| Int(Int&&) = delete; | |
| Int& operator=(Int&&) = delete; | |
| static int counter; | |
| static int max_value; | |
| static void Reset(int max_val) { | |
| counter = 0; | |
| max_value = max_val; | |
| } | |
| static void UpdateCounter() { | |
| ++counter; | |
| if (counter == max_value) { | |
| throw std::logic_error("Fail"); | |
| } | |
| } | |
| bool operator==(const Int& rhs) const { | |
| return x == rhs.x; | |
| } | |
| }; | |
| int Int::counter = 0; | |
| int Int::max_value = 0; | |
| TEST_CASE("Copy predicate only") { | |
| Int::Reset(100); | |
| std::vector<Int> data{Int(1), Int(2), Int(3)}; | |
| auto expected = data; | |
| Int::Reset(3); | |
| REQUIRE_THROWS_AS(TransformIf(data.begin(), data.end(), | |
| [](const Int& val) { | |
| if (val.x == 3) { | |
| throw 42; | |
| } | |
| return val.x == 2; | |
| }, | |
| [](Int& val) { val.x += 2; }), | |
| int); | |
| REQUIRE(expected == data); | |
| } | |
| TEST_CASE("Stress") { | |
| std::deque<Int> data; | |
| const int len = 1e6; | |
| const int iterations = 1e4; | |
| for (int i = 0; i < len; ++i) { | |
| data.emplace_back(i); | |
| } | |
| const int max_count = 100; | |
| for (int i = 0; i < iterations; ++i) { | |
| Int::Reset(100500); | |
| auto predicate = [](const Int& val) { return val.x % 2 == 0; }; | |
| REQUIRE_THROWS_AS(TransformIf(data.begin(), data.end(), predicate, | |
| [](Int& val) { | |
| if (val.x++ == max_count) { | |
| throw std::runtime_error("Stop"); | |
| } | |
| }), | |
| std::runtime_error); | |
| for (int j = 0; j <= max_count; ++j) { | |
| REQUIRE(data[j].x == j); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment