// Copyright 2020 The Tint Authors. // // 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. #include "src/tint/traits.h" #include "gtest/gtest.h" namespace tint::traits { namespace { struct S {}; void F1(S) {} void F3(int, S, float) {} } // namespace TEST(ParamType, Function) { F1({}); // Avoid unused method warning F3(0, {}, 0); // Avoid unused method warning static_assert(std::is_same_v, S>); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, S>); static_assert(std::is_same_v, float>); static_assert(std::is_same_v, void>); static_assert(std::is_same_v, void>); static_assert(SignatureOfT::parameter_count == 1); static_assert(SignatureOfT::parameter_count == 3); } TEST(ParamType, Method) { class C { public: void F1(S) {} void F3(int, S, float) {} }; C().F1({}); // Avoid unused method warning C().F3(0, {}, 0); // Avoid unused method warning static_assert(std::is_same_v, S>); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, S>); static_assert(std::is_same_v, float>); static_assert(std::is_same_v, void>); static_assert(std::is_same_v, void>); static_assert(SignatureOfT::parameter_count == 1); static_assert(SignatureOfT::parameter_count == 3); } TEST(ParamType, ConstMethod) { class C { public: void F1(S) const {} void F3(int, S, float) const {} }; C().F1({}); // Avoid unused method warning C().F3(0, {}, 0); // Avoid unused method warning static_assert(std::is_same_v, S>); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, S>); static_assert(std::is_same_v, float>); static_assert(std::is_same_v, void>); static_assert(std::is_same_v, void>); static_assert(SignatureOfT::parameter_count == 1); static_assert(SignatureOfT::parameter_count == 3); } TEST(ParamType, StaticMethod) { class C { public: static void F1(S) {} static void F3(int, S, float) {} }; C::F1({}); // Avoid unused method warning C::F3(0, {}, 0); // Avoid unused method warning static_assert(std::is_same_v, S>); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, S>); static_assert(std::is_same_v, float>); static_assert(std::is_same_v, void>); static_assert(std::is_same_v, void>); static_assert(SignatureOfT::parameter_count == 1); static_assert(SignatureOfT::parameter_count == 3); } TEST(ParamType, FunctionLike) { using F1 = std::function; using F3 = std::function; static_assert(std::is_same_v, S>); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, S>); static_assert(std::is_same_v, float>); static_assert(std::is_same_v, void>); static_assert(std::is_same_v, void>); static_assert(SignatureOfT::parameter_count == 1); static_assert(SignatureOfT::parameter_count == 3); } TEST(ParamType, Lambda) { auto l1 = [](S) {}; auto l3 = [](int, S, float) {}; static_assert(std::is_same_v, S>); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, S>); static_assert(std::is_same_v, float>); static_assert(std::is_same_v, void>); static_assert(std::is_same_v, void>); static_assert(SignatureOfT::parameter_count == 1); static_assert(SignatureOfT::parameter_count == 3); } TEST(Slice, Empty) { auto sliced = Slice<0, 0>(std::make_tuple<>()); static_assert(std::tuple_size_v == 0); } TEST(Slice, SingleElementSliceEmpty) { auto sliced = Slice<0, 0>(std::make_tuple(1)); static_assert(std::tuple_size_v == 0); } TEST(Slice, SingleElementSliceFull) { auto sliced = Slice<0, 1>(std::make_tuple(1)); static_assert(std::tuple_size_v == 1); static_assert(std::is_same_v, int>, ""); EXPECT_EQ(std::get<0>(sliced), 1); } TEST(Slice, MixedTupleSliceEmpty) { auto sliced = Slice<1, 0>(std::make_tuple(1, true, 2.0f)); static_assert(std::tuple_size_v == 0); } TEST(Slice, MixedTupleSliceFull) { auto sliced = Slice<0, 3>(std::make_tuple(1, true, 2.0f)); static_assert(std::tuple_size_v == 3); static_assert(std::is_same_v, int>, ""); static_assert(std::is_same_v, bool>, ""); static_assert(std::is_same_v, float>); EXPECT_EQ(std::get<0>(sliced), 1); EXPECT_EQ(std::get<1>(sliced), true); EXPECT_EQ(std::get<2>(sliced), 2.0f); } TEST(Slice, MixedTupleSliceLowPart) { auto sliced = Slice<0, 2>(std::make_tuple(1, true, 2.0f)); static_assert(std::tuple_size_v == 2); static_assert(std::is_same_v, int>, ""); static_assert(std::is_same_v, bool>, ""); EXPECT_EQ(std::get<0>(sliced), 1); EXPECT_EQ(std::get<1>(sliced), true); } TEST(Slice, MixedTupleSliceHighPart) { auto sliced = Slice<1, 2>(std::make_tuple(1, true, 2.0f)); static_assert(std::tuple_size_v == 2); static_assert(std::is_same_v, bool>, ""); static_assert(std::is_same_v, float>); EXPECT_EQ(std::get<0>(sliced), true); EXPECT_EQ(std::get<1>(sliced), 2.0f); } TEST(Slice, PreservesRValueRef) { int i; int& int_ref = i; auto tuple = std::forward_as_tuple(std::move(int_ref)); static_assert(std::is_same_v>); auto sliced = Slice<0, 1>(std::move(tuple)); static_assert(std::is_same_v>); } TEST(SliceTuple, Empty) { using sliced = SliceTuple<0, 0, std::tuple<>>; static_assert(std::tuple_size_v == 0); } TEST(SliceTuple, SingleElementSliceEmpty) { using sliced = SliceTuple<0, 0, std::tuple>; static_assert(std::tuple_size_v == 0); } TEST(SliceTuple, SingleElementSliceFull) { using sliced = SliceTuple<0, 1, std::tuple>; static_assert(std::tuple_size_v == 1); static_assert(std::is_same_v, int>); } TEST(SliceTuple, MixedTupleSliceEmpty) { using sliced = SliceTuple<1, 0, std::tuple>; static_assert(std::tuple_size_v == 0); } TEST(SliceTuple, MixedTupleSliceFull) { using sliced = SliceTuple<0, 3, std::tuple>; static_assert(std::tuple_size_v == 3); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, bool>); static_assert(std::is_same_v, float>); } TEST(SliceTuple, MixedTupleSliceLowPart) { using sliced = SliceTuple<0, 2, std::tuple>; static_assert(std::tuple_size_v == 2); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, bool>); } TEST(SliceTuple, MixedTupleSliceHighPart) { using sliced = SliceTuple<1, 2, std::tuple>; static_assert(std::tuple_size_v == 2); static_assert(std::is_same_v, bool>); static_assert(std::is_same_v, float>); } } // namespace tint::traits