| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // scope_guard_tc.h
- // scope_guard
- //
- // Created by Sam Jaffe on 1/10/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #include <memory>
- #include "scope_guard.hpp"
- class scope_guard_TestSuite : public CxxTest::TestSuite {
- public:
- using ptr = std::shared_ptr<int>;
- static ptr make( int i ) { return std::make_shared<int>(i); }
-
- #if __cplusplus > 201402L
- struct example {
- ~example() {
- scope(success) { ++(*_p); };
- }
- ptr _p;
- };
- #endif
- public:
- void test_scope_exit_reset() const {
- ptr p = make( 5 );
- {
- scope(exit) { p.reset(); };
- TS_ASSERT( p );
- }
- TS_ASSERT( ! p );
- }
-
- #if __cplusplus > 201402L
- void test_scope_failure_no_effect() const {
- ptr p = make( 5 );
- try {
- scope(failure) { p.reset(); };
- } catch (...) {
- }
- TS_ASSERT( p );
- }
- void test_scope_failure_on_exception() const {
- ptr p = make( 5 );
- try {
- scope(failure) { p.reset(); };
- throw 5;
- } catch (...) {
- }
- TS_ASSERT( ! p );
- }
-
- void test_scope_success_works() const {
- ptr p = make( 5 );
- try {
- scope(success) { p.reset(); };
- } catch (...) {
- }
- TS_ASSERT( ! p );
- }
-
- void test_scope_success_on_exception_no_effect() const {
- ptr p = make( 5 );
- try {
- scope(success) { p.reset(); };
- throw 5;
- } catch (...) {
- }
- TS_ASSERT( p );
- }
-
- void test_multi_layer_scoping() const {
- ptr p = make( 5 );
- ptr r = p;
- try {
- scope(failure) { p.reset(); };
- example ex { r };
- throw 5;
- } catch (...) {
- }
- TS_ASSERT( ! p );
- TS_ASSERT_EQUALS( *r, 6 );
- }
- #endif
- };
|