OLD | NEW |
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" |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 TestLoad<AtomicType>(); | 273 TestLoad<AtomicType>(); |
274 } | 274 } |
275 | 275 |
276 static void TestCalloc(size_t n, size_t s, bool ok) { | 276 static void TestCalloc(size_t n, size_t s, bool ok) { |
277 char* p = reinterpret_cast<char*>(calloc(n, s)); | 277 char* p = reinterpret_cast<char*>(calloc(n, s)); |
278 if (!ok) { | 278 if (!ok) { |
279 EXPECT_EQ(NULL, p) << "calloc(n, s) should not succeed"; | 279 EXPECT_EQ(NULL, p) << "calloc(n, s) should not succeed"; |
280 } else { | 280 } else { |
281 EXPECT_NE(reinterpret_cast<void*>(NULL), p) << | 281 EXPECT_NE(reinterpret_cast<void*>(NULL), p) << |
282 "calloc(n, s) should succeed"; | 282 "calloc(n, s) should succeed"; |
283 for (int i = 0; i < n*s; i++) { | 283 for (size_t i = 0; i < n*s; i++) { |
284 EXPECT_EQ('\0', p[i]); | 284 EXPECT_EQ('\0', p[i]); |
285 } | 285 } |
286 free(p); | 286 free(p); |
287 } | 287 } |
288 } | 288 } |
289 | 289 |
290 // MSVC C4530 complains about exception handler usage when exceptions are | 290 // MSVC C4530 complains about exception handler usage when exceptions are |
291 // disabled. Temporarily disable that warning so we can test that they are, in | 291 // disabled. Temporarily disable that warning so we can test that they are, in |
292 // fact, disabled. | 292 // fact, disabled. |
293 #if defined(OS_WIN) | 293 #if defined(OS_WIN) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 | 340 |
341 #if defined(OS_WIN) | 341 #if defined(OS_WIN) |
342 #pragma warning(pop) | 342 #pragma warning(pop) |
343 #endif | 343 #endif |
344 | 344 |
345 } // namespace | 345 } // namespace |
346 | 346 |
347 //----------------------------------------------------------------------------- | 347 //----------------------------------------------------------------------------- |
348 | 348 |
349 TEST(Atomics, AtomicIncrementWord) { | 349 TEST(Atomics, AtomicIncrementWord) { |
350 TestAtomicIncrement<AtomicWord>(); | 350 TestAtomicIncrement<base::subtle::AtomicWord>(); |
351 } | 351 } |
352 | 352 |
353 TEST(Atomics, AtomicIncrement32) { | 353 TEST(Atomics, AtomicIncrement32) { |
354 TestAtomicIncrement<Atomic32>(); | 354 TestAtomicIncrement<base::subtle::Atomic32>(); |
355 } | 355 } |
356 | 356 |
357 TEST(Atomics, AtomicOpsWord) { | 357 TEST(Atomics, AtomicOpsWord) { |
358 TestAtomicIncrement<AtomicWord>(); | 358 TestAtomicIncrement<base::subtle::AtomicWord>(); |
359 } | 359 } |
360 | 360 |
361 TEST(Atomics, AtomicOps32) { | 361 TEST(Atomics, AtomicOps32) { |
362 TestAtomicIncrement<Atomic32>(); | 362 TestAtomicIncrement<base::subtle::Atomic32>(); |
363 } | 363 } |
364 | 364 |
365 TEST(Allocators, Malloc) { | 365 TEST(Allocators, Malloc) { |
366 // Try allocating data with a bunch of alignments and sizes | 366 // Try allocating data with a bunch of alignments and sizes |
367 for (int size = 1; size < 1048576; size *= 2) { | 367 for (int size = 1; size < 1048576; size *= 2) { |
368 unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size)); | 368 unsigned char* ptr = reinterpret_cast<unsigned char*>(malloc(size)); |
369 CheckAlignment(ptr, 2); // Should be 2 byte aligned | 369 CheckAlignment(ptr, 2); // Should be 2 byte aligned |
370 Fill(ptr, size); | 370 Fill(ptr, size); |
371 EXPECT_TRUE(Valid(ptr, size)); | 371 EXPECT_TRUE(Valid(ptr, size)); |
372 free(ptr); | 372 free(ptr); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 p[i] = reinterpret_cast<int*>(realloc(p[i], 9000)); | 451 p[i] = reinterpret_cast<int*>(realloc(p[i], 9000)); |
452 } | 452 } |
453 for (int i = 0; i < kNumEntries; i++) { | 453 for (int i = 0; i < kNumEntries; i++) { |
454 sum += p[i][1000]; | 454 sum += p[i][1000]; |
455 free(p[i]); | 455 free(p[i]); |
456 } | 456 } |
457 EXPECT_EQ(kNumEntries/2 * (kNumEntries - 1), sum); // assume kNE is even | 457 EXPECT_EQ(kNumEntries/2 * (kNumEntries - 1), sum); // assume kNE is even |
458 free(p); | 458 free(p); |
459 } | 459 } |
460 | 460 |
| 461 // tcmalloc uses these semantics but system allocators can return NULL for |
| 462 // realloc(ptr, 0). |
| 463 #if defined(USE_TCMALLOC) |
461 TEST(Allocators, ReallocZero) { | 464 TEST(Allocators, ReallocZero) { |
462 // Test that realloc to zero does not return NULL. | 465 // Test that realloc to zero does not return NULL. |
463 for (int size = 0; size >= 0; size = NextSize(size)) { | 466 for (int size = 0; size >= 0; size = NextSize(size)) { |
464 char* ptr = reinterpret_cast<char*>(malloc(size)); | 467 char* ptr = reinterpret_cast<char*>(malloc(size)); |
465 EXPECT_NE(static_cast<char*>(NULL), ptr); | 468 EXPECT_NE(static_cast<char*>(NULL), ptr); |
466 ptr = reinterpret_cast<char*>(realloc(ptr, 0)); | 469 ptr = reinterpret_cast<char*>(realloc(ptr, 0)); |
467 EXPECT_NE(static_cast<char*>(NULL), ptr); | 470 EXPECT_NE(static_cast<char*>(NULL), ptr); |
468 if (ptr) | 471 if (ptr) |
469 free(ptr); | 472 free(ptr); |
470 } | 473 } |
471 } | 474 } |
| 475 #endif |
472 | 476 |
473 #ifdef WIN32 | 477 #ifdef WIN32 |
474 // Test recalloc | 478 // Test recalloc |
475 TEST(Allocators, Recalloc) { | 479 TEST(Allocators, Recalloc) { |
476 for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { | 480 for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) { |
477 for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { | 481 for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) { |
478 unsigned char* src = | 482 unsigned char* src = |
479 reinterpret_cast<unsigned char*>(_recalloc(NULL, 1, src_size)); | 483 reinterpret_cast<unsigned char*>(_recalloc(NULL, 1, src_size)); |
480 EXPECT_TRUE(IsZeroed(src, src_size)); | 484 EXPECT_TRUE(IsZeroed(src, src_size)); |
481 Fill(src, src_size); | 485 Fill(src, src_size); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 } | 526 } |
523 } | 527 } |
524 | 528 |
525 #endif | 529 #endif |
526 | 530 |
527 | 531 |
528 int main(int argc, char** argv) { | 532 int main(int argc, char** argv) { |
529 testing::InitGoogleTest(&argc, argv); | 533 testing::InitGoogleTest(&argc, argv); |
530 return RUN_ALL_TESTS(); | 534 return RUN_ALL_TESTS(); |
531 } | 535 } |
OLD | NEW |