Debian Codegraph @

src/kj/tuple-test.c++

raw
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #include "tuple.h"
23 #include "memory.h"
24 #include "string.h"
25 #include <kj/compat/gtest.h>
26
27 namespace kj {
28
29 struct Foo { uint foo; Foo(uint i): foo(i) {} };
30 struct Bar { uint bar; Bar(uint i): bar(i) {} };
31 struct Baz { uint baz; Baz(uint i): baz(i) {} };
32 struct Qux { uint qux; Qux(uint i): qux(i) {} };
33 struct Quux { uint quux; Quux(uint i): quux(i) {} };
34
35 TEST(Tuple, Tuple) {
36 Tuple<Foo, Bar> t = tuple(Foo(123), Bar(456));
37 EXPECT_EQ(123u, get<0>(t).foo);
38 EXPECT_EQ(456u, get<1>(t).bar);
39
40 Tuple<Foo, Bar, Baz, Qux, Quux> t2 = tuple(t, Baz(789), tuple(Qux(321), Quux(654)));
41 EXPECT_EQ(123u, get<0>(t2).foo);
42 EXPECT_EQ(456u, get<1>(t2).bar);
43 EXPECT_EQ(789u, get<2>(t2).baz);
44 EXPECT_EQ(321u, get<3>(t2).qux);
45 EXPECT_EQ(654u, get<4>(t2).quux);
46
47 Tuple<Own<Foo>, Own<Bar>> t3 = tuple(heap<Foo>(123), heap<Bar>(456));
48 EXPECT_EQ(123u, get<0>(t3)->foo);
49 EXPECT_EQ(456u, get<1>(t3)->bar);
50
51 Tuple<Own<Foo>, Own<Bar>, Own<Baz>, Own<Qux>, Own<Quux>> t4 =
52 tuple(mv(t3), heap<Baz>(789), tuple(heap<Qux>(321), heap<Quux>(654)));
53 EXPECT_EQ(123u, get<0>(t4)->foo);
54 EXPECT_EQ(456u, get<1>(t4)->bar);
55 EXPECT_EQ(789u, get<2>(t4)->baz);
56 EXPECT_EQ(321u, get<3>(t4)->qux);
57 EXPECT_EQ(654u, get<4>(t4)->quux);
58
59 Tuple<String, StringPtr> t5 = tuple(heapString("foo"), "bar");
60 EXPECT_EQ("foo", get<0>(t5));
61 EXPECT_EQ("bar", get<1>(t5));
62
63 Tuple<StringPtr, StringPtr, StringPtr, StringPtr, String> t6 =
64 tuple(Tuple<StringPtr, StringPtr>(t5), "baz", tuple("qux", heapString("quux")));
65 EXPECT_EQ("foo", get<0>(t6));
66 EXPECT_EQ("bar", get<1>(t6));
67 EXPECT_EQ("baz", get<2>(t6));
68 EXPECT_EQ("qux", get<3>(t6));
69 EXPECT_EQ("quux", get<4>(t6));
70
71 kj::apply([](Foo a, Bar b, Own<Foo> c, Own<Bar> d, uint e, StringPtr f, StringPtr g) {
72 EXPECT_EQ(123u, a.foo);
73 EXPECT_EQ(456u, b.bar);
74 EXPECT_EQ(123u, c->foo);
75 EXPECT_EQ(456u, d->bar);
76 EXPECT_EQ(789u, e);
77 EXPECT_EQ("foo", f);
78 EXPECT_EQ("bar", g);
79 }, t, tuple(heap<Foo>(123), heap<Bar>(456)), 789, mv(t5));
80
81 uint i = tuple(123);
82 EXPECT_EQ(123u, i);
83
84 i = tuple(tuple(), 456, tuple(tuple(), tuple()));
85 EXPECT_EQ(456u, i);
86
87 EXPECT_EQ(0, (indexOfType<int, Tuple<int, char, bool>>()));
88 EXPECT_EQ(1, (indexOfType<char, Tuple<int, char, bool>>()));
89 EXPECT_EQ(2, (indexOfType<bool, Tuple<int, char, bool>>()));
90 EXPECT_EQ(0, (indexOfType<int, int>()));
91 }
92
93 TEST(Tuple, RefTuple) {
94 uint i = 123;
95 StringPtr s = "foo";
96
97 Tuple<uint&, StringPtr&, uint, StringPtr> t = refTuple(i, s, 321, "bar");
98 EXPECT_EQ(get<0>(t), 123);
99 EXPECT_EQ(get<1>(t), "foo");
100 EXPECT_EQ(get<2>(t), 321);
101 EXPECT_EQ(get<3>(t), "bar");
102
103 i = 456;
104 s = "baz";
105
106 EXPECT_EQ(get<0>(t), 456);
107 EXPECT_EQ(get<1>(t), "baz");
108 EXPECT_EQ(get<2>(t), 321);
109 EXPECT_EQ(get<3>(t), "bar");
110 }
111
112 } // namespace kj