From b5786e4f67c166a11533660647196c15e04508cd Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Fri, 4 Dec 2026 14:30:06 -0800 Subject: Add interned TypeSet with duals, reset, and reusable scratch storage Retain the local/external default design and explicit rebasing API. Add uniform-default overloads for migrating existing finite/cofinite compiler sets without changing import semantics. Tests cover the lattice, discovery, rebasing, reset, and allocation-free warmed intern hits. Validation: make -k -j8 tests passes (834 executions); existing compiler sites still use XInt at this checkpoint. --- .../java/com/seaofnodes/simple/TypeSetTest.java | 186 +++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java (limited to 'chapter33/src/test/java/com') 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<