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. 48 TEST(IsUint_IsInt) { 49 UintIntTest<uint32_t> test_little_values_unsigned[] = { 50 {true, true, 1, UINT32_C(0x0)}, 51 {true, false, 1, UINT32_C(0x1)}, 52 {false, false, 1, UINT32_C(0x2)}, 53 {false, false, 1, UINT32_C(0x3)}, 54 {false, false, 1, UINT32_C(0x4)}, 55 {false, false, 1, UINT32_C(0x5)}, 56 {false, false, 1, UINT32_C(0x6)}, 57 {false, false, 1, UINT32_C(0x7)}, 58 {false, false, 1, UINT32_C(0x8)}, 59 {false, false, 1, UINT32_C(0x9)}, 60 {false, false, 1, UINT32_C(0xa)}, 61 {false, false, 1, UINT32_C(0xb)}, 62 {false, false, 1, UINT32_C(0xc)}, 63 {false, false, 1, UINT32_C(0xd)}, 64 {false, false, 1, UINT32_C(0xe)}, 65 {false, false, 1, UINT32_C(0xf)}, 66 67 {true, true, 2, UINT32_C(0x0)}, 68 {true, true, 2, UINT32_C(0x1)}, 69 {true, false, 2, UINT32_C(0x2)}, 70 {true, false, 2, UINT32_C(0x3)}, 71 {false, false, 2, UINT32_C(0x4)}, 72 {false, false, 2, UINT32_C(0x5)}, 73 {false, false, 2, UINT32_C(0x6)}, 74 {false, false, 2, UINT32_C(0x7)}, 75 {false, false, 2, UINT32_C(0x8)}, 76 {false, false, 2, UINT32_C(0x9)}, 77 {false, false, 2, UINT32_C(0xa)}, 78 {false, false, 2, UINT32_C(0xb)}, 79 {false, false, 2, UINT32_C(0xc)}, 80 {false, false, 2, UINT32_C(0xd)}, 81 {false, false, 2, UINT32_C(0xe)}, 82 {false, false, 2, UINT32_C(0xf)}, 83 }; 84 85 UintIntTest<int32_t> test_little_values_signed[] = { 86 {true, true, 1, INT32_C(0)}, 87 {true, false, 1, INT32_C(1)}, 88 {false, false, 1, INT32_C(2)}, 89 {false, false, 1, INT32_C(3)}, 90 {false, false, 1, INT32_C(4)}, 91 {false, false, 1, INT32_C(5)}, 92 {false, false, 1, INT32_C(6)}, 93 {false, false, 1, INT32_C(7)}, 94 {false, true, 1, INT32_C(-1)}, 95 {false, false, 1, INT32_C(-2)}, 96 {false, false, 1, INT32_C(-3)}, 97 {false, false, 1, INT32_C(-4)}, 98 {false, false, 1, INT32_C(-5)}, 99 {false, false, 1, INT32_C(-6)}, 100 {false, false, 1, INT32_C(-7)}, 101 {false, false, 1, INT32_C(-8)}, 102 103 {true, true, 2, INT32_C(0)}, 104 {true, true, 2, INT32_C(1)}, 105 {true, false, 2, INT32_C(2)}, 106 {true, false, 2, INT32_C(3)}, 107 {false, false, 2, INT32_C(4)}, 108 {false, false, 2, INT32_C(5)}, 109 {false, false, 2, INT32_C(6)}, 110 {false, false, 2, INT32_C(7)}, 111 {false, true, 2, INT32_C(-1)}, 112 {false, true, 2, INT32_C(-2)}, 113 {false, false, 2, INT32_C(-3)}, 114 {false, false, 2, INT32_C(-4)}, 115 {false, false, 2, INT32_C(-5)}, 116 {false, false, 2, INT32_C(-6)}, 117 {false, false, 2, INT32_C(-7)}, 118 {false, false, 2, INT32_C(-8)}, 119 }; 120 121 UintIntTest<uint32_t> test_u16[] = { 122 {true, true, 16, UINT32_C(0x0)}, 123 {true, false, 16, UINT32_C(0xabcd)}, 124 {true, false, 16, UINT32_C(0x8000)}, 125 {true, false, 16, UINT32_C(0xffff)}, 126 {false, false, 16, UINT32_C(0x10000)}, 127 {false, false, 16, UINT32_C(0xffff0000)}, 128 {false, false, 16, UINT32_C(0xffff8000)}, 129 {false, false, 16, UINT32_C(0xffffffff)}, 130 }; 131 132 UintIntTest<int32_t> test_i16[] = { 133 {true, true, 16, INT32_C(0x0)}, 134 {true, false, 16, INT32_C(0xabcd)}, 135 {true, false, 16, INT32_C(0x8000)}, 136 {true, false, 16, INT32_C(0xffff)}, 137 {false, false, 16, INT32_C(0x10000)}, 138 {true, true, 16, INT32_C(42)}, 139 {false, true, 16, INT32_C(-42)}, 140 {false, true, 16, INT32_C(-1)}, 141 }; 142 143 UintIntTest<uint64_t> test_u32[] = { 144 {true, true, 32, UINT64_C(0x0)}, 145 {true, false, 32, UINT64_C(0xabcdabcd)}, 146 {true, false, 32, UINT64_C(0x80000000)}, 147 {true, false, 32, UINT64_C(0xffffffff)}, 148 }; 149 150 UintIntTest<int64_t> test_i32[] = { 151 {true, true, 32, INT64_C(0)}, 152 {true, true, 32, INT64_C(42)}, 153 {false, true, 32, INT64_C(-42)}, 154 {false, true, 32, INT64_C(-1)}, 155 {true, true, 32, INT64_C(2147483647)}, // (1 << (32 - 1)) - 1 156 {false, true, 32, INT64_C(-2147483648)}, // -(1 << (32 - 1)) 157 }; 158 159 UintIntTest<uint64_t> test_unsigned_higher_than_32[] = { 160 {false, false, 54, UINT64_C(0xabcdef9012345678)}, 161 {true, false, 33, UINT64_C(0x100000000)}, 162 {true, false, 62, UINT64_C(0x3fffffffffffffff)}, 163 {true, false, 63, UINT64_C(0x7fffffffffffffff)}, 164 }; 165 166 UintIntTest<int64_t> test_signed_higher_than_32[] = { 167 {true, true, 54, INT64_C(9007199254740991)}, // (1 << (54 - 1)) - 1 168 {true, false, 54, INT64_C(9007199254740992)}, // 1 << (54 - 1) 169 {true, true, 33, INT64_C(4294967295)}, // (1 << (33 - 1) - 1) 170 {false, true, 33, INT64_C(-4294967296)}, // -(1 << (33 - 1)) 171 }; 172 173 #define TEST_LIST(M) \ 174 M(test_little_values_unsigned) \ 175 M(test_little_values_signed) \ 176 M(test_u16) \ 177 M(test_i16) \ 178 M(test_u32) \ 179 M(test_i32) \ 180 M(test_unsigned_higher_than_32) \ 181 M(test_signed_higher_than_32) 182 183 184 #define TEST_UINT(test_vector) \ 185 for (unsigned i = 0; i < ARRAY_SIZE(test_vector); i++) { \ 186 if (test_vector[i].is_uintn) { \ 187 VIXL_CHECK(IsUintN(test_vector[i].n, test_vector[i].x)); \ 188 } else { \ 189 VIXL_CHECK(!IsUintN(test_vector[i].n, test_vector[i].x)); \ 190 } \ 191 } 192 193 #define TEST_INT(test_vector) \ 194 for (unsigned i = 0; i < ARRAY_SIZE(test_vector); i++) { \ 195 if (test_vector[i].is_intn) { \ 196 VIXL_CHECK(IsIntN(test_vector[i].n, test_vector[i].x)); \ 197 } else { \ 198 VIXL_CHECK(!IsIntN(test_vector[i].n, test_vector[i].x)); \ 199 } \ 200 } 201 202 TEST_LIST(TEST_UINT) 203 TEST_LIST(TEST_INT) 204 205 #undef TEST_UINT 206 #undef TEST_INT 207 208 #undef TEST_LIST 209 } 210 211 } // namespace vixl 212