• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/set.h>
24    #include <fruit/impl/meta/metaprogramming.h>
25
26    struct A1 {};
27    struct B1 {};
28    struct C1 {};
29
30    using A = Type<A1>;
31    using B = Type<B1>;
32    using C = Type<C1>;
33
34    struct Square {
35        template <typename N>
36        struct apply {
37            using type = Int<N::value * N::value>;
38        };
39    };
40    '''
41
42class TestSet(parameterized.TestCase):
43    def test_EmptySet(self):
44        source = '''
45            int main() {
46                AssertNot(IsInSet(A, EmptySet));
47            }
48            '''
49        expect_success(
50            COMMON_DEFINITIONS,
51            source,
52            locals())
53
54    def test_ToSet1(self):
55        source = '''
56            int main() {
57                Assert(IsInSet(A, ToSet1<A>));
58                AssertNot(IsInSet(A, ToSet1<B>));
59            }
60            '''
61        expect_success(
62            COMMON_DEFINITIONS,
63            source,
64            locals())
65
66    def test_ToSet2(self):
67        source = '''
68            int main() {
69                Assert(IsInSet(A, ToSet2<A, B>));
70                Assert(IsInSet(B, ToSet2<A, B>));
71                AssertNot(IsInSet(C, ToSet2<A, B>));
72            }
73            '''
74        expect_success(
75            COMMON_DEFINITIONS,
76            source,
77            locals())
78
79    def test_IsSameSet(self):
80        source = '''
81            int main() {
82                AssertSameSet(EmptySet, EmptySet);
83                AssertSameSet(ToSet<A, B>, ToSet<B, A>);
84            }
85            '''
86        expect_success(
87            COMMON_DEFINITIONS,
88            source,
89            locals())
90
91    def test_FoldSet(self):
92        source = '''
93            int main() {
94                AssertSameType(Id<FoldSet(ToSet<>, Sum, Int<3>)>, Int<3>);
95                AssertSameType(Id<FoldSet(ToSet<Int<2>>, Sum, Int<3>)>, Int<5>);
96                AssertSameType(Id<FoldSet(ToSet<Int<3>, Int<2>, Int<5>, Int<9>, Int<13>>, Sum, Int<7>)>, Int<39>);
97            }
98            '''
99        expect_success(
100            COMMON_DEFINITIONS,
101            source,
102            locals())
103
104    def test_FoldSetWithCombine(self):
105        source = '''
106            int main() {
107                AssertSameType(Id<FoldSetWithCombine(ToSet<>, Square, Sum, Int<0>)>, Int<0>);
108                AssertSameType(Id<FoldSetWithCombine(ToSet<Int<2>>, Square, Sum, Int<0>)>, Int<4>);
109                AssertSameType(Id<FoldSetWithCombine(ToSet<Int<3>, Int<2>, Int<5>, Int<9>, Int<13>>, Square, Sum, Int<0>)>, Int<288>);
110            }
111            '''
112        expect_success(
113            COMMON_DEFINITIONS,
114            source,
115            locals())
116
117    def test_AddToSet(self):
118        source = '''
119            int main() {
120                AssertSameSet(Id<AddToSet(EmptySet, A)>, ToSet<A>);
121                AssertSameSet(Id<AddToSet(ToSet<A, B>, A)>, ToSet<A, B>);
122                AssertSameSet(Id<AddToSet(ToSet<C, B>, A)>, ToSet<A, C, B>);
123            }
124            '''
125        expect_success(
126            COMMON_DEFINITIONS,
127            source,
128            locals())
129
130    def test_TransformSet(self):
131        source = '''
132            int main() {
133                AssertSameSet(Id<TransformSet(ToSet<>, Square)>, ToSet<>);
134                AssertSameSet(Id<TransformSet(ToSet<Int<2>>, Square)>, ToSet<Int<4>>);
135                AssertSameSet(Id<TransformSet(ToSet<Int<3>, Int<2>, Int<5>, Int<9>, Int<13>>, Square)>, ToSet<Int<9>, Int<4>, Int<25>, Int<81>, Int<169>>);
136            }
137            '''
138        expect_success(
139            COMMON_DEFINITIONS,
140            source,
141            locals())
142
143    def test_SetSize(self):
144        source = '''
145            int main() {
146                AssertSameType(Id<SetSize(ToSet<>)>, Int<0>);
147                AssertSameType(Id<SetSize(ToSet<Int<2>>)>, Int<1>);
148                AssertSameType(Id<SetSize(ToSet<Int<3>, Int<2>, Int<5>, Int<9>, Int<13>>)>, Int<5>);
149            }
150            '''
151        expect_success(
152            COMMON_DEFINITIONS,
153            source,
154            locals())
155
156    def test_IsEmptySet(self):
157        source = '''
158            int main() {
159                Assert(IsEmptySet(ToSet<>));
160                AssertNot(IsEmptySet(ToSet<Int<2>>));
161                AssertNot(IsEmptySet(ToSet<Int<3>, Int<2>, Int<5>, Int<9>, Int<13>>));
162            }
163            '''
164        expect_success(
165            COMMON_DEFINITIONS,
166            source,
167            locals())
168
169    def test_SetDifference(self):
170        source = '''
171            int main() {
172                AssertSameSet(Id<SetDifference(ToSet<>, ToSet<>)>, ToSet<>);
173                AssertSameSet(Id<SetDifference(ToSet<>, ToSet<A>)>, ToSet<>);
174                AssertSameSet(Id<SetDifference(ToSet<>, ToSet<A, B>)>, ToSet<>);
175                AssertSameSet(Id<SetDifference(ToSet<>, ToSet<A, B, C>)>, ToSet<>);
176                AssertSameSet(Id<SetDifference(ToSet<A>, ToSet<>)>, ToSet<A>);
177                AssertSameSet(Id<SetDifference(ToSet<A>, ToSet<A>)>, ToSet<>);
178                AssertSameSet(Id<SetDifference(ToSet<A>, ToSet<A, B>)>, ToSet<>);
179                AssertSameSet(Id<SetDifference(ToSet<A>, ToSet<A, B, C>)>, ToSet<>);
180                AssertSameSet(Id<SetDifference(ToSet<B>, ToSet<>)>, ToSet<B>);
181                AssertSameSet(Id<SetDifference(ToSet<B>, ToSet<A>)>, ToSet<B>);
182                AssertSameSet(Id<SetDifference(ToSet<B>, ToSet<A, B>)>, ToSet<>);
183                AssertSameSet(Id<SetDifference(ToSet<B>, ToSet<A, B, C>)>, ToSet<>);
184                AssertSameSet(Id<SetDifference(ToSet<B, C>, ToSet<>)>, ToSet<B, C>);
185                AssertSameSet(Id<SetDifference(ToSet<B, C>, ToSet<A>)>, ToSet<B, C>);
186                AssertSameSet(Id<SetDifference(ToSet<B, C>, ToSet<A, B>)>, ToSet<C>);
187                AssertSameSet(Id<SetDifference(ToSet<B, C>, ToSet<A, B, C>)>, ToSet<>);
188                AssertSameSet(Id<SetDifference(ToSet<A, B>, ToSet<A, B>)>, ToSet<>);
189                AssertSameSet(Id<SetDifference(ToSet<A>, ToSet<A, B>)>, EmptySet);
190                AssertSameSet(Id<SetDifference(ToSet<A, B, C>, ToSet<A>)>, ToSet<B, C>);
191                AssertSameSet(Id<SetDifference(ToSet<A, B, C>, ToSet<B>)>, ToSet<A, C>);
192                AssertSameSet(Id<SetDifference(ToSet<A, B, C>, ToSet<C>)>, ToSet<A, B>);
193            }
194            '''
195        expect_success(
196            COMMON_DEFINITIONS,
197            source,
198            locals())
199
200    def test_SetIntersection(self):
201        source = '''
202            int main() {
203                AssertSameSet(Id<SetIntersection(ToSet<A, B>, ToSet<A, B>)>, ToSet<A, B>);
204                AssertSameSet(Id<SetIntersection(ToSet<A>, ToSet<A, B>)>, ToSet<A>);
205                AssertSameSet(Id<SetIntersection(ToSet<A, B>, ToSet<A>)>, ToSet<A>);
206                AssertSameSet(Id<SetIntersection(ToSet<A>, ToSet<B>)>, ToSet<>);
207                AssertSameSet(Id<SetIntersection(ToSet<>, ToSet<A, B>)>, ToSet<>);
208                AssertSameSet(Id<SetIntersection(ToSet<C>, ToSet<A, B>)>, ToSet<>);
209                AssertSameSet(Id<SetIntersection(ToSet<A, B>, ToSet<>)>, ToSet<>);
210                AssertSameSet(Id<SetIntersection(ToSet<A, B>, ToSet<C>)>, ToSet<>);
211            }
212            '''
213        expect_success(
214            COMMON_DEFINITIONS,
215            source,
216            locals())
217
218    def test_IsContained(self):
219        source = '''
220            int main() {
221                Assert(IsContained(ToSet<>, ToSet<>));
222                Assert(IsContained(ToSet<>, ToSet<A>));
223                Assert(IsContained(ToSet<A, B>, ToSet<A, B>));
224                Assert(IsContained(ToSet<A, B>, ToSet<B, A>));
225                Assert(IsContained(ToSet<A>, ToSet<A, B, C>));
226                Assert(IsContained(ToSet<B>, ToSet<A, B, C>));
227                Assert(IsContained(ToSet<C>, ToSet<A, B, C>));
228                AssertNot(IsContained(ToSet<A, B, C>, ToSet<A, B>));
229                AssertNot(IsContained(ToSet<A, B, C>, ToSet<A, C>));
230                AssertNot(IsContained(ToSet<A, B, C>, ToSet<B, C>));
231            }
232            '''
233        expect_success(
234            COMMON_DEFINITIONS,
235            source,
236            locals())
237
238    def test_IsDisjoint(self):
239        source = '''
240            int main() {
241                AssertNot(IsDisjoint(ToSet<A, B>, ToSet<A, B>));
242                AssertNot(IsDisjoint(ToSet<A>, ToSet<A, B>));
243                AssertNot(IsDisjoint(ToSet<A, B>, ToSet<A>));
244                Assert(IsDisjoint(ToSet<A>, ToSet<B>));
245                Assert(IsDisjoint(ToSet<>, ToSet<A, B>));
246                Assert(IsDisjoint(ToSet<C>, ToSet<A, B>));
247                Assert(IsDisjoint(ToSet<A, B>, ToSet<>));
248                Assert(IsDisjoint(ToSet<A, B>, ToSet<C>));
249            }
250            '''
251        expect_success(
252            COMMON_DEFINITIONS,
253            source,
254            locals())
255
256    def test_SetUnion(self):
257        source = '''
258            int main() {
259                AssertSameSet(Id<SetUnion(ToSet<A, B>, ToSet<A, B>)>, ToSet<A, B>);
260                AssertSameSet(Id<SetUnion(ToSet<A>, ToSet<A, B>)>, ToSet<A, B>);
261                AssertSameSet(Id<SetUnion(ToSet<A, B>, ToSet<A>)>, ToSet<A, B>);
262                AssertSameSet(Id<SetUnion(ToSet<A>, ToSet<B>)>, ToSet<A, B>);
263                AssertSameSet(Id<SetUnion(ToSet<>, ToSet<A, B>)>, ToSet<A, B>);
264                AssertSameSet(Id<SetUnion(ToSet<C>, ToSet<A, B>)>, ToSet<A, B, C>);
265                AssertSameSet(Id<SetUnion(ToSet<A, B>, ToSet<>)>, ToSet<A, B>);
266                AssertSameSet(Id<SetUnion(ToSet<A, B>, ToSet<C>)>, ToSet<A, B, C>);
267            }
268            '''
269        expect_success(
270            COMMON_DEFINITIONS,
271            source,
272            locals())
273
274if __name__ == '__main__':
275    absltest.main()
276