Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(490)

Side by Side Diff: cc/base/math_util_unittest.cc

Issue 1266103005: cc: Handle overflow/underflow in MathUtil::RoundUp/RoundDown functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Small nit. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/base/math_util.h ('k') | cc/layers/picture_layer_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 220 }
221 221
222 TEST(MathUtilTest, RoundUp) { 222 TEST(MathUtilTest, RoundUp) {
223 for (int multiplier = 1; multiplier <= 10; ++multiplier) { 223 for (int multiplier = 1; multiplier <= 10; ++multiplier) {
224 // Try attempts in descending order, so that we can 224 // Try attempts in descending order, so that we can
225 // determine the correct value before it's needed. 225 // determine the correct value before it's needed.
226 int correct; 226 int correct;
227 for (int attempt = 5 * multiplier; attempt >= -5 * multiplier; --attempt) { 227 for (int attempt = 5 * multiplier; attempt >= -5 * multiplier; --attempt) {
228 if ((attempt % multiplier) == 0) 228 if ((attempt % multiplier) == 0)
229 correct = attempt; 229 correct = attempt;
230 EXPECT_EQ(correct, MathUtil::RoundUp(attempt, multiplier)) 230 EXPECT_EQ(correct, MathUtil::UncheckedRoundUp(attempt, multiplier))
231 << "attempt=" << attempt << " multiplier=" << multiplier; 231 << "attempt=" << attempt << " multiplier=" << multiplier;
232 } 232 }
233 } 233 }
234 234
235 for (unsigned multiplier = 1; multiplier <= 10; ++multiplier) { 235 for (unsigned multiplier = 1; multiplier <= 10; ++multiplier) {
236 // Try attempts in descending order, so that we can 236 // Try attempts in descending order, so that we can
237 // determine the correct value before it's needed. 237 // determine the correct value before it's needed.
238 unsigned correct; 238 unsigned correct;
239 for (unsigned attempt = 5 * multiplier; attempt > 0; --attempt) { 239 for (unsigned attempt = 5 * multiplier; attempt > 0; --attempt) {
240 if ((attempt % multiplier) == 0) 240 if ((attempt % multiplier) == 0)
241 correct = attempt; 241 correct = attempt;
242 EXPECT_EQ(correct, MathUtil::RoundUp(attempt, multiplier)) 242 EXPECT_EQ(correct, MathUtil::UncheckedRoundUp(attempt, multiplier))
243 << "attempt=" << attempt << " multiplier=" << multiplier; 243 << "attempt=" << attempt << " multiplier=" << multiplier;
244 } 244 }
245 EXPECT_EQ(0u, MathUtil::RoundUp(0u, multiplier)) 245 EXPECT_EQ(0u, MathUtil::UncheckedRoundUp(0u, multiplier))
246 << "attempt=0 multiplier=" << multiplier; 246 << "attempt=0 multiplier=" << multiplier;
247 } 247 }
248 } 248 }
249 249
250 TEST(MathUtilTest, RoundUpOverFlow) {
251 // Rounding up 123 by 50 is 150, which overflows int8_t, but fits in uint8_t.
252 EXPECT_FALSE(MathUtil::VerifyRoundup<int8_t>(123, 50));
253 EXPECT_TRUE(MathUtil::VerifyRoundup<uint8_t>(123, 50));
254 }
255
250 TEST(MathUtilTest, RoundDown) { 256 TEST(MathUtilTest, RoundDown) {
251 for (int multiplier = 1; multiplier <= 10; ++multiplier) { 257 for (int multiplier = 1; multiplier <= 10; ++multiplier) {
252 // Try attempts in ascending order, so that we can 258 // Try attempts in ascending order, so that we can
253 // determine the correct value before it's needed. 259 // determine the correct value before it's needed.
254 int correct; 260 int correct;
255 for (int attempt = -5 * multiplier; attempt <= 5 * multiplier; ++attempt) { 261 for (int attempt = -5 * multiplier; attempt <= 5 * multiplier; ++attempt) {
256 if ((attempt % multiplier) == 0) 262 if ((attempt % multiplier) == 0)
257 correct = attempt; 263 correct = attempt;
258 EXPECT_EQ(correct, MathUtil::RoundDown(attempt, multiplier)) 264 EXPECT_EQ(correct, MathUtil::UncheckedRoundDown(attempt, multiplier))
259 << "attempt=" << attempt << " multiplier=" << multiplier; 265 << "attempt=" << attempt << " multiplier=" << multiplier;
260 } 266 }
261 } 267 }
262 268
263 for (unsigned multiplier = 1; multiplier <= 10; ++multiplier) { 269 for (unsigned multiplier = 1; multiplier <= 10; ++multiplier) {
264 // Try attempts in ascending order, so that we can 270 // Try attempts in ascending order, so that we can
265 // determine the correct value before it's needed. 271 // determine the correct value before it's needed.
266 unsigned correct; 272 unsigned correct;
267 for (unsigned attempt = 0; attempt <= 5 * multiplier; ++attempt) { 273 for (unsigned attempt = 0; attempt <= 5 * multiplier; ++attempt) {
268 if ((attempt % multiplier) == 0) 274 if ((attempt % multiplier) == 0)
269 correct = attempt; 275 correct = attempt;
270 EXPECT_EQ(correct, MathUtil::RoundDown(attempt, multiplier)) 276 EXPECT_EQ(correct, MathUtil::UncheckedRoundDown(attempt, multiplier))
271 << "attempt=" << attempt << " multiplier=" << multiplier; 277 << "attempt=" << attempt << " multiplier=" << multiplier;
272 } 278 }
273 } 279 }
274 } 280 }
275 281
282 TEST(MathUtilTest, RoundDownUnderFlow) {
283 // Rounding down -123 by 50 is -150, which underflows int8_t, but fits in
284 // int16_t.
285 EXPECT_FALSE(MathUtil::VerifyRoundDown<int8_t>(-123, 50));
286 EXPECT_TRUE(MathUtil::VerifyRoundDown<int16_t>(-123, 50));
287 }
288
276 } // namespace 289 } // namespace
277 } // namespace cc 290 } // namespace cc
OLDNEW
« no previous file with comments | « cc/base/math_util.h ('k') | cc/layers/picture_layer_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698