1 /*
2  * Copyright (C) 2018 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 "dex/primitive.h"
18 
19 #include "gtest/gtest.h"
20 
21 namespace art {
22 
23 namespace {
24 
CheckPrimitiveTypeWidensTo(Primitive::Type from,const std::vector<Primitive::Type> & expected_to_types)25 void CheckPrimitiveTypeWidensTo(Primitive::Type from,
26                                 const std::vector<Primitive::Type>& expected_to_types) {
27   std::vector<Primitive::Type> actual_to_types;
28   int last = static_cast<int>(Primitive::Type::kPrimLast);
29   for (int i = 0; i <= last; ++i) {
30     Primitive::Type to = static_cast<Primitive::Type>(i);
31     if (Primitive::IsWidenable(from, to)) {
32       actual_to_types.push_back(to);
33     }
34   }
35   EXPECT_EQ(expected_to_types, actual_to_types);
36 }
37 
38 }  // namespace
39 
TEST(PrimitiveTest,NotWidensTo)40 TEST(PrimitiveTest, NotWidensTo) {
41   const std::vector<Primitive::Type> to_types = {};
42   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimNot, to_types);
43 }
44 
TEST(PrimitiveTest,BooleanWidensTo)45 TEST(PrimitiveTest, BooleanWidensTo) {
46   const std::vector<Primitive::Type> to_types = {};
47   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimBoolean, to_types);
48 }
49 
TEST(PrimitiveTest,ByteWidensTo)50 TEST(PrimitiveTest, ByteWidensTo) {
51   const std::vector<Primitive::Type> to_types = {
52     Primitive::Type::kPrimByte,
53     Primitive::Type::kPrimShort,
54     Primitive::Type::kPrimInt,
55     Primitive::Type::kPrimLong,
56     Primitive::Type::kPrimFloat,
57     Primitive::Type::kPrimDouble,
58   };
59   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimByte, to_types);
60 }
61 
TEST(PrimitiveTest,CharWidensTo)62 TEST(PrimitiveTest, CharWidensTo) {
63   const std::vector<Primitive::Type> to_types = {
64     Primitive::Type::kPrimChar,
65     Primitive::Type::kPrimInt,
66     Primitive::Type::kPrimLong,
67     Primitive::Type::kPrimFloat,
68     Primitive::Type::kPrimDouble,
69   };
70   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimChar, to_types);
71 }
72 
TEST(PrimitiveTest,ShortWidensTo)73 TEST(PrimitiveTest, ShortWidensTo) {
74   const std::vector<Primitive::Type> to_types = {
75     Primitive::Type::kPrimShort,
76     Primitive::Type::kPrimInt,
77     Primitive::Type::kPrimLong,
78     Primitive::Type::kPrimFloat,
79     Primitive::Type::kPrimDouble,
80   };
81   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimShort, to_types);
82 }
83 
TEST(PrimitiveTest,IntWidensTo)84 TEST(PrimitiveTest, IntWidensTo) {
85   const std::vector<Primitive::Type> to_types = {
86     Primitive::Type::kPrimInt,
87     Primitive::Type::kPrimLong,
88     Primitive::Type::kPrimFloat,
89     Primitive::Type::kPrimDouble,
90   };
91   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimInt, to_types);
92 }
93 
TEST(PrimitiveTest,LongWidensTo)94 TEST(PrimitiveTest, LongWidensTo) {
95   const std::vector<Primitive::Type> to_types = {
96     Primitive::Type::kPrimLong,
97     Primitive::Type::kPrimFloat,
98     Primitive::Type::kPrimDouble,
99   };
100   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimLong, to_types);
101 }
102 
TEST(PrimitiveTest,FloatWidensTo)103 TEST(PrimitiveTest, FloatWidensTo) {
104   const std::vector<Primitive::Type> to_types = {
105     Primitive::Type::kPrimFloat,
106     Primitive::Type::kPrimDouble,
107   };
108   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimFloat, to_types);
109 }
110 
TEST(PrimitiveTest,DoubleWidensTo)111 TEST(PrimitiveTest, DoubleWidensTo) {
112   const std::vector<Primitive::Type> to_types = {
113     Primitive::Type::kPrimDouble,
114   };
115   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimDouble, to_types);
116 }
117 
TEST(PrimitiveTest,VoidWidensTo)118 TEST(PrimitiveTest, VoidWidensTo) {
119   const std::vector<Primitive::Type> to_types = {};
120   CheckPrimitiveTypeWidensTo(Primitive::Type::kPrimVoid, to_types);
121 }
122 
123 }  // namespace art
124