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

Side by Side Diff: base/allocator/allocator_unittest.cc

Issue 415773009: Re-enable various MSVC warnings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Don't test new exception handling Created 6 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | build/common.gypi » ('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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <stdio.h> 5 #include <stdio.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <algorithm> // for min() 7 #include <algorithm> // for min()
8 8
9 #include "base/atomicops.h" 9 #include "base/atomicops.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 // Number of bits in a size_t. 12 // Number of bits in a size_t.
13 static const int kSizeBits = 8 * sizeof(size_t); 13 static const int kSizeBits = 8 * sizeof(size_t);
14 // The maximum size of a size_t. 14 // The maximum size of a size_t.
15 static const size_t kMaxSize = ~static_cast<size_t>(0); 15 static const size_t kMaxSize = ~static_cast<size_t>(0);
16 // Maximum positive size of a size_t if it were signed. 16 // Maximum positive size of a size_t if it were signed.
17 static const size_t kMaxSignedSize = ((size_t(1) << (kSizeBits-1)) - 1); 17 static const size_t kMaxSignedSize = ((size_t(1) << (kSizeBits-1)) - 1);
18 // An allocation size which is not too big to be reasonable.
19 static const size_t kNotTooBig = 100000;
20 // An allocation size which is just too big.
21 static const size_t kTooBig = ~static_cast<size_t>(0);
22 18
23 namespace { 19 namespace {
24 20
25 using std::min; 21 using std::min;
26 22
27 // Fill a buffer of the specified size with a predetermined pattern 23 // Fill a buffer of the specified size with a predetermined pattern
28 static void Fill(unsigned char* buffer, int n) { 24 static void Fill(unsigned char* buffer, int n) {
29 for (int i = 0; i < n; i++) { 25 for (int i = 0; i < n; i++) {
30 buffer[i] = (i & 0xff); 26 buffer[i] = (i & 0xff);
31 } 27 }
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 } else { 276 } else {
281 EXPECT_NE(reinterpret_cast<void*>(NULL), p) << 277 EXPECT_NE(reinterpret_cast<void*>(NULL), p) <<
282 "calloc(n, s) should succeed"; 278 "calloc(n, s) should succeed";
283 for (int i = 0; i < n*s; i++) { 279 for (int i = 0; i < n*s; i++) {
284 EXPECT_EQ('\0', p[i]); 280 EXPECT_EQ('\0', p[i]);
285 } 281 }
286 free(p); 282 free(p);
287 } 283 }
288 } 284 }
289 285
290
291 // A global test counter for number of times the NewHandler is called.
292 static int news_handled = 0;
293 static void TestNewHandler() {
294 ++news_handled;
295 throw std::bad_alloc();
296 }
297
298 // Because we compile without exceptions, we expect these will not throw.
299 static void TestOneNewWithoutExceptions(void* (*func)(size_t),
300 bool should_throw) {
301 // success test
302 try {
303 void* ptr = (*func)(kNotTooBig);
304 EXPECT_NE(reinterpret_cast<void*>(NULL), ptr) <<
305 "allocation should not have failed.";
306 } catch(...) {
307 EXPECT_EQ(0, 1) << "allocation threw unexpected exception.";
308 }
309
310 // failure test
311 try {
312 void* rv = (*func)(kTooBig);
brettw 2014/07/25 20:19:20 I kind of like this test for the behavior that a "
Peter Kasting 2014/07/25 20:54:42 I think there are two options. One would be to #p
313 EXPECT_EQ(NULL, rv);
314 EXPECT_FALSE(should_throw) << "allocation should have thrown.";
315 } catch(...) {
316 EXPECT_TRUE(should_throw) << "allocation threw unexpected exception.";
317 }
318 }
319
320 static void TestNothrowNew(void* (*func)(size_t)) {
321 news_handled = 0;
322
323 // test without new_handler:
324 std::new_handler saved_handler = std::set_new_handler(0);
325 TestOneNewWithoutExceptions(func, false);
326
327 // test with new_handler:
328 std::set_new_handler(TestNewHandler);
329 TestOneNewWithoutExceptions(func, true);
330 EXPECT_EQ(news_handled, 1) << "nothrow new_handler was not called.";
331 std::set_new_handler(saved_handler);
332 }
333
334 } // namespace 286 } // namespace
335 287
336 //----------------------------------------------------------------------------- 288 //-----------------------------------------------------------------------------
337 289
338 TEST(Atomics, AtomicIncrementWord) { 290 TEST(Atomics, AtomicIncrementWord) {
339 TestAtomicIncrement<AtomicWord>(); 291 TestAtomicIncrement<AtomicWord>();
340 } 292 }
341 293
342 TEST(Atomics, AtomicIncrement32) { 294 TEST(Atomics, AtomicIncrement32) {
343 TestAtomicIncrement<Atomic32>(); 295 TestAtomicIncrement<Atomic32>();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 328
377 TestCalloc(kMaxSize, 2, false); 329 TestCalloc(kMaxSize, 2, false);
378 TestCalloc(2, kMaxSize, false); 330 TestCalloc(2, kMaxSize, false);
379 TestCalloc(kMaxSize, kMaxSize, false); 331 TestCalloc(kMaxSize, kMaxSize, false);
380 332
381 TestCalloc(kMaxSignedSize, 3, false); 333 TestCalloc(kMaxSignedSize, 3, false);
382 TestCalloc(3, kMaxSignedSize, false); 334 TestCalloc(3, kMaxSignedSize, false);
383 TestCalloc(kMaxSignedSize, kMaxSignedSize, false); 335 TestCalloc(kMaxSignedSize, kMaxSignedSize, false);
384 } 336 }
385 337
386 TEST(Allocators, New) {
387 TestNothrowNew(&::operator new);
388 TestNothrowNew(&::operator new[]);
389 }
390
391 // This makes sure that reallocing a small number of bytes in either 338 // This makes sure that reallocing a small number of bytes in either
392 // direction doesn't cause us to allocate new memory. 339 // direction doesn't cause us to allocate new memory.
393 TEST(Allocators, Realloc1) { 340 TEST(Allocators, Realloc1) {
394 int start_sizes[] = { 100, 1000, 10000, 100000 }; 341 int start_sizes[] = { 100, 1000, 10000, 100000 };
395 int deltas[] = { 1, -2, 4, -8, 16, -32, 64, -128 }; 342 int deltas[] = { 1, -2, 4, -8, 16, -32, 64, -128 };
396 343
397 for (int s = 0; s < sizeof(start_sizes)/sizeof(*start_sizes); ++s) { 344 for (int s = 0; s < sizeof(start_sizes)/sizeof(*start_sizes); ++s) {
398 void* p = malloc(start_sizes[s]); 345 void* p = malloc(start_sizes[s]);
399 ASSERT_TRUE(p); 346 ASSERT_TRUE(p);
400 // The larger the start-size, the larger the non-reallocing delta. 347 // The larger the start-size, the larger the non-reallocing delta.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 } 458 }
512 } 459 }
513 460
514 #endif 461 #endif
515 462
516 463
517 int main(int argc, char** argv) { 464 int main(int argc, char** argv) {
518 testing::InitGoogleTest(&argc, argv); 465 testing::InitGoogleTest(&argc, argv);
519 return RUN_ALL_TESTS(); 466 return RUN_ALL_TESTS();
520 } 467 }
OLDNEW
« no previous file with comments | « no previous file | build/common.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698