summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/ADT/OptionalTest.cpp
diff options
context:
space:
mode:
authorJordan Rose <jordan_rose@apple.com>2014-09-29 18:56:08 +0000
committerJordan Rose <jordan_rose@apple.com>2014-09-29 18:56:08 +0000
commit59e4e1b5feff4888799636fa18837ca2090a72f5 (patch)
tree249ae54c26a1295690ed911982764859838677d4 /llvm/unittests/ADT/OptionalTest.cpp
parent40424cd6aba4352f1891bce6feb79e9c5a4769f4 (diff)
downloadbcm5719-llvm-59e4e1b5feff4888799636fa18837ca2090a72f5.tar.gz
bcm5719-llvm-59e4e1b5feff4888799636fa18837ca2090a72f5.zip
Add getValueOr to llvm::Optional<T>.
This takes a single argument convertible to T, and - if the Optional has a value, returns the existing value, - otherwise, constructs a T from the argument and returns that. Inspired by std::experimental::optional from the "Library Fundamentals" C++ TS. llvm-svn: 218618
Diffstat (limited to 'llvm/unittests/ADT/OptionalTest.cpp')
-rw-r--r--llvm/unittests/ADT/OptionalTest.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/OptionalTest.cpp b/llvm/unittests/ADT/OptionalTest.cpp
index 2da408c15e0..51c54523b88 100644
--- a/llvm/unittests/ADT/OptionalTest.cpp
+++ b/llvm/unittests/ADT/OptionalTest.cpp
@@ -169,6 +169,14 @@ TEST_F(OptionalTest, NullCopyConstructionTest) {
EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
}
+TEST_F(OptionalTest, GetValueOr) {
+ Optional<int> A;
+ EXPECT_EQ(42, A.getValueOr(42));
+
+ A = 5;
+ EXPECT_EQ(5, A.getValueOr(42));
+}
+
struct MoveOnly {
static unsigned MoveConstructions;
static unsigned Destructions;
@@ -278,5 +286,26 @@ TEST_F(OptionalTest, MoveOnlyAssigningAssignment) {
EXPECT_EQ(1u, MoveOnly::Destructions);
}
+#if LLVM_HAS_RVALUE_REFERENCE_THIS
+
+TEST_F(OptionalTest, MoveGetValueOr) {
+ Optional<MoveOnly> A;
+
+ MoveOnly::ResetCounts();
+ EXPECT_EQ(42, std::move(A).getValueOr(MoveOnly(42)).val);
+ EXPECT_EQ(1u, MoveOnly::MoveConstructions);
+ EXPECT_EQ(0u, MoveOnly::MoveAssignments);
+ EXPECT_EQ(2u, MoveOnly::Destructions);
+
+ A = MoveOnly(5);
+ MoveOnly::ResetCounts();
+ EXPECT_EQ(5, std::move(A).getValueOr(MoveOnly(42)).val);
+ EXPECT_EQ(1u, MoveOnly::MoveConstructions);
+ EXPECT_EQ(0u, MoveOnly::MoveAssignments);
+ EXPECT_EQ(2u, MoveOnly::Destructions);
+}
+
+#endif // LLVM_HAS_RVALUE_REFERENCE_THIS
+
} // end anonymous namespace
OpenPOWER on IntegriCloud