diff options
Diffstat (limited to 'chapter33/src/test')
| -rw-r--r-- | chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java b/chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java new file mode 100644 index 00000000..5f25a901 --- /dev/null +++ b/chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java @@ -0,0 +1,186 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.type.Type; +import com.seaofnodes.simple.type.TypeSet; +import java.util.ArrayList; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +public class TypeSetTest { + @Before public void resetTypes() { Type.reset(); } + + @Test public void testTypeSetDiscoveryAndInterning() { + int[] external = {0xAAAAAAAA,0xAAAAAAAA,2}; // Odd identities are external. + int[] exceptions = {0,31,32,63,65}; + TypeSet s = TypeSet.make(false,true,exceptions); + exceptions[0] = 1; // The caller's array is not retained. + assertSame(s,TypeSet.EXTERNAL.with(0,false).without(31,true).with(32,false).without(63,true).without(65,true)); + assertSame(s,TypeSet.make(false,true,65,32,31,63,0,0)); + assertSame(TypeSet.EXTERNAL,s.without(0,false).with(31,true).without(32,false).with(63,true).with(65,true)); + TypeSet dual = s.dual(); + int hash = s.hashCode(); + for( int i=0; i<66; i++ ) { + boolean exception = i==0 || i==31 || i==32 || i==63 || i==65; + assertEquals(((i&1)!=0)^exception,s.contains(i,(i&1)!=0)); + assertEquals(!s.contains(i,(i&1)!=0),dual.contains(i,(i&1)!=0)); + } + int local = 66, remote = 67; + external[2] |= 1<<3; // Caller discovers an external identity; no set is updated. + assertFalse(s.contains(local,false)); + assertTrue(dual.contains(local,false)); + assertTrue(s.contains(remote,true)); + assertFalse(dual.contains(remote,true)); + assertEquals(hash,s.hashCode()); + assertSame(s,dual.dual()); + assertSame(s,TypeSet.make(false,true,0,31,32,63,65)); + assertSame(TypeSet.EXTERNAL,TypeSet.LOCAL.dual()); + assertSame(TypeSet.FULL,TypeSet.EMPTY.dual()); + assertTrue(TypeSet.EMPTY.isEmpty()); + assertTrue(TypeSet.EMPTY.with(local,false).isFinite()); + assertFalse(s.isFinite()); + + int next = -1; + for( int i=0; i<68; i++ ) if( s.contains(i,(i&1)!=0) ) { + next = s.nextKnown(next,68,external); + assertEquals(i,next); + } + assertEquals(-1,s.nextKnown(next,68,external)); + assertEquals(-1,s.nextKnown(Integer.MAX_VALUE,68,external)); + assertEquals(-1,TypeSet.EMPTY.nextKnown(-1,68,external)); + // FULL's padding bits are not identities, including the sign bit. + next = -1; + for( int i=0; i<68; i++ ) assertEquals(i,next=TypeSet.FULL.nextKnown(next,68,external)); + assertEquals(-1,TypeSet.FULL.nextKnown(next,68,external)); + // Classification and namespace do not participate in interning. + TypeSet same = TypeSet.make(false,true,0,31,32,63,65); + assertSame(s,same); + assertTrue(same.contains(0,false)); + assertFalse(same.contains(0,true)); + } + + @Test public void testTypeSetLattice() { + TypeSet[] sets = new TypeSet[32]; + for( int flags=0; flags<4; flags++ ) + for( int mask=0; mask<8; mask++ ) { + int[] ids = new int[Integer.bitCount(mask)]; + int len = 0; + for( int i=0; i<3; i++ ) if( (mask & (1<<i))!=0 ) ids[len++] = i; + sets[flags*8+mask] = TypeSet.make((flags&1)!=0,(flags&2)!=0,ids); + } + // Use the SAME interned sets with every ownership pattern for IDs 0..2. + // IDs 3 and 4 are later discoveries, one local and one external. + for( int owners=0; owners<8; owners++ ) { + int[] external = {owners | 16}; + for( TypeSet a : sets ) { + assertSame(a,a.dual().dual()); + assertSame(a,a.meet(a,external)); + assertSame(TypeSet.FULL,a.meet(a.dual(),external)); + assertSame(TypeSet.EMPTY,a.join(a.dual(),external)); + for( TypeSet b : sets ) { + TypeSet meet = a.meet(b,external), join = a.join(b,external); + assertSame(meet,b.meet(a,external)); + assertSame(join,b.join(a,external)); + assertSame(a,a.meet(join,external)); + assertSame(a,a.join(meet,external)); + boolean subset = true; + for( int i=0; i<5; i++ ) { + boolean ext = (external[0] & (1<<i))!=0; + assertEquals(a.contains(i,ext) || b.contains(i,ext),meet.contains(i,ext)); + assertEquals(a.contains(i,ext) && b.contains(i,ext),join.contains(i,ext)); + subset &= !a.contains(i,ext) || b.contains(i,ext); + } + assertEquals(subset,a.isa(b,external)); + for( TypeSet c : sets ) { + assertSame(a.meet(b,external).meet(c,external),a.meet(b.meet(c,external),external)); + assertSame(a.join(b,external).join(c,external),a.join(b.join(c,external),external)); + assertSame(a.meet(b.join(c,external),external),a.meet(b,external).join(a.meet(c,external),external)); + } + } + } + } + } + + @Test public void testTypeSetRebase() { + int[] sourceExternal = {4}, targetExternal = {14}; + int a = 0, b = 1, c = 2; + TypeSet s = TypeSet.EXTERNAL.with(a,false); // b absent, c present. + int local = 0, mappedB = 1, mappedC = 2, mappedA = 3; + int[] map = {mappedA,mappedB,mappedC}; + TypeSet copy = s.rebase(map,sourceExternal,targetExternal,false,true); + assertFalse(copy.contains(local,false)); + assertTrue(copy.contains(mappedA,true)); + assertFalse(copy.contains(mappedB,true)); + assertTrue(copy.contains(mappedC,true)); + assertSame(TypeSet.EXTERNAL.without(mappedB,true),copy); + assertSame(s,s.rebase(new int[]{a,b,c},sourceExternal,sourceExternal,false,true)); + assertSame(TypeSet.EMPTY,TypeSet.EMPTY.rebase(map,sourceExternal,targetExternal,false,false)); + assertSame(TypeSet.EMPTY.with(mappedA,true),TypeSet.EMPTY.with(a,false).rebase(map,sourceExternal,targetExternal,false,false)); + TypeSet bound = TypeSet.FULL.rebase(map,sourceExternal,targetExternal,true,true); + assertSame(TypeSet.FULL,bound); + assertFalse(copy.contains(4,false)); + assertTrue(bound.contains(4,false)); + assertTrue(copy.contains(5,true)); + try { s.rebase(new int[]{mappedA,mappedA,mappedC},sourceExternal,targetExternal,false,true); fail("Duplicate mapping"); } + catch( IllegalArgumentException expected ) { } + } + + @Test public void testTypeSetReset() { + Type.reset(); // Initialize the basic-table snapshots first. + TypeSet empty = TypeSet.EMPTY, external = TypeSet.EXTERNAL; + for( int run=0; run<2; run++ ) { + TypeSet old = TypeSet.make(false,true,12345,12347+run); + int hash = old.hashCode(); + assertSame(old,TypeSet.make(false,true,12345,12347+run)); + Type.reset(); // Exercises the actual compiler/test reset hook. + TypeSet fresh = TypeSet.make(false,true,12345,12347+run); + // Reset may recycle old and its dual; only inspect the new result. + assertEquals(hash,fresh.hashCode()); + assertTrue(fresh.contains(12345,false)); + assertTrue(fresh.contains(12347+run,false)); + assertFalse(fresh.contains(12346,false)); + assertSame(fresh,TypeSet.make(false,true,12345,12347+run)); + assertSame(fresh,fresh.dual().dual()); + assertSame(empty,TypeSet.make(false,false)); + assertSame(external,TypeSet.make(false,true)); + assertSame(TypeSet.FULL,empty.dual()); + assertSame(TypeSet.LOCAL,external.dual()); + } + } + + @Test public void testTypeSetInternHitsReuseStorage() { + var bean = java.lang.management.ManagementFactory.getThreadMXBean(); + org.junit.Assume.assumeTrue(bean instanceof com.sun.management.ThreadMXBean); + var allocation = (com.sun.management.ThreadMXBean)bean; + org.junit.Assume.assumeTrue(allocation.isThreadAllocatedMemorySupported()); + if( !allocation.isThreadAllocatedMemoryEnabled() ) allocation.setThreadAllocatedMemoryEnabled(true); + int[] ids = {0,31,63,95}; + int[] external = {0x80000000,0,0x80000000}; + int[] map = {95,31,0}, sourceExternal = {4}; + TypeSet s = TypeSet.make(false,false,ids); + TypeSet source = TypeSet.EMPTY.with(0,false).with(2,true); + TypeSet union = s.meet(TypeSet.EXTERNAL,external); + TypeSet rebased = source.rebase(map,sourceExternal,external,false,true); + TypeSet shorter = s.without(95,true); + // Warm all pool sizes, table entries, and the measured Java methods. + for( int i=0; i<5; i++ ) + typeSetHits(20000,ids,external,map,sourceExternal,s,source,union,rebased,shorter); + long thread = Thread.currentThread().threadId(); + long before = allocation.getThreadAllocatedBytes(thread); + typeSetHits(10000,ids,external,map,sourceExternal,s,source,union,rebased,shorter); + long bytes = allocation.getThreadAllocatedBytes(thread)-before; + assertEquals("Warmed intern hits allocate no storage",0,bytes); + assertSame(s,s.dual().dual()); + assertTrue(s.contains(95,true)); // Scratch reuse did not clear retained bits. + } + + private static void typeSetHits(int count, int[] ids, int[] external, int[] map, int[] sourceExternal, + TypeSet s, TypeSet source, TypeSet union, TypeSet rebased, TypeSet shorter) { + for( int i=0; i<count; i++ ) + if( TypeSet.make(false,false,ids)!=s || s.meet(TypeSet.EXTERNAL,external)!=union || + s.meet(TypeSet.FULL,external)!=TypeSet.FULL || s.without(95,true)!=shorter || + source.rebase(map,sourceExternal,external,false,true)!=rebased ) + throw new AssertionError("Noncanonical intern result"); + } + +} |
