1 | /* |
2 | * Copyright (C) 2015 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 "RefLogger.h" |
29 | #include <wtf/NakedPtr.h> |
30 | |
31 | namespace TestWebKitAPI { |
32 | |
33 | // For these tests, we need a base class and a derived class. For this purpose, |
34 | // we reuse the RefLogger and DerivedRefLogger classes. |
35 | |
36 | TEST(WTF_NakedPtr, Basic) |
37 | { |
38 | DerivedRefLogger a("a" ); |
39 | |
40 | NakedPtr<RefLogger> empty; |
41 | ASSERT_EQ(nullptr, empty.get()); |
42 | |
43 | { |
44 | NakedPtr<RefLogger> ptr(&a); |
45 | ASSERT_EQ(&a, ptr.get()); |
46 | ASSERT_EQ(&a, &*ptr); |
47 | ASSERT_EQ(&a.name, &ptr->name); |
48 | } |
49 | |
50 | { |
51 | NakedPtr<RefLogger> ptr = &a; |
52 | ASSERT_EQ(&a, ptr.get()); |
53 | } |
54 | |
55 | { |
56 | NakedPtr<RefLogger> p1 = &a; |
57 | NakedPtr<RefLogger> p2(p1); |
58 | ASSERT_EQ(&a, p1.get()); |
59 | ASSERT_EQ(&a, p2.get()); |
60 | } |
61 | |
62 | { |
63 | NakedPtr<RefLogger> p1 = &a; |
64 | NakedPtr<RefLogger> p2 = p1; |
65 | ASSERT_EQ(&a, p1.get()); |
66 | ASSERT_EQ(&a, p2.get()); |
67 | } |
68 | |
69 | { |
70 | NakedPtr<RefLogger> p1 = &a; |
71 | NakedPtr<RefLogger> p2 = WTFMove(p1); |
72 | ASSERT_EQ(&a, p1.get()); |
73 | ASSERT_EQ(&a, p2.get()); |
74 | } |
75 | |
76 | { |
77 | NakedPtr<RefLogger> p1 = &a; |
78 | NakedPtr<RefLogger> p2(WTFMove(p1)); |
79 | ASSERT_EQ(&a, p1.get()); |
80 | ASSERT_EQ(&a, p2.get()); |
81 | } |
82 | |
83 | { |
84 | NakedPtr<DerivedRefLogger> p1 = &a; |
85 | NakedPtr<RefLogger> p2 = p1; |
86 | ASSERT_EQ(&a, p1.get()); |
87 | ASSERT_EQ(&a, p2.get()); |
88 | } |
89 | |
90 | { |
91 | NakedPtr<DerivedRefLogger> p1 = &a; |
92 | NakedPtr<RefLogger> p2 = WTFMove(p1); |
93 | ASSERT_EQ(&a, p1.get()); |
94 | ASSERT_EQ(&a, p2.get()); |
95 | } |
96 | |
97 | { |
98 | NakedPtr<RefLogger> ptr(&a); |
99 | ASSERT_EQ(&a, ptr.get()); |
100 | ptr.clear(); |
101 | ASSERT_EQ(nullptr, ptr.get()); |
102 | } |
103 | } |
104 | |
105 | TEST(WTF_NakedPtr, Assignment) |
106 | { |
107 | DerivedRefLogger a("a" ); |
108 | RefLogger b("b" ); |
109 | DerivedRefLogger c("c" ); |
110 | |
111 | { |
112 | NakedPtr<RefLogger> p1(&a); |
113 | NakedPtr<RefLogger> p2(&b); |
114 | ASSERT_EQ(&a, p1.get()); |
115 | ASSERT_EQ(&b, p2.get()); |
116 | p1 = p2; |
117 | ASSERT_EQ(&b, p1.get()); |
118 | ASSERT_EQ(&b, p2.get()); |
119 | } |
120 | |
121 | { |
122 | NakedPtr<RefLogger> ptr(&a); |
123 | ASSERT_EQ(&a, ptr.get()); |
124 | ptr = &b; |
125 | ASSERT_EQ(&b, ptr.get()); |
126 | } |
127 | |
128 | { |
129 | NakedPtr<RefLogger> ptr(&a); |
130 | ASSERT_EQ(&a, ptr.get()); |
131 | ptr = nullptr; |
132 | ASSERT_EQ(nullptr, ptr.get()); |
133 | } |
134 | |
135 | { |
136 | NakedPtr<RefLogger> p1(&a); |
137 | NakedPtr<RefLogger> p2(&b); |
138 | ASSERT_EQ(&a, p1.get()); |
139 | ASSERT_EQ(&b, p2.get()); |
140 | p1 = WTFMove(p2); |
141 | ASSERT_EQ(&b, p1.get()); |
142 | ASSERT_EQ(&b, p2.get()); |
143 | } |
144 | |
145 | { |
146 | NakedPtr<RefLogger> p1(&a); |
147 | NakedPtr<DerivedRefLogger> p2(&c); |
148 | ASSERT_EQ(&a, p1.get()); |
149 | ASSERT_EQ(&c, p2.get()); |
150 | p1 = p2; |
151 | ASSERT_EQ(&c, p1.get()); |
152 | ASSERT_EQ(&c, p2.get()); |
153 | } |
154 | |
155 | { |
156 | NakedPtr<RefLogger> ptr(&a); |
157 | ASSERT_EQ(&a, ptr.get()); |
158 | ptr = &c; |
159 | ASSERT_EQ(&c, ptr.get()); |
160 | } |
161 | |
162 | { |
163 | NakedPtr<RefLogger> p1(&a); |
164 | NakedPtr<DerivedRefLogger> p2(&c); |
165 | ASSERT_EQ(&a, p1.get()); |
166 | ASSERT_EQ(&c, p2.get()); |
167 | p1 = WTFMove(p2); |
168 | ASSERT_EQ(&c, p1.get()); |
169 | ASSERT_EQ(&c, p2.get()); |
170 | } |
171 | |
172 | { |
173 | NakedPtr<RefLogger> ptr(&a); |
174 | ASSERT_EQ(&a, ptr.get()); |
175 | #if COMPILER(CLANG) |
176 | #pragma clang diagnostic push |
177 | #pragma clang diagnostic ignored "-Wunknown-pragmas" |
178 | #pragma clang diagnostic ignored "-Wunknown-warning-option" |
179 | #pragma clang diagnostic ignored "-Wself-assign-overloaded" |
180 | #endif |
181 | ptr = ptr; |
182 | #if COMPILER(CLANG) |
183 | #pragma clang diagnostic pop |
184 | #endif |
185 | ASSERT_EQ(&a, ptr.get()); |
186 | } |
187 | |
188 | { |
189 | NakedPtr<RefLogger> ptr(&a); |
190 | ASSERT_EQ(&a, ptr.get()); |
191 | #if COMPILER(CLANG) |
192 | #pragma clang diagnostic push |
193 | #pragma clang diagnostic ignored "-Wunknown-pragmas" |
194 | #pragma clang diagnostic ignored "-Wself-move" |
195 | #endif |
196 | ptr = WTFMove(ptr); |
197 | #if COMPILER(CLANG) |
198 | #pragma clang diagnostic pop |
199 | #endif |
200 | ASSERT_EQ(&a, ptr.get()); |
201 | } |
202 | } |
203 | |
204 | TEST(WTF_NakedPtr, Swap) |
205 | { |
206 | RefLogger a("a" ); |
207 | RefLogger b("b" ); |
208 | |
209 | { |
210 | NakedPtr<RefLogger> p1(&a); |
211 | NakedPtr<RefLogger> p2(&b); |
212 | ASSERT_EQ(&a, p1.get()); |
213 | ASSERT_EQ(&b, p2.get()); |
214 | p1.swap(p2); |
215 | ASSERT_EQ(&b, p1.get()); |
216 | ASSERT_EQ(&a, p2.get()); |
217 | } |
218 | |
219 | { |
220 | NakedPtr<RefLogger> p1(&a); |
221 | NakedPtr<RefLogger> p2(&b); |
222 | ASSERT_EQ(&a, p1.get()); |
223 | ASSERT_EQ(&b, p2.get()); |
224 | std::swap(p1, p2); |
225 | ASSERT_EQ(&b, p1.get()); |
226 | ASSERT_EQ(&a, p2.get()); |
227 | } |
228 | } |
229 | |
230 | NakedPtr<RefLogger> nakedPtrFoo(RefLogger& logger) |
231 | { |
232 | return NakedPtr<RefLogger>(&logger); |
233 | } |
234 | |
235 | TEST(WTF_NakedPtr, ReturnValue) |
236 | { |
237 | DerivedRefLogger a("a" ); |
238 | |
239 | { |
240 | auto ptr = nakedPtrFoo(a); |
241 | ASSERT_EQ(&a, ptr.get()); |
242 | ASSERT_EQ(&a, &*ptr); |
243 | ASSERT_EQ(&a.name, &ptr->name); |
244 | } |
245 | } |
246 | |
247 | } // namespace TestWebKitAPI |
248 | |