OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "base/atomicops.h" | 8 #include "base/atomicops.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 EXPECT_NE(reinterpret_cast<void*>(NULL), ptr) << | 306 EXPECT_NE(reinterpret_cast<void*>(NULL), ptr) << |
307 "allocation should not have failed."; | 307 "allocation should not have failed."; |
308 } catch(...) { | 308 } catch(...) { |
309 EXPECT_EQ(0, 1) << "allocation threw unexpected exception."; | 309 EXPECT_EQ(0, 1) << "allocation threw unexpected exception."; |
310 } | 310 } |
311 | 311 |
312 // failure test | 312 // failure test |
313 try { | 313 try { |
314 void* rv = (*func)(kTooBig); | 314 void* rv = (*func)(kTooBig); |
315 EXPECT_EQ(NULL, rv); | 315 EXPECT_EQ(NULL, rv); |
316 EXPECT_EQ(false, should_throw) << "allocation should have thrown."; | 316 EXPECT_FALSE(should_throw) << "allocation should have thrown."; |
317 } catch(...) { | 317 } catch(...) { |
318 EXPECT_EQ(true, should_throw) << "allocation threw unexpected exception."; | 318 EXPECT_TRUE(should_throw) << "allocation threw unexpected exception."; |
319 } | 319 } |
320 } | 320 } |
321 | 321 |
322 static void TestNothrowNew(void* (*func)(size_t)) { | 322 static void TestNothrowNew(void* (*func)(size_t)) { |
323 news_handled = 0; | 323 news_handled = 0; |
324 | 324 |
325 // test without new_handler: | 325 // test without new_handler: |
326 std::new_handler saved_handler = std::set_new_handler(0); | 326 std::new_handler saved_handler = std::set_new_handler(0); |
327 TestOneNewWithoutExceptions(func, false); | 327 TestOneNewWithoutExceptions(func, false); |
328 | 328 |
(...skipping 23 matching lines...) Expand all Loading... |
352 TEST(Atomics, AtomicOps32) { | 352 TEST(Atomics, AtomicOps32) { |
353 TestAtomicIncrement<Atomic32>(); | 353 TestAtomicIncrement<Atomic32>(); |
354 } | 354 } |
355 | 355 |
356 TEST(Allocators, Malloc) { | 356 TEST(Allocators, Malloc) { |
357 // Try allocating data with a bunch of alignments and sizes | 357 // Try allocating data with a bunch of alignments and sizes |
358 for (int size = 1; size < 1048576; size *= 2) { | 358 for (int size = 1; size < 1048576; size *= 2) { |
359 unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size)); | 359 unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size)); |
360 CheckAlignment(ptr, 2); // Should be 2 byte aligned | 360 CheckAlignment(ptr, 2); // Should be 2 byte aligned |
361 Fill(ptr, size); | 361 Fill(ptr, size); |
362 EXPECT_EQ(true, Valid(ptr, size)); | 362 EXPECT_TRUE(Valid(ptr, size)); |
363 free(ptr); | 363 free(ptr); |
364 } | 364 } |
365 } | 365 } |
366 | 366 |
367 TEST(Allocators, Calloc) { | 367 TEST(Allocators, Calloc) { |
368 TestCalloc(0, 0, true); | 368 TestCalloc(0, 0, true); |
369 TestCalloc(0, 1, true); | 369 TestCalloc(0, 1, true); |
370 TestCalloc(1, 1, true); | 370 TestCalloc(1, 1, true); |
371 TestCalloc(1<<10, 0, true); | 371 TestCalloc(1<<10, 0, true); |
372 TestCalloc(1<<20, 0, true); | 372 TestCalloc(1<<20, 0, true); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 } | 413 } |
414 } | 414 } |
415 | 415 |
416 TEST(Allocators, Realloc2) { | 416 TEST(Allocators, Realloc2) { |
417 for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { | 417 for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { |
418 for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { | 418 for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { |
419 unsigned char* src = reinterpret_cast<unsigned char*>(malloc(src_size)); | 419 unsigned char* src = reinterpret_cast<unsigned char*>(malloc(src_size)); |
420 Fill(src, src_size); | 420 Fill(src, src_size); |
421 unsigned char* dst = | 421 unsigned char* dst = |
422 reinterpret_cast<unsigned char*>(realloc(src, dst_size)); | 422 reinterpret_cast<unsigned char*>(realloc(src, dst_size)); |
423 EXPECT_EQ(true, Valid(dst, min(src_size, dst_size))); | 423 EXPECT_TRUE(Valid(dst, min(src_size, dst_size))); |
424 Fill(dst, dst_size); | 424 Fill(dst, dst_size); |
425 EXPECT_EQ(true, Valid(dst, dst_size)); | 425 EXPECT_TRUE(Valid(dst, dst_size)); |
426 if (dst != NULL) free(dst); | 426 if (dst != NULL) free(dst); |
427 } | 427 } |
428 } | 428 } |
429 | 429 |
430 // Now make sure realloc works correctly even when we overflow the | 430 // Now make sure realloc works correctly even when we overflow the |
431 // packed cache, so some entries are evicted from the cache. | 431 // packed cache, so some entries are evicted from the cache. |
432 // The cache has 2^12 entries, keyed by page number. | 432 // The cache has 2^12 entries, keyed by page number. |
433 const int kNumEntries = 1 << 14; | 433 const int kNumEntries = 1 << 14; |
434 int** p = reinterpret_cast<int**>(malloc(sizeof(*p) * kNumEntries)); | 434 int** p = reinterpret_cast<int**>(malloc(sizeof(*p) * kNumEntries)); |
435 int sum = 0; | 435 int sum = 0; |
(...skipping 25 matching lines...) Expand all Loading... |
461 } | 461 } |
462 } | 462 } |
463 | 463 |
464 #ifdef WIN32 | 464 #ifdef WIN32 |
465 // Test recalloc | 465 // Test recalloc |
466 TEST(Allocators, Recalloc) { | 466 TEST(Allocators, Recalloc) { |
467 for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { | 467 for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { |
468 for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { | 468 for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { |
469 unsigned char* src = | 469 unsigned char* src = |
470 reinterpret_cast<unsigned char*>(_recalloc(NULL, 1, src_size)); | 470 reinterpret_cast<unsigned char*>(_recalloc(NULL, 1, src_size)); |
471 EXPECT_EQ(true, IsZeroed(src, src_size)); | 471 EXPECT_TRUE(IsZeroed(src, src_size)); |
472 Fill(src, src_size); | 472 Fill(src, src_size); |
473 unsigned char* dst = | 473 unsigned char* dst = |
474 reinterpret_cast<unsigned char*>(_recalloc(src, 1, dst_size)); | 474 reinterpret_cast<unsigned char*>(_recalloc(src, 1, dst_size)); |
475 EXPECT_EQ(true, Valid(dst, min(src_size, dst_size))); | 475 EXPECT_TRUE(Valid(dst, min(src_size, dst_size))); |
476 Fill(dst, dst_size); | 476 Fill(dst, dst_size); |
477 EXPECT_EQ(true, Valid(dst, dst_size)); | 477 EXPECT_TRUE(Valid(dst, dst_size)); |
478 if (dst != NULL) | 478 if (dst != NULL) |
479 free(dst); | 479 free(dst); |
480 } | 480 } |
481 } | 481 } |
482 } | 482 } |
483 #endif | 483 #endif |
484 | 484 |
485 | 485 |
486 int main(int argc, char** argv) { | 486 int main(int argc, char** argv) { |
487 testing::InitGoogleTest(&argc, argv); | 487 testing::InitGoogleTest(&argc, argv); |
488 return RUN_ALL_TESTS(); | 488 return RUN_ALL_TESTS(); |
489 } | 489 } |
OLD | NEW |