malloc_zone_functions_mac_unittest.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/allocator/malloc_zone_functions_mac.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace base {
  7. namespace allocator {
  8. class MallocZoneFunctionsTest : public testing::Test {
  9. protected:
  10. void TearDown() override { ClearAllMallocZonesForTesting(); }
  11. };
  12. TEST_F(MallocZoneFunctionsTest, TestDefaultZoneMallocFree) {
  13. ChromeMallocZone* malloc_zone =
  14. reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
  15. StoreMallocZone(malloc_zone);
  16. int* test = reinterpret_cast<int*>(
  17. g_malloc_zones[0].malloc(malloc_default_zone(), 33));
  18. test[0] = 1;
  19. test[1] = 2;
  20. g_malloc_zones[0].free(malloc_default_zone(), test);
  21. }
  22. TEST_F(MallocZoneFunctionsTest, IsZoneAlreadyStored) {
  23. ChromeMallocZone* malloc_zone =
  24. reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
  25. EXPECT_FALSE(IsMallocZoneAlreadyStored(malloc_zone));
  26. StoreMallocZone(malloc_zone);
  27. EXPECT_TRUE(IsMallocZoneAlreadyStored(malloc_zone));
  28. }
  29. TEST_F(MallocZoneFunctionsTest, CannotDoubleStoreZone) {
  30. ChromeMallocZone* malloc_zone =
  31. reinterpret_cast<ChromeMallocZone*>(malloc_default_zone());
  32. StoreMallocZone(malloc_zone);
  33. StoreMallocZone(malloc_zone);
  34. EXPECT_EQ(1, GetMallocZoneCountForTesting());
  35. }
  36. TEST_F(MallocZoneFunctionsTest, CannotStoreMoreThanMaxZones) {
  37. std::vector<ChromeMallocZone> zones;
  38. zones.resize(kMaxZoneCount * 2);
  39. for (int i = 0; i < kMaxZoneCount * 2; ++i) {
  40. ChromeMallocZone& zone = zones[i];
  41. memcpy(&zone, malloc_default_zone(), sizeof(ChromeMallocZone));
  42. StoreMallocZone(&zone);
  43. }
  44. int max_zone_count = kMaxZoneCount;
  45. EXPECT_EQ(max_zone_count, GetMallocZoneCountForTesting());
  46. }
  47. } // namespace allocator
  48. } // namespace base