blob: 5d42dc3ac3ab2b27a1cb0fc5f92d9c5bd2d80d6e [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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "test/Common.h"
18#include "util/Maybe.h"
19
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <gtest/gtest.h>
21#include <string>
22
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023namespace aapt {
24
25struct Dummy {
26 Dummy() {
Adam Lesinski24aad162015-04-24 19:19:30 -070027 data = new int;
28 *data = 1;
29 std::cerr << "Construct Dummy{0x" << (void *) this
30 << "} with data=0x" << (void*) data
31 << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032 }
33
34 Dummy(const Dummy& rhs) {
Adam Lesinski24aad162015-04-24 19:19:30 -070035 data = nullptr;
36 if (rhs.data) {
37 data = new int;
38 *data = *rhs.data;
39 }
40 std::cerr << "CopyConstruct Dummy{0x" << (void *) this
41 << "} from Dummy{0x" << (const void*) &rhs
42 << "}" << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043 }
44
45 Dummy(Dummy&& rhs) {
Adam Lesinski24aad162015-04-24 19:19:30 -070046 data = rhs.data;
47 rhs.data = nullptr;
48 std::cerr << "MoveConstruct Dummy{0x" << (void *) this
49 << "} from Dummy{0x" << (const void*) &rhs
50 << "}" << std::endl;
51 }
52
53 Dummy& operator=(const Dummy& rhs) {
54 delete data;
55 data = nullptr;
56
57 if (rhs.data) {
58 data = new int;
59 *data = *rhs.data;
60 }
61 std::cerr << "CopyAssign Dummy{0x" << (void *) this
62 << "} from Dummy{0x" << (const void*) &rhs
63 << "}" << std::endl;
64 return *this;
65 }
66
67 Dummy& operator=(Dummy&& rhs) {
68 delete data;
69 data = rhs.data;
70 rhs.data = nullptr;
71 std::cerr << "MoveAssign Dummy{0x" << (void *) this
72 << "} from Dummy{0x" << (const void*) &rhs
73 << "}" << std::endl;
74 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075 }
76
77 ~Dummy() {
Adam Lesinski24aad162015-04-24 19:19:30 -070078 std::cerr << "Destruct Dummy{0x" << (void *) this
79 << "} with data=0x" << (void*) data
80 << std::endl;
81 delete data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082 }
Adam Lesinski24aad162015-04-24 19:19:30 -070083
84 int* data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085};
86
87TEST(MaybeTest, MakeNothing) {
88 Maybe<int> val = make_nothing<int>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089 AAPT_EXPECT_FALSE(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
91 Maybe<std::string> val2 = make_nothing<std::string>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092 AAPT_EXPECT_FALSE(val2);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
94 val2 = make_nothing<std::string>();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095 AAPT_EXPECT_FALSE(val2);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096}
97
98TEST(MaybeTest, MakeSomething) {
99 Maybe<int> val = make_value(23);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100 AAPT_ASSERT_TRUE(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101 EXPECT_EQ(23, val.value());
102
103 Maybe<std::string> val2 = make_value(std::string("hey"));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 AAPT_ASSERT_TRUE(val2);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105 EXPECT_EQ(std::string("hey"), val2.value());
106}
107
108TEST(MaybeTest, Lifecycle) {
109 Maybe<Dummy> val = make_nothing<Dummy>();
110
111 Maybe<Dummy> val2 = make_value(Dummy());
112}
113
Adam Lesinski24aad162015-04-24 19:19:30 -0700114TEST(MaybeTest, MoveAssign) {
115 Maybe<Dummy> val;
116 {
117 Maybe<Dummy> val2 = Dummy();
118 val = std::move(val2);
119 }
120}
121
Adam Lesinskia5870652015-11-20 15:32:30 -0800122TEST(MaybeTest, Equality) {
123 Maybe<int> a = 1;
124 Maybe<int> b = 1;
125 Maybe<int> c;
126
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800127 Maybe<int> emptyA, emptyB;
128
Adam Lesinskia5870652015-11-20 15:32:30 -0800129 EXPECT_EQ(a, b);
130 EXPECT_EQ(b, a);
131 EXPECT_NE(a, c);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800132 EXPECT_EQ(emptyA, emptyB);
Adam Lesinskia5870652015-11-20 15:32:30 -0800133}
134
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135} // namespace aapt