blob: 4c921f13a3caf62fa51ff0bc2e3e356be32168b9 [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 "util/Maybe.h"
18
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include <string>
20
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "test/Test.h"
22
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023namespace aapt {
24
Ryan Mitchell4ea90752020-07-31 08:21:43 -070025struct Fake {
26 Fake() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027 data = new int;
28 *data = 1;
Ryan Mitchell4ea90752020-07-31 08:21:43 -070029 std::cerr << "Construct Fake{0x" << (void*)this << "} with data=0x"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 << (void*)data << std::endl;
31 }
32
Ryan Mitchell4ea90752020-07-31 08:21:43 -070033 Fake(const Fake& rhs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 data = nullptr;
35 if (rhs.data) {
36 data = new int;
37 *data = *rhs.data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038 }
Ryan Mitchell4ea90752020-07-31 08:21:43 -070039 std::cerr << "CopyConstruct Fake{0x" << (void*)this << "} from Fake{0x"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 << (const void*)&rhs << "}" << std::endl;
41 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042
Ryan Mitchell4ea90752020-07-31 08:21:43 -070043 Fake(Fake&& rhs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 data = rhs.data;
45 rhs.data = nullptr;
Ryan Mitchell4ea90752020-07-31 08:21:43 -070046 std::cerr << "MoveConstruct Fake{0x" << (void*)this << "} from Fake{0x"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 << (const void*)&rhs << "}" << std::endl;
48 }
49
Ryan Mitchell4ea90752020-07-31 08:21:43 -070050 Fake& operator=(const Fake& rhs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 delete data;
52 data = nullptr;
53
54 if (rhs.data) {
55 data = new int;
56 *data = *rhs.data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057 }
Ryan Mitchell4ea90752020-07-31 08:21:43 -070058 std::cerr << "CopyAssign Fake{0x" << (void*)this << "} from Fake{0x"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 << (const void*)&rhs << "}" << std::endl;
60 return *this;
61 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Ryan Mitchell4ea90752020-07-31 08:21:43 -070063 Fake& operator=(Fake&& rhs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 delete data;
65 data = rhs.data;
66 rhs.data = nullptr;
Ryan Mitchell4ea90752020-07-31 08:21:43 -070067 std::cerr << "MoveAssign Fake{0x" << (void*)this << "} from Fake{0x"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 << (const void*)&rhs << "}" << std::endl;
69 return *this;
70 }
Adam Lesinski24aad162015-04-24 19:19:30 -070071
Ryan Mitchell4ea90752020-07-31 08:21:43 -070072 ~Fake() {
73 std::cerr << "Destruct Fake{0x" << (void*)this << "} with data=0x"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 << (void*)data << std::endl;
75 delete data;
76 }
Adam Lesinski24aad162015-04-24 19:19:30 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 int* data;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079};
80
81TEST(MaybeTest, MakeNothing) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 Maybe<int> val = make_nothing<int>();
Adam Lesinskia45893a2017-05-30 15:19:02 -070083 EXPECT_FALSE(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 Maybe<std::string> val2 = make_nothing<std::string>();
Adam Lesinskia45893a2017-05-30 15:19:02 -070086 EXPECT_FALSE(val2);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 val2 = make_nothing<std::string>();
Adam Lesinskia45893a2017-05-30 15:19:02 -070089 EXPECT_FALSE(val2);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090}
91
92TEST(MaybeTest, MakeSomething) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 Maybe<int> val = make_value(23);
Adam Lesinskia45893a2017-05-30 15:19:02 -070094 ASSERT_TRUE(val);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 EXPECT_EQ(23, val.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 Maybe<std::string> val2 = make_value(std::string("hey"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070098 ASSERT_TRUE(val2);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 EXPECT_EQ(std::string("hey"), val2.value());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100}
101
102TEST(MaybeTest, Lifecycle) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700103 Maybe<Fake> val = make_nothing<Fake>();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700105 Maybe<Fake> val2 = make_value(Fake());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106}
107
Adam Lesinski24aad162015-04-24 19:19:30 -0700108TEST(MaybeTest, MoveAssign) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700109 Maybe<Fake> val;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700111 Maybe<Fake> val2 = Fake();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 val = std::move(val2);
113 }
Adam Lesinski24aad162015-04-24 19:19:30 -0700114}
115
Adam Lesinskia5870652015-11-20 15:32:30 -0800116TEST(MaybeTest, Equality) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 Maybe<int> a = 1;
118 Maybe<int> b = 1;
119 Maybe<int> c;
Adam Lesinskia5870652015-11-20 15:32:30 -0800120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 Maybe<int> emptyA, emptyB;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 EXPECT_EQ(a, b);
124 EXPECT_EQ(b, a);
125 EXPECT_NE(a, c);
126 EXPECT_EQ(emptyA, emptyB);
Adam Lesinskia5870652015-11-20 15:32:30 -0800127}
128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129} // namespace aapt