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
16from fruit_test_common import *
17
18COMMON_DEFINITIONS = '''
19    #define IN_FRUIT_CPP_FILE 1
20
21    #include "meta/common.h"
22    #include <fruit/impl/meta/vector.h>
23
24    #include <vector>
25
26    struct A {};
27    struct B {};
28    struct C {};
29
30    struct Select1st {
31      template <typename T, typename U>
32      struct apply {
33        using type = T;
34      };
35    };
36
37    struct Select2nd {
38      template <typename T, typename U>
39      struct apply {
40        using type = U;
41      };
42    };
43    '''
44
45def test_ImplicitCall():
46    source = '''
47        int main() {
48          AssertSameType(Type<int>,   Id<Select1st(Type<int>, Type<float>)>);
49          AssertSameType(Type<float>, Id<Select2nd(Type<int>, Type<float>)>);
50          AssertSameType(Type<int>,   Id<Select1st(Type<int>, Type<float>)>);
51          AssertSameType(Type<float>, Id<Select2nd(Type<int>, Type<float>)>);
52        }
53        '''
54    expect_success(
55        COMMON_DEFINITIONS,
56        source,
57        locals())
58
59def test_Call():
60    source = '''
61        int main() {
62          AssertSameType(Type<int>,   Id<Call(Select1st, Type<int>, Type<float>)>);
63          AssertSameType(Type<float>, Id<Call(Select2nd, Type<int>, Type<float>)>);
64          AssertSameType(Type<int>,   Id<Call(Select1st, Type<int>, Type<float>)>);
65          AssertSameType(Type<float>, Id<Call(Select2nd, Type<int>, Type<float>)>);
66        }
67        '''
68    expect_success(
69        COMMON_DEFINITIONS,
70        source,
71        locals())
72
73def test_DeferArgs():
74    source = '''
75        int main() {
76          AssertSameType(Type<int>,   Id<Call(Id<Call(Id<DeferArgs(Select1st)>, Type<int>)>, Type<float>)>);
77          AssertSameType(Type<float>, Id<Call(Id<Call(Id<DeferArgs(Select2nd)>, Type<int>)>, Type<float>)>);
78          AssertSameType(Type<int>,   Id<Call(Id<Call(Id<DeferArgs(Select1st)>, Type<int>)>, Type<float>)>);
79          AssertSameType(Type<float>, Id<Call(Id<Call(Id<DeferArgs(Select2nd)>, Type<int>)>, Type<float>)>);
80        }
81        '''
82    expect_success(
83        COMMON_DEFINITIONS,
84        source,
85        locals())
86
87if __name__== '__main__':
88    main(__file__)
89