diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-07 12:57:31 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-07 12:57:31 -0700 |
| commit | aaec6f8136df6f7020bfc05f79ff5273adf73de1 (patch) | |
| tree | 5df4aae069bd88dd09f45d957c85161e571b7a46 /chapter18/src/test/java/com/seaofnodes | |
| parent | 514b3147bf08817d17aacc492be255621209ec8b (diff) | |
| download | Simple-aaec6f8136df6f7020bfc05f79ff5273adf73de1.tar.gz Simple-aaec6f8136df6f7020bfc05f79ff5273adf73de1.zip | |
flt->int conversion
Diffstat (limited to 'chapter18/src/test/java/com/seaofnodes')
6 files changed, 28 insertions, 8 deletions
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java index fc5b37d9..130ac8ea 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java @@ -123,8 +123,8 @@ struct N { !N? !next; int i; }; n.i = 3.14; return n.i; """); - try { code.parse().opto(); fail(); } - catch( Exception e ) { assertEquals("Type 3.14 is not of declared type int",e.getMessage()); } + code.parse().opto(); + assertEquals("3",Eval2.eval(code,0)); } @Test diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java index 7b065411..7eed49fb 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java @@ -8,6 +8,25 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; 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;", "9223372036854775807", "9223372036854775807"}, + {"int 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]).parse().opto().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;").parse().opto().typeCheck(); // Compare strings so +0.0 and -0.0 remain distinct. diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java index e64d5b27..93b64747 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java @@ -124,8 +124,8 @@ return s.x; @Test public void testInt10() { CodeGen code = new CodeGen("flt x = 1.5; x++; int i = x; return i;"); - try { code.parse().opto(); fail(); } - catch( Exception e ) { assertEquals("Type 2.5f is not of declared type int",e.getMessage()); } + code.parse().opto(); + assertEquals("2",Eval2.eval(code,0)); } diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java index 0758fbf5..22eb91b9 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java @@ -14,11 +14,8 @@ public class Chapter18Test { CodeGen zero = new CodeGen("var a = new "+type+"[arg]; return a[arg-1];") .parse().opto().typeCheck(); assertEquals("0.0",Eval2.eval(zero,3)); - // Eval2 does not execute RoundF32 yet; check nonconstant narrowing here. new CodeGen(type+" x = arg; var a = new "+type+"[arg]; a[0] = x; return a[0];") .parse().opto().typeCheck(); - } - for( String type : new String[]{"flt","f64"} ) { CodeGen loop = new CodeGen("var a = new "+type+"[arg]; "+ "for(int i=0; i<arg; i++) a[i] += i+0.25; return a[0]+a[arg-1];") .parse().opto().typeCheck(); @@ -28,7 +25,7 @@ public class Chapter18Test { CodeGen rounded = new CodeGen("var a = new "+type+"[arg]; "+ "for(int i=0; i<arg; i++) a[i] = 16777216.0+i; return a[arg-1];") .parse().opto().typeCheck(); - assertEquals("1.6777217E7",Eval2.eval(rounded,2)); + assertEquals(type.equals("f32") ? "1.6777216E7" : "1.6777217E7",Eval2.eval(rounded,2)); } CodeGen rounded = new CodeGen("var a = new f32[arg]; "+ "for(int i=0; i<arg; i++) a[i] = 16777217.0; return a[arg-1];") diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Eval2.java b/chapter18/src/test/java/com/seaofnodes/simple/Eval2.java index c517ce6e..3f337e86 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/Eval2.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/Eval2.java @@ -279,6 +279,8 @@ public abstract class Eval2 { case StoreNode st -> store(st); case SubNode sub -> x(sub.in(1)) - x(sub.in(2)); case SubFNode sbf -> d(sbf.in(1)) - d(sbf.in(2)); + case RoundF32Node rnd -> (double)(float)d(rnd.in(1)); + case ToIntegerNode cvt -> (long)d(cvt.in(1)); case ToFloatNode toflt-> (double)x(toflt.in(1)); case XorNode xor -> x(xor.in(1)) ^ x(xor.in(2)); default -> throw Utils.TODO(); diff --git a/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java index 1e2a08a7..7dbc551e 100644 --- a/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java +++ b/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java @@ -296,6 +296,8 @@ public class Evaluator { case OrNode or -> vall(or .in(1)) | vall(or .in(2)); case XorNode xor -> vall(xor.in(1)) ^ vall(xor.in(2)); case CastNode cast -> val(cast.in(1)); + case RoundF32Node rnd -> (double)(float)vald(rnd.in(1)); + case ToIntegerNode cvt -> (long)vald(cvt.in(1)); case ToFloatNode cast -> (double)vall(cast.in(1)); case LoadNode load -> load(load); case StoreNode store -> store(store); |
