xref: /aosp_15_r20/external/google-fruit/tests/meta/test_map.py (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
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 absl.testing import parameterized
17from fruit_test_common import *
18
19COMMON_DEFINITIONS = '''
20    #define IN_FRUIT_CPP_FILE 1
21
22    #include "meta/common.h"
23    #include <fruit/impl/meta/map.h>
24    #include <fruit/impl/meta/metaprogramming.h>
25    '''
26
27class TestMap(parameterized.TestCase):
28    def test_FindInMap(self):
29        source = '''
30            int main() {
31              AssertSameType(Id<FindInMap(ToSet<>, Int<2>)>, None);
32              AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>>, Int<7>)>, None);
33              AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>>, Int<2>)>, None);
34              AssertSameType(Id<FindInMap(ToSet<Pair<Int<2>, Int<1>>>, Int<2>)>, Int<1>);
35              AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<7>)>, None);
36              AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<2>)>, None);
37              AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<20>)>, None);
38              AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<1>)>, Int<2>);
39              AssertSameType(Id<FindInMap(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<10>)>, Int<20>);
40            }
41            '''
42        expect_success(
43            COMMON_DEFINITIONS,
44            source,
45            locals())
46
47    def test_MapContainsKey(self):
48        source = '''
49            int main() {
50              AssertNot(MapContainsKey(ToSet<>, Int<2>));
51              AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>>, Int<7>));
52              AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>>, Int<2>));
53              Assert(MapContainsKey(ToSet<Pair<Int<2>, Int<1>>>, Int<2>));
54              AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<7>));
55              AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<2>));
56              AssertNot(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<20>));
57              Assert(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<1>));
58              Assert(MapContainsKey(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>, Int<10>));
59            }
60            '''
61        expect_success(
62            COMMON_DEFINITIONS,
63            source,
64            locals())
65
66    def test_GetMapKeys(self):
67        source = '''
68            int main() {
69              AssertSameSet(Id<GetMapKeys(ToSet<>)>, ToSet<>);
70              AssertSameSet(Id<GetMapKeys(ToSet<Pair<Int<1>, Int<2>>>)>, ToSet<Int<1>>);
71              AssertSameSet(Id<GetMapKeys(ToSet<Pair<Int<1>, Int<2>>, Pair<Int<10>, Int<20>>>)>, ToSet<Int<1>, Int<10>>);
72            }
73            '''
74        expect_success(
75            COMMON_DEFINITIONS,
76            source,
77            locals())
78
79if __name__ == '__main__':
80    absltest.main()
81