X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.scl.compiler.tests%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ftests%2Funit%2FTestNamespaceFilter.java;fp=tests%2Forg.simantics.scl.compiler.tests%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ftests%2Funit%2FTestNamespaceFilter.java;h=fa3bd9f65988011ca71f8fa7f46fdeec2f7dd4cb;hb=0364f8f54b009e9e5de482d5c9d1cb7efb023141;hp=0000000000000000000000000000000000000000;hpb=7ecf07ff9aacab300f1fb900f1f0f97beb1be139;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestNamespaceFilter.java b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestNamespaceFilter.java new file mode 100644 index 000000000..fa3bd9f65 --- /dev/null +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/unit/TestNamespaceFilter.java @@ -0,0 +1,89 @@ +package org.simantics.scl.compiler.tests.unit; + +import java.util.Collection; + +import org.junit.Test; +import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter; +import org.simantics.scl.compiler.environment.filter.NamespaceFilter; +import org.simantics.scl.compiler.environment.filter.NamespaceFilters; +import org.simantics.scl.compiler.environment.filter.NegativeNamespaceFilter; +import org.simantics.scl.compiler.environment.filter.PositiveNamespaceFilter; + +import gnu.trove.set.hash.THashSet; +import junit.framework.Assert; + +public class TestNamespaceFilter { + + private void testBooleanOperations(Collection all, NamespaceFilter a, NamespaceFilter b) { + { + NamespaceFilter c = NamespaceFilters.union(a, b); + //System.out.println("union(" + a + ", " + b + ") = " + c); + for(String name : all) + Assert.assertEquals( + a.isValueIncluded(name) || b.isValueIncluded(name), + c.isValueIncluded(name)); + } + { + NamespaceFilter c = NamespaceFilters.intersection(a, b); + //System.out.println("intersection(" + a + ", " + b + ") = " + c); + for(String name : all) + Assert.assertEquals( + a.isValueIncluded(name) && b.isValueIncluded(name), + c.isValueIncluded(name)); + } + } + + private void testBooleanOperations(THashSet a, THashSet b) { + THashSet all = new THashSet(); + all.addAll(a); + all.addAll(b); + all.add("dummy"); + + PositiveNamespaceFilter pa = new PositiveNamespaceFilter(a); + NegativeNamespaceFilter na = new NegativeNamespaceFilter(a); + PositiveNamespaceFilter pb = new PositiveNamespaceFilter(b); + NegativeNamespaceFilter nb = new NegativeNamespaceFilter(b); + testBooleanOperations(all, pa, pb); + testBooleanOperations(all, na, pb); + testBooleanOperations(all, pa, nb); + testBooleanOperations(all, na, nb); + } + + private void testBooleanOperations(THashSet a) { + THashSet all = new THashSet(); + all.addAll(a); + all.add("dummy"); + + PositiveNamespaceFilter pa = new PositiveNamespaceFilter(a); + NegativeNamespaceFilter na = new NegativeNamespaceFilter(a); + testBooleanOperations(all, pa, AcceptAllNamespaceFilter.INSTANCE); + testBooleanOperations(all, na, AcceptAllNamespaceFilter.INSTANCE); + testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, pa); + testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, na); + testBooleanOperations(all, AcceptAllNamespaceFilter.INSTANCE, AcceptAllNamespaceFilter.INSTANCE); + } + + @Test + public void testBooleanOperations() { + for(int p=0;p<8;++p) { + THashSet a = new THashSet(); + for(int i=0;i<3;++i) + if(((p >> i) & 1) == 1) + a.add(String.valueOf(i)); + testBooleanOperations(a); + } + + for(int p=0;p<64;++p) { + THashSet a = new THashSet(); + THashSet b = new THashSet(); + for(int i=0;i<3;++i) { + if(((p >> i) & 1) == 1) + a.add(String.valueOf(i)); + if(((p >> (i+3)) & 1) == 1) + b.add(String.valueOf(i)); + } + testBooleanOperations(a, b); + } + } + +}