diff options
-rw-r--r-- | cc/base/util.h | 7 | ||||
-rw-r--r-- | cc/base/util_unittest.cc | 67 | ||||
-rw-r--r-- | cc/cc_tests.gyp | 101 |
3 files changed, 125 insertions, 50 deletions
diff --git a/cc/base/util.h b/cc/base/util.h index 2bf2723..1d716ae 100644 --- a/cc/base/util.h +++ b/cc/base/util.h @@ -5,15 +5,22 @@ #ifndef CC_BASE_UTIL_H_ #define CC_BASE_UTIL_H_ +#include <limits> + +#include "base/basictypes.h" + namespace cc { template <typename T> T RoundUp(T n, T mul) { + COMPILE_ASSERT(std::numeric_limits<T>::is_integer, type_must_be_integral); return (n > 0) ? ((n + mul - 1) / mul) * mul : (n / mul) * mul; } template <typename T> T RoundDown(T n, T mul) { + COMPILE_ASSERT(std::numeric_limits<T>::is_integer, type_must_be_integral); return (n > 0) ? (n / mul) * mul + : (n == 0) ? 0 : ((n - mul + 1) / mul) * mul; } diff --git a/cc/base/util_unittest.cc b/cc/base/util_unittest.cc new file mode 100644 index 0000000..6665a6a --- /dev/null +++ b/cc/base/util_unittest.cc @@ -0,0 +1,67 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cc/base/util.h" + +#include "testing/gtest/include/gtest/gtest.h" + +namespace cc { +namespace { + +TEST(UtilTest, RoundUp) { + for (int multiplier = 1; multiplier <= 10; ++multiplier) { + // Try attempts in descending order, so that we can + // determine the correct value before it's needed. + int correct; + for (int attempt = 5 * multiplier; attempt >= -5 * multiplier; --attempt) { + if ((attempt % multiplier) == 0) + correct = attempt; + EXPECT_EQ(correct, RoundUp(attempt, multiplier)) + << "attempt=" << attempt << " multiplier=" << multiplier; + } + } + + for (unsigned multiplier = 1; multiplier <= 10; ++multiplier) { + // Try attempts in descending order, so that we can + // determine the correct value before it's needed. + unsigned correct; + for (unsigned attempt = 5 * multiplier; attempt > 0; --attempt) { + if ((attempt % multiplier) == 0) + correct = attempt; + EXPECT_EQ(correct, RoundUp(attempt, multiplier)) + << "attempt=" << attempt << " multiplier=" << multiplier; + } + EXPECT_EQ(0u, RoundUp(0u, multiplier)) + << "attempt=0 multiplier=" << multiplier; + } +} + +TEST(UtilTest, RoundDown) { + for (int multiplier = 1; multiplier <= 10; ++multiplier) { + // Try attempts in ascending order, so that we can + // determine the correct value before it's needed. + int correct; + for (int attempt = -5 * multiplier; attempt <= 5 * multiplier; ++attempt) { + if ((attempt % multiplier) == 0) + correct = attempt; + EXPECT_EQ(correct, RoundDown(attempt, multiplier)) + << "attempt=" << attempt << " multiplier=" << multiplier; + } + } + + for (unsigned multiplier = 1; multiplier <= 10; ++multiplier) { + // Try attempts in ascending order, so that we can + // determine the correct value before it's needed. + unsigned correct; + for (unsigned attempt = 0; attempt <= 5 * multiplier; ++attempt) { + if ((attempt % multiplier) == 0) + correct = attempt; + EXPECT_EQ(correct, RoundDown(attempt, multiplier)) + << "attempt=" << attempt << " multiplier=" << multiplier; + } + } +} + +} // namespace +} // namespace cc diff --git a/cc/cc_tests.gyp b/cc/cc_tests.gyp index c9ddde8..b126b45 100644 --- a/cc/cc_tests.gyp +++ b/cc/cc_tests.gyp @@ -7,80 +7,81 @@ 'chromium_code': 1, 'cc_unit_tests_source_files': [ 'animation/animation_unittest.cc', + 'animation/keyframed_animation_curve_unittest.cc', + 'animation/layer_animation_controller_unittest.cc', + 'animation/scrollbar_animation_controller_linear_fade_unittest.cc', + 'animation/timing_function_unittest.cc', + 'animation/transform_operations_unittest.cc', + 'base/float_quad_unittest.cc', + 'base/hash_pair_unittest.cc', + 'base/math_util_unittest.cc', + 'base/region_unittest.cc', + 'base/tiling_data_unittest.cc', + 'base/util_unittest.cc', + 'input/top_controls_manager_unittest.cc', 'layers/content_layer_unittest.cc', 'layers/contents_scaling_layer_unittest.cc', - 'trees/damage_tracker_unittest.cc', - 'scheduler/delay_based_time_source_unittest.cc', 'layers/delegated_renderer_layer_impl_unittest.cc', - 'output/delegating_renderer_unittest.cc', - 'quads/draw_quad_unittest.cc', - 'base/float_quad_unittest.cc', - 'scheduler/frame_rate_controller_unittest.cc', - 'output/gl_renderer_unittest.cc', - 'output/gl_renderer_pixeltest.cc', - 'output/shader_unittest.cc', - 'base/hash_pair_unittest.cc', 'layers/heads_up_display_unittest.cc', - 'animation/keyframed_animation_curve_unittest.cc', - 'animation/layer_animation_controller_unittest.cc', 'layers/layer_impl_unittest.cc', 'layers/layer_iterator_unittest.cc', 'layers/layer_position_constraint_unittest.cc', + 'layers/layer_unittest.cc', + 'layers/nine_patch_layer_impl_unittest.cc', + 'layers/nine_patch_layer_unittest.cc', + 'layers/picture_image_layer_impl_unittest.cc', + 'layers/picture_layer_impl_unittest.cc', + 'layers/render_surface_unittest.cc', + 'layers/scrollbar_layer_unittest.cc', + 'layers/solid_color_layer_impl_unittest.cc', + 'layers/texture_layer_unittest.cc', + 'layers/tiled_layer_impl_unittest.cc', + 'layers/tiled_layer_unittest.cc', + 'output/delegating_renderer_unittest.cc', + 'output/gl_renderer_pixeltest.cc', + 'output/gl_renderer_unittest.cc', + 'output/render_surface_filters_unittest.cc', + 'output/shader_unittest.cc', + 'output/software_renderer_unittest.cc', + 'output/texture_copier_unittest.cc', + 'quads/draw_quad_unittest.cc', + 'quads/render_pass_unittest.cc', 'resources/layer_quad_unittest.cc', + 'resources/picture_layer_tiling_set_unittest.cc', + 'resources/picture_layer_tiling_unittest.cc', + 'resources/picture_pile_impl_unittest.cc', + 'resources/picture_unittest.cc', + 'resources/prioritized_resource_unittest.cc', + 'resources/resource_provider_unittest.cc', + 'resources/resource_update_controller_unittest.cc', + 'resources/scoped_resource_unittest.cc', + 'resources/tile_manager_unittest.cc', + 'resources/tile_priority_unittest.cc', + 'scheduler/delay_based_time_source_unittest.cc', + 'scheduler/frame_rate_controller_unittest.cc', + 'scheduler/scheduler_state_machine_unittest.cc', + 'scheduler/scheduler_unittest.cc', + 'scheduler/texture_uploader_unittest.cc', + 'scheduler/vsync_time_source_unittest.cc', + 'test/fake_web_graphics_context_3d_unittest.cc', + 'trees/damage_tracker_unittest.cc', 'trees/layer_sorter_unittest.cc', 'trees/layer_tree_host_common_unittest.cc', 'trees/layer_tree_host_impl_unittest.cc', 'trees/layer_tree_host_pixeltest_filters.cc', 'trees/layer_tree_host_pixeltest_masks.cc', 'trees/layer_tree_host_pixeltest_on_demand_raster.cc', - 'trees/layer_tree_host_unittest.cc', 'trees/layer_tree_host_unittest_animation.cc', + 'trees/layer_tree_host_unittest.cc', 'trees/layer_tree_host_unittest_context.cc', 'trees/layer_tree_host_unittest_damage.cc', 'trees/layer_tree_host_unittest_delegated.cc', 'trees/layer_tree_host_unittest_occlusion.cc', 'trees/layer_tree_host_unittest_scroll.cc', 'trees/layer_tree_host_unittest_video.cc', - 'layers/layer_unittest.cc', - 'base/math_util_unittest.cc', - 'layers/nine_patch_layer_impl_unittest.cc', - 'layers/nine_patch_layer_unittest.cc', 'trees/occlusion_tracker_unittest.cc', - 'layers/picture_image_layer_impl_unittest.cc', - 'layers/picture_layer_impl_unittest.cc', - 'resources/picture_layer_tiling_set_unittest.cc', - 'resources/picture_layer_tiling_unittest.cc', - 'resources/picture_pile_impl_unittest.cc', - 'resources/picture_unittest.cc', - 'resources/prioritized_resource_unittest.cc', 'trees/quad_culler_unittest.cc', - 'base/region_unittest.cc', - 'quads/render_pass_unittest.cc', - 'output/render_surface_filters_unittest.cc', - 'layers/render_surface_unittest.cc', - 'resources/resource_provider_unittest.cc', - 'resources/resource_update_controller_unittest.cc', - 'scheduler/scheduler_state_machine_unittest.cc', - 'scheduler/scheduler_unittest.cc', - 'resources/scoped_resource_unittest.cc', - 'animation/scrollbar_animation_controller_linear_fade_unittest.cc', - 'layers/scrollbar_layer_unittest.cc', - 'output/software_renderer_unittest.cc', - 'layers/solid_color_layer_impl_unittest.cc', - 'output/texture_copier_unittest.cc', - 'layers/texture_layer_unittest.cc', - 'scheduler/texture_uploader_unittest.cc', - 'resources/tile_priority_unittest.cc', - 'resources/tile_manager_unittest.cc', - 'layers/tiled_layer_impl_unittest.cc', - 'layers/tiled_layer_unittest.cc', - 'base/tiling_data_unittest.cc', - 'input/top_controls_manager_unittest.cc', - 'animation/transform_operations_unittest.cc', 'trees/tree_synchronizer_unittest.cc', - 'animation/timing_function_unittest.cc', - 'test/fake_web_graphics_context_3d_unittest.cc', - 'scheduler/vsync_time_source_unittest.cc', ], 'cc_tests_support_files': [ 'test/animation_test_common.cc', |