blob: 71bbb940beda7d9967db7d972afb99aade0059d4 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18#include <string>
19
20#include "Maybe.h"
21
22namespace aapt {
23
24struct Dummy {
25 Dummy() {
Adam Lesinski24aad162015-04-24 19:19:30 -070026 data = new int;
27 *data = 1;
28 std::cerr << "Construct Dummy{0x" << (void *) this
29 << "} with data=0x" << (void*) data
30 << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031 }
32
33 Dummy(const Dummy& rhs) {
Adam Lesinski24aad162015-04-24 19:19:30 -070034 data = nullptr;
35 if (rhs.data) {
36 data = new int;
37 *data = *rhs.data;
38 }
39 std::cerr << "CopyConstruct Dummy{0x" << (void *) this
40 << "} from Dummy{0x" << (const void*) &rhs
41 << "}" << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042 }
43
44 Dummy(Dummy&& rhs) {
Adam Lesinski24aad162015-04-24 19:19:30 -070045 data = rhs.data;
46 rhs.data = nullptr;
47 std::cerr << "MoveConstruct Dummy{0x" << (void *) this
48 << "} from Dummy{0x" << (const void*) &rhs
49 << "}" << std::endl;
50 }
51
52 Dummy& operator=(const Dummy& rhs) {
53 delete data;
54 data = nullptr;
55
56 if (rhs.data) {
57 data = new int;
58 *data = *rhs.data;
59 }
60 std::cerr << "CopyAssign Dummy{0x" << (void *) this
61 << "} from Dummy{0x" << (const void*) &rhs
62 << "}" << std::endl;
63 return *this;
64 }
65
66 Dummy& operator=(Dummy&& rhs) {
67 delete data;
68 data = rhs.data;
69 rhs.data = nullptr;
70 std::cerr << "MoveAssign Dummy{0x" << (void *) this
71 << "} from Dummy{0x" << (const void*) &rhs
72 << "}" << std::endl;
73 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074 }
75
76 ~Dummy() {
Adam Lesinski24aad162015-04-24 19:19:30 -070077 std::cerr << "Destruct Dummy{0x" << (void *) this
78 << "} with data=0x" << (void*) data
79 << std::endl;
80 delete data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081 }
Adam Lesinski24aad162015-04-24 19:19:30 -070082
83 int* data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084};
85
86TEST(MaybeTest, MakeNothing) {
87 Maybe<int> val = make_nothing<int>();
88 EXPECT_FALSE(val);
89
90 Maybe<std::string> val2 = make_nothing<std::string>();
91 EXPECT_FALSE(val2);
92
93 val2 = make_nothing<std::string>();
94 EXPECT_FALSE(val2);
95}
96
97TEST(MaybeTest, MakeSomething) {
98 Maybe<int> val = make_value(23);
99 ASSERT_TRUE(val);
100 EXPECT_EQ(23, val.value());
101
102 Maybe<std::string> val2 = make_value(std::string("hey"));
103 ASSERT_TRUE(val2);
104 EXPECT_EQ(std::string("hey"), val2.value());
105}
106
107TEST(MaybeTest, Lifecycle) {
108 Maybe<Dummy> val = make_nothing<Dummy>();
109
110 Maybe<Dummy> val2 = make_value(Dummy());
111}
112
Adam Lesinski24aad162015-04-24 19:19:30 -0700113TEST(MaybeTest, MoveAssign) {
114 Maybe<Dummy> val;
115 {
116 Maybe<Dummy> val2 = Dummy();
117 val = std::move(val2);
118 }
119}
120
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121} // namespace aapt