1/*
2 * Copyright (C) 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include "LifecycleLogger.h"
29#include "MoveOnlyLifecycleLogger.h"
30#include <wtf/NeverDestroyed.h>
31#include <wtf/Vector.h>
32
33namespace TestWebKitAPI {
34
35TEST(WTF_NeverDestroyed, Construct)
36{
37 { static NeverDestroyed<LifecycleLogger> x; UNUSED_PARAM(x);}
38 ASSERT_STREQ("construct(<default>) ", takeLogStr().c_str());
39
40 { static NeverDestroyed<LifecycleLogger> x("name"); UNUSED_PARAM(x); }
41 ASSERT_STREQ("construct(name) ", takeLogStr().c_str());
42
43 { static auto x = makeNeverDestroyed(LifecycleLogger { "name" }); UNUSED_PARAM(x); }
44 ASSERT_STREQ("construct(name) move-construct(name) destruct(<default>) ", takeLogStr().c_str());
45
46 {
47 static NeverDestroyed<LifecycleLogger> x("name");
48 LifecycleLogger l = x.get();
49 l.setName("x");
50 ASSERT_STREQ(x.get().name, "name");
51 }
52 ASSERT_STREQ("construct(name) copy-construct(name) set-name(x) destruct(x) ", takeLogStr().c_str());
53
54 { static NeverDestroyed<LifecycleLogger> x { [] { return LifecycleLogger { "name" }; }() }; UNUSED_PARAM(x); }
55 ASSERT_STREQ("construct(name) move-construct(name) destruct(<default>) ", takeLogStr().c_str());
56
57 {
58 static auto x = makeNeverDestroyed([] {
59 LifecycleLogger l { "name" };
60 l.setName("x");
61 return l;
62 }());
63 ASSERT_STREQ(x.get().name, "x");
64 }
65#if COMPILER(MSVC) && !defined(NDEBUG)
66 ASSERT_STREQ("construct(name) set-name(x) move-construct(x) destruct(<default>) move-construct(x) destruct(<default>) ", takeLogStr().c_str());
67#else
68 ASSERT_STREQ("construct(name) set-name(x) move-construct(x) destruct(<default>) ", takeLogStr().c_str());
69#endif
70
71 {
72 static NeverDestroyed<LifecycleLogger> x;
73 static NeverDestroyed<LifecycleLogger> y { WTFMove(x) };
74 UNUSED_PARAM(y);
75 }
76 ASSERT_STREQ("construct(<default>) move-construct(<default>) ", takeLogStr().c_str());
77
78 {
79 static NeverDestroyed<const LifecycleLogger> x;
80 static NeverDestroyed<const LifecycleLogger> y { WTFMove(x) };
81 UNUSED_PARAM(y);
82 }
83 ASSERT_STREQ("construct(<default>) move-construct(<default>) ", takeLogStr().c_str());
84
85 { static NeverDestroyed<MoveOnlyLifecycleLogger> x { [] { return MoveOnlyLifecycleLogger { "name" }; }() }; UNUSED_PARAM(x); }
86 ASSERT_STREQ("construct(name) move-construct(name) destruct(<default>) ", takeLogStr().c_str());
87
88 {
89 static auto x = makeNeverDestroyed([] {
90 MoveOnlyLifecycleLogger l { "name" };
91 l.setName("x");
92 return l;
93 }());
94 UNUSED_PARAM(x);
95 }
96#if COMPILER(MSVC) && !defined(NDEBUG)
97 ASSERT_STREQ("construct(name) set-name(x) move-construct(x) destruct(<default>) move-construct(x) destruct(<default>) ", takeLogStr().c_str());
98#else
99 ASSERT_STREQ("construct(name) set-name(x) move-construct(x) destruct(<default>) ", takeLogStr().c_str());
100#endif
101}
102
103static const Vector<int>& list()
104{
105 static const auto x = makeNeverDestroyed(Vector<int> { 1, 2, 3 });
106 return x;
107}
108
109TEST(WTF_NeverDestroyed, Basic)
110{
111 ASSERT_EQ(list().size(), 3u);
112 ASSERT_EQ(&list(), &list());
113}
114
115} // namespace TestWebKitAPI
116