]> gerrit.simantics Code Review - simantics/platform.git/blob
fa3bd9f65988011ca71f8fa7f46fdeec2f7dd4cb
[simantics/platform.git] /
1 package org.simantics.scl.compiler.tests.unit;\r
2 \r
3 import java.util.Collection;\r
4 \r
5 import org.junit.Test;\r
6 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;\r
7 import org.simantics.scl.compiler.environment.filter.NamespaceFilter;\r
8 import org.simantics.scl.compiler.environment.filter.NamespaceFilters;\r
9 import org.simantics.scl.compiler.environment.filter.NegativeNamespaceFilter;\r
10 import org.simantics.scl.compiler.environment.filter.PositiveNamespaceFilter;\r
11 \r
12 import gnu.trove.set.hash.THashSet;\r
13 import junit.framework.Assert;\r
14 \r
15 public class TestNamespaceFilter {\r
16     \r
17     private void testBooleanOperations(Collection<String> all, NamespaceFilter a, NamespaceFilter b) {\r
18         {\r
19             NamespaceFilter c = NamespaceFilters.union(a, b);\r
20             //System.out.println("union(" + a + ", " + b + ") = " + c);\r
21             for(String name : all)\r
22                 Assert.assertEquals(\r
23                         a.isValueIncluded(name) || b.isValueIncluded(name),\r
24                         c.isValueIncluded(name));\r
25         }\r
26         {\r
27             NamespaceFilter c = NamespaceFilters.intersection(a, b);\r
28             //System.out.println("intersection(" + a + ", " + b + ") = " + c);\r
29             for(String name : all)\r
30                 Assert.assertEquals(\r
31                         a.isValueIncluded(name) && b.isValueIncluded(name),\r
32                         c.isValueIncluded(name));\r
33         }\r
34     }\r
35 \r
36     private void testBooleanOperations(THashSet<String> a, THashSet<String> b) {\r
37         THashSet<String> all = new THashSet<String>();\r
38         all.addAll(a);\r
39         all.addAll(b);\r
40         all.add("dummy");\r
41         \r
42         PositiveNamespaceFilter pa = new PositiveNamespaceFilter(a);\r
43         NegativeNamespaceFilter na = new NegativeNamespaceFilter(a);\r
44         PositiveNamespaceFilter pb = new PositiveNamespaceFilter(b);\r
45         NegativeNamespaceFilter nb = new NegativeNamespaceFilter(b);\r
46         testBooleanOperations(all, pa, pb);\r
47         testBooleanOperations(all, na, pb);\r
48         testBooleanOperations(all, pa, nb);\r
49         testBooleanOperations(all, na, nb);\r
50     }\r
51     \r
52     private void testBooleanOperations(THashSet<String> a) {\r
53         THashSet<String> all = new THashSet<String>();\r
54         all.addAll(a);\r
55         all.add("dummy");\r
56         \r
57         PositiveNamespaceFilter pa = new PositiveNamespaceFilter(a);\r
58         NegativeNamespaceFilter na = new NegativeNamespaceFilter(a);\r
59         testBooleanOperations(all, pa, AcceptAllNamespaceFilter.INSTANCE);\r
60         testBooleanOperations(all, na, AcceptAllNamespaceFilter.INSTANCE);\r
61         testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, pa);\r
62         testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, na);\r
63         testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, AcceptAllNamespaceFilter.INSTANCE);\r
64     }\r
65     \r
66     @Test\r
67     public void testBooleanOperations() {\r
68         for(int p=0;p<8;++p) {\r
69             THashSet<String> a = new THashSet<String>();\r
70             for(int i=0;i<3;++i)\r
71                 if(((p >> i) & 1) == 1)\r
72                     a.add(String.valueOf(i));\r
73             testBooleanOperations(a);\r
74         }\r
75         \r
76         for(int p=0;p<64;++p) {\r
77             THashSet<String> a = new THashSet<String>();\r
78             THashSet<String> b = new THashSet<String>();\r
79             for(int i=0;i<3;++i) {\r
80                 if(((p >> i) & 1) == 1)\r
81                     a.add(String.valueOf(i));\r
82                 if(((p >> (i+3)) & 1) == 1)\r
83                     b.add(String.valueOf(i));\r
84             }\r
85             testBooleanOperations(a, b);\r
86         }\r
87     }\r
88     \r
89 }\r