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

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

Issue 2424063002: Add rtc::Optional::emplace (Closed)
Patch Set: Fix unpoisoning (asan) 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
« webrtc/base/optional.h ('K') | « 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 // clang-format off
kwiberg-webrtc 2016/10/18 08:46:19 Please don't switch off clang-format for such a la
danilchap 2016/10/18 10:37:31 Done.
400 TEST(OptionalTest, TestEmplaceEmptyWithExplicit) {
401 auto log = Logger::Setup();
402 {
403 Optional<Logger> x;
404 log->push_back("---");
405 x.emplace(42);
406 log->push_back("---");
407 }
408 EXPECT_EQ(V("---",
409 "0:42. explicit constructor",
410 "---",
411 "0:42. destructor"),
412 *log);
413 }
414
415 TEST(OptionalTest, TestEmplaceEmptyWithMultipleParameters) {
416 auto log = Logger::Setup();
417 {
418 Optional<Logger> x;
419 Logger ref(21);
420 Logger value(35);
421 log->push_back("---");
422 x.emplace(42, ref, std::move(value));
423 log->push_back("---");
424 }
425 EXPECT_EQ(V("0:21. explicit constructor",
426 "1:35. explicit constructor",
427 "---",
428 "2:35. move constructor (from 1:35)",
429 "3:42. multi parameter constructor",
430 "2:35. destructor",
431 "---",
432 "1:35. destructor",
433 "0:21. destructor",
434 "3:42. destructor"),
435 *log);
436 }
437
438 TEST(OptionalTest, TestEmplaceEmptyWithCopy) {
439 auto log = Logger::Setup();
440 {
441 Optional<Logger> x;
442 Logger y(42);
443 log->push_back("---");
444 x.emplace(y);
445 log->push_back("---");
446 }
447 EXPECT_EQ(V("0:42. explicit constructor",
448 "---",
449 "1:42. copy constructor (from 0:42)",
450 "---",
451 "0:42. destructor",
452 "1:42. destructor"),
453 *log);
454 }
455
456 TEST(OptionalTest, TestEmplaceEmptyWithMove) {
457 auto log = Logger::Setup();
458 {
459 Optional<Logger> x;
460 Logger y(42);
461 log->push_back("---");
462 x.emplace(std::move(y));
463 log->push_back("---");
464 }
465 EXPECT_EQ(V("0:42. explicit constructor",
466 "---",
467 "1:42. move constructor (from 0:42)",
468 "---",
469 "0:42. destructor",
470 "1:42. destructor"),
471 *log);
472 }
473
474 TEST(OptionalTest, TestEmplaceFullWithExplicit) {
475 auto log = Logger::Setup();
476 {
477 Optional<Logger> x(Logger(17));
478 log->push_back("---");
479 x.emplace(42);
480 log->push_back("---");
481 }
482 EXPECT_EQ(
483 V("0:17. explicit constructor",
484 "1:17. move constructor (from 0:17)",
485 "0:17. destructor",
486 "---",
487 "1:17. destructor",
488 "2:42. explicit constructor",
489 "---",
490 "2:42. destructor"),
491 *log);
492 }
493
494 TEST(OptionalTest, TestEmplaceFullWithMultipleParameters) {
495 auto log = Logger::Setup();
496 {
497 Optional<Logger> x(Logger(17));
498 Logger ref(21);
499 Logger value(35);
500 log->push_back("---");
501 x.emplace(42, ref, std::move(value));
502 log->push_back("---");
503 }
504 EXPECT_EQ(V("0:17. explicit constructor",
505 "1:17. move constructor (from 0:17)",
506 "0:17. destructor",
507 "2:21. explicit constructor",
508 "3:35. explicit constructor",
509 "---",
510 "1:17. destructor",
511 "4:35. move constructor (from 3:35)",
512 "5:42. multi parameter constructor",
513 "4:35. destructor",
514 "---",
515 "3:35. destructor",
516 "2:21. destructor",
517 "5:42. destructor"),
518 *log);
519 }
520
521 TEST(OptionalTest, TestEmplaceFullWithCopy) {
522 auto log = Logger::Setup();
523 {
524 Optional<Logger> x(Logger(17));
525 Logger y(42);
526 log->push_back("---");
527 x.emplace(y);
528 log->push_back("---");
529 }
530 EXPECT_EQ(V("0:17. explicit constructor",
531 "1:17. move constructor (from 0:17)",
532 "0:17. destructor",
533 "2:42. explicit constructor",
534 "---",
535 "1:17. destructor",
536 "3:42. copy constructor (from 2:42)",
537 "---",
538 "2:42. destructor",
539 "3:42. destructor"),
540 *log);
541 }
542
543 TEST(OptionalTest, TestEmplaceFullWithMove) {
544 auto log = Logger::Setup();
545 {
546 Optional<Logger> x(Logger(17));
547 Logger y(42);
548 log->push_back("---");
549 x.emplace(std::move(y));
550 log->push_back("---");
551 }
552 EXPECT_EQ(V("0:17. explicit constructor",
553 "1:17. move constructor (from 0:17)",
554 "0:17. destructor",
555 "2:42. explicit constructor",
556 "---",
557 "1:17. destructor",
558 "3:42. move constructor (from 2:42)",
559 "---",
560 "2:42. destructor",
561 "3:42. destructor"),
562 *log);
563 }
564 // clang-format on
565
395 TEST(OptionalTest, TestDereference) { 566 TEST(OptionalTest, TestDereference) {
396 auto log = Logger::Setup(); 567 auto log = Logger::Setup();
397 { 568 {
398 Optional<Logger> x(Logger(42)); 569 Optional<Logger> x(Logger(42));
399 const auto& y = x; 570 const auto& y = x;
400 log->push_back("---"); 571 log->push_back("---");
401 x->Foo(); 572 x->Foo();
402 y->Foo(); 573 y->Foo();
403 std::move(x)->Foo(); 574 std::move(x)->Foo();
404 std::move(y)->Foo(); 575 std::move(y)->Foo();
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 "2:17. copy constructor (from 0:17)", 657 "2:17. copy constructor (from 0:17)",
487 "3:42. copy constructor (from 1:42)", 658 "3:42. copy constructor (from 1:42)",
488 "4:17. copy constructor (from 0:17)", "---", "swap 2:42, 3:17", 659 "4:17. copy constructor (from 0:17)", "---", "swap 2:42, 3:17",
489 "5:17. move constructor (from 4:17)", "4:17. destructor", "---", 660 "5:17. move constructor (from 4:17)", "4:17. destructor", "---",
490 "5:17. destructor", "3:17. destructor", "2:42. destructor", 661 "5:17. destructor", "3:17. destructor", "2:42. destructor",
491 "1:42. destructor", "0:17. destructor"), 662 "1:42. destructor", "0:17. destructor"),
492 *log); 663 *log);
493 } 664 }
494 665
495 } // namespace rtc 666 } // namespace rtc
OLDNEW
« webrtc/base/optional.h ('K') | « webrtc/base/optional.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698