1/*
2 * Copyright (C) 2014 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
28#include <wtf/Optional.h>
29
30namespace TestWebKitAPI {
31
32TEST(WTF_Optional, Disengaged)
33{
34 {
35 Optional<int> optional;
36
37 EXPECT_FALSE(static_cast<bool>(optional));
38 }
39
40 {
41 Optional<int> optional { WTF::nullopt };
42
43 EXPECT_FALSE(static_cast<bool>(optional));
44 }
45}
46
47TEST(WTF_Optional, Engaged)
48{
49 Optional<int> optional { 10 };
50
51 EXPECT_TRUE(static_cast<bool>(optional));
52 EXPECT_EQ(10, optional.value());
53}
54
55TEST(WTF_Optional, Destructor)
56{
57 static bool didCallDestructor = false;
58 struct A {
59 ~A()
60 {
61 EXPECT_FALSE(didCallDestructor);
62 didCallDestructor = true;
63 }
64 };
65
66 {
67 Optional<A> optional { std::in_place };
68
69 EXPECT_TRUE(static_cast<bool>(optional));
70 }
71
72 EXPECT_TRUE(didCallDestructor);
73}
74
75TEST(WTF_Optional, Callback)
76{
77 bool called = false;
78 Optional<int> a;
79 int result = valueOrCompute(a, [&] {
80 called = true;
81 return 300;
82 });
83 EXPECT_TRUE(called);
84 EXPECT_EQ(result, 300);
85
86 a = 250;
87 called = false;
88 result = valueOrCompute(a, [&] {
89 called = true;
90 return 300;
91 });
92 EXPECT_FALSE(called);
93 EXPECT_EQ(result, 250);
94}
95
96TEST(WTF_Optional, Equality)
97{
98 Optional<int> unengaged1;
99 Optional<int> unengaged2;
100
101 Optional<int> engaged1 { 1 };
102 Optional<int> engaged2 { 2 };
103 Optional<int> engagedx2 { 2 };
104
105 EXPECT_TRUE(unengaged1 == unengaged2);
106 EXPECT_FALSE(engaged1 == engaged2);
107 EXPECT_FALSE(engaged1 == unengaged1);
108 EXPECT_TRUE(engaged2 == engagedx2);
109
110 EXPECT_TRUE(unengaged1 == WTF::nullopt);
111 EXPECT_FALSE(engaged1 == WTF::nullopt);
112 EXPECT_TRUE(WTF::nullopt == unengaged1);
113 EXPECT_FALSE(WTF::nullopt == engaged1);
114
115 EXPECT_TRUE(engaged1 == 1);
116 EXPECT_TRUE(1 == engaged1);
117 EXPECT_FALSE(unengaged1 == 1);
118 EXPECT_FALSE(1 == unengaged1);
119}
120
121TEST(WTF_Optional, Inequality)
122{
123 Optional<int> unengaged1;
124 Optional<int> unengaged2;
125
126 Optional<int> engaged1 { 1 };
127 Optional<int> engaged2 { 2 };
128 Optional<int> engagedx2 { 2 };
129
130 EXPECT_FALSE(unengaged1 != unengaged2);
131 EXPECT_TRUE(engaged1 != engaged2);
132 EXPECT_TRUE(engaged1 != unengaged1);
133 EXPECT_FALSE(engaged2 != engagedx2);
134
135 EXPECT_FALSE(unengaged1 != WTF::nullopt);
136 EXPECT_TRUE(engaged1 != WTF::nullopt);
137 EXPECT_FALSE(WTF::nullopt != unengaged1);
138 EXPECT_TRUE(WTF::nullopt != engaged1);
139
140 EXPECT_FALSE(engaged1 != 1);
141 EXPECT_TRUE(engaged1 != 2);
142 EXPECT_FALSE(1 != engaged1);
143 EXPECT_TRUE(2 != engaged1);
144
145 EXPECT_TRUE(unengaged1 != 1);
146 EXPECT_TRUE(1 != unengaged1);
147}
148
149TEST(WTF_Optional, MoveSemanticsConstructor)
150{
151 {
152 Optional<int> unengaged1 { };
153 EXPECT_FALSE(!!unengaged1);
154
155 Optional<int> unengaged2(std::move(unengaged1));
156 EXPECT_FALSE(!!unengaged1);
157 EXPECT_FALSE(!!unengaged2);
158 }
159
160 {
161 Optional<int> engagedToUnengaged { 2 };
162 EXPECT_TRUE(!!engagedToUnengaged);
163 EXPECT_EQ(2, engagedToUnengaged.value());
164
165 Optional<int> engaged(std::move(engagedToUnengaged));
166 EXPECT_FALSE(!!engagedToUnengaged);
167 EXPECT_TRUE(!!engaged);
168 EXPECT_EQ(2, engaged.value());
169 }
170}
171
172TEST(WTF_Optional, MoveSemanticsAssignment)
173{
174 {
175 Optional<int> unengaged1 { };
176 EXPECT_FALSE(!!unengaged1);
177
178 Optional<int> unengaged2;
179 EXPECT_FALSE(!!unengaged2);
180
181 unengaged2 = std::move(unengaged1);
182 EXPECT_FALSE(!!unengaged1);
183 EXPECT_FALSE(!!unengaged2);
184 }
185
186 {
187 Optional<int> engagedToUnengaged { 3 };
188 EXPECT_TRUE(!!engagedToUnengaged);
189 EXPECT_EQ(3, engagedToUnengaged.value());
190
191 engagedToUnengaged = std::move(WTF::nullopt);
192 EXPECT_FALSE(!!engagedToUnengaged);
193 }
194
195 {
196 Optional<int> engagedToUnengaged { 4 };
197 EXPECT_TRUE(!!engagedToUnengaged);
198 EXPECT_EQ(4, engagedToUnengaged.value());
199
200 Optional<int> unengagedToEngaged;
201 EXPECT_FALSE(!!unengagedToEngaged);
202
203 unengagedToEngaged = std::move(engagedToUnengaged);
204 EXPECT_FALSE(!!engagedToUnengaged);
205 EXPECT_TRUE(!!unengagedToEngaged);
206 EXPECT_EQ(4, unengagedToEngaged.value());
207 }
208
209 {
210 Optional<int> engagedToUnengaged { 5 };
211 EXPECT_TRUE(!!engagedToUnengaged);
212 EXPECT_EQ(5, engagedToUnengaged.value());
213
214 Optional<int> unengaged;
215 EXPECT_FALSE(!!unengaged);
216
217 engagedToUnengaged = std::move(unengaged);
218 EXPECT_FALSE(!!engagedToUnengaged);
219 EXPECT_FALSE(!!unengaged);
220 }
221
222 {
223 Optional<int> engagedToUnengaged { 6 };
224 EXPECT_TRUE(!!engagedToUnengaged);
225 EXPECT_EQ(6, engagedToUnengaged.value());
226
227 Optional<int> engagedToEngaged { 7 };
228 EXPECT_TRUE(!!engagedToEngaged);
229 EXPECT_EQ(7, engagedToEngaged.value());
230
231 engagedToEngaged = std::move(engagedToUnengaged);
232 EXPECT_FALSE(!!engagedToUnengaged);
233 EXPECT_TRUE(!!engagedToEngaged);
234 EXPECT_EQ(6, engagedToEngaged.value());
235 }
236}
237
238} // namespace TestWebKitAPI
239