1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package com.seaofnodes.simple;
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
import org.junit.Ignore;
import org.junit.Test;
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.
assertEquals("0.0", Evaluator.evaluate(stop,0).toString());
assertEquals("-1.0", Evaluator.evaluate(stop,1).toString());
}
@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 stop = new Parser(src).parse().iterate();
assertEquals(src,test[1],com.seaofnodes.simple.evaluator.Evaluator.evaluate(stop,0).toString());
}
}
@Ignore
@Test
public void testJig() {
Parser parser = new Parser(
"""
return 3.14;
""");
StopNode stop = parser.parse().iterate();
assertEquals("return 3.14;", stop.toString());
assertEquals(3.14, Evaluator.evaluate(stop, 0));
}
@Test
public void testFloat() {
Parser parser = new Parser(
"""
return 3.14;
""");
StopNode stop = parser.parse().iterate();
assertEquals("return 3.14;", stop.toString());
assertEquals(3.14, Evaluator.evaluate(stop, 0));
}
@Test
public void testSquareRoot() {
Parser parser = new Parser(
"""
flt guess = arg;
while( 1 ) {
flt next = (arg/guess + guess)/2;
if( next == guess ) break;
guess = next;
}
return guess;
""");
StopNode stop = parser.parse().iterate();
assertEquals("return Phi(Loop,(flt)arg,(((ToFloat/Phi_guess)+Phi_guess)/2.0));", stop.toString());
assertEquals(3.0, Evaluator.evaluate(stop, 9));
assertEquals(1.414213562373095, Evaluator.evaluate(stop, 2));
}
@Test
public void testFPOps() {
Parser parser = new Parser(
"""
flt x = arg;
return x+1==x;
""");
StopNode stop = parser.parse().iterate();
assertEquals("return ((flt)arg==(ToFloat+1.0));", stop.toString());
assertEquals(0L, Evaluator.evaluate(stop, 1));
}
}
|