OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/base/math_util.h" | 5 #include "cc/base/math_util.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "cc/test/geometry_test_utils.h" | 9 #include "cc/test/geometry_test_utils.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 } | 377 } |
378 } | 378 } |
379 | 379 |
380 TEST(MathUtilTest, RoundDownUnderflow) { | 380 TEST(MathUtilTest, RoundDownUnderflow) { |
381 // Rounding down -123 by 50 is -150, which underflows int8_t, but fits in | 381 // Rounding down -123 by 50 is -150, which underflows int8_t, but fits in |
382 // int16_t. | 382 // int16_t. |
383 EXPECT_FALSE(MathUtil::VerifyRoundDown<int8_t>(-123, 50)); | 383 EXPECT_FALSE(MathUtil::VerifyRoundDown<int8_t>(-123, 50)); |
384 EXPECT_TRUE(MathUtil::VerifyRoundDown<int16_t>(-123, 50)); | 384 EXPECT_TRUE(MathUtil::VerifyRoundDown<int16_t>(-123, 50)); |
385 } | 385 } |
386 | 386 |
| 387 TEST(MathUtilTest, AlmostEqualFloats) { |
| 388 // Two almost equal floats. |
| 389 float value1 = 7.33907556533813f; |
| 390 float value2 = 7.33907508850098f; |
| 391 |
| 392 EXPECT_TRUE(value1 != value2); |
| 393 |
| 394 int max_precision = std::numeric_limits<float>::digits10 - 1; |
| 395 |
| 396 // Check with all possible precisions, the almost equal floats (differing |
| 397 // by small magnitude of epsilon) are same. |
| 398 for (int i = 0; i <= max_precision; ++i) { |
| 399 float value1_rounded_to_same_precision = |
| 400 MathUtil::RoundToFixedPrecision(value1, i); |
| 401 float value2_rounded_to_same_precision = |
| 402 MathUtil::RoundToFixedPrecision(value2, i); |
| 403 EXPECT_TRUE(value1_rounded_to_same_precision == |
| 404 value2_rounded_to_same_precision); |
| 405 } |
| 406 |
| 407 // Check the same float with different precisions. Different values should |
| 408 // be returned. |
| 409 for (int i = 0; i <= max_precision; ++i) { |
| 410 for (int j = i + 1; j <= max_precision; ++j) { |
| 411 float value1_rounded_to_precision_i = |
| 412 MathUtil::RoundToFixedPrecision(value1, i); |
| 413 float value1_rounded_to_precision_j = |
| 414 MathUtil::RoundToFixedPrecision(value1, j); |
| 415 EXPECT_FALSE(value1_rounded_to_precision_i == |
| 416 value1_rounded_to_precision_j); |
| 417 } |
| 418 } |
| 419 |
| 420 // Different floats created by differing some magnitude of epsilon. |
| 421 float value3 = value1 + (10 * std::numeric_limits<float>::epsilon()); |
| 422 EXPECT_TRUE(value1 != value3); |
| 423 EXPECT_TRUE(MathUtil::RoundToFixedPrecision(value1, 2) == |
| 424 MathUtil::RoundToFixedPrecision(value3, 2)); |
| 425 |
| 426 // Check different floats. |
| 427 float value4 = 7.3f; |
| 428 float value5 = 7.4f; |
| 429 // Rounded to precision 0, should be equal. |
| 430 EXPECT_TRUE(MathUtil::RoundToFixedPrecision(value4, 0) == |
| 431 MathUtil::RoundToFixedPrecision(value5, 0)); |
| 432 // Rounded to precision 1, should not be equal. |
| 433 EXPECT_FALSE(MathUtil::RoundToFixedPrecision(value4, 1) == |
| 434 MathUtil::RoundToFixedPrecision(value5, 1)); |
| 435 } |
| 436 |
387 } // namespace | 437 } // namespace |
388 } // namespace cc | 438 } // namespace cc |
OLD | NEW |