| 6 | #include "fl/stl/scope_exit.h" |
| 7 | |
| 8 | FL_TEST_FILE(FL_FILEPATH) { |
| 9 | |
| 10 | using namespace fl; |
| 11 | |
| 12 | FL_TEST_CASE("scope_exit - basic execution") { |
| 13 | int count = 0; |
| 14 | { |
| 15 | auto guard = make_scope_exit([&] { ++count; }); |
| 16 | FL_CHECK_EQ(count, 0); |
| 17 | } |
| 18 | FL_CHECK_EQ(count, 1); |
| 19 | } |
| 20 | |
| 21 | FL_TEST_CASE("scope_exit - release prevents execution") { |
| 22 | int count = 0; |
| 23 | { |
| 24 | auto guard = make_scope_exit([&] { ++count; }); |
| 25 | guard.release(); |
| 26 | } |
| 27 | FL_CHECK_EQ(count, 0); |
| 28 | } |
| 29 | |
| 30 | FL_TEST_CASE("scope_exit - move transfers ownership") { |
| 31 | int count = 0; |
| 32 | { |
| 33 | auto guard1 = make_scope_exit([&] { ++count; }); |
| 34 | auto guard2 = fl::move(guard1); |
| 35 | // guard1 moved-from — should not fire |
| 36 | } |
| 37 | // guard2 fires once |
| 38 | FL_CHECK_EQ(count, 1); |
| 39 | } |
| 40 | |
| 41 | FL_TEST_CASE("scope_exit - multiple guards run in reverse order") { |
| 42 | fl::string order; |
| 43 | { |
| 44 | auto g1 = make_scope_exit([&] { order += "1"; }); |
| 45 | auto g2 = make_scope_exit([&] { order += "2"; }); |
| 46 | auto g3 = make_scope_exit([&] { order += "3"; }); |
| 47 | } |
| 48 | // Destructors run in reverse declaration order |
| 49 | FL_CHECK_EQ(order, fl::string("321")); |
| 50 | } |
| 51 | |
| 52 | } // FL_TEST_FILE |
nothing calls this directly
no test coverage detected