Index: test/plugin/PlaceholderTest.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/test/plugin/PlaceholderTest.cpp |
@@ -0,0 +1,147 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+#include <gtest/gtest.h> |
+#include "../../src/plugin/ActiveQueue.h" |
+#include <ostream> |
+#include <memory> |
+ |
+// The C compiler version for VS2012 is 17.00 |
+#if defined(_MSC_VER) && _MSC_VER <= 1700 |
+#define DELETED |
+#else |
+#define DELETED =delete |
+#endif |
+ |
+/** |
+ * Class with only a copy constructor for an instantiation test. |
+ */ |
+template<class T> |
+struct MinimalCopy |
+{ |
+ T value; |
+ |
+ MinimalCopy() DELETED; |
+ MinimalCopy(MinimalCopy&&) DELETED; |
+ MinimalCopy& operator=(const MinimalCopy&) DELETED; |
+ MinimalCopy& operator=(MinimalCopy&&) DELETED; |
+ |
+ MinimalCopy(T x) |
+ : value(x) |
+ {} |
+ |
+ MinimalCopy(const MinimalCopy& t) |
+ : value(t.value) |
+ {} |
+}; |
+ |
+/** |
+ * Class with only a move constructor for an instantiation test. |
+ */ |
+template<class T> |
+struct MinimalMove |
+{ |
+ std::unique_ptr<int> a; // Not really needed, but illustrative |
+ T value; |
+ |
+ MinimalMove() DELETED; |
+ MinimalMove(const MinimalMove&) DELETED; |
+ MinimalMove& operator=(const MinimalMove&) DELETED; |
+ MinimalMove& operator=(MinimalMove&&) DELETED; |
+ |
+ MinimalMove(T x) |
+ : a(nullptr), value(x) |
+ {} |
+ |
+ MinimalMove(MinimalMove&& x) |
+ : a(std::move(x.a)), value(std::move(x.value)) |
+ {} |
+}; |
+ |
+/* |
+ * These are mostly a compilation tests. |
+ * They check that the Placeholder only needs a single constructor, copy or move, to instantiate. |
+ * |
+ * For VS2012, using C++03 syntax, failure to instantiate the 'Placeholder' template |
+ * will show up as a link error for one of declared-but-not-defined members. |
+ * Using C++11 syntax, it will show up as a compile error. |
+ * |
+ * Extra blocks in test code illustrate the anticipated use of 'Placeholder'. |
+ */ |
+ |
+/* |
+ * Placeholder works with simple intrinsic types. |
+ */ |
+TEST(Placeholder, Int) |
+{ |
+ Placeholder<int> x; |
+ { |
+ x.Construct(42); |
+ } |
+ { |
+ ASSERT_EQ(42, x.Object()); |
+ } |
+} |
+ |
+/* |
+ * Placeholder works with only a copy constructor. |
+ */ |
+TEST(Placeholder, MinimalCopy) |
+{ |
+ typedef MinimalCopy<int> T; |
+ Placeholder<T> x; |
+ { |
+ T a(42); |
+ x.Construct(a); // Can't use a temporary because MinimalCopy has no move constructor (arbitrarily) |
+ } |
+ int y = 1; |
+ { |
+ y = x.Object().value; |
+ } |
+ ASSERT_EQ(42, y); |
+} |
+ |
+TEST(Placeholder, MinimalMove) |
+{ |
+ typedef MinimalMove<int> T; |
+ Placeholder<T> x; |
+ { |
+ x.Construct(std::move(T(42))); |
+ } |
+ int y = 1; |
+ { |
+ y = x.Object().value; |
+ } |
+ ASSERT_EQ(42, y); |
+} |
+ |
+TEST(Placeholder, MinimalMoveParameter) |
+{ |
+ typedef MinimalMove<int> T; |
+ Placeholder<T> x; |
+ { |
+ x.Construct(std::move(T(42))); |
+ } |
+ int y = 1; |
+ { |
+ y = [&](T&& t) |
+ { |
+ return t.value; |
+ }(std::move(x.Object())); |
+ } |
+ ASSERT_EQ(42, y); |
+} |
+ |