Maybe_test.cpp revision a6fe345be955368a13aea76aefb4db821aad11df
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 "test/Common.h" 18#include "util/Maybe.h" 19 20#include <gtest/gtest.h> 21#include <string> 22 23namespace aapt { 24 25struct Dummy { 26 Dummy() { 27 data = new int; 28 *data = 1; 29 std::cerr << "Construct Dummy{0x" << (void *) this 30 << "} with data=0x" << (void*) data 31 << std::endl; 32 } 33 34 Dummy(const Dummy& rhs) { 35 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; 43 } 44 45 Dummy(Dummy&& rhs) { 46 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; 75 } 76 77 ~Dummy() { 78 std::cerr << "Destruct Dummy{0x" << (void *) this 79 << "} with data=0x" << (void*) data 80 << std::endl; 81 delete data; 82 } 83 84 int* data; 85}; 86 87TEST(MaybeTest, MakeNothing) { 88 Maybe<int> val = make_nothing<int>(); 89 AAPT_EXPECT_FALSE(val); 90 91 Maybe<std::string> val2 = make_nothing<std::string>(); 92 AAPT_EXPECT_FALSE(val2); 93 94 val2 = make_nothing<std::string>(); 95 AAPT_EXPECT_FALSE(val2); 96} 97 98TEST(MaybeTest, MakeSomething) { 99 Maybe<int> val = make_value(23); 100 AAPT_ASSERT_TRUE(val); 101 EXPECT_EQ(23, val.value()); 102 103 Maybe<std::string> val2 = make_value(std::string("hey")); 104 AAPT_ASSERT_TRUE(val2); 105 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 114TEST(MaybeTest, MoveAssign) { 115 Maybe<Dummy> val; 116 { 117 Maybe<Dummy> val2 = Dummy(); 118 val = std::move(val2); 119 } 120} 121 122TEST(MaybeTest, Equality) { 123 Maybe<int> a = 1; 124 Maybe<int> b = 1; 125 Maybe<int> c; 126 127 Maybe<int> emptyA, emptyB; 128 129 EXPECT_EQ(a, b); 130 EXPECT_EQ(b, a); 131 EXPECT_NE(a, c); 132 EXPECT_EQ(emptyA, emptyB); 133} 134 135} // namespace aapt 136