Implement WTF::Expected
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WTF / Expected.cpp
1 /*
2  * Copyright (C) 2016 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 <string>
29 #include <unordered_map>
30
31 #include <wtf/Expected.h>
32
33 namespace WTF {
34
35 template <class E> std::ostream& operator<<(std::ostream& os, const UnexpectedType<E>& u)
36 {
37     return os << u.value();
38 }
39
40 template <class T, class E> std::ostream& operator<<(std::ostream& os, const Expected<T, E>& e)
41 {
42     if (e.hasValue())
43         return os << e.value();
44     return os << e.error();
45 }
46
47 template <class E> std::ostream& operator<<(std::ostream& os, const Expected<void, E>& e)
48 {
49     if (e.hasValue())
50         return os << "";
51     return os << e.error();
52 }
53
54 }
55
56 namespace TestWebKitAPI {
57
58 constexpr const char* oops = "oops";
59 constexpr const char* foof = "foof";
60
61 TEST(WTF_Expected, UnexpectedType)
62 {
63     {
64         auto u = UnexpectedType<int>(42);
65         EXPECT_EQ(u.value(), 42);
66         constexpr auto c = makeUnexpected(42);
67         EXPECT_EQ(c.value(), 42);
68         EXPECT_EQ(u, c);
69         EXPECT_FALSE(u != c);
70         EXPECT_FALSE(u < c);
71         EXPECT_FALSE(u > c);
72         EXPECT_LE(u, c);
73         EXPECT_GE(u, c);
74     }
75     {
76         auto c = makeUnexpected(oops);
77         EXPECT_EQ(c.value(), oops);
78     }
79     {
80         auto s = makeUnexpected(std::string(oops));
81         EXPECT_EQ(s.value(), oops);
82     }
83     {
84         constexpr auto s0 = makeUnexpected(oops);
85         constexpr auto s1(s0);
86         EXPECT_EQ(s0, s1);
87     }
88 }
89
90 struct foo {
91     int v;
92     foo(int v)
93         : v(v)
94     { }
95     ~foo() { }
96     bool operator==(const foo& y) const { return v == y.v; }
97     friend std::ostream& operator<<(std::ostream&, const foo&);
98 };
99 std::ostream& operator<<(std::ostream& os, const foo& f) { return os << f.v; }
100
101 TEST(WTF_Expected, expected)
102 {
103     typedef Expected<int, const char*> E;
104     typedef Expected<int, const void*> EV;
105     typedef Expected<foo, const char*> FooChar;
106     typedef Expected<foo, std::string> FooString;
107     {
108         auto e = E();
109         EXPECT_TRUE(e.hasValue());
110         EXPECT_EQ(e.value(), 0);
111         EXPECT_EQ(e.valueOr(3.14), 0);
112     }
113     {
114         constexpr E e;
115         EXPECT_TRUE(e.hasValue());
116         EXPECT_EQ(e.value(), 0);
117         EXPECT_EQ(e.valueOr(3.14), 0);
118     }
119     {
120         auto e = E(42);
121         EXPECT_TRUE(e.hasValue());
122         EXPECT_EQ(e.value(), 42);
123         EXPECT_EQ(e.valueOr(3.14), 42);
124         const auto e2(e);
125         EXPECT_TRUE(e2.hasValue());
126         EXPECT_EQ(e2.value(), 42);
127         EXPECT_EQ(e2.valueOr(3.14), 42);
128         E e3;
129         e3 = e2;
130         EXPECT_TRUE(e3.hasValue());
131         EXPECT_EQ(e3.value(), 42);
132         EXPECT_EQ(e3.valueOr(3.14), 42);
133         const E e4 = e2;
134         EXPECT_TRUE(e4.hasValue());
135         EXPECT_EQ(e4.value(), 42);
136         EXPECT_EQ(e4.valueOr(3.14), 42);
137     }
138     {
139         constexpr E c(42);
140         EXPECT_TRUE(c.hasValue());
141         EXPECT_EQ(c.value(), 42);
142         EXPECT_EQ(c.valueOr(3.14), 42);
143         constexpr const auto c2(c);
144         EXPECT_TRUE(c2.hasValue());
145         EXPECT_EQ(c2.value(), 42);
146         EXPECT_EQ(c2.valueOr(3.14), 42);
147     }
148     {
149         auto u = E(makeUnexpected(oops));
150         EXPECT_FALSE(u.hasValue());
151         EXPECT_EQ(u.error(), oops);
152         EXPECT_EQ(u.getUnexpected().value(), oops);
153         EXPECT_EQ(u.valueOr(3.14), 3);
154     }
155     {
156         auto uv = EV(makeUnexpected(oops));
157         EXPECT_FALSE(uv.hasValue());
158         EXPECT_EQ(uv.error(), oops);
159         EXPECT_EQ(uv.getUnexpected().value(), oops);
160         EXPECT_EQ(uv.valueOr(3.14), 3);
161     }
162     {
163         E e = makeUnexpected(oops);
164         EXPECT_FALSE(e.hasValue());
165         EXPECT_EQ(e.error(), oops);
166         EXPECT_EQ(e.getUnexpected().value(), oops);
167         EXPECT_EQ(e.valueOr(3.14), 3);
168     }
169     {
170         auto e = makeExpectedFromError<int, const char*>(oops);
171         EXPECT_FALSE(e.hasValue());
172         EXPECT_EQ(e.error(), oops);
173         EXPECT_EQ(e.getUnexpected().value(), oops);
174         EXPECT_EQ(e.valueOr(3.14), 3);
175     }
176     {
177         auto e = makeExpectedFromError<int, const void*>(oops);
178         EXPECT_FALSE(e.hasValue());
179         EXPECT_EQ(e.error(), oops);
180         EXPECT_EQ(e.getUnexpected().value(), oops);
181         EXPECT_EQ(e.valueOr(3.14), 3);
182     }
183     {
184         auto e = FooChar(42);
185         EXPECT_EQ(e->v, 42);
186         EXPECT_EQ((*e).v, 42);
187     }
188     {
189         auto e0 = E(42);
190         auto e1 = E(1024);
191         swap(e0, e1);
192         EXPECT_EQ(e0.value(), 1024);
193         EXPECT_EQ(e1.value(), 42);
194     }
195     {
196         auto e0 = E(makeUnexpected(oops));
197         auto e1 = E(makeUnexpected(foof));
198         swap(e0, e1);
199         EXPECT_EQ(e0.error(), foof);
200         EXPECT_EQ(e1.error(), oops);
201     }
202     {
203         FooChar c(foo(42));
204         EXPECT_EQ(c->v, 42);
205         EXPECT_EQ((*c).v, 42);
206     }
207     {
208         FooString s(foo(42));
209         EXPECT_EQ(s->v, 42);
210         EXPECT_EQ((*s).v, 42);
211         const char* message = "very long failure string, for very bad failure cases";
212         FooString e0(makeUnexpected<std::string>(message));
213         FooString e1(makeUnexpected<std::string>(message));
214         FooString e2(makeUnexpected<std::string>(std::string()));
215         EXPECT_EQ(e0.error(), std::string(message));
216         EXPECT_EQ(e0, e1);
217         EXPECT_NE(e0, e2);
218         FooString* e4 = new FooString(makeUnexpected<std::string>(message));
219         FooString* e5 = new FooString(*e4);
220         EXPECT_EQ(e0, *e4);
221         delete e4;
222         EXPECT_EQ(e0, *e5);
223         delete e5;
224     }
225 }
226
227 TEST(WTF_Expected, Expected_void)
228 {
229     typedef Expected<void, const char*> E;
230     typedef Expected<void, const void*> EV;
231     typedef Expected<void, std::string> String;
232     {
233         auto e = E();
234         EXPECT_TRUE(e.hasValue());
235         const auto e2(e);
236         EXPECT_TRUE(e2.hasValue());
237         EXPECT_EQ(e, e2);
238         E e3;
239         e3 = e2;
240         EXPECT_TRUE(e3.hasValue());
241         EXPECT_EQ(e, e3);
242     }
243     {
244         constexpr E c;
245         EXPECT_TRUE(c.hasValue());
246         constexpr const auto c2(c);
247         EXPECT_TRUE(c2.hasValue());
248         EXPECT_EQ(c, c2);
249     }
250     {
251         auto u = E(makeUnexpected(oops));
252         EXPECT_FALSE(u.hasValue());
253         EXPECT_EQ(u.error(), oops);
254         EXPECT_EQ(u.getUnexpected().value(), oops);
255     }
256     {
257         auto uv = EV(makeUnexpected(oops));
258         EXPECT_FALSE(uv.hasValue());
259         EXPECT_EQ(uv.error(), oops);
260         EXPECT_EQ(uv.getUnexpected().value(), oops);
261     }
262     {
263         E e = makeUnexpected(oops);
264         EXPECT_FALSE(e.hasValue());
265         EXPECT_EQ(e.error(), oops);
266         EXPECT_EQ(e.getUnexpected().value(), oops);
267     }
268     {
269         auto e = makeExpectedFromError<void, const char*>(oops);
270         EXPECT_FALSE(e.hasValue());
271         EXPECT_EQ(e.error(), oops);
272         EXPECT_EQ(e.getUnexpected().value(), oops);
273     }
274     {
275         auto e = makeExpectedFromError<void, const void*>(oops);
276         EXPECT_FALSE(e.hasValue());
277         EXPECT_EQ(e.error(), oops);
278         EXPECT_EQ(e.getUnexpected().value(), oops);
279     }
280     {
281         auto e0 = E();
282         auto e1 = E();
283         swap(e0, e1);
284         EXPECT_EQ(e0, e1);
285     }
286     {
287         auto e0 = E(makeUnexpected(oops));
288         auto e1 = E(makeUnexpected(foof));
289         swap(e0, e1);
290         EXPECT_EQ(e0.error(), foof);
291         EXPECT_EQ(e1.error(), oops);
292     }
293     {
294         const char* message = "very long failure string, for very bad failure cases";
295         String e0(makeUnexpected<std::string>(message));
296         String e1(makeUnexpected<std::string>(message));
297         String e2(makeUnexpected<std::string>(std::string()));
298         EXPECT_EQ(e0.error(), std::string(message));
299         EXPECT_EQ(e0, e1);
300         EXPECT_NE(e0, e2);
301         String* e4 = new String(makeUnexpected<std::string>(message));
302         String* e5 = new String(*e4);
303         EXPECT_EQ(e0, *e4);
304         delete e4;
305         EXPECT_EQ(e0, *e5);
306         delete e5;
307     }
308 }
309
310 TEST(WTF_Expected, comparison)
311 {
312     typedef Expected<int, const char*> Ex;
313     typedef Expected<int, int> Er;
314
315     // Two Expected, no errors.
316     EXPECT_EQ(Ex(42), Ex(42));
317     EXPECT_NE(Ex(42), Ex(1024));
318     EXPECT_LT(Ex(42), Ex(1024));
319     EXPECT_GT(Ex(1024), Ex(42));
320     EXPECT_LE(Ex(42), Ex(42));
321     EXPECT_GE(Ex(42), Ex(42));
322     EXPECT_LE(Ex(42), Ex(1024));
323     EXPECT_GE(Ex(1024), Ex(42));
324
325     EXPECT_FALSE(Ex(42) == Ex(1024));
326     EXPECT_FALSE(Ex(42) != Ex(42));
327     EXPECT_FALSE(Ex(1024) < Ex(42));
328     EXPECT_FALSE(Ex(42) > Ex(1024));
329     EXPECT_FALSE(Ex(1024) < Ex(42));
330     EXPECT_FALSE(Ex(42) >= Ex(1024));
331
332     // Two Expected, half errors.
333     EXPECT_FALSE(Ex(42) == Ex(makeUnexpected(oops)));
334     EXPECT_NE(Ex(42), Ex(makeUnexpected(oops)));
335     EXPECT_LT(Ex(42), Ex(makeUnexpected(oops)));
336     EXPECT_FALSE(Ex(42) > Ex(makeUnexpected(oops)));
337     EXPECT_LE(Ex(42), Ex(makeUnexpected(oops)));
338     EXPECT_FALSE(Ex(42) >= Ex(makeUnexpected(oops)));
339
340     EXPECT_FALSE(Ex(makeUnexpected(oops)) == Ex(42));
341     EXPECT_NE(Ex(makeUnexpected(oops)), Ex(42));
342     EXPECT_FALSE(Ex(makeUnexpected(oops)) < Ex(42));
343     EXPECT_GT(Ex(makeUnexpected(oops)), Ex(42));
344     EXPECT_FALSE(Ex(makeUnexpected(oops)) <= Ex(42));
345     EXPECT_GE(Ex(makeUnexpected(oops)), Ex(42));
346
347     // Two Expected, all errors.
348     EXPECT_EQ(Er(42), Er(42));
349     EXPECT_NE(Er(42), Er(1024));
350     EXPECT_LT(Er(42), Er(1024));
351     EXPECT_GT(Er(1024), Er(42));
352     EXPECT_LE(Er(42), Er(42));
353     EXPECT_GE(Er(42), Er(42));
354     EXPECT_LE(Er(42), Er(1024));
355     EXPECT_GE(Er(1024), Er(42));
356
357     EXPECT_FALSE(Er(42) == Er(1024));
358     EXPECT_FALSE(Er(42) != Er(42));
359     EXPECT_FALSE(Er(1024) < Er(42));
360     EXPECT_FALSE(Er(42) > Er(1024));
361     EXPECT_FALSE(Er(1024) <= Er(42));
362     EXPECT_FALSE(Er(42) >= Er(1024));
363
364     // One Expected, one value.
365     EXPECT_EQ(Ex(42), 42);
366     EXPECT_NE(Ex(42), 0);
367     EXPECT_LT(Ex(42), 1024);
368     EXPECT_GT(Ex(1024), 42);
369     EXPECT_LE(Ex(42), 42);
370     EXPECT_GE(Ex(42), 42);
371     EXPECT_LE(Ex(42), 1024);
372     EXPECT_GE(Ex(1024), 42);
373
374     EXPECT_FALSE(Ex(42) == 0);
375     EXPECT_FALSE(Ex(42) != 42);
376     EXPECT_FALSE(Ex(1024) < 42);
377     EXPECT_FALSE(Ex(42) > 1024);
378     EXPECT_FALSE(Ex(1024) < 42);
379     EXPECT_FALSE(Ex(42) >= 1024);
380
381     EXPECT_EQ(42, Ex(42));
382     EXPECT_NE(42, Ex(1024));
383     EXPECT_LT(42, Ex(1024));
384     EXPECT_GT(1024, Ex(42));
385     EXPECT_LE(42, Ex(42));
386     EXPECT_GE(42, Ex(42));
387     EXPECT_LE(42, Ex(1024));
388     EXPECT_GE(1024, Ex(42));
389
390     EXPECT_FALSE(42 == Ex(1024));
391     EXPECT_FALSE(42 != Ex(42));
392     EXPECT_FALSE(1024 < Ex(42));
393     EXPECT_FALSE(42 > Ex(1024));
394     EXPECT_FALSE(1024 <= Ex(42));
395     EXPECT_FALSE(42 >= Ex(1024));
396
397     // One Expected, one unexpected.
398     EXPECT_FALSE(Ex(42) == makeUnexpected(oops));
399     EXPECT_NE(Ex(42), makeUnexpected(oops));
400     EXPECT_LT(Ex(42), makeUnexpected(oops));
401     EXPECT_FALSE(Ex(42) > makeUnexpected(oops));
402     EXPECT_LE(Ex(42), makeUnexpected(oops));
403     EXPECT_FALSE(Ex(42) >= makeUnexpected(oops));
404
405     EXPECT_FALSE(makeUnexpected(oops) == Ex(42));
406     EXPECT_NE(makeUnexpected(oops), Ex(42));
407     EXPECT_FALSE(makeUnexpected(oops) < Ex(42));
408     EXPECT_GT(makeUnexpected(oops), Ex(42));
409     EXPECT_FALSE(makeUnexpected(oops) <= Ex(42));
410     EXPECT_GE(makeUnexpected(oops), Ex(42));
411 }
412
413 struct NonTrivialDtor {
414     ~NonTrivialDtor() { ++count; }
415     static int count;
416 };
417 int NonTrivialDtor::count = 0;
418
419 TEST(WTF_Expected, destructors)
420 {
421     typedef Expected<NonTrivialDtor, const char*> NT;
422     typedef Expected<const char*, NonTrivialDtor> TN;
423     typedef Expected<NonTrivialDtor, NonTrivialDtor> NN;
424     typedef Expected<void, NonTrivialDtor> VN;
425     EXPECT_EQ(NonTrivialDtor::count, 0);
426     { NT nt; }
427     EXPECT_EQ(NonTrivialDtor::count, 1);
428     { NT nt = makeUnexpected(oops); }
429     EXPECT_EQ(NonTrivialDtor::count, 1);
430     { TN tn; }
431     EXPECT_EQ(NonTrivialDtor::count, 1);
432     { TN tn = makeUnexpected(NonTrivialDtor()); }
433     EXPECT_EQ(NonTrivialDtor::count, 4);
434     { NN nn; }
435     EXPECT_EQ(NonTrivialDtor::count, 5);
436     { NN nn = makeUnexpected(NonTrivialDtor()); }
437     EXPECT_EQ(NonTrivialDtor::count, 8);
438     { VN vn; }
439     EXPECT_EQ(NonTrivialDtor::count, 8);
440     { VN vn = makeUnexpected(NonTrivialDtor()); }
441     EXPECT_EQ(NonTrivialDtor::count, 11);
442 }
443
444 TEST(WTF_Expected, hash)
445 {
446     typedef Expected<int, const char*> E;
447     std::unordered_map<E, int> m;
448     m.insert({ E(42), 42 });
449     m.insert({ E(makeUnexpected(oops)), 5 });
450     m.insert({ E(1024), 1024 });
451     m.insert({ E(makeUnexpected(foof)), 0xf00f });
452     EXPECT_EQ(m[E(42)], 42);
453     EXPECT_EQ(m[E(1024)], 1024);
454     EXPECT_EQ(m[E(makeUnexpected(oops))], 5);
455     EXPECT_EQ(m[E(makeUnexpected(foof))], 0xf00f);
456 }
457
458 TEST(WTF_Expected, hash_void)
459 {
460     typedef Expected<void, const char*> E;
461     std::unordered_map<E, int> m;
462     m.insert({ E(), 42 });
463     m.insert({ E(makeUnexpected(oops)), 5 });
464     m.insert({ E(makeUnexpected(foof)), 0xf00f });
465     EXPECT_EQ(m[E()], 42);
466     EXPECT_EQ(m[E(makeUnexpected(oops))], 5);
467     EXPECT_EQ(m[E(makeUnexpected(foof))], 0xf00f);
468 }
469
470 } // namespace TestWebkitAPI