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 float f1 = 7.33907556533813f; | |
vmpstr
2015/10/19 18:58:38
nit: f1 -> first_value, f2 -> second_value (or som
| |
389 float f2 = 7.33907508850098f; | |
390 | |
391 EXPECT_TRUE(f1 != f2); | |
392 | |
393 int max_precision = std::numeric_limits<float>::digits10 - 1; | |
394 | |
395 // Check with all possible precisions, the almost equal floats (differing by | |
396 // by small magnitude of epsilon) are same. | |
397 for (int i = 1; i <= max_precision; ++i) { | |
398 float f3 = MathUtil::RoundToFixedPrecision(f1, i); | |
vmpstr
2015/10/19 18:58:38
nit: f3 -> rounded_first_value, f2 -> rounded_seco
| |
399 float f4 = MathUtil::RoundToFixedPrecision(f2, i); | |
400 EXPECT_TRUE(f3 == f4); | |
401 } | |
402 | |
403 // Check with different precisions, different values are returned. | |
404 for (int i = 1; i <= max_precision; ++i) { | |
405 for (int j = i + 1; j <= max_precision; ++j) { | |
406 float f3 = MathUtil::RoundToFixedPrecision(f1, i); | |
407 float f4 = MathUtil::RoundToFixedPrecision(f1, j); | |
408 EXPECT_FALSE(f3 == f4); | |
409 } | |
410 } | |
411 | |
412 // Different floats differing by some magnitude of epsilon. | |
413 float f5 = f1 + (10 * std::numeric_limits<float>::epsilon()); | |
414 EXPECT_TRUE(f1 != f5); | |
415 EXPECT_TRUE(MathUtil::RoundToFixedPrecision(f1, 2) == | |
416 MathUtil::RoundToFixedPrecision(f5, 2)); | |
417 | |
418 // Check different floats. | |
419 float f6 = 7.3f; | |
420 float f7 = 7.4f; | |
421 for (int i = 1; i <= max_precision; ++i) { | |
422 float f8 = MathUtil::RoundToFixedPrecision(f6, i); | |
423 float f9 = MathUtil::RoundToFixedPrecision(f7, i); | |
424 EXPECT_FALSE(f8 == f9); | |
425 } | |
426 } | |
427 | |
387 } // namespace | 428 } // namespace |
388 } // namespace cc | 429 } // namespace cc |
OLD | NEW |