xref: /aosp_15_r20/external/google-fruit/tests/meta/test_vector.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/vector.h>
24    #include <fruit/impl/meta/metaprogramming.h>
25
26    struct A1 {};
27    struct B1 {};
28    struct C1 {};
29
30    using A = A1;
31    using B = B1;
32    using C = C1;
33    '''
34
35class TestVector(parameterized.TestCase):
36    def test_IsInVector(self):
37        source = '''
38            int main() {
39                AssertNot(IsInVector(A, Vector<>));
40                AssertNot(IsInVector(A, Vector<B>));
41                Assert(IsInVector(A, Vector<A>));
42            }
43            '''
44        expect_success(
45            COMMON_DEFINITIONS,
46            source,
47            locals())
48
49    def test_IsSameVector(self):
50        source = '''
51            int main() {
52                AssertNotSameType(Vector<A, B>, Vector<B, A>);
53                AssertNotSameType(Vector<A>, Vector<>);
54                AssertNotSameType(Vector<>, Vector<A>);
55            }
56            '''
57        expect_success(
58            COMMON_DEFINITIONS,
59            source,
60            locals())
61
62    def test_VectorSize(self):
63        source = '''
64            int main() {
65                AssertSameType(Id<VectorSize(Vector<>)>, Int<0>);
66                AssertSameType(Id<VectorSize(Vector<A>)>, Int<1>);
67                AssertSameType(Id<VectorSize(Vector<A, B>)>, Int<2>);
68            }
69            '''
70        expect_success(
71            COMMON_DEFINITIONS,
72            source,
73            locals())
74
75    def test_ConcatVectors(self):
76        source = '''
77            int main() {
78                AssertSameType(Id<ConcatVectors(Vector<>, Vector<>)>, Vector<>);
79                AssertSameType(Id<ConcatVectors(Vector<>, Vector<A, B>)>, Vector<A, B>);
80                AssertSameType(Id<ConcatVectors(Vector<A, B>, Vector<>)>, Vector<A, B>);
81                AssertSameType(Id<ConcatVectors(Vector<A>, Vector<A, B>)>, Vector<A, A, B>);
82                AssertSameType(Id<ConcatVectors(Vector<A, B>, Vector<A, C>)>, Vector<A, B, A, C>);
83            }
84            '''
85        expect_success(
86            COMMON_DEFINITIONS,
87            source,
88            locals())
89
90    def test_VectorEndsWith(self):
91        source = '''
92            int main() {
93                Assert(VectorEndsWith(Vector<A, B>, B));
94                AssertNot(VectorEndsWith(Vector<A, B>, A));
95                AssertNot(VectorEndsWith(Vector<>, A));
96            }
97            '''
98        expect_success(
99            COMMON_DEFINITIONS,
100            source,
101            locals())
102
103    def test_VectorRemoveFirstN(self):
104        source = '''
105            int main() {
106                AssertSameType(Id<VectorRemoveFirstN(Vector<>, Int<0>)>, Vector<>);
107                AssertSameType(Id<VectorRemoveFirstN(Vector<A>, Int<0>)>, Vector<A>);
108                AssertSameType(Id<VectorRemoveFirstN(Vector<A>, Int<1>)>, Vector<>);
109                AssertSameType(Id<VectorRemoveFirstN(Vector<A, B, C>, Int<0>)>, Vector<A, B, C>);
110                AssertSameType(Id<VectorRemoveFirstN(Vector<A, B, C>, Int<1>)>, Vector<B, C>);
111                AssertSameType(Id<VectorRemoveFirstN(Vector<A, B, C>, Int<2>)>, Vector<C>);
112                AssertSameType(Id<VectorRemoveFirstN(Vector<A, B, C>, Int<3>)>, Vector<>);
113            }
114            '''
115        expect_success(
116            COMMON_DEFINITIONS,
117            source,
118            locals())
119
120if __name__ == '__main__':
121    absltest.main()
122