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

Side by Side Diff: webrtc/base/optional_unittest.cc

Issue 2424063002: Add rtc::Optional::emplace (Closed)
Patch Set: Feedback Created 4 years, 2 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
« no previous file with comments | « webrtc/base/optional.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 /* 1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 15 matching lines...) Expand all
26 // an origin ID. When a copy is made, the new object gets a fresh ID but copies 26 // an origin ID. When a copy is made, the new object gets a fresh ID but copies
27 // the origin ID from the original. When a new Logger is created from scratch, 27 // the origin ID from the original. When a new Logger is created from scratch,
28 // it gets a fresh ID, and the origin ID is the same as the ID (default 28 // it gets a fresh ID, and the origin ID is the same as the ID (default
29 // constructor) or given as an argument (explicit constructor). 29 // constructor) or given as an argument (explicit constructor).
30 class Logger { 30 class Logger {
31 public: 31 public:
32 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); } 32 Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); }
33 explicit Logger(int origin) : id_(g_next_id++), origin_(origin) { 33 explicit Logger(int origin) : id_(g_next_id++), origin_(origin) {
34 Log("explicit constructor"); 34 Log("explicit constructor");
35 } 35 }
36 Logger(int origin, const Logger& pass_by_ref, Logger pass_by_value)
37 : id_(g_next_id++), origin_(origin) {
38 Log("multi parameter constructor");
39 }
36 Logger(const Logger& other) : id_(g_next_id++), origin_(other.origin_) { 40 Logger(const Logger& other) : id_(g_next_id++), origin_(other.origin_) {
37 LogFrom("copy constructor", other); 41 LogFrom("copy constructor", other);
38 } 42 }
39 Logger(Logger&& other) : id_(g_next_id++), origin_(other.origin_) { 43 Logger(Logger&& other) : id_(g_next_id++), origin_(other.origin_) {
40 LogFrom("move constructor", other); 44 LogFrom("move constructor", other);
41 } 45 }
42 ~Logger() { Log("destructor"); } 46 ~Logger() { Log("destructor"); }
43 Logger& operator=(const Logger& other) { 47 Logger& operator=(const Logger& other) {
44 origin_ = other.origin_; 48 origin_ = other.origin_;
45 LogFrom("operator= copy", other); 49 LogFrom("operator= copy", other);
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 log->push_back("---"); 389 log->push_back("---");
386 x.reset(); 390 x.reset();
387 log->push_back("---"); 391 log->push_back("---");
388 } 392 }
389 EXPECT_EQ( 393 EXPECT_EQ(
390 V("0:17. explicit constructor", "1:17. move constructor (from 0:17)", 394 V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
391 "0:17. destructor", "---", "1:17. destructor", "---"), 395 "0:17. destructor", "---", "1:17. destructor", "---"),
392 *log); 396 *log);
393 } 397 }
394 398
399 TEST(OptionalTest, TestEmplaceEmptyWithExplicit) {
400 auto log = Logger::Setup();
401 {
402 Optional<Logger> x;
403 log->push_back("---");
404 x.emplace(42);
405 log->push_back("---");
406 }
407 // clang-format off
408 EXPECT_EQ(V("---",
409 "0:42. explicit constructor",
410 "---",
411 "0:42. destructor"),
412 *log);
413 // clang-format on
414 }
415
416 TEST(OptionalTest, TestEmplaceEmptyWithMultipleParameters) {
417 auto log = Logger::Setup();
418 {
419 Optional<Logger> x;
420 Logger ref(21);
421 Logger value(35);
422 log->push_back("---");
423 x.emplace(42, ref, std::move(value));
424 log->push_back("---");
425 }
426 // clang-format off
427 EXPECT_EQ(V("0:21. explicit constructor",
428 "1:35. explicit constructor",
429 "---",
430 "2:35. move constructor (from 1:35)",
431 "3:42. multi parameter constructor",
432 "2:35. destructor",
433 "---",
434 "1:35. destructor",
435 "0:21. destructor",
436 "3:42. destructor"),
437 *log);
438 // clang-format on
439 }
440
441 TEST(OptionalTest, TestEmplaceEmptyWithCopy) {
442 auto log = Logger::Setup();
443 {
444 Optional<Logger> x;
445 Logger y(42);
446 log->push_back("---");
447 x.emplace(y);
448 log->push_back("---");
449 }
450 // clang-format off
451 EXPECT_EQ(V("0:42. explicit constructor",
452 "---",
453 "1:42. copy constructor (from 0:42)",
454 "---",
455 "0:42. destructor",
456 "1:42. destructor"),
457 *log);
458 // clang-format on
459 }
460
461 TEST(OptionalTest, TestEmplaceEmptyWithMove) {
462 auto log = Logger::Setup();
463 {
464 Optional<Logger> x;
465 Logger y(42);
466 log->push_back("---");
467 x.emplace(std::move(y));
468 log->push_back("---");
469 }
470 // clang-format off
471 EXPECT_EQ(V("0:42. explicit constructor",
472 "---",
473 "1:42. move constructor (from 0:42)",
474 "---",
475 "0:42. destructor",
476 "1:42. destructor"),
477 *log);
478 // clang-format on
479 }
480
481 TEST(OptionalTest, TestEmplaceFullWithExplicit) {
482 auto log = Logger::Setup();
483 {
484 Optional<Logger> x(Logger(17));
485 log->push_back("---");
486 x.emplace(42);
487 log->push_back("---");
488 }
489 // clang-format off
490 EXPECT_EQ(
491 V("0:17. explicit constructor",
492 "1:17. move constructor (from 0:17)",
493 "0:17. destructor",
494 "---",
495 "1:17. destructor",
496 "2:42. explicit constructor",
497 "---",
498 "2:42. destructor"),
499 *log);
500 // clang-format on
501 }
502
503 TEST(OptionalTest, TestEmplaceFullWithMultipleParameters) {
504 auto log = Logger::Setup();
505 {
506 Optional<Logger> x(Logger(17));
507 Logger ref(21);
508 Logger value(35);
509 log->push_back("---");
510 x.emplace(42, ref, std::move(value));
511 log->push_back("---");
512 }
513 // clang-format off
514 EXPECT_EQ(V("0:17. explicit constructor",
515 "1:17. move constructor (from 0:17)",
516 "0:17. destructor",
517 "2:21. explicit constructor",
518 "3:35. explicit constructor",
519 "---",
520 "1:17. destructor",
521 "4:35. move constructor (from 3:35)",
522 "5:42. multi parameter constructor",
523 "4:35. destructor",
524 "---",
525 "3:35. destructor",
526 "2:21. destructor",
527 "5:42. destructor"),
528 *log);
529 // clang-format on
530 }
531
532 TEST(OptionalTest, TestEmplaceFullWithCopy) {
533 auto log = Logger::Setup();
534 {
535 Optional<Logger> x(Logger(17));
536 Logger y(42);
537 log->push_back("---");
538 x.emplace(y);
539 log->push_back("---");
540 }
541 // clang-format off
542 EXPECT_EQ(V("0:17. explicit constructor",
543 "1:17. move constructor (from 0:17)",
544 "0:17. destructor",
545 "2:42. explicit constructor",
546 "---",
547 "1:17. destructor",
548 "3:42. copy constructor (from 2:42)",
549 "---",
550 "2:42. destructor",
551 "3:42. destructor"),
552 *log);
553 // clang-format on
554 }
555
556 TEST(OptionalTest, TestEmplaceFullWithMove) {
557 auto log = Logger::Setup();
558 {
559 Optional<Logger> x(Logger(17));
560 Logger y(42);
561 log->push_back("---");
562 x.emplace(std::move(y));
563 log->push_back("---");
564 }
565 // clang-format off
566 EXPECT_EQ(V("0:17. explicit constructor",
567 "1:17. move constructor (from 0:17)",
568 "0:17. destructor",
569 "2:42. explicit constructor",
570 "---",
571 "1:17. destructor",
572 "3:42. move constructor (from 2:42)",
573 "---",
574 "2:42. destructor",
575 "3:42. destructor"),
576 *log);
577 // clang-format on
578 }
579
395 TEST(OptionalTest, TestDereference) { 580 TEST(OptionalTest, TestDereference) {
396 auto log = Logger::Setup(); 581 auto log = Logger::Setup();
397 { 582 {
398 Optional<Logger> x(Logger(42)); 583 Optional<Logger> x(Logger(42));
399 const auto& y = x; 584 const auto& y = x;
400 log->push_back("---"); 585 log->push_back("---");
401 x->Foo(); 586 x->Foo();
402 y->Foo(); 587 y->Foo();
403 std::move(x)->Foo(); 588 std::move(x)->Foo();
404 std::move(y)->Foo(); 589 std::move(y)->Foo();
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 "2:17. copy constructor (from 0:17)", 671 "2:17. copy constructor (from 0:17)",
487 "3:42. copy constructor (from 1:42)", 672 "3:42. copy constructor (from 1:42)",
488 "4:17. copy constructor (from 0:17)", "---", "swap 2:42, 3:17", 673 "4:17. copy constructor (from 0:17)", "---", "swap 2:42, 3:17",
489 "5:17. move constructor (from 4:17)", "4:17. destructor", "---", 674 "5:17. move constructor (from 4:17)", "4:17. destructor", "---",
490 "5:17. destructor", "3:17. destructor", "2:42. destructor", 675 "5:17. destructor", "3:17. destructor", "2:42. destructor",
491 "1:42. destructor", "0:17. destructor"), 676 "1:42. destructor", "0:17. destructor"),
492 *log); 677 *log);
493 } 678 }
494 679
495 } // namespace rtc 680 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/optional.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698