aboutsummaryrefslogtreecommitdiff
path: root/src/test/PrivateTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/PrivateTest.cpp')
-rw-r--r--src/test/PrivateTest.cpp158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/test/PrivateTest.cpp b/src/test/PrivateTest.cpp
new file mode 100644
index 0000000..0ea67d6
--- /dev/null
+++ b/src/test/PrivateTest.cpp
@@ -0,0 +1,158 @@
+#include <gtest/gtest.h>
+
+#include <crepe/util/Private.h>
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class PrivateTest : public Test {
+public:
+ static unsigned constructors;
+ static unsigned destructors;
+
+ void SetUp() override {
+ PrivateTest::constructors = 0;
+ PrivateTest::destructors = 0;
+ }
+
+ class TestClass {
+ public:
+ TestClass() { PrivateTest::constructors++; }
+ ~TestClass() { PrivateTest::destructors++; }
+ };
+ class Unrelated {};
+};
+unsigned PrivateTest::constructors;
+unsigned PrivateTest::destructors;
+
+TEST_F(PrivateTest, Empty) {
+ {
+ Private foo;
+ }
+
+ EXPECT_EQ(PrivateTest::constructors, 0);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+}
+
+TEST_F(PrivateTest, WithObject) {
+ {
+ Private foo;
+ foo.set<TestClass>();
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+ }
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 1);
+}
+
+TEST_F(PrivateTest, EmptyException) {
+ Private foo;
+ EXPECT_THROW(foo.get<TestClass>(), std::out_of_range);
+
+ foo.set<TestClass>();
+ EXPECT_NO_THROW(foo.get<TestClass>());
+}
+
+TEST_F(PrivateTest, IncorrectTypeException) {
+ Private foo;
+ foo.set<TestClass>();
+
+ EXPECT_THROW(foo.get<Unrelated>(), std::logic_error);
+ EXPECT_NO_THROW(foo.get<TestClass>());
+}
+
+TEST_F(PrivateTest, MoveConstructor) {
+ {
+ Private foo;
+ foo.set<TestClass>();
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+
+ Private bar(std::move(foo));
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+ }
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 1);
+}
+
+TEST_F(PrivateTest, MoveOperator) {
+ {
+ Private foo;
+ foo.set<TestClass>();
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+
+ Private bar = std::move(foo);
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+ }
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 1);
+}
+
+TEST_F(PrivateTest, CopyConstructor) {
+ {
+ Private foo;
+ foo.set<TestClass>();
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+
+ Private bar(foo);
+
+ EXPECT_TRUE(bar.empty());
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+ }
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 1);
+}
+
+TEST_F(PrivateTest, CopyOperator) {
+ {
+ Private foo;
+ foo.set<TestClass>();
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+
+ Private bar = foo;
+
+ EXPECT_TRUE(bar.empty());
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+ }
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 1);
+}
+
+TEST_F(PrivateTest, DoubleAssignment) {
+ {
+ Private foo;
+ foo.set<TestClass>();
+
+ EXPECT_EQ(PrivateTest::constructors, 1);
+ EXPECT_EQ(PrivateTest::destructors, 0);
+
+ foo.set<TestClass>();
+
+ EXPECT_EQ(PrivateTest::constructors, 2);
+ EXPECT_EQ(PrivateTest::destructors, 1);
+ }
+
+ EXPECT_EQ(PrivateTest::constructors, 2);
+ EXPECT_EQ(PrivateTest::destructors, 2);
+}
+