-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraii_test.cpp
58 lines (45 loc) · 1.17 KB
/
raii_test.cpp
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
48
49
50
51
52
53
54
55
56
57
58
#include "raii.hpp"
// std
#include <iostream>
// local
#include "simple_unit_test.hpp"
namespace util::test {
class A
{
public:
A() { ++static_count; }
~A() { --static_count; }
static int static_count;
};
int A::static_count = 0;
}
int main(int argc, char* argv[])
{
using namespace util;
using namespace util::test;
// check we can tidyup
{
auto a = new A{};
{
RAII< A* > guard( a, [](auto a){ delete a; });
ASSERT_EQUAL( a, guard.managed() );
ASSERT_EQUAL( A::static_count, 1 );
}
ASSERT_EQUAL( A::static_count, 0 );
}
// check we can move
{
auto a = new A{};
{
RAII< A* > guard( a, [](auto a){ delete a; });
ASSERT_EQUAL( a, guard.managed() );
ASSERT_EQUAL( A::static_count, 1 );
auto moved_guard{ std::move(guard) };
ASSERT_EQUAL( a, moved_guard.managed() );
ASSERT_EQUAL( (A*)nullptr, guard.managed() );
ASSERT_EQUAL( A::static_count, 1 );
}
ASSERT_EQUAL( A::static_count, 0 );
}
return failed_tests;
}