1 package org.simantics.scl.compiler.tests.unit;
3 import java.util.Collection;
5 import org.junit.Assert;
7 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
8 import org.simantics.scl.compiler.environment.filter.NamespaceFilter;
9 import org.simantics.scl.compiler.environment.filter.NamespaceFilters;
10 import org.simantics.scl.compiler.environment.filter.NegativeNamespaceFilter;
11 import org.simantics.scl.compiler.environment.filter.PositiveNamespaceFilter;
13 import gnu.trove.set.hash.THashSet;
15 public class TestNamespaceFilter {
17 private void testBooleanOperations(Collection<String> all, NamespaceFilter a, NamespaceFilter b) {
19 NamespaceFilter c = NamespaceFilters.union(a, b);
20 //System.out.println("union(" + a + ", " + b + ") = " + c);
21 for(String name : all)
23 a.isValueIncluded(name) || b.isValueIncluded(name),
24 c.isValueIncluded(name));
27 NamespaceFilter c = NamespaceFilters.intersection(a, b);
28 //System.out.println("intersection(" + a + ", " + b + ") = " + c);
29 for(String name : all)
31 a.isValueIncluded(name) && b.isValueIncluded(name),
32 c.isValueIncluded(name));
36 private void testBooleanOperations(THashSet<String> a, THashSet<String> b) {
37 THashSet<String> all = new THashSet<String>();
42 PositiveNamespaceFilter pa = new PositiveNamespaceFilter(a);
43 NegativeNamespaceFilter na = new NegativeNamespaceFilter(a);
44 PositiveNamespaceFilter pb = new PositiveNamespaceFilter(b);
45 NegativeNamespaceFilter nb = new NegativeNamespaceFilter(b);
46 testBooleanOperations(all, pa, pb);
47 testBooleanOperations(all, na, pb);
48 testBooleanOperations(all, pa, nb);
49 testBooleanOperations(all, na, nb);
52 private void testBooleanOperations(THashSet<String> a) {
53 THashSet<String> all = new THashSet<String>();
57 PositiveNamespaceFilter pa = new PositiveNamespaceFilter(a);
58 NegativeNamespaceFilter na = new NegativeNamespaceFilter(a);
59 testBooleanOperations(all, pa, AcceptAllNamespaceFilter.INSTANCE);
60 testBooleanOperations(all, na, AcceptAllNamespaceFilter.INSTANCE);
61 testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, pa);
62 testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, na);
63 testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, AcceptAllNamespaceFilter.INSTANCE);
67 public void testBooleanOperations() {
68 for(int p=0;p<8;++p) {
69 THashSet<String> a = new THashSet<String>();
71 if(((p >> i) & 1) == 1)
72 a.add(String.valueOf(i));
73 testBooleanOperations(a);
76 for(int p=0;p<64;++p) {
77 THashSet<String> a = new THashSet<String>();
78 THashSet<String> b = new THashSet<String>();
79 for(int i=0;i<3;++i) {
80 if(((p >> i) & 1) == 1)
81 a.add(String.valueOf(i));
82 if(((p >> (i+3)) & 1) == 1)
83 b.add(String.valueOf(i));
85 testBooleanOperations(a, b);