Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <mail@jlucke.com>2020-02-10 15:54:57 +0300
committerJacques Lucke <mail@jlucke.com>2020-02-10 16:09:01 +0300
commit68cc982dcb7c1063a96f7ec9b7ccb95da4919d6b (patch)
tree9d2076363b54cb6b6da96064453ac3499a5f65c8 /tests/gtests/blenlib/BLI_optional_test.cc
parent76208a5670bc9d70f99f22a3c49463959461b5c1 (diff)
BLI: improve various C++ data structures
The changes come from the `functions` branch, where I'm using these structures a lot. This also includes a new `BLI::Optional<T>` type, which is similar to `std::Optional<T>` which can be used when Blender starts using C++17.
Diffstat (limited to 'tests/gtests/blenlib/BLI_optional_test.cc')
-rw-r--r--tests/gtests/blenlib/BLI_optional_test.cc74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/gtests/blenlib/BLI_optional_test.cc b/tests/gtests/blenlib/BLI_optional_test.cc
new file mode 100644
index 00000000000..2e672967680
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_optional_test.cc
@@ -0,0 +1,74 @@
+#include "testing/testing.h"
+#include "BLI_optional.h"
+#include <string>
+
+using namespace BLI;
+
+TEST(optional, DefaultConstructor)
+{
+ Optional<int> a;
+ EXPECT_FALSE(a.has_value());
+}
+
+TEST(optional, ValueConstructor)
+{
+ Optional<int> a(5);
+ EXPECT_TRUE(a.has_value());
+ EXPECT_EQ(a.value(), 5);
+}
+
+TEST(optional, CopyConstructor)
+{
+ Optional<std::string> a("Hello");
+ Optional<std::string> b = a;
+ EXPECT_TRUE(a.has_value());
+ EXPECT_TRUE(b.has_value());
+ b.value()[0] = 'T';
+ EXPECT_EQ(a.value(), "Hello");
+ EXPECT_EQ(b.value(), "Tello");
+}
+
+TEST(optional, Reset)
+{
+ Optional<int> a(4);
+ EXPECT_TRUE(a.has_value());
+ a.reset();
+ EXPECT_FALSE(a.has_value());
+}
+
+TEST(optional, FromNullPointer)
+{
+ Optional<int> a = Optional<int>::FromPointer(nullptr);
+ EXPECT_FALSE(a.has_value());
+}
+
+TEST(optional, FromNonNullPointer)
+{
+ int value = 42;
+ Optional<int> a = Optional<int>::FromPointer(&value);
+ EXPECT_TRUE(a.has_value());
+ EXPECT_EQ(a.value(), 42);
+}
+
+TEST(optional, Extract)
+{
+ Optional<int> a(32);
+ EXPECT_TRUE(a.has_value());
+ EXPECT_EQ(a.extract(), 32);
+ EXPECT_FALSE(a.has_value());
+}
+
+TEST(optional, ArrowOperator)
+{
+ Optional<std::string> value = std::string("Hello");
+ EXPECT_TRUE(value.has_value());
+ EXPECT_EQ(value->size(), 5);
+}
+
+TEST(optional, StarOperator)
+{
+ Optional<std::string> value = std::string("Hello");
+ EXPECT_TRUE(value.has_value());
+ std::string &s = *value;
+ EXPECT_EQ(s.size(), 5);
+}