#!/usr/bin/env python3 # Copyright 2016 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS-IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from absl.testing import parameterized from fruit_test_common import * COMMON_DEFINITIONS = ''' #define IN_FRUIT_CPP_FILE 1 #include "meta/common.h" #include #include struct A1 {}; struct B1 {}; struct C1 {}; using A = Type; using B = Type; using C = Type; struct Square { template struct apply { using type = Int; }; }; ''' class TestSet(parameterized.TestCase): def test_EmptySet(self): source = ''' int main() { AssertNot(IsInSet(A, EmptySet)); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_ToSet1(self): source = ''' int main() { Assert(IsInSet(A, ToSet1)); AssertNot(IsInSet(A, ToSet1)); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_ToSet2(self): source = ''' int main() { Assert(IsInSet(A, ToSet2)); Assert(IsInSet(B, ToSet2)); AssertNot(IsInSet(C, ToSet2)); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_IsSameSet(self): source = ''' int main() { AssertSameSet(EmptySet, EmptySet); AssertSameSet(ToSet, ToSet); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_FoldSet(self): source = ''' int main() { AssertSameType(Id, Sum, Int<3>)>, Int<3>); AssertSameType(Id>, Sum, Int<3>)>, Int<5>); AssertSameType(Id, Int<2>, Int<5>, Int<9>, Int<13>>, Sum, Int<7>)>, Int<39>); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_FoldSetWithCombine(self): source = ''' int main() { AssertSameType(Id, Square, Sum, Int<0>)>, Int<0>); AssertSameType(Id>, Square, Sum, Int<0>)>, Int<4>); AssertSameType(Id, Int<2>, Int<5>, Int<9>, Int<13>>, Square, Sum, Int<0>)>, Int<288>); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_AddToSet(self): source = ''' int main() { AssertSameSet(Id, ToSet); AssertSameSet(Id, A)>, ToSet); AssertSameSet(Id, A)>, ToSet); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_TransformSet(self): source = ''' int main() { AssertSameSet(Id, Square)>, ToSet<>); AssertSameSet(Id>, Square)>, ToSet>); AssertSameSet(Id, Int<2>, Int<5>, Int<9>, Int<13>>, Square)>, ToSet, Int<4>, Int<25>, Int<81>, Int<169>>); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_SetSize(self): source = ''' int main() { AssertSameType(Id)>, Int<0>); AssertSameType(Id>)>, Int<1>); AssertSameType(Id, Int<2>, Int<5>, Int<9>, Int<13>>)>, Int<5>); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_IsEmptySet(self): source = ''' int main() { Assert(IsEmptySet(ToSet<>)); AssertNot(IsEmptySet(ToSet>)); AssertNot(IsEmptySet(ToSet, Int<2>, Int<5>, Int<9>, Int<13>>)); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_SetDifference(self): source = ''' int main() { AssertSameSet(Id, ToSet<>)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet<>)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet<>)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet<>)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, EmptySet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_SetIntersection(self): source = ''' int main() { AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); AssertSameSet(Id, ToSet<>)>, ToSet<>); AssertSameSet(Id, ToSet)>, ToSet<>); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_IsContained(self): source = ''' int main() { Assert(IsContained(ToSet<>, ToSet<>)); Assert(IsContained(ToSet<>, ToSet)); Assert(IsContained(ToSet, ToSet)); Assert(IsContained(ToSet, ToSet)); Assert(IsContained(ToSet, ToSet)); Assert(IsContained(ToSet, ToSet)); Assert(IsContained(ToSet, ToSet)); AssertNot(IsContained(ToSet, ToSet)); AssertNot(IsContained(ToSet, ToSet)); AssertNot(IsContained(ToSet, ToSet)); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_IsDisjoint(self): source = ''' int main() { AssertNot(IsDisjoint(ToSet, ToSet)); AssertNot(IsDisjoint(ToSet, ToSet)); AssertNot(IsDisjoint(ToSet, ToSet)); Assert(IsDisjoint(ToSet, ToSet)); Assert(IsDisjoint(ToSet<>, ToSet)); Assert(IsDisjoint(ToSet, ToSet)); Assert(IsDisjoint(ToSet, ToSet<>)); Assert(IsDisjoint(ToSet, ToSet)); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_SetUnion(self): source = ''' int main() { AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); AssertSameSet(Id, ToSet<>)>, ToSet); AssertSameSet(Id, ToSet)>, ToSet); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) if __name__ == '__main__': absltest.main()