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

import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SchedulingScratchTest {
    @Test public void testScratchAcrossCompilations() {
        String src = """
            struct Box { int x; };
            Box b = new Box;
            b.x = arg;
            int sum = 0;
            int i = 0;
            while( i<5 ) {
                int old = b.x;
                b.x = 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++ ) {
            StopNode stop = new Parser(src).parse().iterate();
            assertEquals(6L,Evaluator.evaluate(stop,0));
            assertEquals(7L,Evaluator.evaluate(stop,1));
            assertEquals(8L,Evaluator.evaluate(stop,2));
        }
    }
}