summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/utils.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/utils.cpp b/test/utils.cpp
index d65467b5..129807a6 100644
--- a/test/utils.cpp
+++ b/test/utils.cpp
@@ -7,6 +7,7 @@
#include <iostream>
#include <map>
+#include <optional>
#include <sstream>
#include <string>
#include <vector>
@@ -169,6 +170,55 @@ protected:
return TestPass;
}
+ int testDefopt()
+ {
+ static bool defaultConstructed = false;
+
+ struct ValueType {
+ ValueType()
+ : value_(-1)
+ {
+ defaultConstructed = true;
+ }
+
+ ValueType(int value)
+ : value_(value)
+ {
+ }
+
+ int value_;
+ };
+
+ /*
+ * Test that utils::defopt doesn't cause default-construction
+ * of a ValueType instance when value_or(utils::defopt) is
+ * called on a std::optional that has a value.
+ */
+ std::optional<ValueType> opt = ValueType(0);
+ ValueType value = opt.value_or(utils::defopt);
+
+ if (defaultConstructed || value.value_ != 0) {
+ std::cerr << "utils::defopt didn't prevent default construction"
+ << std::endl;
+ return TestFail;
+ }
+
+ /*
+ * Then test that the ValueType is correctly default-constructed
+ * when the std::optional has no value.
+ */
+ opt = std::nullopt;
+ value = opt.value_or(utils::defopt);
+
+ if (!defaultConstructed || value.value_ != -1) {
+ std::cerr << "utils::defopt didn't cause default construction"
+ << std::endl;
+ return TestFail;
+ }
+
+ return TestPass;
+ }
+
int run()
{
/* utils::hex() test. */
@@ -281,6 +331,10 @@ protected:
if (testDuration() != TestPass)
return TestFail;
+ /* utils::defopt test. */
+ if (testDefopt() != TestPass)
+ return TestFail;
+
return TestPass;
}
};