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 <gtest/gtest.h>
18 #include <string>
19 
20 #include "Maybe.h"
21 
22 namespace aapt {
23 
24 struct Dummy {
Dummyaapt::Dummy25     Dummy() {
26         data = new int;
27         *data = 1;
28         std::cerr << "Construct Dummy{0x" << (void *) this
29                   << "} with data=0x" << (void*) data
30                   << 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
40                   << "} from Dummy{0x" << (const void*) &rhs
41                   << "}" << std::endl;
42     }
43 
Dummyaapt::Dummy44     Dummy(Dummy&& rhs) {
45         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 
operator =aapt::Dummy52     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 
operator =aapt::Dummy66     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;
74     }
75 
~Dummyaapt::Dummy76     ~Dummy() {
77         std::cerr << "Destruct Dummy{0x" << (void *) this
78                   << "} with data=0x" << (void*) data
79                   << std::endl;
80         delete data;
81     }
82 
83     int* data;
84 };
85 
TEST(MaybeTest,MakeNothing)86 TEST(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 
TEST(MaybeTest,MakeSomething)97 TEST(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 
TEST(MaybeTest,Lifecycle)107 TEST(MaybeTest, Lifecycle) {
108     Maybe<Dummy> val = make_nothing<Dummy>();
109 
110     Maybe<Dummy> val2 = make_value(Dummy());
111 }
112 
TEST(MaybeTest,MoveAssign)113 TEST(MaybeTest, MoveAssign) {
114     Maybe<Dummy> val;
115     {
116         Maybe<Dummy> val2 = Dummy();
117         val = std::move(val2);
118     }
119 }
120 
121 } // namespace aapt
122