| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 id = TestIds++; | 54 id = TestIds++; |
| 55 } | 55 } |
| 56 int id; | 56 int id; |
| 57 int padding; | 57 int padding; |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 } // anonymous namespace | 60 } // anonymous namespace |
| 61 | 61 |
| 62 class PODFreeListArenaTest : public testing::Test { | 62 class PODFreeListArenaTest : public testing::Test { |
| 63 protected: | 63 protected: |
| 64 int getFreeListSize(const PassRefPtr<PODFreeListArena<TestClass1> > arena) c
onst | 64 int getFreeListSize(const PassRefPtr<PODFreeListArena<TestClass1>> arena) co
nst |
| 65 { | 65 { |
| 66 return arena->getFreeListSizeForTesting(); | 66 return arena->getFreeListSizeForTesting(); |
| 67 } | 67 } |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 // Make sure the arena can successfully allocate from more than one | 70 // Make sure the arena can successfully allocate from more than one |
| 71 // region. | 71 // region. |
| 72 TEST_F(PODFreeListArenaTest, CanAllocateFromMoreThanOneRegion) | 72 TEST_F(PODFreeListArenaTest, CanAllocateFromMoreThanOneRegion) |
| 73 { | 73 { |
| 74 RefPtr<TrackedAllocator> allocator = TrackedAllocator::create(); | 74 RefPtr<TrackedAllocator> allocator = TrackedAllocator::create(); |
| 75 RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::
create(allocator); | 75 RefPtr<PODFreeListArena<TestClass1>> arena = PODFreeListArena<TestClass1>::c
reate(allocator); |
| 76 int numIterations = 10 * PODArena::DefaultChunkSize / sizeof(TestClass1); | 76 int numIterations = 10 * PODArena::DefaultChunkSize / sizeof(TestClass1); |
| 77 for (int i = 0; i < numIterations; ++i) | 77 for (int i = 0; i < numIterations; ++i) |
| 78 arena->allocateObject(); | 78 arena->allocateObject(); |
| 79 EXPECT_GT(allocator->numRegions(), 1); | 79 EXPECT_GT(allocator->numRegions(), 1); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Make sure the arena frees all allocated regions during destruction. | 82 // Make sure the arena frees all allocated regions during destruction. |
| 83 TEST_F(PODFreeListArenaTest, FreesAllAllocatedRegions) | 83 TEST_F(PODFreeListArenaTest, FreesAllAllocatedRegions) |
| 84 { | 84 { |
| 85 RefPtr<TrackedAllocator> allocator = TrackedAllocator::create(); | 85 RefPtr<TrackedAllocator> allocator = TrackedAllocator::create(); |
| 86 { | 86 { |
| 87 RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass
1>::create(allocator); | 87 RefPtr<PODFreeListArena<TestClass1>> arena = PODFreeListArena<TestClass1
>::create(allocator); |
| 88 for (int i = 0; i < 3; i++) | 88 for (int i = 0; i < 3; i++) |
| 89 arena->allocateObject(); | 89 arena->allocateObject(); |
| 90 EXPECT_GT(allocator->numRegions(), 0); | 90 EXPECT_GT(allocator->numRegions(), 0); |
| 91 } | 91 } |
| 92 EXPECT_TRUE(allocator->isEmpty()); | 92 EXPECT_TRUE(allocator->isEmpty()); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Make sure the arena runs constructors of the objects allocated within. | 95 // Make sure the arena runs constructors of the objects allocated within. |
| 96 TEST_F(PODFreeListArenaTest, RunsConstructorsOnNewObjects) | 96 TEST_F(PODFreeListArenaTest, RunsConstructorsOnNewObjects) |
| 97 { | 97 { |
| 98 RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::
create(); | 98 RefPtr<PODFreeListArena<TestClass1>> arena = PODFreeListArena<TestClass1>::c
reate(); |
| 99 for (int i = 0; i < 10000; i++) { | 99 for (int i = 0; i < 10000; i++) { |
| 100 TestClass1* tc1 = arena->allocateObject(); | 100 TestClass1* tc1 = arena->allocateObject(); |
| 101 EXPECT_EQ(0, tc1->x); | 101 EXPECT_EQ(0, tc1->x); |
| 102 EXPECT_EQ(0, tc1->y); | 102 EXPECT_EQ(0, tc1->y); |
| 103 EXPECT_EQ(0, tc1->z); | 103 EXPECT_EQ(0, tc1->z); |
| 104 EXPECT_EQ(1, tc1->w); | 104 EXPECT_EQ(1, tc1->w); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Make sure the arena runs constructors of the objects allocated within. | 108 // Make sure the arena runs constructors of the objects allocated within. |
| 109 TEST_F(PODFreeListArenaTest, RunsConstructorsOnReusedObjects) | 109 TEST_F(PODFreeListArenaTest, RunsConstructorsOnReusedObjects) |
| 110 { | 110 { |
| 111 std::set<TestClass1*> objects; | 111 std::set<TestClass1*> objects; |
| 112 RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::
create(); | 112 RefPtr<PODFreeListArena<TestClass1>> arena = PODFreeListArena<TestClass1>::c
reate(); |
| 113 for (int i = 0; i < 100; i++) { | 113 for (int i = 0; i < 100; i++) { |
| 114 TestClass1* tc1 = arena->allocateObject(); | 114 TestClass1* tc1 = arena->allocateObject(); |
| 115 tc1->x = 100; | 115 tc1->x = 100; |
| 116 tc1->y = 101; | 116 tc1->y = 101; |
| 117 tc1->z = 102; | 117 tc1->z = 102; |
| 118 tc1->w = 103; | 118 tc1->w = 103; |
| 119 | 119 |
| 120 objects.insert(tc1); | 120 objects.insert(tc1); |
| 121 } | 121 } |
| 122 for (std::set<TestClass1*>::iterator it = objects.begin(); it != objects.end
(); ++it) { | 122 for (std::set<TestClass1*>::iterator it = objects.begin(); it != objects.end
(); ++it) { |
| 123 arena->freeObject(*it); | 123 arena->freeObject(*it); |
| 124 } | 124 } |
| 125 for (int i = 0; i < 100; i++) { | 125 for (int i = 0; i < 100; i++) { |
| 126 TestClass1* cur = arena->allocateObject(); | 126 TestClass1* cur = arena->allocateObject(); |
| 127 EXPECT_TRUE(objects.find(cur) != objects.end()); | 127 EXPECT_TRUE(objects.find(cur) != objects.end()); |
| 128 EXPECT_EQ(0, cur->x); | 128 EXPECT_EQ(0, cur->x); |
| 129 EXPECT_EQ(0, cur->y); | 129 EXPECT_EQ(0, cur->y); |
| 130 EXPECT_EQ(0, cur->z); | 130 EXPECT_EQ(0, cur->z); |
| 131 EXPECT_EQ(1, cur->w); | 131 EXPECT_EQ(1, cur->w); |
| 132 | 132 |
| 133 objects.erase(cur); | 133 objects.erase(cur); |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Make sure freeObject puts the object in the free list. | 137 // Make sure freeObject puts the object in the free list. |
| 138 TEST_F(PODFreeListArenaTest, AddsFreedObjectsToFreedList) | 138 TEST_F(PODFreeListArenaTest, AddsFreedObjectsToFreedList) |
| 139 { | 139 { |
| 140 std::vector<TestClass1*> objects; | 140 std::vector<TestClass1*> objects; |
| 141 RefPtr<PODFreeListArena<TestClass1> > arena = PODFreeListArena<TestClass1>::
create(); | 141 RefPtr<PODFreeListArena<TestClass1>> arena = PODFreeListArena<TestClass1>::c
reate(); |
| 142 for (int i = 0; i < 100; i++) { | 142 for (int i = 0; i < 100; i++) { |
| 143 objects.push_back(arena->allocateObject()); | 143 objects.push_back(arena->allocateObject()); |
| 144 } | 144 } |
| 145 for (std::vector<TestClass1*>::iterator it = objects.begin(); it != objects.
end(); ++it) { | 145 for (std::vector<TestClass1*>::iterator it = objects.begin(); it != objects.
end(); ++it) { |
| 146 arena->freeObject(*it); | 146 arena->freeObject(*it); |
| 147 } | 147 } |
| 148 EXPECT_EQ(100, getFreeListSize(arena)); | 148 EXPECT_EQ(100, getFreeListSize(arena)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // Make sure allocations use previously freed memory. | 151 // Make sure allocations use previously freed memory. |
| 152 TEST_F(PODFreeListArenaTest, ReusesPreviouslyFreedObjects) | 152 TEST_F(PODFreeListArenaTest, ReusesPreviouslyFreedObjects) |
| 153 { | 153 { |
| 154 std::set<TestClass2*> objects; | 154 std::set<TestClass2*> objects; |
| 155 RefPtr<PODFreeListArena<TestClass2> > arena = PODFreeListArena<TestClass2>::
create(); | 155 RefPtr<PODFreeListArena<TestClass2>> arena = PODFreeListArena<TestClass2>::c
reate(); |
| 156 for (int i = 0; i < 100; i++) { | 156 for (int i = 0; i < 100; i++) { |
| 157 objects.insert(arena->allocateObject()); | 157 objects.insert(arena->allocateObject()); |
| 158 } | 158 } |
| 159 for (std::set<TestClass2*>::iterator it = objects.begin(); it != objects.end
(); ++it) { | 159 for (std::set<TestClass2*>::iterator it = objects.begin(); it != objects.end
(); ++it) { |
| 160 arena->freeObject(*it); | 160 arena->freeObject(*it); |
| 161 } | 161 } |
| 162 for (int i = 0; i < 100; i++) { | 162 for (int i = 0; i < 100; i++) { |
| 163 TestClass2* cur = arena->allocateObject(); | 163 TestClass2* cur = arena->allocateObject(); |
| 164 EXPECT_TRUE(objects.find(cur) != objects.end()); | 164 EXPECT_TRUE(objects.find(cur) != objects.end()); |
| 165 EXPECT_TRUE(cur->id >= 100 && cur->id < 200); | 165 EXPECT_TRUE(cur->id >= 100 && cur->id < 200); |
| 166 objects.erase(cur); | 166 objects.erase(cur); |
| 167 } | 167 } |
| 168 } | 168 } |
| 169 | 169 |
| 170 } // namespace blink | 170 } // namespace blink |
| OLD | NEW |