OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/intermediate_language.h" | 5 #include "vm/intermediate_language.h" |
6 #include "vm/unit_test.h" | 6 #include "vm/unit_test.h" |
7 | 7 |
8 namespace dart { | 8 namespace dart { |
9 | 9 |
10 TEST_CASE(InstructionTests) { | 10 TEST_CASE(InstructionTests) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 RangeBoundary::FromConstant(0)); | 46 RangeBoundary::FromConstant(0)); |
47 Range* positive = new Range( | 47 Range* positive = new Range( |
48 RangeBoundary::FromConstant(0), | 48 RangeBoundary::FromConstant(0), |
49 RangeBoundary::FromConstant(100)); | 49 RangeBoundary::FromConstant(100)); |
50 Range* negative = new Range( | 50 Range* negative = new Range( |
51 RangeBoundary::FromConstant(-1), | 51 RangeBoundary::FromConstant(-1), |
52 RangeBoundary::FromConstant(-100)); | 52 RangeBoundary::FromConstant(-100)); |
53 Range* range_x = new Range( | 53 Range* range_x = new Range( |
54 RangeBoundary::FromConstant(-15), | 54 RangeBoundary::FromConstant(-15), |
55 RangeBoundary::FromConstant(100)); | 55 RangeBoundary::FromConstant(100)); |
56 EXPECT(negative->IsNegative()); | |
57 EXPECT(positive->IsPositive()); | 56 EXPECT(positive->IsPositive()); |
58 EXPECT(zero->Overlaps(0, 0)); | 57 EXPECT(zero->Overlaps(0, 0)); |
59 EXPECT(positive->Overlaps(0, 0)); | 58 EXPECT(positive->Overlaps(0, 0)); |
60 EXPECT(!negative->Overlaps(0, 0)); | 59 EXPECT(!negative->Overlaps(0, 0)); |
61 EXPECT(range_x->Overlaps(0, 0)); | 60 EXPECT(range_x->Overlaps(0, 0)); |
62 EXPECT(range_x->IsWithin(-15, 100)); | 61 EXPECT(range_x->IsWithin(-15, 100)); |
63 EXPECT(!range_x->IsWithin(-15, 99)); | 62 EXPECT(!range_x->IsWithin(-15, 99)); |
64 EXPECT(!range_x->IsWithin(-14, 100)); | 63 EXPECT(!range_x->IsWithin(-14, 100)); |
65 | 64 |
66 #define TEST_RANGE_OP(Op, l_min, l_max, r_min, r_max, result_min, result_max) \ | 65 #define TEST_RANGE_OP(Op, l_min, l_max, r_min, r_max, result_min, result_max) \ |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 RangeBoundary(0), RangeBoundary::PositiveInfinity()); | 111 RangeBoundary(0), RangeBoundary::PositiveInfinity()); |
113 TEST_RANGE_OP(Range::Shl, -100, 0, 0, 64, | 112 TEST_RANGE_OP(Range::Shl, -100, 0, 0, 64, |
114 RangeBoundary::NegativeInfinity(), RangeBoundary(0)); | 113 RangeBoundary::NegativeInfinity(), RangeBoundary(0)); |
115 | 114 |
116 #undef TEST_RANGE_OP | 115 #undef TEST_RANGE_OP |
117 } | 116 } |
118 | 117 |
119 | 118 |
120 TEST_CASE(RangeTestsInfinity) { | 119 TEST_CASE(RangeTestsInfinity) { |
121 // +/- inf overflowed. | 120 // +/- inf overflowed. |
122 EXPECT(RangeBoundary::NegativeInfinity().Overflowed()); | 121 EXPECT(RangeBoundary::NegativeInfinity().OverflowedSmi()); |
123 EXPECT(RangeBoundary::PositiveInfinity().Overflowed()); | 122 EXPECT(RangeBoundary::PositiveInfinity().OverflowedSmi()); |
| 123 |
| 124 EXPECT(RangeBoundary::NegativeInfinity().OverflowedMint()); |
| 125 EXPECT(RangeBoundary::PositiveInfinity().OverflowedMint()); |
124 | 126 |
125 Range* all = new Range(RangeBoundary::NegativeInfinity(), | 127 Range* all = new Range(RangeBoundary::NegativeInfinity(), |
126 RangeBoundary::PositiveInfinity()); | 128 RangeBoundary::PositiveInfinity()); |
127 EXPECT(all->Overlaps(0, 0)); | 129 EXPECT(all->Overlaps(0, 0)); |
128 EXPECT(all->Overlaps(-1, 1)); | 130 EXPECT(all->Overlaps(-1, 1)); |
129 EXPECT(!all->IsWithin(0, 100)); | 131 EXPECT(!all->IsWithin(0, 100)); |
130 Range* positive = new Range(RangeBoundary::FromConstant(0), | 132 Range* positive = new Range(RangeBoundary::FromConstant(0), |
131 RangeBoundary::PositiveInfinity()); | 133 RangeBoundary::PositiveInfinity()); |
132 EXPECT(positive->IsPositive()); | 134 EXPECT(positive->IsPositive()); |
133 EXPECT(!positive->IsNegative()); | |
134 EXPECT(positive->Overlaps(0, 1)); | 135 EXPECT(positive->Overlaps(0, 1)); |
135 EXPECT(positive->Overlaps(1, 100)); | 136 EXPECT(positive->Overlaps(1, 100)); |
136 EXPECT(positive->Overlaps(-1, 0)); | 137 EXPECT(positive->Overlaps(-1, 0)); |
137 EXPECT(!positive->Overlaps(-2, -1)); | 138 EXPECT(!positive->Overlaps(-2, -1)); |
138 Range* negative = new Range(RangeBoundary::NegativeInfinity(), | 139 Range* negative = new Range(RangeBoundary::NegativeInfinity(), |
139 RangeBoundary::FromConstant(-1)); | 140 RangeBoundary::FromConstant(-1)); |
140 EXPECT(negative->IsNegative()); | |
141 EXPECT(!negative->IsPositive()); | 141 EXPECT(!negative->IsPositive()); |
142 EXPECT(!negative->Overlaps(0, 1)); | 142 EXPECT(!negative->Overlaps(0, 1)); |
143 EXPECT(!negative->Overlaps(1, 100)); | 143 EXPECT(!negative->Overlaps(1, 100)); |
144 EXPECT(negative->Overlaps(-1, 0)); | 144 EXPECT(negative->Overlaps(-1, 0)); |
145 EXPECT(negative->Overlaps(-2, -1)); | 145 EXPECT(negative->Overlaps(-2, -1)); |
146 Range* negpos = new Range(RangeBoundary::NegativeInfinity(), | 146 Range* negpos = new Range(RangeBoundary::NegativeInfinity(), |
147 RangeBoundary::FromConstant(0)); | 147 RangeBoundary::FromConstant(0)); |
148 EXPECT(!negpos->IsNegative()); | |
149 EXPECT(!negpos->IsPositive()); | 148 EXPECT(!negpos->IsPositive()); |
150 | 149 |
151 Range* a = new Range(RangeBoundary::NegativeInfinity(), | 150 Range* a = new Range(RangeBoundary::NegativeInfinity(), |
152 RangeBoundary::FromConstant(1)); | 151 RangeBoundary::FromConstant(1)); |
153 | 152 |
154 Range* b = new Range(RangeBoundary::NegativeInfinity(), | 153 Range* b = new Range(RangeBoundary::NegativeInfinity(), |
155 RangeBoundary::FromConstant(31)); | 154 RangeBoundary::FromConstant(31)); |
156 | 155 |
157 Range* c = new Range(RangeBoundary::NegativeInfinity(), | 156 Range* c = new Range(RangeBoundary::NegativeInfinity(), |
158 RangeBoundary::FromConstant(32)); | 157 RangeBoundary::FromConstant(32)); |
159 | 158 |
160 EXPECT(a->OnlyLessThanOrEqualTo(31)); | 159 EXPECT(a->OnlyLessThanOrEqualTo(31)); |
161 EXPECT(b->OnlyLessThanOrEqualTo(31)); | 160 EXPECT(b->OnlyLessThanOrEqualTo(31)); |
162 EXPECT(!c->OnlyLessThanOrEqualTo(31)); | 161 EXPECT(!c->OnlyLessThanOrEqualTo(31)); |
163 | 162 |
164 Range* unsatisfiable = new Range(RangeBoundary::PositiveInfinity(), | 163 Range* unsatisfiable = new Range(RangeBoundary::PositiveInfinity(), |
165 RangeBoundary::NegativeInfinity()); | 164 RangeBoundary::NegativeInfinity()); |
166 EXPECT(unsatisfiable->IsUnsatisfiable()); | 165 EXPECT(unsatisfiable->IsUnsatisfiable()); |
167 | 166 |
168 Range* unsatisfiable_right = new Range(RangeBoundary::PositiveInfinity(), | 167 Range* unsatisfiable_right = new Range(RangeBoundary::PositiveInfinity(), |
169 RangeBoundary::FromConstant(0)); | 168 RangeBoundary::FromConstant(0)); |
170 EXPECT(unsatisfiable_right->IsUnsatisfiable()); | 169 EXPECT(unsatisfiable_right->IsUnsatisfiable()); |
171 | 170 |
172 Range* unsatisfiable_left = new Range(RangeBoundary::FromConstant(0), | 171 Range* unsatisfiable_left = new Range(RangeBoundary::FromConstant(0), |
173 RangeBoundary::NegativeInfinity()); | 172 RangeBoundary::NegativeInfinity()); |
174 EXPECT(unsatisfiable_left->IsUnsatisfiable()); | 173 EXPECT(unsatisfiable_left->IsUnsatisfiable()); |
175 } | 174 } |
176 | 175 |
| 176 |
| 177 TEST_CASE(RangeUtils) { |
| 178 // [-inf, +inf]. |
| 179 const Range& range_0 = *(new Range(RangeBoundary::NegativeInfinity(), |
| 180 RangeBoundary::PositiveInfinity())); |
| 181 // [-inf, -1]. |
| 182 const Range& range_a = *(new Range(RangeBoundary::NegativeInfinity(), |
| 183 RangeBoundary::FromConstant(-1))); |
| 184 // [-inf, 0]. |
| 185 const Range& range_b = *(new Range(RangeBoundary::NegativeInfinity(), |
| 186 RangeBoundary::FromConstant(0))); |
| 187 // [-inf, 1]. |
| 188 const Range& range_c = *(new Range(RangeBoundary::NegativeInfinity(), |
| 189 RangeBoundary::FromConstant(1))); |
| 190 // [-1, +inf] |
| 191 const Range& range_d = *(new Range(RangeBoundary::FromConstant(-1), |
| 192 RangeBoundary::PositiveInfinity())); |
| 193 // [0, +inf] |
| 194 const Range& range_e = *(new Range(RangeBoundary::FromConstant(0), |
| 195 RangeBoundary::PositiveInfinity())); |
| 196 // [1, +inf]. |
| 197 const Range& range_f = *(new Range(RangeBoundary::FromConstant(1), |
| 198 RangeBoundary::PositiveInfinity())); |
| 199 // [1, 2]. |
| 200 const Range& range_g = *(new Range(RangeBoundary::FromConstant(1), |
| 201 RangeBoundary::FromConstant(2))); |
| 202 // [-1, -2]. |
| 203 const Range& range_h = *(new Range(RangeBoundary::FromConstant(-1), |
| 204 RangeBoundary::FromConstant(-2))); |
| 205 // [-1, 1]. |
| 206 const Range& range_i = *(new Range(RangeBoundary::FromConstant(-1), |
| 207 RangeBoundary::FromConstant(1))); |
| 208 |
| 209 // OnlyPositiveOrZero. |
| 210 EXPECT(!Range::OnlyPositiveOrZero(range_a, range_b)); |
| 211 EXPECT(!Range::OnlyPositiveOrZero(range_b, range_c)); |
| 212 EXPECT(!Range::OnlyPositiveOrZero(range_c, range_d)); |
| 213 EXPECT(!Range::OnlyPositiveOrZero(range_d, range_e)); |
| 214 EXPECT(Range::OnlyPositiveOrZero(range_e, range_f)); |
| 215 EXPECT(!Range::OnlyPositiveOrZero(range_d, range_d)); |
| 216 EXPECT(Range::OnlyPositiveOrZero(range_e, range_e)); |
| 217 EXPECT(Range::OnlyPositiveOrZero(range_f, range_g)); |
| 218 EXPECT(!Range::OnlyPositiveOrZero(range_g, range_h)); |
| 219 EXPECT(!Range::OnlyPositiveOrZero(range_i, range_i)); |
| 220 |
| 221 // OnlyNegativeOrZero. |
| 222 EXPECT(Range::OnlyNegativeOrZero(range_a, range_b)); |
| 223 EXPECT(!Range::OnlyNegativeOrZero(range_b, range_c)); |
| 224 EXPECT(Range::OnlyNegativeOrZero(range_b, range_b)); |
| 225 EXPECT(!Range::OnlyNegativeOrZero(range_c, range_c)); |
| 226 EXPECT(!Range::OnlyNegativeOrZero(range_c, range_d)); |
| 227 EXPECT(!Range::OnlyNegativeOrZero(range_d, range_e)); |
| 228 EXPECT(!Range::OnlyNegativeOrZero(range_e, range_f)); |
| 229 EXPECT(!Range::OnlyNegativeOrZero(range_f, range_g)); |
| 230 EXPECT(!Range::OnlyNegativeOrZero(range_g, range_h)); |
| 231 EXPECT(Range::OnlyNegativeOrZero(range_h, range_h)); |
| 232 EXPECT(!Range::OnlyNegativeOrZero(range_i, range_i)); |
| 233 |
| 234 // [-inf, +inf]. |
| 235 EXPECT(!Range::OnlyNegativeOrZero(range_0, range_0)); |
| 236 EXPECT(!Range::OnlyPositiveOrZero(range_0, range_0)); |
| 237 |
| 238 EXPECT(Range::ConstantAbsMax(&range_0) == RangeBoundary::kMax); |
| 239 EXPECT(Range::ConstantAbsMax(&range_h) == 2); |
| 240 EXPECT(Range::ConstantAbsMax(&range_i) == 1); |
| 241 |
| 242 // RangeBOundary.Equals. |
| 243 EXPECT(RangeBoundary::FromConstant(1).Equals( |
| 244 RangeBoundary::FromConstant(1))); |
| 245 EXPECT(!RangeBoundary::FromConstant(2).Equals( |
| 246 RangeBoundary::FromConstant(1))); |
| 247 EXPECT(RangeBoundary::PositiveInfinity().Equals( |
| 248 RangeBoundary::PositiveInfinity())); |
| 249 EXPECT(!RangeBoundary::PositiveInfinity().Equals( |
| 250 RangeBoundary::NegativeInfinity())); |
| 251 EXPECT(RangeBoundary::NegativeInfinity().Equals( |
| 252 RangeBoundary::NegativeInfinity())); |
| 253 EXPECT(!RangeBoundary::NegativeInfinity().Equals( |
| 254 RangeBoundary::PositiveInfinity())); |
| 255 EXPECT(!RangeBoundary::FromConstant(1).Equals( |
| 256 RangeBoundary::NegativeInfinity())); |
| 257 EXPECT(!RangeBoundary::FromConstant(1).Equals( |
| 258 RangeBoundary::NegativeInfinity())); |
| 259 EXPECT(!RangeBoundary::FromConstant(2).Equals( |
| 260 RangeBoundary::PositiveInfinity())); |
| 261 } |
| 262 |
| 263 |
| 264 TEST_CASE(RangeBinaryOp) { |
| 265 Range* range_a = new Range(RangeBoundary::FromConstant(-1), |
| 266 RangeBoundary::PositiveInfinity()); |
| 267 range_a->Clamp(RangeBoundary::kRangeBoundaryInt64); |
| 268 EXPECT(range_a->min().ConstantValue() == -1); |
| 269 EXPECT(range_a->max().ConstantValue() == RangeBoundary::kMax); |
| 270 Range* range_b = new Range(RangeBoundary::NegativeInfinity(), |
| 271 RangeBoundary::FromConstant(1)); |
| 272 range_b->Clamp(RangeBoundary::kRangeBoundaryInt64); |
| 273 EXPECT(range_b->min().ConstantValue() == RangeBoundary::kMin); |
| 274 EXPECT(range_b->max().ConstantValue() == 1); |
| 275 Range* result = Range::BinaryOp(Token::kADD, |
| 276 range_a, |
| 277 range_b, |
| 278 NULL); |
| 279 ASSERT(result != NULL); |
| 280 EXPECT(result->min().IsNegativeInfinity()); |
| 281 EXPECT(result->max().IsPositiveInfinity()); |
| 282 |
| 283 // Test that [5, 10] + [0, 5] = [5, 15]. |
| 284 Range* range_c = new Range(RangeBoundary::FromConstant(5), |
| 285 RangeBoundary::FromConstant(10)); |
| 286 Range* range_d = new Range(RangeBoundary::FromConstant(0), |
| 287 RangeBoundary::FromConstant(5)); |
| 288 result = Range::BinaryOp(Token::kADD, |
| 289 range_c, |
| 290 range_d, |
| 291 NULL); |
| 292 ASSERT(result != NULL); |
| 293 EXPECT(result->min().ConstantValue() == 5); |
| 294 EXPECT(result->max().ConstantValue() == 15); |
| 295 |
| 296 |
| 297 // Test that [0xff, 0xfff] & [0xf, 0xf] = [0x0, 0xf]. |
| 298 Range* range_e = new Range(RangeBoundary::FromConstant(0xff), |
| 299 RangeBoundary::FromConstant(0xfff)); |
| 300 Range* range_f = new Range(RangeBoundary::FromConstant(0xf), |
| 301 RangeBoundary::FromConstant(0xf)); |
| 302 result = Range::BinaryOp(Token::kBIT_AND, |
| 303 range_e, |
| 304 range_f, |
| 305 NULL); |
| 306 ASSERT(result != NULL); |
| 307 EXPECT(result->min().ConstantValue() == 0x0); |
| 308 EXPECT(result->max().ConstantValue() == 0xf); |
| 309 } |
| 310 |
| 311 |
| 312 TEST_CASE(RangeAdd) { |
| 313 #define TEST_RANGE_ADD(l_min, l_max, r_min, r_max, result_min, result_max) \ |
| 314 { \ |
| 315 RangeBoundary min, max; \ |
| 316 Range* left_range = new Range( \ |
| 317 RangeBoundary::FromConstant(l_min), \ |
| 318 RangeBoundary::FromConstant(l_max)); \ |
| 319 Range* right_range = new Range( \ |
| 320 RangeBoundary::FromConstant(r_min), \ |
| 321 RangeBoundary::FromConstant(r_max)); \ |
| 322 EXPECT(left_range->min().ConstantValue() == l_min); \ |
| 323 EXPECT(left_range->max().ConstantValue() == l_max); \ |
| 324 EXPECT(right_range->min().ConstantValue() == r_min); \ |
| 325 EXPECT(right_range->max().ConstantValue() == r_max); \ |
| 326 Range::Add(left_range, right_range, &min, &max, NULL); \ |
| 327 EXPECT(min.Equals(result_min)); \ |
| 328 if (!min.Equals(result_min)) { \ |
| 329 OS::Print("%s != %s\n", min.ToCString(), result_min.ToCString()); \ |
| 330 } \ |
| 331 EXPECT(max.Equals(result_max)); \ |
| 332 if (!max.Equals(result_max)) { \ |
| 333 OS::Print("%s != %s\n", max.ToCString(), result_max.ToCString()); \ |
| 334 } \ |
| 335 } |
| 336 |
| 337 // [kMaxInt32, kMaxInt32 + 15] + [10, 20] = [kMaxInt32 + 10, kMaxInt32 + 35]. |
| 338 TEST_RANGE_ADD(static_cast<int64_t>(kMaxInt32), |
| 339 static_cast<int64_t>(kMaxInt32) + 15, |
| 340 static_cast<int64_t>(10), |
| 341 static_cast<int64_t>(20), |
| 342 RangeBoundary(static_cast<int64_t>(kMaxInt32) + 10), |
| 343 RangeBoundary(static_cast<int64_t>(kMaxInt32) + 35)); |
| 344 |
| 345 // [kMaxInt32 - 15, kMaxInt32 + 15] + [15, -15] = [kMaxInt32, kMaxInt32]. |
| 346 TEST_RANGE_ADD(static_cast<int64_t>(kMaxInt32) - 15, |
| 347 static_cast<int64_t>(kMaxInt32) + 15, |
| 348 static_cast<int64_t>(15), |
| 349 static_cast<int64_t>(-15), |
| 350 RangeBoundary(static_cast<int64_t>(kMaxInt32)), |
| 351 RangeBoundary(static_cast<int64_t>(kMaxInt32))); |
| 352 |
| 353 // [kMaxInt32, kMaxInt32 + 15] + [10, kMaxInt64] = [kMaxInt32 + 10, +inf]. |
| 354 TEST_RANGE_ADD(static_cast<int64_t>(kMaxInt32), |
| 355 static_cast<int64_t>(kMaxInt32) + 15, |
| 356 static_cast<int64_t>(10), |
| 357 static_cast<int64_t>(kMaxInt64), |
| 358 RangeBoundary(static_cast<int64_t>(kMaxInt32) + 10), |
| 359 RangeBoundary::PositiveInfinity()); |
| 360 |
| 361 // [kMinInt64, kMaxInt32 + 15] + [10, 20] = [kMinInt64 + 10, kMaxInt32 + 35]. |
| 362 TEST_RANGE_ADD(static_cast<int64_t>(kMinInt64), |
| 363 static_cast<int64_t>(kMaxInt32) + 15, |
| 364 static_cast<int64_t>(10), |
| 365 static_cast<int64_t>(20), |
| 366 RangeBoundary(static_cast<int64_t>(kMinInt64) + 10), |
| 367 RangeBoundary(static_cast<int64_t>(kMaxInt32) + 35)); |
| 368 |
| 369 // [0, 0] + [kMinInt64, kMaxInt64] = [kMinInt64, kMaxInt64]. |
| 370 TEST_RANGE_ADD(static_cast<int64_t>(0), |
| 371 static_cast<int64_t>(0), |
| 372 static_cast<int64_t>(kMinInt64), |
| 373 static_cast<int64_t>(kMaxInt64), |
| 374 RangeBoundary(kMinInt64), |
| 375 RangeBoundary(kMaxInt64)); |
| 376 |
| 377 // Overflows. |
| 378 |
| 379 // [-1, 1] + [kMinInt64, kMaxInt64] = [-inf, +inf]. |
| 380 TEST_RANGE_ADD(static_cast<int64_t>(-1), |
| 381 static_cast<int64_t>(1), |
| 382 static_cast<int64_t>(kMinInt64), |
| 383 static_cast<int64_t>(kMaxInt64), |
| 384 RangeBoundary::NegativeInfinity(), |
| 385 RangeBoundary::PositiveInfinity()); |
| 386 |
| 387 // [kMaxInt64, kMaxInt64] + [kMaxInt64, kMaxInt64] = [-inf, +inf]. |
| 388 TEST_RANGE_ADD(static_cast<int64_t>(kMaxInt64), |
| 389 static_cast<int64_t>(kMaxInt64), |
| 390 static_cast<int64_t>(kMaxInt64), |
| 391 static_cast<int64_t>(kMaxInt64), |
| 392 RangeBoundary::NegativeInfinity(), |
| 393 RangeBoundary::PositiveInfinity()); |
| 394 |
| 395 // [kMaxInt64, kMaxInt64] + [1, 1] = [-inf, +inf]. |
| 396 TEST_RANGE_ADD(static_cast<int64_t>(kMaxInt64), |
| 397 static_cast<int64_t>(kMaxInt64), |
| 398 static_cast<int64_t>(1), |
| 399 static_cast<int64_t>(1), |
| 400 RangeBoundary::NegativeInfinity(), |
| 401 RangeBoundary::PositiveInfinity()); |
| 402 |
| 403 #undef TEST_RANGE_ADD |
| 404 } |
| 405 |
| 406 |
| 407 TEST_CASE(RangeSub) { |
| 408 #define TEST_RANGE_SUB(l_min, l_max, r_min, r_max, result_min, result_max) \ |
| 409 { \ |
| 410 RangeBoundary min, max; \ |
| 411 Range* left_range = new Range( \ |
| 412 RangeBoundary::FromConstant(l_min), \ |
| 413 RangeBoundary::FromConstant(l_max)); \ |
| 414 Range* right_range = new Range( \ |
| 415 RangeBoundary::FromConstant(r_min), \ |
| 416 RangeBoundary::FromConstant(r_max)); \ |
| 417 EXPECT(left_range->min().ConstantValue() == l_min); \ |
| 418 EXPECT(left_range->max().ConstantValue() == l_max); \ |
| 419 EXPECT(right_range->min().ConstantValue() == r_min); \ |
| 420 EXPECT(right_range->max().ConstantValue() == r_max); \ |
| 421 Range::Sub(left_range, right_range, &min, &max, NULL); \ |
| 422 EXPECT(min.Equals(result_min)); \ |
| 423 if (!min.Equals(result_min)) { \ |
| 424 OS::Print("%s != %s\n", min.ToCString(), result_min.ToCString()); \ |
| 425 } \ |
| 426 EXPECT(max.Equals(result_max)); \ |
| 427 if (!max.Equals(result_max)) { \ |
| 428 OS::Print("%s != %s\n", max.ToCString(), result_max.ToCString()); \ |
| 429 } \ |
| 430 } |
| 431 |
| 432 // [kMaxInt32, kMaxInt32 + 15] - [10, 20] = [kMaxInt32 - 20, kMaxInt32 + 5]. |
| 433 TEST_RANGE_SUB(static_cast<int64_t>(kMaxInt32), |
| 434 static_cast<int64_t>(kMaxInt32) + 15, |
| 435 static_cast<int64_t>(10), |
| 436 static_cast<int64_t>(20), |
| 437 RangeBoundary(static_cast<int64_t>(kMaxInt32) - 20), |
| 438 RangeBoundary(static_cast<int64_t>(kMaxInt32) + 5)); |
| 439 |
| 440 // [kMintInt64, kMintInt64] - [1, 1] = [-inf, +inf]. |
| 441 TEST_RANGE_SUB(static_cast<int64_t>(kMinInt64), |
| 442 static_cast<int64_t>(kMinInt64), |
| 443 static_cast<int64_t>(1), |
| 444 static_cast<int64_t>(1), |
| 445 RangeBoundary::NegativeInfinity(), |
| 446 RangeBoundary::PositiveInfinity()); |
| 447 |
| 448 // [1, 1] - [kMintInt64, kMintInt64] = [-inf, +inf]. |
| 449 TEST_RANGE_SUB(static_cast<int64_t>(1), |
| 450 static_cast<int64_t>(1), |
| 451 static_cast<int64_t>(kMinInt64), |
| 452 static_cast<int64_t>(kMinInt64), |
| 453 RangeBoundary::NegativeInfinity(), |
| 454 RangeBoundary::PositiveInfinity()); |
| 455 |
| 456 // [kMaxInt32 + 10, kMaxInt32 + 20] - [-20, -20] = |
| 457 // [kMaxInt32 + 30, kMaxInt32 + 40]. |
| 458 TEST_RANGE_SUB(static_cast<int64_t>(kMaxInt32) + 10, |
| 459 static_cast<int64_t>(kMaxInt32) + 20, |
| 460 static_cast<int64_t>(-20), |
| 461 static_cast<int64_t>(-20), |
| 462 RangeBoundary(static_cast<int64_t>(kMaxInt32) + 30), |
| 463 RangeBoundary(static_cast<int64_t>(kMaxInt32) + 40)); |
| 464 |
| 465 |
| 466 #undef TEST_RANGE_SUB |
| 467 } |
| 468 |
| 469 |
| 470 TEST_CASE(RangeAnd) { |
| 471 #define TEST_RANGE_AND(l_min, l_max, r_min, r_max, result_min, result_max) \ |
| 472 { \ |
| 473 RangeBoundary min, max; \ |
| 474 Range* left_range = new Range( \ |
| 475 RangeBoundary::FromConstant(l_min), \ |
| 476 RangeBoundary::FromConstant(l_max)); \ |
| 477 Range* right_range = new Range( \ |
| 478 RangeBoundary::FromConstant(r_min), \ |
| 479 RangeBoundary::FromConstant(r_max)); \ |
| 480 EXPECT(left_range->min().ConstantValue() == l_min); \ |
| 481 EXPECT(left_range->max().ConstantValue() == l_max); \ |
| 482 EXPECT(right_range->min().ConstantValue() == r_min); \ |
| 483 EXPECT(right_range->max().ConstantValue() == r_max); \ |
| 484 Range::And(left_range, right_range, &min, &max); \ |
| 485 EXPECT(min.Equals(result_min)); \ |
| 486 if (!min.Equals(result_min)) { \ |
| 487 OS::Print("%s != %s\n", min.ToCString(), result_min.ToCString()); \ |
| 488 } \ |
| 489 EXPECT(max.Equals(result_max)); \ |
| 490 if (!max.Equals(result_max)) { \ |
| 491 OS::Print("%s != %s\n", max.ToCString(), result_max.ToCString()); \ |
| 492 } \ |
| 493 } |
| 494 |
| 495 // [0xff, 0xfff] & [0xf, 0xf] = [0x0, 0xf]. |
| 496 TEST_RANGE_AND(static_cast<int64_t>(0xff), |
| 497 static_cast<int64_t>(0xfff), |
| 498 static_cast<int64_t>(0xf), |
| 499 static_cast<int64_t>(0xf), |
| 500 RangeBoundary(0), |
| 501 RangeBoundary(0xf)); |
| 502 |
| 503 // [0xffffffff, 0xffffffff] & [0xfffffffff, 0xfffffffff] = [0x0, 0xfffffffff]. |
| 504 TEST_RANGE_AND(static_cast<int64_t>(0xffffffff), |
| 505 static_cast<int64_t>(0xffffffff), |
| 506 static_cast<int64_t>(0xfffffffff), |
| 507 static_cast<int64_t>(0xfffffffff), |
| 508 RangeBoundary(0), |
| 509 RangeBoundary(static_cast<int64_t>(0xfffffffff))); |
| 510 |
| 511 // [0xffffffff, 0xffffffff] & [-20, 20] = [0x0, 0xffffffff]. |
| 512 TEST_RANGE_AND(static_cast<int64_t>(0xffffffff), |
| 513 static_cast<int64_t>(0xffffffff), |
| 514 static_cast<int64_t>(-20), |
| 515 static_cast<int64_t>(20), |
| 516 RangeBoundary(0), |
| 517 RangeBoundary(static_cast<int64_t>(0xffffffff))); |
| 518 |
| 519 // [-20, 20] & [0xffffffff, 0xffffffff] = [0x0, 0xffffffff]. |
| 520 TEST_RANGE_AND(static_cast<int64_t>(-20), |
| 521 static_cast<int64_t>(20), |
| 522 static_cast<int64_t>(0xffffffff), |
| 523 static_cast<int64_t>(0xffffffff), |
| 524 RangeBoundary(0), |
| 525 RangeBoundary(static_cast<int64_t>(0xffffffff))); |
| 526 |
| 527 // Test that [-20, 20] & [-20, 20] = [Unknown, Unknown]. |
| 528 TEST_RANGE_AND(static_cast<int64_t>(-20), |
| 529 static_cast<int64_t>(20), |
| 530 static_cast<int64_t>(-20), |
| 531 static_cast<int64_t>(20), |
| 532 RangeBoundary(), |
| 533 RangeBoundary()); |
| 534 |
| 535 #undef TEST_RANGE_AND |
| 536 } |
| 537 |
| 538 |
| 539 TEST_CASE(RangeMinMax) { |
| 540 // Constants. |
| 541 // MIN(0, 1) == 0 |
| 542 EXPECT(RangeBoundary::Min( |
| 543 RangeBoundary::FromConstant(0), |
| 544 RangeBoundary::FromConstant(1), |
| 545 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 0); |
| 546 // MIN(0, -1) == -1 |
| 547 EXPECT(RangeBoundary::Min( |
| 548 RangeBoundary::FromConstant(0), |
| 549 RangeBoundary::FromConstant(-1), |
| 550 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == -1); |
| 551 |
| 552 // MIN(1, 0) == 0 |
| 553 EXPECT(RangeBoundary::Min( |
| 554 RangeBoundary::FromConstant(1), |
| 555 RangeBoundary::FromConstant(0), |
| 556 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 0); |
| 557 // MIN(-1, 0) == -1 |
| 558 EXPECT(RangeBoundary::Min( |
| 559 RangeBoundary::FromConstant(-1), |
| 560 RangeBoundary::FromConstant(0), |
| 561 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == -1); |
| 562 |
| 563 // MAX(0, 1) == 1 |
| 564 EXPECT(RangeBoundary::Max( |
| 565 RangeBoundary::FromConstant(0), |
| 566 RangeBoundary::FromConstant(1), |
| 567 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 1); |
| 568 |
| 569 // MAX(0, -1) == 0 |
| 570 EXPECT(RangeBoundary::Max( |
| 571 RangeBoundary::FromConstant(0), |
| 572 RangeBoundary::FromConstant(-1), |
| 573 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 0); |
| 574 |
| 575 // MAX(1, 0) == 1 |
| 576 EXPECT(RangeBoundary::Max( |
| 577 RangeBoundary::FromConstant(1), |
| 578 RangeBoundary::FromConstant(0), |
| 579 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 1); |
| 580 // MAX(-1, 0) == 0 |
| 581 EXPECT(RangeBoundary::Max( |
| 582 RangeBoundary::FromConstant(-1), |
| 583 RangeBoundary::FromConstant(0), |
| 584 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 0); |
| 585 |
| 586 RangeBoundary n_infinity = RangeBoundary::NegativeInfinity(); |
| 587 RangeBoundary p_infinity = RangeBoundary::PositiveInfinity(); |
| 588 |
| 589 // Constants vs. infinity. |
| 590 EXPECT(RangeBoundary::Max( |
| 591 n_infinity, |
| 592 RangeBoundary::FromConstant(-1), |
| 593 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == -1); |
| 594 |
| 595 EXPECT(RangeBoundary::Max( |
| 596 RangeBoundary::FromConstant(-1), |
| 597 n_infinity, |
| 598 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == -1); |
| 599 |
| 600 EXPECT(RangeBoundary::Max( |
| 601 RangeBoundary::FromConstant(1), |
| 602 n_infinity, |
| 603 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 1); |
| 604 |
| 605 EXPECT(RangeBoundary::Max( |
| 606 n_infinity, |
| 607 RangeBoundary::FromConstant(1), |
| 608 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 1); |
| 609 |
| 610 EXPECT(RangeBoundary::Min( |
| 611 p_infinity, |
| 612 RangeBoundary::FromConstant(-1), |
| 613 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == -1); |
| 614 |
| 615 EXPECT(RangeBoundary::Min( |
| 616 RangeBoundary::FromConstant(-1), |
| 617 p_infinity, |
| 618 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == -1); |
| 619 |
| 620 EXPECT(RangeBoundary::Min( |
| 621 RangeBoundary::FromConstant(1), |
| 622 p_infinity, |
| 623 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 1); |
| 624 |
| 625 EXPECT(RangeBoundary::Min( |
| 626 p_infinity, |
| 627 RangeBoundary::FromConstant(1), |
| 628 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == 1); |
| 629 |
| 630 // 64-bit values. |
| 631 EXPECT(RangeBoundary::Min( |
| 632 RangeBoundary(static_cast<int64_t>(kMinInt64)), |
| 633 RangeBoundary(static_cast<int64_t>(kMinInt32)), |
| 634 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == kMinInt64); |
| 635 |
| 636 EXPECT(RangeBoundary::Max( |
| 637 RangeBoundary(static_cast<int64_t>(kMinInt64)), |
| 638 RangeBoundary(static_cast<int64_t>(kMinInt32)), |
| 639 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == kMinInt32); |
| 640 |
| 641 EXPECT(RangeBoundary::Min( |
| 642 RangeBoundary(static_cast<int64_t>(kMaxInt64)), |
| 643 RangeBoundary(static_cast<int64_t>(kMaxInt32)), |
| 644 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == kMaxInt32); |
| 645 |
| 646 EXPECT(RangeBoundary::Max( |
| 647 RangeBoundary(static_cast<int64_t>(kMaxInt64)), |
| 648 RangeBoundary(static_cast<int64_t>(kMaxInt32)), |
| 649 RangeBoundary::kRangeBoundaryInt64).ConstantValue() == kMaxInt64); |
| 650 } |
| 651 |
177 } // namespace dart | 652 } // namespace dart |
OLD | NEW |