summaryrefslogtreecommitdiff
path: root/chapter25/src/test/java/com
diff options
context:
space:
mode:
authorHsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com>2026-09-26 06:35:22 +0800
committerGitHub <noreply@github.com>2026-09-25 15:35:22 -0700
commitdd907c2d86eaaad0910e7bfe3f0d25e5caf8bdf7 (patch)
tree8b71264016c53914912e1cd30527b1bf86ed9bf3 /chapter25/src/test/java/com
parente922879e9dfa402934e2b5860900661b1169492c (diff)
downloadSimple-dd907c2d86eaaad0910e7bfe3f0d25e5caf8bdf7.tar.gz
Simple-dd907c2d86eaaad0910e7bfe3f0d25e5caf8bdf7.zip
Extend and backport SubNode peephole (#254)
* Extend and backport SubNode peephole * Add tests * Restrict subtraction zero peepholes to integer operands Require resolved integer mode in chapter 25 so zero subtraction preserves floating-point signed zero. Keep nonnumeric operands in the graph for subtraction diagnostics. Add signed-zero coverage from chapter 12 and null-subtraction diagnostics from chapter 22. Confirmed the regressions fail on the original PR. Full Make suites pass in chapters 4-25 (5462 tests), including fresh chapter 25 sys.o compilation. --------- Co-authored-by: Cliff Click <cliffc@acm.org>
Diffstat (limited to 'chapter25/src/test/java/com')
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter04Test.java28
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter12Test.java7
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter22Test.java9
3 files changed, 44 insertions, 0 deletions
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index eaa959cb..1132375e 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -53,6 +53,34 @@ public class Chapter04Test {
}
@Test
+ public void testSub0() {
+ CodeGen code = new CodeGen("return arg-0;");
+ code.parse();
+ assertEquals("return arg;", code.print());
+ }
+
+ @Test
+ public void test0Sub() {
+ CodeGen code = new CodeGen("return 0-arg;");
+ code.parse();
+ assertEquals("return (-arg);", code.print());
+ }
+
+ @Test
+ public void testSubNeg() {
+ CodeGen code = new CodeGen("return 3--arg;");
+ code.parse();
+ assertEquals("return (arg+3);", code.print());
+ }
+
+ @Test
+ public void testNegSub() {
+ CodeGen code = new CodeGen("return -arg-3;");
+ code.parse();
+ assertEquals("return (-(arg+3));", code.print());
+ }
+
+ @Test
public void testVarArg() {
CodeGen code = new CodeGen("return arg; ");
code.parse();
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 4b2d48d8..0f4bb0b4 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -5,6 +5,13 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class Chapter12Test {
+ @Test public void testSubZeroFloat() {
+ var code = new CodeGen("flt x = arg; return 0-x;").parse().opto().typeCheck();
+ // Compare strings so +0.0 and -0.0 remain distinct.
+ assertEquals("0.0", Eval2.eval(code,0));
+ assertEquals("-1.0", Eval2.eval(code,1));
+ }
+
@Test public void testDeadNumericReturns() {
for( String[] test : new String[][] {
{"if(1) return 7; else return 2.5;", "7"},
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter22Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter22Test.java
index fde523cf..4b036e62 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter22Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter22Test.java
@@ -11,6 +11,15 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter22Test {
+ @Test public void testSubZeroTypeError() {
+ try {
+ new CodeGen("return null-0;").parse().opto().typeCheck();
+ fail("Subtraction must reject null even when the other operand is zero");
+ } catch( Parser.ParseException e ) {
+ assertEquals("Cannot null - 0",e.getMessage());
+ }
+ }
+
@Test
public void testJig() throws IOException {