summaryrefslogtreecommitdiff
path: root/chapter15/src/test/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'chapter15/src/test/java/com')
-rw-r--r--chapter15/src/test/java/com/seaofnodes/simple/Chapter12Test.java3
-rw-r--r--chapter15/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java19
-rw-r--r--chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java2
3 files changed, 22 insertions, 2 deletions
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index e885deb3..813dd757 100644
--- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter15/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -126,8 +126,7 @@ N n = new N;
n.i = 3.14;
return n.i;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot store 3.14 into field int i",e.getMessage()); }
+ assertEquals("3",com.seaofnodes.simple.evaluator.Evaluator.evaluate(parser.parse().iterate(),0).toString());
}
@Test
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
index f4323fe0..fd71ad5f 100644
--- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
+++ b/chapter15/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 stop = new Parser(test[0]).parse().iterate();
+ assertEquals(test[0],test[1],Evaluator.evaluate(stop,17).toString());
+ assertEquals(test[0],test[2],Evaluator.evaluate(stop,-17).toString());
+ }
+ }
+
+
@Test public void testSubZeroFloat() {
var stop = new Parser("flt x = arg; return 0-x;").parse().iterate();
// Compare strings so +0.0 and -0.0 remain distinct.
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
index 9eed23de..485f73c5 100644
--- a/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
+++ b/chapter15/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
@@ -305,6 +305,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);