aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/scope_ut.cpp
blob: 2df66fda57639921f9f16529e2e4dbed90f24b10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "scope.h"

#include <util/generic/ptr.h>
#include <library/cpp/testing/unittest/registar.h>

Y_UNIT_TEST_SUITE(ScopeToolsTest) {
    Y_UNIT_TEST(OnScopeExitTest) {
        int i = 0;

        {
            Y_SCOPE_EXIT(&i) {
                i = i * 2;
            };

            Y_SCOPE_EXIT(&i) {
                i = i + 1;
            };
        }

        UNIT_ASSERT_VALUES_EQUAL(2, i);
    }

    Y_UNIT_TEST(OnScopeExitMoveTest) {
        THolder<int> i{new int{10}};
        int p = 0;

        {
            Y_SCOPE_EXIT(i = std::move(i), &p) {
                p = *i * 2;
            };
        }

        UNIT_ASSERT_VALUES_EQUAL(20, p);
    }

    Y_UNIT_TEST(TestDefer) {
        int i = 0;

        {
            Y_DEFER {
                i = 20;
            };
        }

        UNIT_ASSERT_VALUES_EQUAL(i, 20);
    }
} // Y_UNIT_TEST_SUITE(ScopeToolsTest)