aboutsummaryrefslogtreecommitdiff
path: root/tests/meta
diff options
context:
space:
mode:
authorMarco Poletti <poletti.marco@gmail.com>2016-04-30 13:39:16 +0100
committerMarco Poletti <poletti.marco@gmail.com>2016-04-30 13:39:16 +0100
commit5fa85e2eafb514159b9cd6157711d6b2ff945074 (patch)
tree0244a2f47997cce4299747b3a0b36ecf8cda13c3 /tests/meta
parent866e0fb0a526f91244f4ae077a987fbe4840ad12 (diff)
downloadgoogle-fruit-5fa85e2eafb514159b9cd6157711d6b2ff945074.tar.gz
Add more unit tests for metafunctions used by registerFactory().
Diffstat (limited to 'tests/meta')
-rw-r--r--tests/meta/CMakeLists.txt2
-rw-r--r--tests/meta/common.h10
-rw-r--r--tests/meta/component.cpp73
-rw-r--r--tests/meta/metaprogramming.cpp40
4 files changed, 125 insertions, 0 deletions
diff --git a/tests/meta/CMakeLists.txt b/tests/meta/CMakeLists.txt
index 6426762..46f1bc8 100644
--- a/tests/meta/CMakeLists.txt
+++ b/tests/meta/CMakeLists.txt
@@ -7,6 +7,8 @@ set.cpp
map.cpp
graph.cpp
proof_trees.cpp
+component.cpp
+metaprogramming.cpp
)
add_fruit_tests(metaprogramming ${METAPROGRAMMING_TEST_SOURCES})
diff --git a/tests/meta/common.h b/tests/meta/common.h
index c5f21c6..a542242 100644
--- a/tests/meta/common.h
+++ b/tests/meta/common.h
@@ -13,6 +13,7 @@
#include <fruit/impl/injection_debug_errors.h>
using namespace std;
+using namespace fruit;
using namespace fruit::impl;
using namespace fruit::impl::meta;
@@ -42,12 +43,21 @@ struct SameErrorTag {
template <typename... Types>
using ToSet = Vector<Types...>;
+struct ConstructErrorWithoutUnwrapping {
+ template <typename ErrorTag, typename... Args>
+ struct apply {
+ using type = ConstructError(ErrorTag, Type<Args>...);
+ };
+};
+
#define Assert(...) static_assert(Eval<__VA_ARGS__>::value, "")
#define AssertNot(...) Assert(Not(__VA_ARGS__))
+#define AssertSame(...) static_assert(true || sizeof(typename CheckIfError<Eval<If(IsSame(__VA_ARGS__), Bool<true>, ConstructErrorWithoutUnwrapping(DifferentErrorTag, __VA_ARGS__))>>::type), "")
#define AssertSameType(...) static_assert(true || sizeof(typename CheckIfError<Eval<If(IsSame(__VA_ARGS__), Bool<true>, ConstructError(DifferentErrorTag, __VA_ARGS__))>>::type), "")
#define AssertSameSet(...) static_assert(true || sizeof(typename CheckIfError<Eval<If(IsSameSet(__VA_ARGS__), Bool<true>, ConstructError(DifferentErrorTag, __VA_ARGS__))>>::type), "")
#define AssertSameProof(...) static_assert(true || sizeof(typename CheckIfError<Eval<If(IsProofTreeEqualTo(__VA_ARGS__), Bool<true>, ConstructError(DifferentErrorTag, __VA_ARGS__))>>::type), "")
#define AssertSameForest(...) static_assert(true || sizeof(typename CheckIfError<Eval<CheckForestEqualTo(__VA_ARGS__)>>::type), "")
+#define AssertNotSame(...) static_assert(true || sizeof(typename CheckIfError<Eval<If(Not(IsSame(__VA_ARGS__)), Bool<true>, ConstructErrorWithoutUnwrapping(SameErrorTag, __VA_ARGS__))>>::type), "")
#define AssertNotSameType(...) static_assert(true || sizeof(typename CheckIfError<Eval<If(Not(IsSame(__VA_ARGS__)), Bool<true>, ConstructError(SameErrorTag, __VA_ARGS__))>>::type), "")
#define AssertNotSameProof(...) static_assert(true || sizeof(typename CheckIfError<Eval<If(Not(IsProofTreeEqualTo(__VA_ARGS__)), Bool<true>, ConstructError(SameErrorTag, __VA_ARGS__))>>::type), "")
#define AssertNotSameForest(...) static_assert(true || sizeof(typename CheckIfError<Eval<If(Not(IsForestEqualTo(__VA_ARGS__)), Bool<true>, ConstructError(SameErrorTag, __VA_ARGS__))>>::type), "")
diff --git a/tests/meta/component.cpp b/tests/meta/component.cpp
new file mode 100644
index 0000000..778f34c
--- /dev/null
+++ b/tests/meta/component.cpp
@@ -0,0 +1,73 @@
+// expect-success
+/*
+ * Copyright 2014 Google Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define IN_FRUIT_CPP_FILE
+
+#include "common.h"
+#include <fruit/impl/meta/component.h>
+
+struct A1 {};
+struct B1 {};
+
+using A = Type<A1>;
+using B = Type<B1>;
+
+using AssistedA = Type<Assisted<A1>>;
+using AssistedB = Type<Assisted<B1>>;
+
+void test_NumAssisted() {
+ AssertSame(Int<0>, NumAssisted(Vector<>));
+ AssertSame(Int<0>, NumAssisted(Vector<A>));
+ AssertSame(Int<1>, NumAssisted(Vector<AssistedA>));
+ AssertSame(Int<0>, NumAssisted(Vector<A, B>));
+ AssertSame(Int<1>, NumAssisted(Vector<AssistedA, B>));
+ AssertSame(Int<1>, NumAssisted(Vector<A, AssistedB>));
+ AssertSame(Int<2>, NumAssisted(Vector<AssistedA, AssistedB>));
+}
+
+void test_NumAssistedBefore() {
+ AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector<>));
+
+ AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector<A>));
+ AssertSame(Int<0>, NumAssistedBefore(Int<1>, Vector<A>));
+
+ AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector<AssistedA>));
+ AssertSame(Int<1>, NumAssistedBefore(Int<1>, Vector<AssistedA>));
+
+ AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector<A, B>));
+ AssertSame(Int<0>, NumAssistedBefore(Int<1>, Vector<A, B>));
+ AssertSame(Int<0>, NumAssistedBefore(Int<2>, Vector<A, B>));
+
+ AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector<AssistedA, B>));
+ AssertSame(Int<1>, NumAssistedBefore(Int<1>, Vector<AssistedA, B>));
+ AssertSame(Int<1>, NumAssistedBefore(Int<2>, Vector<AssistedA, B>));
+
+ AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector<A, AssistedB>));
+ AssertSame(Int<0>, NumAssistedBefore(Int<1>, Vector<A, AssistedB>));
+ AssertSame(Int<1>, NumAssistedBefore(Int<2>, Vector<A, AssistedB>));
+
+ AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector<AssistedA, AssistedB>));
+ AssertSame(Int<1>, NumAssistedBefore(Int<1>, Vector<AssistedA, AssistedB>));
+ AssertSame(Int<2>, NumAssistedBefore(Int<2>, Vector<AssistedA, AssistedB>));
+}
+
+int main() {
+ test_NumAssisted();
+ test_NumAssistedBefore();
+
+ return 0;
+}
diff --git a/tests/meta/metaprogramming.cpp b/tests/meta/metaprogramming.cpp
new file mode 100644
index 0000000..e52ab24
--- /dev/null
+++ b/tests/meta/metaprogramming.cpp
@@ -0,0 +1,40 @@
+// expect-success
+/*
+ * Copyright 2014 Google Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define IN_FRUIT_CPP_FILE
+
+#include "common.h"
+#include <fruit/impl/meta/component.h>
+
+struct A1 {};
+struct B1 {};
+
+using A = Type<A1>;
+using B = Type<B1>;
+
+void test_GetNthType() {
+ AssertSameType(A, GetNthType(Int<0>, Vector<A>));
+
+ AssertSameType(A, GetNthType(Int<0>, Vector<A, B>));
+ AssertSameType(B, GetNthType(Int<1>, Vector<A, B>));
+}
+
+int main() {
+ test_GetNthType();
+
+ return 0;
+}