tcuEither.cpp revision f824ffe1538233bb05416cb5c7bd7167756c7314
1/*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
4 *
5 * Copyright 2015 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Template class that is either type of Left or Right.
22 *//*--------------------------------------------------------------------*/
23
24#include "tcuEither.hpp"
25
26namespace tcu
27{
28namespace
29{
30
31enum
32{
33	COPYCHECK_VALUE = 1637423219
34};
35
36class TestClassWithConstructor
37{
38public:
39	TestClassWithConstructor (int i)
40		: m_i			(i)
41		, m_copyCheck	(COPYCHECK_VALUE)
42	{
43	}
44
45	~TestClassWithConstructor (void)
46	{
47		TCU_CHECK(m_copyCheck == COPYCHECK_VALUE);
48	}
49
50	TestClassWithConstructor (const TestClassWithConstructor& other)
51		: m_i			(other.m_i)
52		, m_copyCheck	(other.m_copyCheck)
53	{
54	}
55
56	TestClassWithConstructor& operator= (const TestClassWithConstructor& other)
57	{
58		TCU_CHECK(m_copyCheck == COPYCHECK_VALUE);
59
60		if (this == &other)
61			return *this;
62
63		m_i = other.m_i;
64		m_copyCheck = other.m_copyCheck;
65
66		TCU_CHECK(m_copyCheck == COPYCHECK_VALUE);
67
68		return *this;
69	}
70
71	int getValue (void) const
72	{
73		TCU_CHECK(m_copyCheck == COPYCHECK_VALUE);
74
75		return m_i;
76	}
77
78private:
79	int m_i;
80	int m_copyCheck;
81};
82
83} // anonymous
84
85void Either_selfTest (void)
86{
87	// Simple test for first
88	{
89		const int					intValue	= 1503457782;
90		const Either<int, float>	either		(intValue);
91
92		TCU_CHECK(either.isFirst());
93		TCU_CHECK(!either.isSecond());
94
95		TCU_CHECK(either.is<int>());
96		TCU_CHECK(!either.is<float>());
97
98		TCU_CHECK(either.getFirst() == intValue);
99		TCU_CHECK(either.get<int>() == intValue);
100	}
101
102	// Simple test for second
103	{
104		const float					floatValue	= 0.43223332995f;
105		const Either<int, float>	either		(floatValue);
106
107		TCU_CHECK(!either.isFirst());
108		TCU_CHECK(either.isSecond());
109
110		TCU_CHECK(!either.is<int>());
111		TCU_CHECK(either.is<float>());
112
113		TCU_CHECK(either.getSecond() == floatValue);
114		TCU_CHECK(either.get<float>() == floatValue);
115	}
116
117	// Assign first value
118	{
119		const int			intValue	= 1942092699;
120		const float			floatValue	= 0.43223332995f;
121		Either<int, float>	either		(floatValue);
122
123		either = intValue;
124
125		TCU_CHECK(either.isFirst());
126		TCU_CHECK(!either.isSecond());
127
128		TCU_CHECK(either.is<int>());
129		TCU_CHECK(!either.is<float>());
130
131		TCU_CHECK(either.getFirst() == intValue);
132		TCU_CHECK(either.get<int>() == intValue);
133	}
134
135	// Assign second value
136	{
137		const int			intValue	= 1942092699;
138		const float			floatValue	= 0.43223332995f;
139		Either<int, float>	either		(intValue);
140
141		either = floatValue;
142
143		TCU_CHECK(!either.isFirst());
144		TCU_CHECK(either.isSecond());
145
146		TCU_CHECK(!either.is<int>());
147		TCU_CHECK(either.is<float>());
148
149		TCU_CHECK(either.getSecond() == floatValue);
150		TCU_CHECK(either.get<float>() == floatValue);
151	}
152
153	// Assign first either value
154	{
155		const int					intValue	= 1942092699;
156		const float					floatValue	= 0.43223332995f;
157		Either<int, float>			either		(floatValue);
158		const Either<int, float>	otherEither	(intValue);
159
160		either = otherEither;
161
162		TCU_CHECK(either.isFirst());
163		TCU_CHECK(!either.isSecond());
164
165		TCU_CHECK(either.is<int>());
166		TCU_CHECK(!either.is<float>());
167
168		TCU_CHECK(either.getFirst() == intValue);
169		TCU_CHECK(either.get<int>() == intValue);
170	}
171
172	// Assign second either value
173	{
174		const int					intValue	= 1942092699;
175		const float					floatValue	= 0.43223332995f;
176		Either<int, float>			either		(intValue);
177		const Either<int, float>	otherEither	(floatValue);
178
179		either = otherEither;
180
181		TCU_CHECK(!either.isFirst());
182		TCU_CHECK(either.isSecond());
183
184		TCU_CHECK(!either.is<int>());
185		TCU_CHECK(either.is<float>());
186
187		TCU_CHECK(either.getSecond() == floatValue);
188		TCU_CHECK(either.get<float>() == floatValue);
189	}
190
191	// Simple test for first with constructor
192	{
193		const TestClassWithConstructor				testObject	(171899615);
194		const Either<TestClassWithConstructor, int>	either		(testObject);
195
196		TCU_CHECK(either.isFirst());
197		TCU_CHECK(!either.isSecond());
198
199		TCU_CHECK(either.is<TestClassWithConstructor>());
200		TCU_CHECK(!either.is<int>());
201
202		TCU_CHECK(either.getFirst().getValue() == testObject.getValue());
203		TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
204	}
205
206	// Simple test for second with constructor
207	{
208		const TestClassWithConstructor				testObject	(171899615);
209		const Either<int, TestClassWithConstructor>	either		(testObject);
210
211		TCU_CHECK(!either.isFirst());
212		TCU_CHECK(either.isSecond());
213
214		TCU_CHECK(either.is<TestClassWithConstructor>());
215		TCU_CHECK(!either.is<int>());
216
217		TCU_CHECK(either.getSecond().getValue() == testObject.getValue());
218		TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
219	}
220
221	// Assign first with constructor
222	{
223		const int								intValue	= 1942092699;
224		const TestClassWithConstructor			testObject	(171899615);
225		Either<TestClassWithConstructor, int>	either		(intValue);
226
227		either = testObject;
228
229		TCU_CHECK(either.isFirst());
230		TCU_CHECK(!either.isSecond());
231
232		TCU_CHECK(either.is<TestClassWithConstructor>());
233		TCU_CHECK(!either.is<int>());
234
235		TCU_CHECK(either.getFirst().getValue() == testObject.getValue());
236		TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
237	}
238
239	// Assign second with constructor
240	{
241		const int								intValue	= 1942092699;
242		const TestClassWithConstructor			testObject	(171899615);
243		Either<int, TestClassWithConstructor>	either		(intValue);
244
245		either = testObject;
246
247		TCU_CHECK(!either.isFirst());
248		TCU_CHECK(either.isSecond());
249
250		TCU_CHECK(either.is<TestClassWithConstructor>());
251		TCU_CHECK(!either.is<int>());
252
253		TCU_CHECK(either.getSecond().getValue() == testObject.getValue());
254		TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
255	}
256
257	// Assign first either with constructor
258	{
259		const int									intValue	= 1942092699;
260		const TestClassWithConstructor				testObject	(171899615);
261		Either<TestClassWithConstructor, int>		either		(intValue);
262		const Either<TestClassWithConstructor, int>	otherEither	(testObject);
263
264		either = otherEither;
265
266		TCU_CHECK(either.isFirst());
267		TCU_CHECK(!either.isSecond());
268
269		TCU_CHECK(either.is<TestClassWithConstructor>());
270		TCU_CHECK(!either.is<int>());
271
272		TCU_CHECK(either.getFirst().getValue() == testObject.getValue());
273		TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
274	}
275
276	// Assign second either with constructor
277	{
278		const int									intValue	= 1942092699;
279		const TestClassWithConstructor				testObject	(171899615);
280		Either<int, TestClassWithConstructor>		either		(intValue);
281		const Either<int, TestClassWithConstructor>	otherEither	(testObject);
282
283
284		either = testObject;
285
286		TCU_CHECK(!either.isFirst());
287		TCU_CHECK(either.isSecond());
288
289		TCU_CHECK(either.is<TestClassWithConstructor>());
290		TCU_CHECK(!either.is<int>());
291
292		TCU_CHECK(either.getSecond().getValue() == testObject.getValue());
293		TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
294	}
295}
296
297} // tcu
298