1 /*
2  * Copyright 2019 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 
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include "Scheduler/StrongTyping.h"
20 
21 using namespace testing;
22 
23 namespace android {
24 
TEST(StrongTypeTest,comparison)25 TEST(StrongTypeTest, comparison) {
26     using SpunkyType = StrongTyping<int, struct SpunkyTypeTag, Compare>;
27     SpunkyType f1(10);
28 
29     EXPECT_TRUE(f1 == f1);
30     EXPECT_TRUE(SpunkyType(10) != SpunkyType(11));
31     EXPECT_FALSE(SpunkyType(31) != SpunkyType(31));
32 
33     EXPECT_TRUE(SpunkyType(10) < SpunkyType(11));
34     EXPECT_TRUE(SpunkyType(-1) < SpunkyType(0));
35     EXPECT_FALSE(SpunkyType(-10) < SpunkyType(-20));
36 
37     EXPECT_TRUE(SpunkyType(10) <= SpunkyType(11));
38     EXPECT_TRUE(SpunkyType(10) <= SpunkyType(10));
39     EXPECT_TRUE(SpunkyType(-10) <= SpunkyType(1));
40     EXPECT_FALSE(SpunkyType(10) <= SpunkyType(9));
41 
42     EXPECT_TRUE(SpunkyType(11) >= SpunkyType(11));
43     EXPECT_TRUE(SpunkyType(12) >= SpunkyType(11));
44     EXPECT_FALSE(SpunkyType(11) >= SpunkyType(12));
45 
46     EXPECT_FALSE(SpunkyType(11) > SpunkyType(12));
47     EXPECT_TRUE(SpunkyType(-11) < SpunkyType(7));
48 }
49 
TEST(StrongTypeTest,addition)50 TEST(StrongTypeTest, addition) {
51     using FunkyType = StrongTyping<int, struct FunkyTypeTag, Compare, Add>;
52     FunkyType f2(22);
53     FunkyType f1(10);
54 
55     EXPECT_THAT(f1 + f2, Eq(FunkyType(32)));
56     EXPECT_THAT(f2 + f1, Eq(FunkyType(32)));
57 
58     EXPECT_THAT(++f1.value(), Eq(11));
59     EXPECT_THAT(f1.value(), Eq(11));
60     EXPECT_THAT(f1++.value(), Eq(11));
61     EXPECT_THAT(f1++.value(), Eq(12));
62     EXPECT_THAT(f1.value(), Eq(13));
63 
64     auto f3 = f1;
65     EXPECT_THAT(f1, Eq(f3));
66     EXPECT_THAT(f1, Lt(f2));
67 
68     f3 += f1;
69     EXPECT_THAT(f1.value(), Eq(13));
70     EXPECT_THAT(f3.value(), Eq(26));
71 }
72 } // namespace android
73