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