summaryrefslogtreecommitdiff
path: root/chapter23/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-07 12:57:31 -0700
committerCliff Click <cliffc@acm.org>2026-10-07 12:57:31 -0700
commitaaec6f8136df6f7020bfc05f79ff5273adf73de1 (patch)
tree5df4aae069bd88dd09f45d957c85161e571b7a46 /chapter23/src/test/java/com/seaofnodes
parent514b3147bf08817d17aacc492be255621209ec8b (diff)
downloadSimple-aaec6f8136df6f7020bfc05f79ff5273adf73de1.tar.gz
Simple-aaec6f8136df6f7020bfc05f79ff5273adf73de1.zip
flt->int conversion
Diffstat (limited to 'chapter23/src/test/java/com/seaofnodes')
-rw-r--r--chapter23/src/test/java/com/seaofnodes/simple/Chapter12Test.java4
-rw-r--r--chapter23/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java19
-rw-r--r--chapter23/src/test/java/com/seaofnodes/simple/Chapter17bTest.java4
-rw-r--r--chapter23/src/test/java/com/seaofnodes/simple/Eval2.java2
-rw-r--r--chapter23/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java2
5 files changed, 27 insertions, 4 deletions
diff --git a/chapter23/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter23/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 8e9aaed6..041e0d06 100644
--- a/chapter23/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter23/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -124,8 +124,8 @@ struct N { !N? !next; int i; };
n.i = 3.14;
return n.i;
""");
- try { code.parse().opto().typeCheck(); fail(); }
- catch( Exception e ) { assertEquals("Type 3.14 is not of declared type i64",e.getMessage()); }
+ code.parse().opto().typeCheck();
+ assertEquals("3",Eval2.eval(code,0));
}
@Test
diff --git a/chapter23/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java b/chapter23/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
index 009beb0b..23d0ef37 100644
--- a/chapter23/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
+++ b/chapter23/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
@@ -7,6 +7,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/chapter23/src/test/java/com/seaofnodes/simple/Chapter17bTest.java b/chapter23/src/test/java/com/seaofnodes/simple/Chapter17bTest.java
index 813fc4c2..59bd3b0c 100644
--- a/chapter23/src/test/java/com/seaofnodes/simple/Chapter17bTest.java
+++ b/chapter23/src/test/java/com/seaofnodes/simple/Chapter17bTest.java
@@ -123,8 +123,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().typeCheck(); fail(); }
- catch( Exception e ) { assertEquals("Type 2.5f is not of declared type i64",e.getMessage()); }
+ code.parse().opto().typeCheck();
+ assertEquals("2",Eval2.eval(code,0));
}
diff --git a/chapter23/src/test/java/com/seaofnodes/simple/Eval2.java b/chapter23/src/test/java/com/seaofnodes/simple/Eval2.java
index feb67ba9..908cc2e0 100644
--- a/chapter23/src/test/java/com/seaofnodes/simple/Eval2.java
+++ b/chapter23/src/test/java/com/seaofnodes/simple/Eval2.java
@@ -283,6 +283,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/chapter23/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter23/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
index 1c116162..20aac367 100644
--- a/chapter23/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
+++ b/chapter23/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
@@ -298,6 +298,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);