Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 { |
| 25 | Dummy() { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 26 | data = new int; |
| 27 | *data = 1; |
| 28 | std::cerr << "Construct Dummy{0x" << (void *) this |
| 29 | << "} with data=0x" << (void*) data |
| 30 | << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | Dummy(const Dummy& rhs) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 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; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | Dummy(Dummy&& rhs) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 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 | |
| 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ~Dummy() { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 77 | std::cerr << "Destruct Dummy{0x" << (void *) this |
| 78 | << "} with data=0x" << (void*) data |
| 79 | << std::endl; |
| 80 | delete data; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | } |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 82 | |
| 83 | int* data; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 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 | |
| 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 | |
| 107 | TEST(MaybeTest, Lifecycle) { |
| 108 | Maybe<Dummy> val = make_nothing<Dummy>(); |
| 109 | |
| 110 | Maybe<Dummy> val2 = make_value(Dummy()); |
| 111 | } |
| 112 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 113 | TEST(MaybeTest, MoveAssign) { |
| 114 | Maybe<Dummy> val; |
| 115 | { |
| 116 | Maybe<Dummy> val2 = Dummy(); |
| 117 | val = std::move(val2); |
| 118 | } |
| 119 | } |
| 120 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | } // namespace aapt |