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

Side by Side Diff: base/memory/scoped_ptr_unittest.cc

Issue 14081006: Remove scoped_array from Chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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
« no previous file with comments | « base/memory/scoped_ptr.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 scoped_ptr<ConDecLogger[]> scoper3; 371 scoped_ptr<ConDecLogger[]> scoper3;
372 scoper3 = scoper2.Pass(); 372 scoper3 = scoper2.Pass();
373 EXPECT_EQ(kNumLoggers, constructed); 373 EXPECT_EQ(kNumLoggers, constructed);
374 EXPECT_FALSE(scoper); 374 EXPECT_FALSE(scoper);
375 EXPECT_FALSE(scoper2); 375 EXPECT_FALSE(scoper2);
376 EXPECT_TRUE(scoper3); 376 EXPECT_TRUE(scoper3);
377 } 377 }
378 EXPECT_EQ(0, constructed); 378 EXPECT_EQ(0, constructed);
379 } 379 }
380 380
381 TEST(ScopedPtrTest, ScopedArray) {
382 static const int kNumLoggers = 12;
383
384 int constructed = 0;
385
386 {
387 scoped_array<ConDecLogger> scoper(new ConDecLogger[kNumLoggers]);
388 EXPECT_TRUE(scoper.get());
389 EXPECT_EQ(&scoper[0], scoper.get());
390 for (int i = 0; i < kNumLoggers; ++i) {
391 scoper[i].SetPtr(&constructed);
392 }
393 EXPECT_EQ(12, constructed);
394
395 EXPECT_EQ(10, scoper.get()->SomeMeth(10));
396 EXPECT_EQ(10, scoper[2].SomeMeth(10));
397 }
398 EXPECT_EQ(0, constructed);
399
400 // Test reset() and release()
401 {
402 scoped_array<ConDecLogger> scoper;
403 EXPECT_FALSE(scoper.get());
404 EXPECT_FALSE(scoper.release());
405 EXPECT_FALSE(scoper.get());
406 scoper.reset();
407 EXPECT_FALSE(scoper.get());
408
409 scoper.reset(new ConDecLogger[kNumLoggers]);
410 for (int i = 0; i < kNumLoggers; ++i) {
411 scoper[i].SetPtr(&constructed);
412 }
413 EXPECT_EQ(12, constructed);
414 scoper.reset();
415 EXPECT_EQ(0, constructed);
416
417 scoper.reset(new ConDecLogger[kNumLoggers]);
418 for (int i = 0; i < kNumLoggers; ++i) {
419 scoper[i].SetPtr(&constructed);
420 }
421 EXPECT_EQ(12, constructed);
422 ConDecLogger* ptr = scoper.release();
423 EXPECT_EQ(12, constructed);
424 delete[] ptr;
425 EXPECT_EQ(0, constructed);
426 }
427 EXPECT_EQ(0, constructed);
428
429 // Test swap(), ==, !=, and type-safe Boolean.
430 {
431 scoped_array<ConDecLogger> scoper1;
432 scoped_array<ConDecLogger> scoper2;
433 EXPECT_TRUE(scoper1 == scoper2.get());
434 EXPECT_FALSE(scoper1 != scoper2.get());
435
436 ConDecLogger* loggers = new ConDecLogger[kNumLoggers];
437 for (int i = 0; i < kNumLoggers; ++i) {
438 loggers[i].SetPtr(&constructed);
439 }
440 scoper1.reset(loggers);
441 EXPECT_TRUE(scoper1);
442 EXPECT_EQ(loggers, scoper1.get());
443 EXPECT_FALSE(scoper2);
444 EXPECT_FALSE(scoper2.get());
445 EXPECT_FALSE(scoper1 == scoper2.get());
446 EXPECT_TRUE(scoper1 != scoper2.get());
447
448 scoper2.swap(scoper1);
449 EXPECT_EQ(loggers, scoper2.get());
450 EXPECT_FALSE(scoper1.get());
451 EXPECT_FALSE(scoper1 == scoper2.get());
452 EXPECT_TRUE(scoper1 != scoper2.get());
453 }
454 EXPECT_EQ(0, constructed);
455 }
456
457 TEST(ScopedPtrTest, PassBehavior) { 381 TEST(ScopedPtrTest, PassBehavior) {
458 int constructed = 0; 382 int constructed = 0;
459 { 383 {
460 ConDecLogger* logger = new ConDecLogger(&constructed); 384 ConDecLogger* logger = new ConDecLogger(&constructed);
461 scoped_ptr<ConDecLogger> scoper(logger); 385 scoped_ptr<ConDecLogger> scoper(logger);
462 EXPECT_EQ(1, constructed); 386 EXPECT_EQ(1, constructed);
463 387
464 // Test Pass() with constructor; 388 // Test Pass() with constructor;
465 scoped_ptr<ConDecLogger> scoper2(scoper.Pass()); 389 scoped_ptr<ConDecLogger> scoper2(scoper.Pass());
466 EXPECT_EQ(1, constructed); 390 EXPECT_EQ(1, constructed);
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 scoped_ptr<OverloadedNewAndDelete> scoper(new OverloadedNewAndDelete()); 597 scoped_ptr<OverloadedNewAndDelete> scoper(new OverloadedNewAndDelete());
674 EXPECT_TRUE(scoper.get()); 598 EXPECT_TRUE(scoper.get());
675 599
676 scoped_ptr<OverloadedNewAndDelete> scoper2(scoper.Pass()); 600 scoped_ptr<OverloadedNewAndDelete> scoper2(scoper.Pass());
677 } 601 }
678 EXPECT_EQ(1, OverloadedNewAndDelete::delete_count()); 602 EXPECT_EQ(1, OverloadedNewAndDelete::delete_count());
679 EXPECT_EQ(1, OverloadedNewAndDelete::new_count()); 603 EXPECT_EQ(1, OverloadedNewAndDelete::new_count());
680 } 604 }
681 605
682 // TODO scoped_ptr_malloc 606 // TODO scoped_ptr_malloc
OLDNEW
« no previous file with comments | « base/memory/scoped_ptr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698