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

Side by Side Diff: base/rand_util_unittest.cc

Issue 1419703005: Add missing overflow handling to base::RandInt(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: stdint hyyyyyyype Created 5 years, 1 month 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 | « base/rand_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/rand_util.h" 5 #include "base/rand_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace { 15 namespace {
16 16
17 const int kIntMin = std::numeric_limits<int>::min(); 17 const int kIntMin = std::numeric_limits<int>::min();
18 const int kIntMax = std::numeric_limits<int>::max(); 18 const int kIntMax = std::numeric_limits<int>::max();
19 19
20 } // namespace 20 } // namespace
21 21
22 TEST(RandUtilTest, SameMinAndMax) { 22 TEST(RandUtilTest, RandInt) {
23 EXPECT_EQ(base::RandInt(0, 0), 0); 23 EXPECT_EQ(base::RandInt(0, 0), 0);
24 EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin); 24 EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin);
25 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); 25 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax);
26
27 // Check that the DCHECKS in RandInt() don't fire due to internal overflow.
28 // There was a 50% chance of that happening, so calling it 40 times means
29 // the chances of this passing by accident are tiny (9e-13).
30 for (int i = 0; i < 40; ++i)
31 base::RandInt(kIntMin, kIntMax);
26 } 32 }
27 33
28 TEST(RandUtilTest, RandDouble) { 34 TEST(RandUtilTest, RandDouble) {
29 // Force 64-bit precision, making sure we're not in a 80-bit FPU register. 35 // Force 64-bit precision, making sure we're not in a 80-bit FPU register.
30 volatile double number = base::RandDouble(); 36 volatile double number = base::RandDouble();
31 EXPECT_GT(1.0, number); 37 EXPECT_GT(1.0, number);
32 EXPECT_LE(0.0, number); 38 EXPECT_LE(0.0, number);
33 } 39 }
34 40
35 TEST(RandUtilTest, RandBytes) { 41 TEST(RandUtilTest, RandBytes) {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 141
136 scoped_ptr<uint8[]> buffer(new uint8[kTestBufferSize]); 142 scoped_ptr<uint8[]> buffer(new uint8[kTestBufferSize]);
137 const base::TimeTicks now = base::TimeTicks::Now(); 143 const base::TimeTicks now = base::TimeTicks::Now();
138 for (int i = 0; i < kTestIterations; ++i) 144 for (int i = 0; i < kTestIterations; ++i)
139 base::RandBytes(buffer.get(), kTestBufferSize); 145 base::RandBytes(buffer.get(), kTestBufferSize);
140 const base::TimeTicks end = base::TimeTicks::Now(); 146 const base::TimeTicks end = base::TimeTicks::Now();
141 147
142 LOG(INFO) << "RandBytes(" << kTestBufferSize << ") took: " 148 LOG(INFO) << "RandBytes(" << kTestBufferSize << ") took: "
143 << (end - now).InMicroseconds() << "µs"; 149 << (end - now).InMicroseconds() << "µs";
144 } 150 }
OLDNEW
« no previous file with comments | « base/rand_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698