Skip to content

Commit

Permalink
Add more unit tests for metafunctions used by registerFactory().
Browse files Browse the repository at this point in the history
  • Loading branch information
poletti-marco committed Apr 30, 2016
1 parent 866e0fb commit 5fa85e2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/meta/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ set.cpp
map.cpp
graph.cpp
proof_trees.cpp
component.cpp
metaprogramming.cpp
)

add_fruit_tests(metaprogramming ${METAPROGRAMMING_TEST_SOURCES})
Expand Down
10 changes: 10 additions & 0 deletions tests/meta/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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), "")
Expand Down
73 changes: 73 additions & 0 deletions tests/meta/component.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
40 changes: 40 additions & 0 deletions tests/meta/metaprogramming.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 5fa85e2

Please sign in to comment.