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 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 } | 391 } |
392 | 392 |
393 // This makes sure that reallocing a small number of bytes in either | 393 // This makes sure that reallocing a small number of bytes in either |
394 // direction doesn't cause us to allocate new memory. | 394 // direction doesn't cause us to allocate new memory. |
395 TEST(Allocators, Realloc1) { | 395 TEST(Allocators, Realloc1) { |
396 int start_sizes[] = { 100, 1000, 10000, 100000 }; | 396 int start_sizes[] = { 100, 1000, 10000, 100000 }; |
397 int deltas[] = { 1, -2, 4, -8, 16, -32, 64, -128 }; | 397 int deltas[] = { 1, -2, 4, -8, 16, -32, 64, -128 }; |
398 | 398 |
399 for (int s = 0; s < sizeof(start_sizes)/sizeof(*start_sizes); ++s) { | 399 for (int s = 0; s < sizeof(start_sizes)/sizeof(*start_sizes); ++s) { |
400 void* p = malloc(start_sizes[s]); | 400 void* p = malloc(start_sizes[s]); |
401 CHECK(p); | 401 ASSERT_TRUE(p); |
402 // The larger the start-size, the larger the non-reallocing delta. | 402 // The larger the start-size, the larger the non-reallocing delta. |
403 for (int d = 0; d < s*2; ++d) { | 403 for (int d = 0; d < s*2; ++d) { |
404 void* new_p = realloc(p, start_sizes[s] + deltas[d]); | 404 void* new_p = realloc(p, start_sizes[s] + deltas[d]); |
405 CHECK_EQ(p, new_p); // realloc should not allocate new memory | 405 ASSERT_EQ(p, new_p); // realloc should not allocate new memory |
406 } | 406 } |
407 // Test again, but this time reallocing smaller first. | 407 // Test again, but this time reallocing smaller first. |
408 for (int d = 0; d < s*2; ++d) { | 408 for (int d = 0; d < s*2; ++d) { |
409 void* new_p = realloc(p, start_sizes[s] - deltas[d]); | 409 void* new_p = realloc(p, start_sizes[s] - deltas[d]); |
410 CHECK_EQ(p, new_p); // realloc should not allocate new memory | 410 ASSERT_EQ(p, new_p); // realloc should not allocate new memory |
411 } | 411 } |
412 free(p); | 412 free(p); |
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); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |