From 9fe7c6f9697b5bbf3041261fdc60040e75cc674f Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sun, 15 Nov 2026 16:11:39 -0800 Subject: Split off chapter 31 Ch 32 will be focused on compiler performance issues. --- .../com/seaofnodes/simple/Chapter14FloatTest.java | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 chapter32/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java (limited to 'chapter32/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java') diff --git a/chapter32/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java b/chapter32/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java new file mode 100644 index 00000000..9a3bb87a --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java @@ -0,0 +1,106 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.CodeGen; +import org.junit.Ignore; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class Chapter14FloatTest { + @Test public void testNumericAssignments() { + String[][] tests = { + {"int rounded=17.3/2.5; return rounded;", "6", "6"}, + {"int rounded=arg/2.5; return rounded;", "6", "-6"}, + {"int rounded=0; rounded=arg/2.5; return rounded;", "6", "-6"}, + {"i8 rounded=arg*16.0; return rounded;", "16", "-16"}, + {"int rounded=1e100; return rounded;", "-1", "-1"}, + {"int rounded=-1e100; return rounded;", "0", "0"}, + {"i64 rounded=1e100; return rounded;", "9223372036854775807", "9223372036854775807"}, + {"i64 rounded=-1e100; return rounded;", "-9223372036854775808", "-9223372036854775808"}, + {"int rounded=1e300*1e300-1e300*1e300; return rounded;", "0", "0"}, + {"f32 rounded=arg+16777216.0; return rounded;", "1.6777232E7", "1.6777199E7"} + }; + for( String[] test : tests ) { + var code = new CodeGen(test[0]).driver(CodeGen.Phase.TypeCheck); + assertEquals(test[0],test[1],Eval2.eval(code,17)); + assertEquals(test[0],test[2],Eval2.eval(code,-17)); + } + } + + + @Test public void testSubZeroFloat() { + var code = new CodeGen("flt x = arg; return 0-x;").driver(CodeGen.Phase.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"}, + {"if(0) return 7; else return 2.5;", "2.5"}, + {"return 7; return 2.5;", "7"}, + {"return 2.5; return 7;", "2.5"} + } ) { + String src = test[0]; + var code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck); + assertEquals(src,test[1],Eval2.eval(code,0)); + } + } + + + @Ignore + @Test + public void testJig() { + CodeGen code = new CodeGen( +""" +return 0; +"""); + code.driver(CodeGen.Phase.FinalizeIR); + assertEquals("return 0;", code.print()); + assertEquals("0", Eval2.eval(code, 0)); + } + + @Test + public void testFloat() { + CodeGen code = new CodeGen( +""" +flt x = 3.14; +return 0; +"""); + code.driver(CodeGen.Phase.FinalizeIR); + assertEquals("return 0;", code.print()); + assertEquals("0", Eval2.eval(code, 0)); + } + + @Test + public void testSquareRoot() { + CodeGen code = new CodeGen( +""" +flt guess = arg; +while( 1 ) { + flt next = (arg/guess + guess)/2; + if( next == guess ) break; + guess = next; +} +return guess; +"""); + // TODO: One of the problems with returning an OS error code, + // is it chops all results to int. + code.driver(CodeGen.Phase.FinalizeIR); + assertEquals("return Phi(Loop,(flt)arg,((Phi_guess+(ToFloat/Phi_guess))*0.5f));", code.print()); + assertEquals("3.0", Eval2.eval(code, 9)); + assertEquals("1.414213562373095", Eval2.eval(code, 2)); + } + + @Test + public void testFPOps() { + CodeGen code = new CodeGen( +""" +flt x = arg; +return x+1==x; +"""); + code.driver(CodeGen.Phase.FinalizeIR); + assertEquals("return ((flt)arg==(ToFloat+1.0f));", code.print()); + assertEquals("0", Eval2.eval(code, 1)); + } +} -- cgit v1.2.3