1
+ #include " Common.h"
2
+ #include < set>
3
+
4
+ namespace Testing
5
+ {
6
+ TEST_CLASS ( Guard )
7
+ {
8
+ public:
9
+ TEST_METHOD_INITIALIZE ( ClassInitialize )
10
+ {
11
+ _closed.clear ();
12
+ }
13
+
14
+ TEST_METHOD ( ValidHandle )
15
+ {
16
+ {
17
+ auto h = test_handle ( testValue );
18
+ AssertEx::IsTrue ( h.valid () );
19
+ AssertEx::IsTrue ( bool ( h ) );
20
+ AssertEx::AreEqual ( testValue, h.get () );
21
+ AssertEx::IsTrue ( h == testValue );
22
+ AssertEx::IsTrue ( h < testValue + 1 );
23
+ }
24
+
25
+ AssertEx::IsTrue ( was_closed ( testValue ) );
26
+ }
27
+
28
+ TEST_METHOD ( InvalidHandle )
29
+ {
30
+ {
31
+ auto h = test_handle ( -testValue );
32
+ AssertEx::IsFalse ( h.valid () );
33
+ AssertEx::IsFalse ( bool ( h ) );
34
+ AssertEx::AreEqual ( -testValue, h.get () );
35
+ }
36
+
37
+ AssertEx::IsTrue ( nothing_closed () );
38
+ }
39
+
40
+ TEST_METHOD ( Release )
41
+ {
42
+ {
43
+ auto h = test_handle ( testValue );
44
+ const auto v = h.release ();
45
+ AssertEx::AreEqual ( testValue, v );
46
+ }
47
+
48
+ AssertEx::IsTrue ( nothing_closed () );
49
+ }
50
+
51
+ TEST_METHOD ( Reset )
52
+ {
53
+ {
54
+ auto h = test_handle ( 0 );
55
+
56
+ h.reset ( testValue );
57
+ AssertEx::IsTrue ( nothing_closed () );
58
+
59
+ h.reset ( testValue );
60
+ AssertEx::IsTrue ( nothing_closed () );
61
+
62
+ h.reset ( testValue + 5 );
63
+ }
64
+
65
+ AssertEx::IsTrue ( was_closed ( testValue + 5 ) );
66
+ }
67
+
68
+ TEST_METHOD ( Assign )
69
+ {
70
+ {
71
+ auto h = test_handle ( testValue );
72
+ h = testValue + 5 ;
73
+ AssertEx::IsTrue ( was_closed ( testValue ) );
74
+ }
75
+
76
+ AssertEx::IsTrue ( was_closed ( testValue + 5 ) );
77
+ }
78
+
79
+ TEST_METHOD ( Move )
80
+ {
81
+ {
82
+ auto h1 = test_handle ( testValue );
83
+ auto h2 = test_handle ( testValue + 5 );
84
+
85
+ h1 = std::move ( h1 );
86
+ AssertEx::IsTrue ( nothing_closed () );
87
+
88
+ h1 = std::move ( h2 );
89
+ }
90
+
91
+ AssertEx::IsTrue ( was_closed ( testValue ) );
92
+ AssertEx::IsTrue ( was_closed ( testValue + 5 ) );
93
+ }
94
+
95
+ TEST_METHOD ( GetAddress )
96
+ {
97
+ {
98
+ auto h = test_handle ( testValue );
99
+ []( int * target, int value ) { *target = value; }( &h, testValue + 5 );
100
+ }
101
+
102
+ AssertEx::IsFalse ( was_closed ( testValue ) );
103
+ AssertEx::IsTrue ( was_closed ( testValue + 5 ) );
104
+ }
105
+
106
+ TEST_METHOD ( CurrentProcess )
107
+ {
108
+ {
109
+ test_handle_process h;
110
+ AssertEx::IsFalse ( bool ( h ) );
111
+ AssertEx::IsFalse ( h.valid () );
112
+
113
+ h = static_cast <int >(reinterpret_cast <intptr_t >(GetCurrentProcess ()));
114
+ AssertEx::IsTrue ( bool ( h ) );
115
+ AssertEx::IsTrue ( h.valid () );
116
+ }
117
+
118
+ AssertEx::IsTrue ( nothing_closed () );
119
+ }
120
+
121
+ TEST_METHOD ( ValidProcess )
122
+ {
123
+ {
124
+ auto h = test_handle_process ( testValue );
125
+ AssertEx::IsTrue ( bool ( h ) );
126
+ AssertEx::IsTrue ( h.valid () );
127
+ }
128
+
129
+ AssertEx::IsTrue ( was_closed ( testValue ) );
130
+ }
131
+
132
+ TEST_METHOD ( InvalidProcess )
133
+ {
134
+ {
135
+ auto h = test_handle_process ( -testValue );
136
+ AssertEx::IsFalse ( bool ( h ) );
137
+ AssertEx::IsFalse ( h.valid () );
138
+ }
139
+
140
+ AssertEx::IsTrue ( nothing_closed () );
141
+ }
142
+
143
+ TEST_METHOD ( InvalidFile )
144
+ {
145
+ {
146
+ auto h = HandleGuard<int , &Guard::close>( static_cast <int >(reinterpret_cast <intptr_t >(INVALID_HANDLE_VALUE)) );
147
+ AssertEx::IsFalse ( bool ( h ) );
148
+ AssertEx::IsFalse ( h.valid () );
149
+ }
150
+
151
+ AssertEx::IsTrue ( nothing_closed () );
152
+ }
153
+
154
+ private:
155
+ static void close ( int value )
156
+ {
157
+ _closed.emplace ( value );
158
+ }
159
+
160
+ static bool was_closed ( int value )
161
+ {
162
+ return _closed.find ( value ) != _closed.end ();
163
+ }
164
+
165
+ static bool nothing_closed ()
166
+ {
167
+ return _closed.empty ();
168
+ }
169
+
170
+ private:
171
+ using test_handle = HandleGuard<int , &Guard::close>;
172
+ using test_handle_process = HandleGuard<int , &Guard::close, with_pseudo<non_negative>::type>;
173
+
174
+ static constexpr int testValue = 10 ;
175
+ static inline std::set<int > _closed;
176
+ };
177
+ }
0 commit comments