summaryrefslogtreecommitdiff
path: root/chapter31/src/test/java/com/seaofnodes/simple/ArrayInitTest.java
blob: d298828e1c154ec5e3d725cc4ea081909d7cb69a (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.seaofnodes.simple;

import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.ArrayInitNode;
import com.seaofnodes.simple.node.ConstantNode;
import com.seaofnodes.simple.node.Node;
import com.seaofnodes.simple.type.*;
import com.seaofnodes.simple.util.Ary;
import java.util.ArrayList;
import org.junit.Test;
import static org.junit.Assert.*;

public class ArrayInitTest {
    @Test public void constantListComputeIsMonotone() {
        CodeGen code = new CodeGen("return 0;").parse();
        TypeStruct layout = TypeStruct.makeAry("[]i32",TypeInteger.U32,2,TypeInteger.I32,3,true);
        Type array = TypeMemPtr.make(layout);
        Node ctrl = ConstantNode.seed(Type.CONTROL);
        Node ptr = ConstantNode.seed(Type.TOP);
        Node len = ConstantNode.seed(Type.TOP);
        Node x = ConstantNode.seed(Type.TOP), y = ConstantNode.seed(Type.TOP);
        ArrayInitNode init = new ArrayInitNode(ctrl,code.ZERO,ptr,len,2,null,new Ary<>(new Node[]{x,y}));
        Type[][] inputs = {
            {Type.TOP,TypeMemPtr.TOP,array},
            {Type.TOP,TypeInteger.TOP,TypeInteger.TWO,TypeInteger.U32},
            {Type.TOP,TypeInteger.TOP,TypeInteger.constant(5),TypeInteger.BOT},
            {Type.TOP,TypeInteger.TOP,TypeInteger.constant(6),TypeInteger.BOT}
        };
        var states = new ArrayList<Type[]>();
        for( Type p : inputs[0] )
            for( Type l : inputs[1] )
                for( Type a : inputs[2] )
                    for( Type b : inputs[3] ) {
                        ptr._type = p; len._type = l; x._type = a; y._type = b;
                        states.add(new Type[]{p,l,a,b,init.compute()});
                    }
        for( Type[] before : states )
            for( Type[] after : states )
                if( before[0].isa(after[0]) && before[1].isa(after[1]) &&
                    before[2].isa(after[2]) && before[3].isa(after[3]) )
                    assertTrue("Array initializer rose from "+before[4]+" to "+after[4],before[4].isa(after[4]));
    }

    private static void value(String src, long expected) {
        CodeGen code=new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
        assertEquals(Long.toString(expected),Eval2.eval(code,0,100000));
    }

    private static void bad(String src, String message) {
        try {
            new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
            fail("Expected: "+message);
        } catch( com.seaofnodes.simple.parser.Parser.ParseException e ) {
            assertTrue(e.getMessage(),e.getMessage().contains(message));
        }
    }

    @Test public void lists() {
        value("val a=new u16[3]{025755,1,65538}; return a#*100000+a[0]+a[1]+a[2];",311248);
        value("val a=new int[]{4,7,9,}; a[1]=2; return a#+a[0]+a[1]+a[2];",18);
        value("val a=new int[]{}; return a#;",0);
        bad("val a=new int[2]{1}; return a[0];","initializer has 1 elements");
    }

    @Test public void generators() {
        value("int n=6; val a=new int[n]{int i -> i*i;}; return a[5]+a#;",31);
        value("int base=7; val a=new int[3]{int i -> return base+i;}; return a[2];",9);
        value("val a=new int[3]{int i -> if(i==1) return 9; i+1;}; return a[0]+a[1]+a[2];",13);
        value("val a=new int[0]{int i -> 1/i;}; return a#;",0);
        bad("val a=new int[]{int i -> i;}; return a#;","requires a length");
        bad("val f={-> int x=1; val a=new int[2]{int i -> x++;}; return a[0];}; return f();","captured local");
    }

    @Test public void referencesAndReadonly() {
        value("struct S { int x; new S={i64 v -> x=v;}; }; val a=new S[~3]{int i -> new S(i+4);}; return a[2].x;",6);
        value("val a=new u8[~]{1,2,3}; return a[1];",2);
        value("val names=new u8[~][~2]{\"Alice\",\"Bob\"}; return names[1][0];",66);
        value("val a=new int[ ~ ]{4,5}; return a[1];",5);
        value("val a=new {i64 -> i64}[]{ {i64 x -> x+1;}, {i64 x -> x*2;} }; return a[1](7);",14);
        value("struct S { int x=1; }; val a=new !S[1]{new S}; a[0].x=8; return a[0].x;",8);
        bad("struct S { int x=1; }; val a=new !S[~1]{new S}; a[0].x=8; return a[0].x;","final field");
        value("struct S { int x; }; val a=new S?[~2]{u32 i -> null;}; return a[1]==null;",1);
        bad("val a=new int[~2]{1,2}; a[0]=3; return a[0];","final field");
        bad("struct S { int x; }; val a=new S[1]{null}; return a#;","declared type");
    }

    @Test public void nativeInitializers() throws Exception {
        TestC.runSF("""
            val main = { ->
                int n=8;
                val a=new u16[~n]{int i -> i*i+1;};
                val b=new u8[]{257,2,3};
                return a[7]==50 && b[0]==1 && a#==8 ? 0 : 1;
            };
            ""","arrayInitializers","",-1);
    }

    @Test public void nestedBodies() {
        value("""
            val f={ ->
                int offset=5;
                val a=new int[4]{int i ->
                    int sum=offset;
                    for(int j=0;j<i;j++) { if(j==1) continue; sum+=j; }
                    if(i==2) return 20;
                    return sum;
                };
                return a[0]+a[1]+a[2]+a[3];
            };
            return f();
            """,37);
        value("""
            val a=new int[][3]{int i ->
                new int[2]{int j -> i*10+j;};
            };
            return a[2][1]+a[0][0];
            """,21);
        value("""
            val a=new int[2]{int i ->
                val twice={i64 x -> return x*2;};
                twice(i);
            };
            return a[1];
            """,2);
    }

    @Test public void orderAndIdentity() throws Exception {
        TestC.runSF("""
            struct Counter {
                int calls=0;
                val next={->calls++;};
            };
            struct Item {
                int x;
                new Item={i64 v -> x=v;};
            };
            val build_noInline={u32 n, !Counter c ->
                new Item[~n]{u32 i -> new Item(c.next());};
            };
            val main={->
                val c=new Counter;
                val empty=build_noInline(0,c);
                if(empty#!=0 || c.calls!=0) return 1;
                val a=build_noInline(4,c);
                if(c.calls!=4 || a[0].x!=0 || a[3].x!=3 || a[0]==a[1]) return 2;
                val b=new int[]{c.next(),c.next(),c.next()};
                if(b[0]!=4 || b[2]!=6) return 3;
                val d=new int[c.next()]{i64 i -> c.next();};
                if(d#!=7 || d[0]!=8 || d[6]!=14 || c.calls!=15) return 4;
                return 0;
            };
            ""","arrayInitOrder","",-1);
    }

    @Test public void diagnostics() {
        bad("val f={u32 n -> new int[n]{1,2};}; return f(arg)#;","length must equal that constant");
        bad("val a=new int[2]{u8 i -> i;}; return a[0];","generator index");
        bad("val a=new int[2]{int return -> 0;}; return a[0];","index name");
        bad("val a=new int[2]{int i -> i++;}; return a[0];","final 'i'");
        bad("val f={->int x=0; return new int[2]{int i -> x=i;};}; return f()#;","captured local");
        bad("val a=new int[2]{int i -> break;}; return a[0];","No active loop");
        bad("val a=new int[-1]{}; return a#;","outside the u32 range");
        bad("val a=new int[1.5]{int i -> i;}; return a#;","integer");
        bad("val a=new int[~]{1,2}; val b=a; b[0]++; return b[0];","final field");
        for( String init : new String[]{"self","int i -> self;"} )
            bad("struct C { int x; new C={-> val a=new C[1]{"+init+"}; x=a#;}; }; return new C().x;",
                "fully initialized elements");
    }

    @Test public void forwardReferencesAndLoopScopes() {
        value("""
            val f={ ->
                int sum=0;
                for(int k=0;k<3;k++) {
                    val a=new int[2]{int i ->
                        if(i) return later(i);
                        later(3);
                    };
                    if(k==1) continue;
                    sum+=a[0]+a[1];
                }
                return sum;
            };
            val later={i64 i -> i*2;};
            return f();
            """,16);
    }

    @Test public void optimizationOrder() {
        String src="""
            struct Box { int x; new Box={i64 v -> x=v;}; };
            val f={u32 n ->
                int base=7;
                val a=new Box[~n]{u32 i ->
                    int x=base;
                    for(int j=0;j<i;j++) x+=j;
                    if(i==2) return new Box(20);
                    new Box(x);
                };
                return n==0 ? 0 : a[n-1].x;
            };
            return f(arg&7);
            """;
        for( long seed : new long[]{1,123,987654321} ) {
            CodeGen code=new CodeGen(src,seed,true).driver(CodeGen.Phase.TypeCheck);
            assertEquals("0",Eval2.eval(code,0));
            assertEquals("20",Eval2.eval(code,3));
            assertEquals("13",Eval2.eval(code,5));
        }
    }
}