xref: /aosp_15_r20/external/deqp/framework/common/tcuEither.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
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 
26 namespace tcu
27 {
28 namespace
29 {
30 
31 enum
32 {
33     COPYCHECK_VALUE = 1637423219
34 };
35 
36 class TestClassWithConstructor
37 {
38 public:
TestClassWithConstructor(int i)39     TestClassWithConstructor(int i) : m_i(i), m_copyCheck(COPYCHECK_VALUE)
40     {
41     }
42 
~TestClassWithConstructor(void)43     ~TestClassWithConstructor(void)
44     {
45         DE_TEST_ASSERT(m_copyCheck == COPYCHECK_VALUE);
46     }
47 
TestClassWithConstructor(const TestClassWithConstructor & other)48     TestClassWithConstructor(const TestClassWithConstructor &other) : m_i(other.m_i), m_copyCheck(other.m_copyCheck)
49     {
50     }
51 
operator =(const TestClassWithConstructor & other)52     TestClassWithConstructor &operator=(const TestClassWithConstructor &other)
53     {
54         TCU_CHECK(m_copyCheck == COPYCHECK_VALUE);
55 
56         if (this == &other)
57             return *this;
58 
59         m_i         = other.m_i;
60         m_copyCheck = other.m_copyCheck;
61 
62         TCU_CHECK(m_copyCheck == COPYCHECK_VALUE);
63 
64         return *this;
65     }
66 
getValue(void) const67     int getValue(void) const
68     {
69         TCU_CHECK(m_copyCheck == COPYCHECK_VALUE);
70 
71         return m_i;
72     }
73 
74 private:
75     int m_i;
76     int m_copyCheck;
77 };
78 
79 } // namespace
80 
Either_selfTest(void)81 void Either_selfTest(void)
82 {
83     // Simple test for first
84     {
85         const int intValue = 1503457782;
86         const Either<int, float> either(intValue);
87 
88         TCU_CHECK(either.isFirst());
89         TCU_CHECK(!either.isSecond());
90 
91         TCU_CHECK(either.is<int>());
92         TCU_CHECK(!either.is<float>());
93 
94         TCU_CHECK(either.getFirst() == intValue);
95         TCU_CHECK(either.get<int>() == intValue);
96     }
97 
98     // Simple test for second
99     {
100         const float floatValue = 0.43223332995f;
101         const Either<int, float> either(floatValue);
102 
103         TCU_CHECK(!either.isFirst());
104         TCU_CHECK(either.isSecond());
105 
106         TCU_CHECK(!either.is<int>());
107         TCU_CHECK(either.is<float>());
108 
109         TCU_CHECK(either.getSecond() == floatValue);
110         TCU_CHECK(either.get<float>() == floatValue);
111     }
112 
113     // Assign first value
114     {
115         const int intValue     = 1942092699;
116         const float floatValue = 0.43223332995f;
117         Either<int, float> either(floatValue);
118 
119         either = intValue;
120 
121         TCU_CHECK(either.isFirst());
122         TCU_CHECK(!either.isSecond());
123 
124         TCU_CHECK(either.is<int>());
125         TCU_CHECK(!either.is<float>());
126 
127         TCU_CHECK(either.getFirst() == intValue);
128         TCU_CHECK(either.get<int>() == intValue);
129     }
130 
131     // Assign second value
132     {
133         const int intValue     = 1942092699;
134         const float floatValue = 0.43223332995f;
135         Either<int, float> either(intValue);
136 
137         either = floatValue;
138 
139         TCU_CHECK(!either.isFirst());
140         TCU_CHECK(either.isSecond());
141 
142         TCU_CHECK(!either.is<int>());
143         TCU_CHECK(either.is<float>());
144 
145         TCU_CHECK(either.getSecond() == floatValue);
146         TCU_CHECK(either.get<float>() == floatValue);
147     }
148 
149     // Assign first either value
150     {
151         const int intValue     = 1942092699;
152         const float floatValue = 0.43223332995f;
153         Either<int, float> either(floatValue);
154         const Either<int, float> otherEither(intValue);
155 
156         either = otherEither;
157 
158         TCU_CHECK(either.isFirst());
159         TCU_CHECK(!either.isSecond());
160 
161         TCU_CHECK(either.is<int>());
162         TCU_CHECK(!either.is<float>());
163 
164         TCU_CHECK(either.getFirst() == intValue);
165         TCU_CHECK(either.get<int>() == intValue);
166     }
167 
168     // Assign second either value
169     {
170         const int intValue     = 1942092699;
171         const float floatValue = 0.43223332995f;
172         Either<int, float> either(intValue);
173         const Either<int, float> otherEither(floatValue);
174 
175         either = otherEither;
176 
177         TCU_CHECK(!either.isFirst());
178         TCU_CHECK(either.isSecond());
179 
180         TCU_CHECK(!either.is<int>());
181         TCU_CHECK(either.is<float>());
182 
183         TCU_CHECK(either.getSecond() == floatValue);
184         TCU_CHECK(either.get<float>() == floatValue);
185     }
186 
187     // Simple test for first with constructor
188     {
189         const TestClassWithConstructor testObject(171899615);
190         const Either<TestClassWithConstructor, int> either(testObject);
191 
192         TCU_CHECK(either.isFirst());
193         TCU_CHECK(!either.isSecond());
194 
195         TCU_CHECK(either.is<TestClassWithConstructor>());
196         TCU_CHECK(!either.is<int>());
197 
198         TCU_CHECK(either.getFirst().getValue() == testObject.getValue());
199         TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
200     }
201 
202     // Simple test for second with constructor
203     {
204         const TestClassWithConstructor testObject(171899615);
205         const Either<int, TestClassWithConstructor> either(testObject);
206 
207         TCU_CHECK(!either.isFirst());
208         TCU_CHECK(either.isSecond());
209 
210         TCU_CHECK(either.is<TestClassWithConstructor>());
211         TCU_CHECK(!either.is<int>());
212 
213         TCU_CHECK(either.getSecond().getValue() == testObject.getValue());
214         TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
215     }
216 
217     // Assign first with constructor
218     {
219         const int intValue = 1942092699;
220         const TestClassWithConstructor testObject(171899615);
221         Either<TestClassWithConstructor, int> either(intValue);
222 
223         either = testObject;
224 
225         TCU_CHECK(either.isFirst());
226         TCU_CHECK(!either.isSecond());
227 
228         TCU_CHECK(either.is<TestClassWithConstructor>());
229         TCU_CHECK(!either.is<int>());
230 
231         TCU_CHECK(either.getFirst().getValue() == testObject.getValue());
232         TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
233     }
234 
235     // Assign second with constructor
236     {
237         const int intValue = 1942092699;
238         const TestClassWithConstructor testObject(171899615);
239         Either<int, TestClassWithConstructor> either(intValue);
240 
241         either = testObject;
242 
243         TCU_CHECK(!either.isFirst());
244         TCU_CHECK(either.isSecond());
245 
246         TCU_CHECK(either.is<TestClassWithConstructor>());
247         TCU_CHECK(!either.is<int>());
248 
249         TCU_CHECK(either.getSecond().getValue() == testObject.getValue());
250         TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
251     }
252 
253     // Assign first either with constructor
254     {
255         const int intValue = 1942092699;
256         const TestClassWithConstructor testObject(171899615);
257         Either<TestClassWithConstructor, int> either(intValue);
258         const Either<TestClassWithConstructor, int> otherEither(testObject);
259 
260         either = otherEither;
261 
262         TCU_CHECK(either.isFirst());
263         TCU_CHECK(!either.isSecond());
264 
265         TCU_CHECK(either.is<TestClassWithConstructor>());
266         TCU_CHECK(!either.is<int>());
267 
268         TCU_CHECK(either.getFirst().getValue() == testObject.getValue());
269         TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
270     }
271 
272     // Assign second either with constructor
273     {
274         const int intValue = 1942092699;
275         const TestClassWithConstructor testObject(171899615);
276         Either<int, TestClassWithConstructor> either(intValue);
277         const Either<int, TestClassWithConstructor> otherEither(testObject);
278 
279         either = otherEither;
280 
281         TCU_CHECK(!either.isFirst());
282         TCU_CHECK(either.isSecond());
283 
284         TCU_CHECK(either.is<TestClassWithConstructor>());
285         TCU_CHECK(!either.is<int>());
286 
287         TCU_CHECK(either.getSecond().getValue() == testObject.getValue());
288         TCU_CHECK(either.get<TestClassWithConstructor>().getValue() == testObject.getValue());
289     }
290 }
291 
292 } // namespace tcu
293