1 /*
2  * Copyright (C) 2016 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 
19 #include "chre/util/optional.h"
20 
21 using chre::Optional;
22 
TEST(Optional,NoValueByDefault)23 TEST(Optional, NoValueByDefault) {
24   Optional<int> myInt;
25   EXPECT_FALSE(myInt.has_value());
26 }
27 
TEST(Optional,NonDefaultValueByDefault)28 TEST(Optional, NonDefaultValueByDefault) {
29   Optional<int> myInt(0x1337);
30   EXPECT_TRUE(myInt.has_value());
31   EXPECT_EQ(*myInt, 0x1337);
32 }
33 
TEST(Optional,NonDefaultMovedValueByDefault)34 TEST(Optional, NonDefaultMovedValueByDefault) {
35   Optional<int> myInt(std::move(0x1337));
36   EXPECT_TRUE(myInt.has_value());
37   EXPECT_EQ(*myInt, 0x1337);
38 }
39 
TEST(Optional,CopyAssignAndRead)40 TEST(Optional, CopyAssignAndRead) {
41   Optional<int> myInt;
42   EXPECT_FALSE(myInt.has_value());
43   myInt = 0x1337;
44   EXPECT_EQ(*myInt, 0x1337);
45   EXPECT_TRUE(myInt.has_value());
46   myInt.reset();
47   EXPECT_FALSE(myInt.has_value());
48 }
49 
TEST(Optional,MoveAssignAndRead)50 TEST(Optional, MoveAssignAndRead) {
51   Optional<int> myInt;
52   EXPECT_FALSE(myInt.has_value());
53   myInt = std::move(0xcafe);
54   EXPECT_TRUE(myInt.has_value());
55   EXPECT_EQ(*myInt, 0xcafe);
56 }
57 
TEST(Optional,OptionalMoveAssignAndRead)58 TEST(Optional, OptionalMoveAssignAndRead) {
59   Optional<int> myInt(0x1337);
60   Optional<int> myMovedInt;
61   EXPECT_FALSE(myMovedInt.has_value());
62   myMovedInt = std::move(myInt);
63   EXPECT_FALSE(myInt.has_value());
64   EXPECT_TRUE(myMovedInt.has_value());
65   EXPECT_EQ(*myMovedInt, 0x1337);
66 }
67 
TEST(Optional,OptionalCopyAssignAndRead)68 TEST(Optional, OptionalCopyAssignAndRead) {
69   Optional<int> myInt(0x1337);
70   Optional<int> myCopiedInt;
71   EXPECT_FALSE(myCopiedInt.has_value());
72   myCopiedInt = myInt;
73   EXPECT_TRUE(myInt.has_value());
74   EXPECT_TRUE(myCopiedInt.has_value());
75   EXPECT_EQ(*myInt, 0x1337);
76   EXPECT_EQ(*myCopiedInt, 0x1337);
77 }
78