xref: /aosp_15_r20/external/google-fruit/tests/util/test_lambda_invoker.py (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
1*a65addddSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*a65addddSAndroid Build Coastguard Worker#  Copyright 2016 Google Inc. All Rights Reserved.
3*a65addddSAndroid Build Coastguard Worker#
4*a65addddSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*a65addddSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*a65addddSAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*a65addddSAndroid Build Coastguard Worker#
8*a65addddSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
9*a65addddSAndroid Build Coastguard Worker#
10*a65addddSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*a65addddSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS-IS" BASIS,
12*a65addddSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*a65addddSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*a65addddSAndroid Build Coastguard Worker# limitations under the License.
15*a65addddSAndroid Build Coastguard Worker
16*a65addddSAndroid Build Coastguard Workerfrom absl.testing import parameterized
17*a65addddSAndroid Build Coastguard Workerfrom fruit_test_common import *
18*a65addddSAndroid Build Coastguard Worker
19*a65addddSAndroid Build Coastguard WorkerCOMMON_DEFINITIONS = '''
20*a65addddSAndroid Build Coastguard Worker    #include "test_common.h"
21*a65addddSAndroid Build Coastguard Worker
22*a65addddSAndroid Build Coastguard Worker    using namespace std;
23*a65addddSAndroid Build Coastguard Worker    using namespace fruit::impl;
24*a65addddSAndroid Build Coastguard Worker    '''
25*a65addddSAndroid Build Coastguard Worker
26*a65addddSAndroid Build Coastguard Workerclass TestLambdaInvoker(parameterized.TestCase):
27*a65addddSAndroid Build Coastguard Worker    def test_invoke_no_args(self):
28*a65addddSAndroid Build Coastguard Worker        source = '''
29*a65addddSAndroid Build Coastguard Worker            int main() {
30*a65addddSAndroid Build Coastguard Worker              // This is static because the lambda must have no captures.
31*a65addddSAndroid Build Coastguard Worker              static int num_invocations = 0;
32*a65addddSAndroid Build Coastguard Worker
33*a65addddSAndroid Build Coastguard Worker              auto l = []() {
34*a65addddSAndroid Build Coastguard Worker                ++num_invocations;
35*a65addddSAndroid Build Coastguard Worker              };
36*a65addddSAndroid Build Coastguard Worker              using L = decltype(l);
37*a65addddSAndroid Build Coastguard Worker              LambdaInvoker::invoke<L>();
38*a65addddSAndroid Build Coastguard Worker              Assert(num_invocations == 1);
39*a65addddSAndroid Build Coastguard Worker            }
40*a65addddSAndroid Build Coastguard Worker            '''
41*a65addddSAndroid Build Coastguard Worker        expect_success(
42*a65addddSAndroid Build Coastguard Worker            COMMON_DEFINITIONS,
43*a65addddSAndroid Build Coastguard Worker            source,
44*a65addddSAndroid Build Coastguard Worker            locals())
45*a65addddSAndroid Build Coastguard Worker
46*a65addddSAndroid Build Coastguard Worker    def test_invoke_some_args(self):
47*a65addddSAndroid Build Coastguard Worker        source = '''
48*a65addddSAndroid Build Coastguard Worker            int main() {
49*a65addddSAndroid Build Coastguard Worker              // This is static because the lambda must have no captures.
50*a65addddSAndroid Build Coastguard Worker              static int num_invocations = 0;
51*a65addddSAndroid Build Coastguard Worker
52*a65addddSAndroid Build Coastguard Worker              auto l = [](int n, double x) {
53*a65addddSAndroid Build Coastguard Worker                Assert(n == 5);
54*a65addddSAndroid Build Coastguard Worker                Assert(x == 3.14);
55*a65addddSAndroid Build Coastguard Worker                ++num_invocations;
56*a65addddSAndroid Build Coastguard Worker              };
57*a65addddSAndroid Build Coastguard Worker              using L = decltype(l);
58*a65addddSAndroid Build Coastguard Worker              LambdaInvoker::invoke<L>(5, 3.14);
59*a65addddSAndroid Build Coastguard Worker              Assert(num_invocations == 1);
60*a65addddSAndroid Build Coastguard Worker            }
61*a65addddSAndroid Build Coastguard Worker            '''
62*a65addddSAndroid Build Coastguard Worker        expect_success(
63*a65addddSAndroid Build Coastguard Worker            COMMON_DEFINITIONS,
64*a65addddSAndroid Build Coastguard Worker            source,
65*a65addddSAndroid Build Coastguard Worker            locals())
66*a65addddSAndroid Build Coastguard Worker
67*a65addddSAndroid Build Coastguard Workerif __name__ == '__main__':
68*a65addddSAndroid Build Coastguard Worker    absltest.main()
69