-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathauto_release_tests.cpp
More file actions
124 lines (92 loc) · 2.12 KB
/
Copy pathauto_release_tests.cpp
File metadata and controls
124 lines (92 loc) · 2.12 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
#include "core/auto_release.h"
namespace
{
using AutoIntPtr = iris::AutoRelease<int *, nullptr>;
void deleter(int *value)
{
--*value;
}
}
TEST(auto_release, ctor)
{
int x = 1;
AutoIntPtr v{&x, deleter};
ASSERT_EQ(v.get(), &x);
ASSERT_EQ(static_cast<int *>(v), &x);
ASSERT_TRUE(v);
}
TEST(auto_release, function_deleter)
{
int x = 1;
{
AutoIntPtr v{&x, deleter};
}
ASSERT_EQ(x, 0);
}
TEST(auto_release, lambda_deleter)
{
int x = 1;
int y = 2;
{
AutoIntPtr v{&x, [y](int *p) { *p += y; }};
}
ASSERT_EQ(x, 3);
}
TEST(auto_release, invalid)
{
AutoIntPtr v{nullptr, deleter};
ASSERT_EQ(v.get(), nullptr);
ASSERT_FALSE(v);
}
TEST(auto_release, move_ctor)
{
int x = 1;
{
AutoIntPtr v1{&x, deleter};
AutoIntPtr v2{std::move(v1)};
ASSERT_FALSE(v1);
ASSERT_EQ(v1.get(), nullptr);
ASSERT_TRUE(v2);
ASSERT_EQ(v2.get(), &x);
}
ASSERT_EQ(x, 0);
}
TEST(auto_release, move_assignment)
{
int x = 1;
int y = 1;
{
AutoIntPtr v1{&x, deleter};
AutoIntPtr v2{&y, deleter};
v2 = std::move(v1);
ASSERT_FALSE(v1);
ASSERT_EQ(v1.get(), nullptr);
ASSERT_TRUE(v2);
ASSERT_EQ(v2.get(), &x);
}
ASSERT_EQ(x, 0);
ASSERT_EQ(y, 0);
}
TEST(auto_release, address)
{
int x = 1;
{
AutoIntPtr v{nullptr, deleter};
const auto setter = [&x](int **p) { *p = std::addressof(x); };
setter(&v);
}
ASSERT_EQ(x, 0);
}
TEST(auto_release, pointer)
{
std::string str;
iris::AutoRelease<std::string *, nullptr> v{&str, nullptr};
v->push_back('c');
ASSERT_EQ(str, "c");
}