summaryrefslogtreecommitdiff
path: root/chapter25/src/test/java/com/seaofnodes/simple/Chapter16Test.java
blob: 91e5cd381dd9b2eea21c8bed15dc7d821a62815f (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.seaofnodes.simple;

import com.seaofnodes.simple.codegen.CodeGen;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class Chapter16Test {

    @Ignore
    @Test
    public void testJig() {
        CodeGen code = new CodeGen(
"""
return 3.14;
""");
        code.parse().opto();
        assertEquals("return 3.14;", code.print());
        assertEquals("3.14", Eval2.eval(code,  0));
    }


    @Test
    public void testMulti0() {
        CodeGen code = new CodeGen(
"""
int x, y;
return x+y;
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
        assertEquals("0", Eval2.eval(code,  0));
    }
    @Test
    public void testMulti1() {
        CodeGen code = new CodeGen(
"""
int x=2, y=x+1;
return x+y;
""");
        code.parse().opto();
        assertEquals("return 5;", code.print());
        assertEquals("5", Eval2.eval(code,  0));
    }


    @Test
    public void testConstruct0() {
        CodeGen code = new CodeGen("""
struct _X { int x=3; };
_X z = new _X;
return z.x;
""");
        code.parse().opto();
        assertEquals("return 3;", code.print());
        assertEquals("3", Eval2.eval(code,  0));
    }

    @Test
    public void testConstruct2() {
        CodeGen code = new CodeGen("""
struct _X { int x=3; new _X = { int xx -> x=xx; }; };
_X z = new _X(4);
return z.x;
""");
        code.parse().opto();
        assertEquals("return 4;", code.print());
        assertEquals("4", Eval2.eval(code,  0));
    }


    @Test
    public void testLinkedList1() {
        CodeGen code = new CodeGen(
"""
struct _LLI { !_LLI? !next; int i; new _LLI = { !_LLI? !n, int ii -> next=n; i=ii; }; };
!_LLI? !head = null;
while( arg ) {
    head = new _LLI(head,arg);
    arg = arg-1;
}
if( !head ) return 0;
_LLI? next = head.next;
if( !next ) return 1;
return next.i;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,1,.i,0);", code.print());
        assertEquals("0", Eval2.eval(code,  0));
        assertEquals("1", Eval2.eval(code,  1));
        assertEquals("2", Eval2.eval(code,  3));
    }

    @Test
    public void testLinkedList2() {
        CodeGen code = new CodeGen(
"""
struct _LLI { !_LLI? !next; int i; new _LLI = { !_LLI? !n, int a ->
    next=n;
    int !tmp=a;
    while( a > 10 ) {
        tmp = tmp + a;
        a = a - 1;
    }
    i=tmp;
}; };
!_LLI? !head = null;
while( arg ) {
    head = new _LLI(head,arg);
    arg = arg-1;
}
if( !head ) return 0;
_LLI? next = head.next;
if( !next ) return 1;
return next.i;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,1,.i,0);", code.print());
        assertEquals("0", Eval2.eval(code,  0));
        assertEquals("1", Eval2.eval(code,  1));
        assertEquals("2", Eval2.eval(code, 11));
    }

    @Test
    public void testSquare() {
        CodeGen code = new CodeGen(
"""
struct _Square {
    flt !side = arg;
    // Newtons approximation to the square root, computed in a constructor.
    // The actual allocation will copy in this result as the initial
    // value for 'diag'.
    flt !diag = arg*arg/2;
    while( 1 ) {
        flt next = (side/diag + diag)/2;
        if( next == diag ) break;
        diag = next;
    }
};
return new _Square;
""");
        code.parse().opto();
        assertEquals("return Test._Square;", code.print());
        assertEquals("Test._Square{side=3.0,diag=1.7320508075688772}", Eval2.eval(code,  3));
        assertEquals("Test._Square{side=4.0,diag=2.0}", Eval2.eval(code, 4));
    }
}