Home | History | Annotate | Download | only in meta
      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     #define IN_FRUIT_CPP_FILE 1
     20     
     21     #include "meta/common.h"
     22     #include <fruit/impl/meta/map.h>
     23     #include <fruit/impl/meta/metaprogramming.h>
     24     '''
     25 
     26 def test_FindInMap():
     27     source = '''
     28         int main() {
     29           AssertSameType(Id<FindInMap(ToSet<>, Int<2>)>, None);
     30           AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>>, Int<7>)>, None);
     31           AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>>, Int<2>)>, None);
     32           AssertSameType(Id<FindInMap(ToSet<Pair<Int<2>, Int<1>>>, Int<2>)>, Int<1>);
     33           AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<7>)>, None);
     34           AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<2>)>, None);
     35           AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<20>)>, None);
     36           AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<1>)>, Int<2>);
     37           AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<10>)>, Int<20>);
     38         }
     39         '''
     40     expect_success(
     41         COMMON_DEFINITIONS,
     42         source,
     43         locals())
     44 
     45 def test_MapContainsKey():
     46     source = '''
     47         int main() {
     48           AssertNot(MapContainsKey(ToSet<>, Int<2>));
     49           AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>>, Int<7>));
     50           AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>>, Int<2>));
     51           Assert(MapContainsKey(ToSet<Pair<Int<2>, Int<1>>>, Int<2>));
     52           AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<7>));
     53           AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<2>));
     54           AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<20>));
     55           Assert(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<1>));
     56           Assert(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<10>));
     57         }
     58         '''
     59     expect_success(
     60         COMMON_DEFINITIONS,
     61         source,
     62         locals())
     63 
     64 def test_GetMapKeys():
     65     source = '''
     66         int main() {
     67           AssertSameSet(Id<GetMapKeys(ToSet<>)>, ToSet<>);
     68           AssertSameSet(Id<GetMapKeys(ToSet<Pair<Int<1>, Int<2>>>)>, ToSet<Int<1>>);
     69           AssertSameSet(Id<GetMapKeys(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>)>, ToSet<Int<1>, Int<10>>);
     70         }
     71         '''
     72     expect_success(
     73         COMMON_DEFINITIONS,
     74         source,
     75         locals())
     76 
     77 if __name__== '__main__':
     78     main(__file__)
     79