Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(642)

Side by Side Diff: third_party/tcmalloc/chromium/src/tests/debugallocation_test.cc

Issue 1076002: Revert 41938 - Merged third_party/tcmalloc/vendor/src(googleperftools r87) in... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2007, Google Inc. 1 // Copyright (c) 2007, Google Inc.
2 // All rights reserved. 2 // 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 are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * 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 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 95
96 // Allocate with new[]. 96 // Allocate with new[].
97 { 97 {
98 int* x = new int[1]; 98 int* x = new int[1];
99 IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free"); 99 IF_DEBUG_EXPECT_DEATH(free(x), "mismatch.*being dealloc.*free");
100 IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete"); 100 IF_DEBUG_EXPECT_DEATH(delete x, "mismatch.*being dealloc.*delete");
101 delete [] x; 101 delete [] x;
102 } 102 }
103 } 103 }
104 104
105 TEST(DebugAllocationTest, DoubleFree) {
106 int* pint = new int;
107 delete pint;
108 IF_DEBUG_EXPECT_DEATH(delete pint, "has been already deallocated");
109 }
110
111 TEST(DebugAllocationTest, StompBefore) {
112 int* pint = new int;
113 #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it
114 pint[-1] = 5;
115 IF_DEBUG_EXPECT_DEATH(delete pint, "a word before object");
116 #endif
117 }
118
119 TEST(DebugAllocationTest, StompAfter) {
120 int* pint = new int;
121 #ifndef NDEBUG // don't stomp memory if we're not in a position to detect it
122 pint[1] = 5;
123 IF_DEBUG_EXPECT_DEATH(delete pint, "a word after object");
124 #endif
125 }
126
127 TEST(DebugAllocationTest, FreeQueueTest) { 105 TEST(DebugAllocationTest, FreeQueueTest) {
128 // Verify that the allocator doesn't return blocks that were recently freed. 106 // Verify that the allocator doesn't return blocks that were recently freed.
129 int* x = new int; 107 int* x = new int;
130 int* old_x = x; 108 int* old_x = x;
131 delete x; 109 delete x;
132 x = new int; 110 x = new int;
133 #if 1 111 #if 1
134 // This check should not be read as a universal guarantee of behavior. If 112 // This check should not be read as a universal guarantee of behavior. If
135 // other threads are executing, it would be theoretically possible for this 113 // other threads are executing, it would be theoretically possible for this
136 // check to fail despite the efforts of debugallocation.cc to the contrary. 114 // check to fail despite the efforts of debugallocation.cc to the contrary.
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 198 }
221 #endif 199 #endif
222 void* a = malloc(1000); 200 void* a = malloc(1000);
223 EXPECT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000); 201 EXPECT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000);
224 // This is just a sanity check. If we allocated too much, alloc is broken 202 // This is just a sanity check. If we allocated too much, alloc is broken
225 EXPECT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000); 203 EXPECT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000);
226 EXPECT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000); 204 EXPECT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000);
227 free(a); 205 free(a);
228 } 206 }
229 207
230 TEST(DebugAllocationTest, HugeAlloc) {
231 const size_t kTooBig = ~static_cast<size_t>(0);
232 void* a = NULL;
233 char* b = NULL;
234
235 #ifndef NDEBUG
236
237 a = malloc(kTooBig);
238 EXPECT_EQ(NULL, a);
239 b = NULL;
240 IF_DEBUG_EXPECT_DEATH(b = new char[kTooBig],
241 "Unable to allocate.*new\\[\\] failed\\.");
242 EXPECT_EQ(NULL, b);
243
244 // kAlsoTooBig is small enough not to get caught by debugallocation's check,
245 // but will still fall through to tcmalloc's check.
246 const size_t kAlsoTooBig = kTooBig - 1024;
247
248 a = malloc(kAlsoTooBig);
249 EXPECT_EQ(NULL, a);
250 IF_DEBUG_EXPECT_DEATH(b = new char[kAlsoTooBig], "Unable to allocate.*new fail ed");
251 EXPECT_EQ(NULL, b);
252 #endif
253 }
254
255 int main(int argc, char** argv) { 208 int main(int argc, char** argv) {
256 // If you run without args, we run the non-death parts of the test. 209 // If you run without args, we run the non-death parts of the test.
257 // Otherwise, argv[1] should be a number saying which death-test 210 // Otherwise, argv[1] should be a number saying which death-test
258 // to run. We will output a regexp we expect the death-message 211 // to run. We will output a regexp we expect the death-message
259 // to include, and then run the given death test (which hopefully 212 // to include, and then run the given death test (which hopefully
260 // will produce that error message). If argv[1] > the number of 213 // will produce that error message). If argv[1] > the number of
261 // death tests, we will run only the non-death parts. One way to 214 // death tests, we will run only the non-death parts. One way to
262 // tell when you are done with all tests is when no 'expected 215 // tell when you are done with all tests is when no 'expected
263 // regexp' message is printed for a given argv[1]. 216 // regexp' message is printed for a given argv[1].
264 if (argc < 2) { 217 if (argc < 2) {
265 test_to_run = -1; // will never match 218 test_to_run = -1; // will never match
266 } else { 219 } else {
267 test_to_run = atoi(argv[1]); 220 test_to_run = atoi(argv[1]);
268 } 221 }
269 return RUN_ALL_TESTS(); 222 return RUN_ALL_TESTS();
270 } 223 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698