summaryrefslogtreecommitdiff
path: root/chapter18/src/test/java/com/seaofnodes/simple/SchedulingScratchTest.java
blob: 3927d517f9798ac8fcd612e2963ed5371c0a7d43 (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
package com.seaofnodes.simple;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SchedulingScratchTest {
    @Test public void testScratchAcrossCompilations() {
        String src = """
            int[] !xs = new int[2];
            xs[0] = arg;
            int sum = 0;
            int i = 0;
            while( i<5 ) {
                int old = xs[arg&1];
                xs[0] = i;
                sum = sum + old;
                i = i + 1;
            }
            return sum;
            """;
        // Each fresh compilation reuses node IDs and the scheduler's scratch.
        for( int round=0; round<3; round++ ) {
            CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
            assertEquals("6",Eval2.eval(code,0));
            assertEquals("0",Eval2.eval(code,1));
            assertEquals("8",Eval2.eval(code,2));
        }
    }
}