1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "TriState.h"
17 #include <gtest/gtest.h>
18 
19 #include <cassert>
20 #include <cstdint>
21 #include <cstdio>
22 #include <iostream>
23 
24 using HidUtil::TriState;
25 typedef TriState<uint32_t> tri_uint32_t;
26 typedef TriState<int32_t> tri_int32_t;
27 typedef TriState<int16_t> tri_int16_t;
28 
TEST(TriStateTest,Constructor)29 TEST(TriStateTest, Constructor) {
30     tri_uint32_t a;
31     EXPECT_FALSE(a.isSet());
32 
33     a += 1;
34     EXPECT_FALSE(a.isSet());
35 
36     a -= 1;
37     EXPECT_FALSE(a.isSet());
38 
39     a *= 1;
40     EXPECT_FALSE(a.isSet());
41 
42     a /= 1;
43     EXPECT_FALSE(a.isSet());
44 
45     tri_uint32_t b;
46     b = a;
47     EXPECT_FALSE(b.isSet());
48 
49     a = 1;
50     EXPECT_TRUE(a.isSet());
51 
52     b = a;
53     EXPECT_TRUE(b.isSet());
54 
55     a.clear();
56     EXPECT_FALSE(a.isSet());
57     EXPECT_TRUE(b.isSet());
58 
59     tri_uint32_t c(b);
60     EXPECT_TRUE(c.isSet());
61 
62     c.clear();
63     EXPECT_FALSE(c.isSet());
64 
65     tri_uint32_t d(a);
66     EXPECT_FALSE(c.isSet());
67 }
68 
TEST(TriStateTest,IncAndDecOperation)69 TEST(TriStateTest, IncAndDecOperation) {
70     tri_int32_t a(1);
71 
72     EXPECT_EQ(2, (++a).get(0));
73     EXPECT_EQ(2, (a++).get(0));
74     EXPECT_EQ(3, a.get(0));
75 
76     EXPECT_EQ(2, (--a).get(0));
77     EXPECT_EQ(2, (a--).get(0));
78     EXPECT_EQ(1, a.get(0));
79 
80     tri_uint32_t b;
81     EXPECT_EQ(static_cast<uint32_t>(100), (++b).get(100));
82     EXPECT_EQ(static_cast<uint32_t>(101), (b++).get(101));
83     EXPECT_EQ(static_cast<uint32_t>(102), b.get(102));
84     EXPECT_FALSE(b.isSet());
85 
86     EXPECT_EQ(static_cast<uint32_t>(103), (--b).get(103));
87     EXPECT_EQ(static_cast<uint32_t>(104), (b--).get(104));
88     EXPECT_EQ(static_cast<uint32_t>(105), b.get(105));
89     EXPECT_FALSE(b.isSet());
90 }
91 
TEST(TriStateTest,Comparison)92 TEST(TriStateTest, Comparison) {
93     tri_int32_t a(1);
94     tri_int32_t b(1);
95     tri_int32_t c(2);
96     tri_int32_t d;
97 
98     EXPECT_EQ(a, b);
99     EXPECT_FALSE((a != b));
100     EXPECT_TRUE(!(a != b));
101     EXPECT_NE(-1, a);
102 
103     EXPECT_LT(a, c);
104     EXPECT_LT(a, 3);
105 
106     EXPECT_GT(c, b);
107     EXPECT_GT(c, 0);
108 
109     EXPECT_LE(a, 1);
110     EXPECT_LE(a, c);
111     EXPECT_LE(a, 3);
112     EXPECT_LE(a, b);
113 
114     EXPECT_GE(c, b);
115     EXPECT_GE(b, a);
116     EXPECT_GE(c, 0);
117     EXPECT_GE(c, 2);
118 
119     EXPECT_FALSE((a == d).isSet());
120     EXPECT_FALSE((a >= d).isSet());
121     EXPECT_FALSE((a <= d).isSet());
122     EXPECT_FALSE((a != d).isSet());
123     EXPECT_FALSE((a > d).isSet());
124     EXPECT_FALSE((a < d).isSet());
125 
126     //EXPECT_EQ(a, d); // will cause runtime failure
127     // due to converting a not-set TriState<bool> to bool
128 }
129 
TEST(TriStateTest,CompoundAssign)130 TEST(TriStateTest, CompoundAssign) {
131     tri_uint32_t x;
132 
133     x += 10;
134     EXPECT_FALSE(x.isSet());
135     x -= 10;
136     EXPECT_FALSE(x.isSet());
137     x *= 10;
138     EXPECT_FALSE(x.isSet());
139     x /= 10;
140     EXPECT_FALSE(x.isSet());
141     x &= 10;
142     EXPECT_FALSE(x.isSet());
143     x |= 10;
144     EXPECT_FALSE(x.isSet());
145     x %= 10;
146     EXPECT_FALSE(x.isSet());
147     x <<= 10;
148     EXPECT_FALSE(x.isSet());
149     x >>= 10;
150     EXPECT_FALSE(x.isSet());
151 
152     tri_int32_t y, z, w;
153 #define TEST_COMPOUND_ASSIGN(a, op, op_c, b) \
154     y = z = a; \
155     w = b; \
156     y op_c b; \
157     EXPECT_TRUE(y.isSet()); \
158     EXPECT_EQ(y, (a op b)); \
159     EXPECT_EQ(y, (z op b)); \
160     EXPECT_EQ(y, (a op w));
161 
162     TEST_COMPOUND_ASSIGN(123, +, +=, 456);
163     TEST_COMPOUND_ASSIGN(123, -, -=, 456);
164     TEST_COMPOUND_ASSIGN(123, *, *=, 456);
165     TEST_COMPOUND_ASSIGN(123, /, /=, 456);
166     TEST_COMPOUND_ASSIGN(123, |, |=, 456);
167     TEST_COMPOUND_ASSIGN(123, &, &=, 456);
168     TEST_COMPOUND_ASSIGN(123, ^, ^=, 456);
169     TEST_COMPOUND_ASSIGN(123, %, %=, 456);
170 #undef TEST_COMPOUND_ASSIGN
171     y = z = 123;
172     w = 10;
173     y <<= 10;
174     EXPECT_TRUE(y.isSet());
175     EXPECT_EQ(123 << 10, y);
176 
177     y = z = 12345;
178     w = 10;
179     y >>= 10;
180     EXPECT_TRUE(y.isSet());
181     EXPECT_EQ(12345 >> 10, y);
182 }
183 
TEST(TriStateTest,UnaryOperation)184 TEST(TriStateTest, UnaryOperation) {
185     tri_int16_t p;
186     EXPECT_FALSE((!p).isSet());
187     EXPECT_FALSE((-p).isSet());
188     EXPECT_FALSE((~p).isSet());
189 
190     tri_int16_t q(1234);
191     EXPECT_TRUE((!q).isSet());
192     EXPECT_EQ(!static_cast<int16_t>(1234), (!q));
193 
194     tri_int16_t r(1234);
195     EXPECT_TRUE((-r).isSet());
196     EXPECT_EQ(-1234, (-r));
197 
198     tri_int16_t s(1234);
199     EXPECT_TRUE((~s).isSet());
200     EXPECT_EQ(~static_cast<int16_t>(1234), ~s);
201 }
202 
203