From 0c62c5a642132fc8b2de4905bb4adce31069ece1 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sat, 19 Sep 2026 10:58:24 -0700 Subject: Tighter recursive-struct test --- .../main/java/com/seaofnodes/simple/type/Type.java | 2 +- .../test/java/com/seaofnodes/simple/TypeTest.java | 21 +++++++++++++++++++++ .../main/java/com/seaofnodes/simple/type/Type.java | 2 +- .../test/java/com/seaofnodes/simple/TypeTest.java | 21 +++++++++++++++++++++ .../test/java/com/seaofnodes/simple/TypeTest.java | 21 +++++++++++++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/chapter23/src/main/java/com/seaofnodes/simple/type/Type.java b/chapter23/src/main/java/com/seaofnodes/simple/type/Type.java index b7a8408e..fe0c33f3 100644 --- a/chapter23/src/main/java/com/seaofnodes/simple/type/Type.java +++ b/chapter23/src/main/java/com/seaofnodes/simple/type/Type.java @@ -216,7 +216,7 @@ public class Type /*implements Cloneable*/ { } // Overridden in subclasses; subclass can assume "this!=t" and java classes are same boolean eq(Type t) { return this==t; } - boolean cycle_eq(Type t) { assert _type < TCYCLIC; return eq(t); } + boolean cycle_eq(Type t) { assert _type < TCYCLIC; return _type==t._type && eq(t); } // A pair of uids int pid( Type that ) { return _uid < that._uid ? (_uid<<16 | that._uid) : (that._uid<<16 | _uid); diff --git a/chapter23/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter23/src/test/java/com/seaofnodes/simple/TypeTest.java index 0fd56607..40264d1d 100644 --- a/chapter23/src/test/java/com/seaofnodes/simple/TypeTest.java +++ b/chapter23/src/test/java/com/seaofnodes/simple/TypeTest.java @@ -7,6 +7,27 @@ import static org.junit.Assert.assertSame; public class TypeTest { + // Struct hashes omit field types, so interning compares unlike leaf kinds. + @Test + public void testCyclicLeafKinds() { + TypeStruct bottom = TypeStruct.make("B14LeafKinds",false, + Field.make("x",Type.BOTTOM,2,false, false)); + TypeStruct one = TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.constant(1.0),2,false, false)); + TypeStruct two = TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.constant(2.0),2,false, false)); + + // Meeting creates a new struct while cyclic equality is active. Its + // float leaf must compare unequal to the previously interned BOTTOM. + TypeStruct meet = (TypeStruct)one.meet(two); + assertSame(TypeFloat.F32,meet._fields[0]._t); + assertSame(meet,two.meet(one)); + assertSame(meet,TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.F32,2,false, false))); + assertSame(bottom,meet.meet(bottom)); + assertSame(meet.dual(),one.dual().join(two.dual())); + } + // Test basic properties and GLB @Test public void testTypeAdHoc() { diff --git a/chapter24/src/main/java/com/seaofnodes/simple/type/Type.java b/chapter24/src/main/java/com/seaofnodes/simple/type/Type.java index 2d5a6d04..fd474a84 100644 --- a/chapter24/src/main/java/com/seaofnodes/simple/type/Type.java +++ b/chapter24/src/main/java/com/seaofnodes/simple/type/Type.java @@ -216,7 +216,7 @@ public class Type /*implements Cloneable*/ { } // Overridden in subclasses; subclass can assume "this!=t" and java classes are same boolean eq(Type t) { return this==t; } - boolean cycle_eq(Type t) { assert _type < TCYCLIC; return eq(t); } + boolean cycle_eq(Type t) { assert _type < TCYCLIC; return _type==t._type && eq(t); } // A pair of uids int pid( Type that ) { return _uid < that._uid ? (_uid<<16 | that._uid) : (that._uid<<16 | _uid); diff --git a/chapter24/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter24/src/test/java/com/seaofnodes/simple/TypeTest.java index 0fd56607..40264d1d 100644 --- a/chapter24/src/test/java/com/seaofnodes/simple/TypeTest.java +++ b/chapter24/src/test/java/com/seaofnodes/simple/TypeTest.java @@ -7,6 +7,27 @@ import static org.junit.Assert.assertSame; public class TypeTest { + // Struct hashes omit field types, so interning compares unlike leaf kinds. + @Test + public void testCyclicLeafKinds() { + TypeStruct bottom = TypeStruct.make("B14LeafKinds",false, + Field.make("x",Type.BOTTOM,2,false, false)); + TypeStruct one = TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.constant(1.0),2,false, false)); + TypeStruct two = TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.constant(2.0),2,false, false)); + + // Meeting creates a new struct while cyclic equality is active. Its + // float leaf must compare unequal to the previously interned BOTTOM. + TypeStruct meet = (TypeStruct)one.meet(two); + assertSame(TypeFloat.F32,meet._fields[0]._t); + assertSame(meet,two.meet(one)); + assertSame(meet,TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.F32,2,false, false))); + assertSame(bottom,meet.meet(bottom)); + assertSame(meet.dual(),one.dual().join(two.dual())); + } + // Test basic properties and GLB @Test public void testTypeAdHoc() { diff --git a/chapter25/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter25/src/test/java/com/seaofnodes/simple/TypeTest.java index 65635afe..b83cc9e6 100644 --- a/chapter25/src/test/java/com/seaofnodes/simple/TypeTest.java +++ b/chapter25/src/test/java/com/seaofnodes/simple/TypeTest.java @@ -7,6 +7,27 @@ import static org.junit.Assert.assertSame; public class TypeTest { + // Struct hashes omit field types, so interning compares unlike leaf kinds. + @Test + public void testCyclicLeafKinds() { + TypeStruct bottom = TypeStruct.make("B14LeafKinds",false, + Field.make("x",Type.BOTTOM,2,false)); + TypeStruct one = TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.constant(1.0),2,false)); + TypeStruct two = TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.constant(2.0),2,false)); + + // Meeting creates a new struct while cyclic equality is active. Its + // float leaf must compare unequal to the previously interned BOTTOM. + TypeStruct meet = (TypeStruct)one.meet(two); + assertSame(TypeFloat.F32,meet._fields[0]._t); + assertSame(meet,two.meet(one)); + assertSame(meet,TypeStruct.make("B14LeafKinds",false, + Field.make("x",TypeFloat.F32,2,false))); + assertSame(bottom,meet.meet(bottom)); + assertSame(meet.dual(),one.dual().join(two.dual())); + } + // Test basic type properties @Test public void testTypeAdHoc() { -- cgit v1.2.3