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