From 5fa85e2eafb514159b9cd6157711d6b2ff945074 Mon Sep 17 00:00:00 2001 From: Marco Poletti Date: Sat, 30 Apr 2016 13:39:16 +0100 Subject: [PATCH] Add more unit tests for metafunctions used by registerFactory(). --- tests/meta/CMakeLists.txt | 2 + tests/meta/common.h | 10 +++++ tests/meta/component.cpp | 73 ++++++++++++++++++++++++++++++++++ tests/meta/metaprogramming.cpp | 40 +++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 tests/meta/component.cpp create mode 100644 tests/meta/metaprogramming.cpp diff --git a/tests/meta/CMakeLists.txt b/tests/meta/CMakeLists.txt index 6426762b..46f1bc89 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 c5f21c66..a5422420 100644 --- a/tests/meta/common.h +++ b/tests/meta/common.h @@ -13,6 +13,7 @@ #include using namespace std; +using namespace fruit; using namespace fruit::impl; using namespace fruit::impl::meta; @@ -42,12 +43,21 @@ struct SameErrorTag { template using ToSet = Vector; +struct ConstructErrorWithoutUnwrapping { + template + struct apply { + using type = ConstructError(ErrorTag, Type...); + }; +}; + #define Assert(...) static_assert(Eval<__VA_ARGS__>::value, "") #define AssertNot(...) Assert(Not(__VA_ARGS__)) +#define AssertSame(...) static_assert(true || sizeof(typename CheckIfError, ConstructErrorWithoutUnwrapping(DifferentErrorTag, __VA_ARGS__))>>::type), "") #define AssertSameType(...) static_assert(true || sizeof(typename CheckIfError, ConstructError(DifferentErrorTag, __VA_ARGS__))>>::type), "") #define AssertSameSet(...) static_assert(true || sizeof(typename CheckIfError, ConstructError(DifferentErrorTag, __VA_ARGS__))>>::type), "") #define AssertSameProof(...) static_assert(true || sizeof(typename CheckIfError, ConstructError(DifferentErrorTag, __VA_ARGS__))>>::type), "") #define AssertSameForest(...) static_assert(true || sizeof(typename CheckIfError>::type), "") +#define AssertNotSame(...) static_assert(true || sizeof(typename CheckIfError, ConstructErrorWithoutUnwrapping(SameErrorTag, __VA_ARGS__))>>::type), "") #define AssertNotSameType(...) static_assert(true || sizeof(typename CheckIfError, ConstructError(SameErrorTag, __VA_ARGS__))>>::type), "") #define AssertNotSameProof(...) static_assert(true || sizeof(typename CheckIfError, ConstructError(SameErrorTag, __VA_ARGS__))>>::type), "") #define AssertNotSameForest(...) static_assert(true || sizeof(typename CheckIfError, ConstructError(SameErrorTag, __VA_ARGS__))>>::type), "") diff --git a/tests/meta/component.cpp b/tests/meta/component.cpp new file mode 100644 index 00000000..778f34c0 --- /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 + +struct A1 {}; +struct B1 {}; + +using A = Type; +using B = Type; + +using AssistedA = Type>; +using AssistedB = Type>; + +void test_NumAssisted() { + AssertSame(Int<0>, NumAssisted(Vector<>)); + AssertSame(Int<0>, NumAssisted(Vector)); + AssertSame(Int<1>, NumAssisted(Vector)); + AssertSame(Int<0>, NumAssisted(Vector)); + AssertSame(Int<1>, NumAssisted(Vector)); + AssertSame(Int<1>, NumAssisted(Vector)); + AssertSame(Int<2>, NumAssisted(Vector)); +} + +void test_NumAssistedBefore() { + AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector<>)); + + AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector)); + AssertSame(Int<0>, NumAssistedBefore(Int<1>, Vector)); + + AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector)); + AssertSame(Int<1>, NumAssistedBefore(Int<1>, Vector)); + + AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector)); + AssertSame(Int<0>, NumAssistedBefore(Int<1>, Vector)); + AssertSame(Int<0>, NumAssistedBefore(Int<2>, Vector)); + + AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector)); + AssertSame(Int<1>, NumAssistedBefore(Int<1>, Vector)); + AssertSame(Int<1>, NumAssistedBefore(Int<2>, Vector)); + + AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector)); + AssertSame(Int<0>, NumAssistedBefore(Int<1>, Vector)); + AssertSame(Int<1>, NumAssistedBefore(Int<2>, Vector)); + + AssertSame(Int<0>, NumAssistedBefore(Int<0>, Vector)); + AssertSame(Int<1>, NumAssistedBefore(Int<1>, Vector)); + AssertSame(Int<2>, NumAssistedBefore(Int<2>, Vector)); +} + +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 00000000..e52ab24c --- /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 + +struct A1 {}; +struct B1 {}; + +using A = Type; +using B = Type; + +void test_GetNthType() { + AssertSameType(A, GetNthType(Int<0>, Vector)); + + AssertSameType(A, GetNthType(Int<0>, Vector)); + AssertSameType(B, GetNthType(Int<1>, Vector)); +} + +int main() { + test_GetNthType(); + + return 0; +}