1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "util/function_traits.h"
17
18 #include "util/tests/verifier_test.h"
19
20 #include <gtest/gtest.h>
21
22 namespace panda::verifier::test {
23
24 struct square_sum {
operator ()panda::verifier::test::square_sum25 int operator()(int x, int y) const
26 {
27 return (x + y) * (x + y);
28 }
29 };
30
31 struct square_diversity {
operator ()panda::verifier::test::square_diversity32 int operator()(int x, int y) const
33 {
34 return (x - y) * (x - y);
35 }
36 };
37
38 struct mult_by_mod {
39 int mod;
mult_by_modpanda::verifier::test::mult_by_mod40 explicit mult_by_mod(int module) : mod {module} {}
operator ()panda::verifier::test::mult_by_mod41 int operator()(int x, int y) const
42 {
43 return (x * y) % mod;
44 }
45 };
46
TEST_F(VerifierTest,function_traits)47 TEST_F(VerifierTest, function_traits)
48 {
49 square_sum sq_sum;
50 square_diversity sq_div;
51 n_ary<square_sum> op_s_sum {sq_sum};
52 n_ary<square_diversity> op_s_div {sq_div};
53 EXPECT_EQ(op_s_sum(2, 2), 16);
54 EXPECT_EQ(op_s_div(2, 1), 1);
55 EXPECT_EQ(op_s_sum(2, 1, 2), 121);
56 EXPECT_EQ(op_s_div(2, 1, 2), 1);
57
58 mult_by_mod mod5 {5};
59 mult_by_mod mod10 {10};
60 n_ary<mult_by_mod> op_mult_mod5 {mod5};
61 n_ary<mult_by_mod> op_mult_mod10 {mod10};
62 EXPECT_EQ(op_mult_mod5(2, 4), 3);
63 EXPECT_EQ(op_mult_mod10(2, 4), 8);
64 EXPECT_EQ(op_mult_mod5(2, 4, 2), 1);
65 EXPECT_EQ(op_mult_mod10(2, 4, 2), 6);
66 }
67
68 } // namespace panda::verifier::test