5b900007a3a48411773673d480df983b44e437d5
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WTF / WeakPtr.cpp
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. 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 "test.h"
28
29 #include <wtf/WeakPtr.h>
30
31 namespace TestWebKitAPI {
32
33 TEST(WTF_WeakPtr, Basic)
34 {
35     int dummy = 5;
36     WeakPtrFactory<int>* factory = new WeakPtrFactory<int>(&dummy);
37     WeakPtr<int> weakPtr1 = factory->createWeakPtr();
38     WeakPtr<int> weakPtr2 = factory->createWeakPtr();
39     WeakPtr<int> weakPtr3 = factory->createWeakPtr();
40     EXPECT_EQ(weakPtr1.get(), &dummy);
41     EXPECT_EQ(weakPtr2.get(), &dummy);
42     EXPECT_EQ(weakPtr3.get(), &dummy);
43     EXPECT_TRUE(weakPtr1);
44     EXPECT_TRUE(weakPtr2);
45     EXPECT_TRUE(weakPtr3);
46     delete factory;
47     EXPECT_NULL(weakPtr1.get());
48     EXPECT_NULL(weakPtr2.get());
49     EXPECT_NULL(weakPtr3.get());
50     EXPECT_FALSE(weakPtr1);
51     EXPECT_FALSE(weakPtr2);
52     EXPECT_FALSE(weakPtr3);
53 }
54
55 TEST(WTF_WeakPtr, Assignment)
56 {
57     int dummy = 5;
58     WeakPtr<int> weakPtr;
59     {
60         WeakPtrFactory<int> factory(&dummy);
61         EXPECT_NULL(weakPtr.get());
62         weakPtr = factory.createWeakPtr();
63         EXPECT_EQ(weakPtr.get(), &dummy);
64     }
65     EXPECT_NULL(weakPtr.get());
66 }
67
68 TEST(WTF_WeakPtr, MultipleFactories)
69 {
70     int dummy1 = 5;
71     int dummy2 = 7;
72     WeakPtrFactory<int>* factory1 = new WeakPtrFactory<int>(&dummy1);
73     WeakPtrFactory<int>* factory2 = new WeakPtrFactory<int>(&dummy2);
74     WeakPtr<int> weakPtr1 = factory1->createWeakPtr();
75     WeakPtr<int> weakPtr2 = factory2->createWeakPtr();
76     EXPECT_EQ(weakPtr1.get(), &dummy1);
77     EXPECT_EQ(weakPtr2.get(), &dummy2);
78     delete factory1;
79     EXPECT_NULL(weakPtr1.get());
80     EXPECT_EQ(weakPtr2.get(), &dummy2);
81     delete factory2;
82     EXPECT_NULL(weakPtr2.get());
83 }
84
85 TEST(WTF_WeakPtr, RevokeAll)
86 {
87     int dummy = 5;
88     WeakPtrFactory<int> factory(&dummy);
89     WeakPtr<int> weakPtr1 = factory.createWeakPtr();
90     WeakPtr<int> weakPtr2 = factory.createWeakPtr();
91     WeakPtr<int> weakPtr3 = factory.createWeakPtr();
92     EXPECT_EQ(weakPtr1.get(), &dummy);
93     EXPECT_EQ(weakPtr2.get(), &dummy);
94     EXPECT_EQ(weakPtr3.get(), &dummy);
95     factory.revokeAll();
96     EXPECT_NULL(weakPtr1.get());
97     EXPECT_NULL(weakPtr2.get());
98     EXPECT_NULL(weakPtr3.get());
99 }
100
101 TEST(WTF_WeakPtr, NullFactory)
102 {
103     WeakPtrFactory<int> factory(nullptr);
104     WeakPtr<int> weakPtr = factory.createWeakPtr();
105     EXPECT_NULL(weakPtr.get());
106     factory.revokeAll();
107     EXPECT_NULL(weakPtr.get());
108 }
109
110 struct Foo {
111     void bar() { };
112 };
113
114 TEST(WTF_WeakPtr, Dereference)
115 {
116     Foo f;
117     WeakPtrFactory<Foo> factory(&f);
118     WeakPtr<Foo> weakPtr = factory.createWeakPtr();
119     weakPtr->bar();
120 }
121
122 TEST(WTF_WeakPtr, Forget)
123 {
124     int dummy = 5;
125     int dummy2 = 7;
126
127     WeakPtrFactory<int> outerFactory(&dummy2);
128     WeakPtr<int> weakPtr1, weakPtr2, weakPtr3, weakPtr4;
129     {
130         WeakPtrFactory<int> innerFactory(&dummy);
131         weakPtr1 = innerFactory.createWeakPtr();
132         weakPtr2 = innerFactory.createWeakPtr();
133         weakPtr3 = innerFactory.createWeakPtr();
134         EXPECT_EQ(weakPtr1.get(), &dummy);
135         EXPECT_EQ(weakPtr2.get(), &dummy);
136         EXPECT_EQ(weakPtr3.get(), &dummy);
137         weakPtr1.clear();
138         weakPtr3 = nullptr;
139         EXPECT_NULL(weakPtr1.get());
140         EXPECT_EQ(weakPtr2.get(), &dummy);
141         EXPECT_NULL(weakPtr3.get());
142         weakPtr1.clear();
143         weakPtr3.clear();
144         EXPECT_NULL(weakPtr1.get());
145         EXPECT_EQ(weakPtr2.get(), &dummy);
146         EXPECT_NULL(weakPtr3.get());
147         weakPtr3 = nullptr;
148         EXPECT_NULL(weakPtr1.get());
149         EXPECT_EQ(weakPtr2.get(), &dummy);
150         EXPECT_NULL(weakPtr3.get());
151         
152         weakPtr4 = weakPtr2;
153         EXPECT_EQ(weakPtr2.get(), &dummy);
154         EXPECT_EQ(weakPtr4.get(), &dummy);
155
156         WeakPtr<int> weakPtr5 = weakPtr2;
157         EXPECT_EQ(weakPtr2.get(), &dummy);
158         EXPECT_EQ(weakPtr5.get(), &dummy);
159         weakPtr5.clear();
160         EXPECT_NULL(weakPtr5.get());
161         EXPECT_EQ(weakPtr2.get(), &dummy);
162
163         weakPtr4 = outerFactory.createWeakPtr();
164         EXPECT_EQ(weakPtr2.get(), &dummy);
165         EXPECT_EQ(weakPtr4.get(), &dummy2);
166     }
167
168     EXPECT_NULL(weakPtr1.get());
169     EXPECT_NULL(weakPtr2.get());
170     EXPECT_EQ(weakPtr4.get(), &dummy2);
171
172     WeakPtr<int> weakPtr5 = weakPtr4;
173     EXPECT_EQ(weakPtr4.get(), &dummy2);
174     EXPECT_EQ(weakPtr5.get(), &dummy2);
175     weakPtr5.clear();
176     EXPECT_NULL(weakPtr5.get());
177     WeakPtr<int> weakPtr6 = weakPtr5;
178     EXPECT_NULL(weakPtr6.get());
179     EXPECT_EQ(weakPtr5.get(), weakPtr6.get());
180
181     WeakPtr<int> weakPtr7 = outerFactory.createWeakPtr();
182     EXPECT_EQ(weakPtr7.get(), &dummy2);
183     weakPtr7 = nullptr;
184     EXPECT_NULL(weakPtr7.get());
185 }
186     
187 } // namespace TestWebKitAPI