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