package com.seaofnodes.simple; import com.seaofnodes.simple.codegen.CodeGen; import com.seaofnodes.simple.node.*; import java.util.BitSet; import org.junit.Test; import static org.junit.Assert.*; // Execution regressions are separate from the fixed spill-measurement cohorts. public class MemoryEncodingTest { @Test public void testLoopLoadSearch() { String[] sources = { """ u8[] !program=new u8[106]; u8[] !out=new u8[0]; int sum=0; for(int i=0; i n instanceof LoadNode ld && ld.ptr() instanceof ProjNode ptr && ptr.in(0) instanceof NewNode nn && nn.size()._type==com.seaofnodes.simple.type.TypeInteger.constant(110) ? ld : null)); for( int arg=0; arg<7; arg++ ) { long expected=switch(p) { case 0 -> 106*arg+(arg>=2 ? 2 : 0); case 1 -> (arg&1)==0 ? 140 : 60; case 2 -> 130; default -> 16; }; assertEquals(Long.toString(expected),Eval2.eval(code,arg)); } } } @Test public void testKeepReadsBeforeWrites() { var code = new CodeGen(""" struct S { int x; int y; }; !S !a = new S; !S !b = new S; a.x=arg; b.x=arg+1; !S !p=a; !S !q=b; if (arg<0) { p=b; q=a; } int v; if (arg>1) { v=p.x; p.x=41; p.y=5; } else { v=q.x; q.x=42; } return v; """).parse().opto(); StopNode stop = code._stop; assertEquals(2,countMemoryNodes(stop,LoadNode.class,new BitSet())); assertEquals("-1",Eval2.eval(code,-1)); assertEquals("1",Eval2.eval(code,0)); assertEquals("3",Eval2.eval(code,3)); } private static int countMemoryNodes(Node n, Class kind, BitSet seen) { if (n==null || seen.get(n._nid)) return 0; seen.set(n._nid); int cnt=kind.isInstance(n) ? 1 : 0; for (Node def : n._inputs) cnt+=countMemoryNodes(def,kind,seen); return cnt; } }