Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
safe_math_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <limits>
6
8#include "gtest/gtest.h"
9
10namespace flutter {
11namespace testing {
12
13TEST(SafeMathTest, MultiplySizeT) {
14 // Multiplication with no overflow.
15 fml::SafeMath safe1;
16 EXPECT_EQ(safe1.mul(1000, 2000), static_cast<size_t>(2000000));
17 EXPECT_FALSE(safe1.overflow_detected());
18
19 // Overflow detection when multiplying size_t values at or near the maximum.
20 fml::SafeMath safe2;
21 safe2.mul(std::numeric_limits<size_t>::max(),
22 std::numeric_limits<size_t>::max());
23 EXPECT_TRUE(safe2.overflow_detected());
24
25 fml::SafeMath safe3;
26 safe3.mul(std::numeric_limits<size_t>::max() >> 2, 5);
27 EXPECT_TRUE(safe3.overflow_detected());
28
29 // Overflow detection for a result that slightly exceeds the range of a
30 // uint64_t.
31 if (sizeof(size_t) == sizeof(uint64_t)) {
32 fml::SafeMath safe4;
33 safe4.mul(static_cast<size_t>(1ULL << 32), static_cast<size_t>(1ULL << 32));
34 EXPECT_TRUE(safe4.overflow_detected());
35 }
36}
37
38} // namespace testing
39} // namespace flutter
size_t mul(size_t x, size_t y)
Definition safe_math.cc:12
bool overflow_detected() const
Definition safe_math.h:17
TEST(NativeAssetsManagerTest, NoAvailableAssets)