| 1 | /* |
| 2 | * Copyright (C) 2013-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "LifecycleLogger.h" |
| 28 | |
| 29 | namespace TestWebKitAPI { |
| 30 | |
| 31 | LifecycleLogger::LifecycleLogger() |
| 32 | { |
| 33 | log() << "construct(" << name << ") " ; |
| 34 | } |
| 35 | |
| 36 | LifecycleLogger::LifecycleLogger(const char* name) |
| 37 | : name { name } |
| 38 | { |
| 39 | log() << "construct(" << name << ") " ; |
| 40 | } |
| 41 | |
| 42 | LifecycleLogger::LifecycleLogger(const LifecycleLogger& other) |
| 43 | : name { other.name } |
| 44 | { |
| 45 | log() << "copy-construct(" << name << ") " ; |
| 46 | } |
| 47 | |
| 48 | LifecycleLogger::LifecycleLogger(LifecycleLogger&& other) |
| 49 | { |
| 50 | std::swap(name, other.name); |
| 51 | log() << "move-construct(" << name << ") " ; |
| 52 | } |
| 53 | |
| 54 | LifecycleLogger& LifecycleLogger::operator=(const LifecycleLogger& other) |
| 55 | { |
| 56 | name = other.name; |
| 57 | log() << "copy-assign(" << name << ") " ; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | LifecycleLogger& LifecycleLogger::operator=(LifecycleLogger&& other) |
| 62 | { |
| 63 | std::swap(name, other.name); |
| 64 | log() << "move-assign(" << name << ") " ; |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | LifecycleLogger::~LifecycleLogger() |
| 69 | { |
| 70 | log() << "destruct(" << name << ") " ; |
| 71 | } |
| 72 | |
| 73 | void LifecycleLogger::setName(const char* newName) |
| 74 | { |
| 75 | name = newName; |
| 76 | log() << "set-name(" << name << ") " ; |
| 77 | } |
| 78 | |
| 79 | TEST(LifecycleLogger, Basic) |
| 80 | { |
| 81 | { LifecycleLogger l; } |
| 82 | ASSERT_STREQ("construct(<default>) destruct(<default>) " , takeLogStr().c_str()); |
| 83 | |
| 84 | { LifecycleLogger l("a" ); } |
| 85 | ASSERT_STREQ("construct(a) destruct(a) " , takeLogStr().c_str()); |
| 86 | |
| 87 | { LifecycleLogger l("b" ); LifecycleLogger l2(l); } |
| 88 | ASSERT_STREQ("construct(b) copy-construct(b) destruct(b) destruct(b) " , takeLogStr().c_str()); |
| 89 | |
| 90 | { LifecycleLogger l("c" ); LifecycleLogger l2; l2 = l; } |
| 91 | ASSERT_STREQ("construct(c) construct(<default>) copy-assign(c) destruct(c) destruct(c) " , takeLogStr().c_str()); |
| 92 | |
| 93 | { LifecycleLogger l("d" ); LifecycleLogger l2(WTFMove(l)); } |
| 94 | ASSERT_STREQ("construct(d) move-construct(d) destruct(d) destruct(<default>) " , takeLogStr().c_str()); |
| 95 | |
| 96 | { LifecycleLogger l("e" ); LifecycleLogger l2; l2 = WTFMove(l); } |
| 97 | ASSERT_STREQ("construct(e) construct(<default>) move-assign(e) destruct(e) destruct(<default>) " , takeLogStr().c_str()); |
| 98 | |
| 99 | { LifecycleLogger l("f" ); l.setName("x" ); } |
| 100 | ASSERT_STREQ("construct(f) set-name(x) destruct(x) " , takeLogStr().c_str()); |
| 101 | |
| 102 | { static LifecycleLogger l("g" ); } |
| 103 | ASSERT_STREQ("construct(g) " , takeLogStr().c_str()); |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |