1 // Copyright 2016, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of ARM Limited nor the names of its contributors may be
13 //     used to endorse or promote products derived from this software without
14 //     specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "test-runner.h"
28 
29 #include "utils-vixl.h"
30 
31 #define TEST(name) TEST_(API_##name)
32 
33 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
34 
35 namespace vixl {
36 
37 // Describe the result of a test. Should IsUintN() and IsIntN() return true or
38 // false for N and X?
39 template <typename T>
40 struct UintIntTest {
41   bool is_uintn;
42   bool is_intn;
43   unsigned n;
44   T x;
45 };
46 
47 // Test IsUintN() and IsIntN() against various values and integral types.
TEST(IsUint_IsInt)48 TEST(IsUint_IsInt) {
49   UintIntTest<uint32_t> test_little_values_unsigned[] = {
50       {true, true, 1, UINT32_C(0x0)},   {true, false, 1, UINT32_C(0x1)},
51       {false, false, 1, UINT32_C(0x2)}, {false, false, 1, UINT32_C(0x3)},
52       {false, false, 1, UINT32_C(0x4)}, {false, false, 1, UINT32_C(0x5)},
53       {false, false, 1, UINT32_C(0x6)}, {false, false, 1, UINT32_C(0x7)},
54       {false, false, 1, UINT32_C(0x8)}, {false, false, 1, UINT32_C(0x9)},
55       {false, false, 1, UINT32_C(0xa)}, {false, false, 1, UINT32_C(0xb)},
56       {false, false, 1, UINT32_C(0xc)}, {false, false, 1, UINT32_C(0xd)},
57       {false, false, 1, UINT32_C(0xe)}, {false, false, 1, UINT32_C(0xf)},
58 
59       {true, true, 2, UINT32_C(0x0)},   {true, true, 2, UINT32_C(0x1)},
60       {true, false, 2, UINT32_C(0x2)},  {true, false, 2, UINT32_C(0x3)},
61       {false, false, 2, UINT32_C(0x4)}, {false, false, 2, UINT32_C(0x5)},
62       {false, false, 2, UINT32_C(0x6)}, {false, false, 2, UINT32_C(0x7)},
63       {false, false, 2, UINT32_C(0x8)}, {false, false, 2, UINT32_C(0x9)},
64       {false, false, 2, UINT32_C(0xa)}, {false, false, 2, UINT32_C(0xb)},
65       {false, false, 2, UINT32_C(0xc)}, {false, false, 2, UINT32_C(0xd)},
66       {false, false, 2, UINT32_C(0xe)}, {false, false, 2, UINT32_C(0xf)},
67   };
68 
69   UintIntTest<int32_t> test_little_values_signed[] = {
70       {true, true, 1, INT32_C(0)},    {true, false, 1, INT32_C(1)},
71       {false, false, 1, INT32_C(2)},  {false, false, 1, INT32_C(3)},
72       {false, false, 1, INT32_C(4)},  {false, false, 1, INT32_C(5)},
73       {false, false, 1, INT32_C(6)},  {false, false, 1, INT32_C(7)},
74       {false, true, 1, INT32_C(-1)},  {false, false, 1, INT32_C(-2)},
75       {false, false, 1, INT32_C(-3)}, {false, false, 1, INT32_C(-4)},
76       {false, false, 1, INT32_C(-5)}, {false, false, 1, INT32_C(-6)},
77       {false, false, 1, INT32_C(-7)}, {false, false, 1, INT32_C(-8)},
78 
79       {true, true, 2, INT32_C(0)},    {true, true, 2, INT32_C(1)},
80       {true, false, 2, INT32_C(2)},   {true, false, 2, INT32_C(3)},
81       {false, false, 2, INT32_C(4)},  {false, false, 2, INT32_C(5)},
82       {false, false, 2, INT32_C(6)},  {false, false, 2, INT32_C(7)},
83       {false, true, 2, INT32_C(-1)},  {false, true, 2, INT32_C(-2)},
84       {false, false, 2, INT32_C(-3)}, {false, false, 2, INT32_C(-4)},
85       {false, false, 2, INT32_C(-5)}, {false, false, 2, INT32_C(-6)},
86       {false, false, 2, INT32_C(-7)}, {false, false, 2, INT32_C(-8)},
87   };
88 
89   UintIntTest<uint32_t> test_u16[] = {
90       {true, true, 16, UINT32_C(0x0)},
91       {true, false, 16, UINT32_C(0xabcd)},
92       {true, false, 16, UINT32_C(0x8000)},
93       {true, false, 16, UINT32_C(0xffff)},
94       {false, false, 16, UINT32_C(0x10000)},
95       {false, false, 16, UINT32_C(0xffff0000)},
96       {false, false, 16, UINT32_C(0xffff8000)},
97       {false, false, 16, UINT32_C(0xffffffff)},
98   };
99 
100   UintIntTest<int32_t> test_i16[] = {
101       {true, true, 16, INT32_C(0x0)},
102       {true, false, 16, INT32_C(0xabcd)},
103       {true, false, 16, INT32_C(0x8000)},
104       {true, false, 16, INT32_C(0xffff)},
105       {false, false, 16, INT32_C(0x10000)},
106       {true, true, 16, INT32_C(42)},
107       {false, true, 16, INT32_C(-42)},
108       {false, true, 16, INT32_C(-1)},
109   };
110 
111   UintIntTest<uint64_t> test_u32[] = {
112       {true, true, 32, UINT64_C(0x0)},
113       {true, false, 32, UINT64_C(0xabcdabcd)},
114       {true, false, 32, UINT64_C(0x80000000)},
115       {true, false, 32, UINT64_C(0xffffffff)},
116   };
117 
118   UintIntTest<int64_t> test_i32[] = {
119       {true, true, 32, INT64_C(0)},
120       {true, true, 32, INT64_C(42)},
121       {false, true, 32, INT64_C(-42)},
122       {false, true, 32, INT64_C(-1)},
123       {true, true, 32, INT64_C(2147483647)},    // (1 << (32 - 1)) - 1
124       {false, true, 32, INT64_C(-2147483648)},  // -(1 << (32 - 1))
125   };
126 
127   UintIntTest<uint64_t> test_unsigned_higher_than_32[] = {
128       {false, false, 54, UINT64_C(0xabcdef9012345678)},
129       {true, false, 33, UINT64_C(0x100000000)},
130       {true, false, 62, UINT64_C(0x3fffffffffffffff)},
131       {true, false, 63, UINT64_C(0x7fffffffffffffff)},
132   };
133 
134   UintIntTest<int64_t> test_signed_higher_than_32[] = {
135       {true, true, 54, INT64_C(9007199254740991)},   // (1 << (54 - 1)) - 1
136       {true, false, 54, INT64_C(9007199254740992)},  // 1 << (54 - 1)
137       {true, true, 33, INT64_C(4294967295)},         // (1 << (33 - 1) - 1)
138       {false, true, 33, INT64_C(-4294967296)},       // -(1 << (33 - 1))
139   };
140 
141 #define TEST_LIST(M)              \
142   M(test_little_values_unsigned)  \
143   M(test_little_values_signed)    \
144   M(test_u16)                     \
145   M(test_i16)                     \
146   M(test_u32)                     \
147   M(test_i32)                     \
148   M(test_unsigned_higher_than_32) \
149   M(test_signed_higher_than_32)
150 
151 
152 #define TEST_UINT(test_vector)                                  \
153   for (unsigned i = 0; i < ARRAY_SIZE(test_vector); i++) {      \
154     if (test_vector[i].is_uintn) {                              \
155       VIXL_CHECK(IsUintN(test_vector[i].n, test_vector[i].x));  \
156     } else {                                                    \
157       VIXL_CHECK(!IsUintN(test_vector[i].n, test_vector[i].x)); \
158     }                                                           \
159   }
160 
161 #define TEST_INT(test_vector)                                  \
162   for (unsigned i = 0; i < ARRAY_SIZE(test_vector); i++) {     \
163     if (test_vector[i].is_intn) {                              \
164       VIXL_CHECK(IsIntN(test_vector[i].n, test_vector[i].x));  \
165     } else {                                                   \
166       VIXL_CHECK(!IsIntN(test_vector[i].n, test_vector[i].x)); \
167     }                                                          \
168   }
169 
170   TEST_LIST(TEST_UINT)
171   TEST_LIST(TEST_INT)
172 
173 #undef TEST_UINT
174 #undef TEST_INT
175 
176 #undef TEST_LIST
177 }
178 
179 }  // namespace vixl
180