1 /*
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 "util/Maybe.h"
18 
19 #include <string>
20 
21 #include "test/Test.h"
22 
23 namespace aapt {
24 
25 struct Dummy {
Dummyaapt::Dummy26   Dummy() {
27     data = new int;
28     *data = 1;
29     std::cerr << "Construct Dummy{0x" << (void*)this << "} with data=0x"
30               << (void*)data << std::endl;
31   }
32 
Dummyaapt::Dummy33   Dummy(const Dummy& rhs) {
34     data = nullptr;
35     if (rhs.data) {
36       data = new int;
37       *data = *rhs.data;
38     }
39     std::cerr << "CopyConstruct Dummy{0x" << (void*)this << "} from Dummy{0x"
40               << (const void*)&rhs << "}" << std::endl;
41   }
42 
Dummyaapt::Dummy43   Dummy(Dummy&& rhs) {
44     data = rhs.data;
45     rhs.data = nullptr;
46     std::cerr << "MoveConstruct Dummy{0x" << (void*)this << "} from Dummy{0x"
47               << (const void*)&rhs << "}" << std::endl;
48   }
49 
operator =aapt::Dummy50   Dummy& operator=(const Dummy& rhs) {
51     delete data;
52     data = nullptr;
53 
54     if (rhs.data) {
55       data = new int;
56       *data = *rhs.data;
57     }
58     std::cerr << "CopyAssign Dummy{0x" << (void*)this << "} from Dummy{0x"
59               << (const void*)&rhs << "}" << std::endl;
60     return *this;
61   }
62 
operator =aapt::Dummy63   Dummy& operator=(Dummy&& rhs) {
64     delete data;
65     data = rhs.data;
66     rhs.data = nullptr;
67     std::cerr << "MoveAssign Dummy{0x" << (void*)this << "} from Dummy{0x"
68               << (const void*)&rhs << "}" << std::endl;
69     return *this;
70   }
71 
~Dummyaapt::Dummy72   ~Dummy() {
73     std::cerr << "Destruct Dummy{0x" << (void*)this << "} with data=0x"
74               << (void*)data << std::endl;
75     delete data;
76   }
77 
78   int* data;
79 };
80 
TEST(MaybeTest,MakeNothing)81 TEST(MaybeTest, MakeNothing) {
82   Maybe<int> val = make_nothing<int>();
83   EXPECT_FALSE(val);
84 
85   Maybe<std::string> val2 = make_nothing<std::string>();
86   EXPECT_FALSE(val2);
87 
88   val2 = make_nothing<std::string>();
89   EXPECT_FALSE(val2);
90 }
91 
TEST(MaybeTest,MakeSomething)92 TEST(MaybeTest, MakeSomething) {
93   Maybe<int> val = make_value(23);
94   ASSERT_TRUE(val);
95   EXPECT_EQ(23, val.value());
96 
97   Maybe<std::string> val2 = make_value(std::string("hey"));
98   ASSERT_TRUE(val2);
99   EXPECT_EQ(std::string("hey"), val2.value());
100 }
101 
TEST(MaybeTest,Lifecycle)102 TEST(MaybeTest, Lifecycle) {
103   Maybe<Dummy> val = make_nothing<Dummy>();
104 
105   Maybe<Dummy> val2 = make_value(Dummy());
106 }
107 
TEST(MaybeTest,MoveAssign)108 TEST(MaybeTest, MoveAssign) {
109   Maybe<Dummy> val;
110   {
111     Maybe<Dummy> val2 = Dummy();
112     val = std::move(val2);
113   }
114 }
115 
TEST(MaybeTest,Equality)116 TEST(MaybeTest, Equality) {
117   Maybe<int> a = 1;
118   Maybe<int> b = 1;
119   Maybe<int> c;
120 
121   Maybe<int> emptyA, emptyB;
122 
123   EXPECT_EQ(a, b);
124   EXPECT_EQ(b, a);
125   EXPECT_NE(a, c);
126   EXPECT_EQ(emptyA, emptyB);
127 }
128 
129 }  // namespace aapt
130