aboutsummaryrefslogtreecommitdiffhomepage
path: root/testexec/mainapp/definedness.slul
blob: 8056ec94b825be686b45412bf35155a0a065f1a0 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

#
# Test of tracking of variable definedness
#
# Copyright © 2022-2024 Samuel Lidén Borell <samuel@kodafritt.se>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

func test_definedness_1(bool choice) -> bool
{
    bool b = true
    return b
}

func test_definedness_2(bool choice) -> bool
{
    var bool b
    b = true
    return b
}

func test_definedness_3(bool choice) -> bool
{
    var bool b
    if choice {
        b = true
    } else {
        b = false
    }
    return b
}

func test_definedness_4(bool choice) -> bool
{
    var bool b
    while true {
        b = false
        break
    }
    if choice {
        b = true
    } else {
        b = false
    }
    return b
}

func test_definedness_if1(bool choice, bool c2, bool c3) -> bool
{
    var bool b
    if choice {
        b = true
    } else if not c2 {
        b = c3
    } else {
        b = false
    }
    return b
}

func test_definedness_if2(bool choice, bool c2, bool c3) -> bool
{
    var bool b
    if choice {
        return false
    } else if not c2 {
        b = c3
    } else {
        b = false
    }
    return b
}

func test_definedness_if3(bool choice, bool c2, bool c3) -> bool
{
    var bool b
    if choice {
        b = true
    } else if not c2 {
        return false
    } else {
        b = false
    }
    return b
}

func test_definedness_if4(bool choice, bool c2, bool c3) -> bool
{
    var bool b
    if choice {
        b = true
    } else if not c2 {
        b = c3
    } else {
        return false
    }
    return b
}

func test_definedness_if5(bool choice, int x) -> bool
{
    var bool b
    if choice {
        b = true
    } else if x < 0 {
        if x == -3 {
            return false
        } else {
            b = (x == -1)
        }
    } else if x >= 10 {
        b = false
        if x == 11 { b = true }
    } else {
        int y = x+1
        if y == 3 {
            b = true
        } else {
            return false
        }
    }
    return b
}

func test_definedness_if6(int choice, int x) -> int
{
    var int i
    if choice < 4 {
        if choice == 1 {
            if x == 0 {
                i = 123
            } else if x < 0 {
                i = -x
            } else {
                return -1
            }
        } else if choice == 2 {
            return -2
        } else {
            i = 789
        }
    } else {
        i = choice
    }
    return i
}

func test_definedness_block(int choice, int x) -> int
{
    var int i
    {
        {
            if x == 1 return 11
            else if x == 2 { i = 2 }
            else return 0
        }
    }
    return i
}

func test_definedness_goto1(int num, bool flag) -> int
{
    var int a
    var int b
    if num == 1 {
        a = 1
        goto other
    } else if num == 2 {
        a = 2
      other:
        b = 20
    } else {
        a = 3
        b = 30
    }
    if flag return b
    return a
}

func test_definedness_goto2(int num, bool flag) -> int
{
    var int a
    var int b
    var int c
    if num == 1 {
        a = 1
        b = 1
      first:
        c = 10
    } else if num == 2 {
        a = 2
      other:
        b = 20
        goto first
    } else if num == 3 {
        a = 3
        goto other
    } else {
        a = 3
        b = 30
        goto first
    }
    if flag return b
    return a
}

func test_definedness_switch1(int choice) -> int
{
    var int i
    switch choice {
    case 1:
        i = 123
    case 2:
        i = 456
    default:
        i = 789
    }
    return i
}

func test_definedness_switch2(int choice, int x) -> int
{
    var int i
    if choice < 4 {
        switch choice {
        case 1:
            if x == 0 {
                i = 111
            } else if x < 0 {
                i = -x
            } else {
                i = 222
            }
        case 2:
            i = 333
        default:
            i = 444
        }
    } else {
        i = choice
    }
    return i
}

func test_definedness_switch3(int choice, int x) -> int
{
    var int i
    if choice < 4 {
        switch choice {
        case 1:
            if x == 0 {
                i = 123
            } else if x < 0 {
                i = -x
            } else {
                return -1
            }
        case 2:
            return -2
        default:
            i = 789
        }
    } else {
        i = choice
    }
    return i
}

func test_definedness_switch4(int choice, int x) -> int
{
    var int i
    if choice < 4 {
        switch choice {
        case 1:
            switch x {
            case 10:
                i = 123
            default:
                return -1
            }
        case 2:
            return -2
        default:
            i = 789
        }
    } else {
        i = choice
    }
    return i
}

func test_definedness_switch5(int choice, int x) -> int
{
    var int i
    if choice < 4 {
        switch choice {
        case 1:
            switch x {
            case 10:
                i = 123
            default:
                return -1
            }
        case 2:
            return -2
        default:
            i = 789
        }
    } else {
        return -2
    }
    return i
}

func test_definedness_switch6(int x, int y) -> int
{
    # This is simply a test of reference counting, that it does not go below 0
    switch x {
    case 123:
        switch y {
        case 456:
        }
    default:
    }
    return 3
}

func take_nonopt_param(ref ImplType non_opt)
{
}

func test_definedness_nullability(
        ?ref ImplType opt_param,
        ?ref [3]byte opt_arr,
        int i)
{
    var ref ImplType non_opt

    if opt_param != none {
        assert opt_param.x == 456
    }

    if opt_param == none {
        # dummy
    } else {
        assert opt_param.x == 456
        if i == 123 {
            goto not_none
        }
    }

    while opt_param != none {
        assert opt_param.x == 456
        break
    }

    assert opt_param != none
    assert opt_param.x == 456
  not_none:
    assert opt_param.x == 456
    non_opt = opt_param
    take_nonopt_param(opt_param)

    # TODO boolean short-circuiting
    #if opt_arr != none and opt_arr[2] == 123 {}

    if opt_arr != none {
        assert opt_arr[2] == 789
        # FIXME this generates IR with a strange DEREF
        #       (dereferencing and storing the result to a DPTR)
        #assert (deref opt_arr)[2] == 789
    }
}

func test_definedness_nullability_index(?ref usize indptr) -> byte
{
    [3]byte arr = [11,22,33]
    if indptr != none {
        # TODO implement type detection of comparison operations
        # TODO require range check before accessing arrays
        #if deref indptr >= 0 and deref indptr < 3 {
        return arr[deref indptr]
        #}
    }
    return 0
}

func definedness_testfunc(int x) -> int
{
    return x * 111
}

func take_nonopt_funcref(funcref(int x)->int f)
{
    assert f(-1) == -11
}

func test_definedness_nullability_funcref(
        ?funcref(int x)->int opt_f,
        funcref(int x)->int f,
        int i)
        -> int
{
    var int r = 0
    var funcref(int x)->int non_opt

    if opt_f != none {
        assert opt_f(1) == 11
        r += 1
    }

    if opt_f == none {
        # dummy
    } else {
        assert opt_f(2) == 22
        r += 2
        if i == 123 {
            r += 4
            goto not_none
        }
    }

    while opt_f != none {
        r += 8
        assert opt_f(3) == 33
        break
    }

    assert opt_f != none
    assert opt_f(4) == 44
    r += 16
  not_none:
    assert opt_f(5) == 55
    non_opt = opt_f
    take_nonopt_funcref(opt_f)
    non_opt = f
    r += 32
    return opt_f(99)
}

func definedness_test()
{
    assert test_definedness_1(false)
    assert test_definedness_1(true)
    assert test_definedness_2(false)
    assert test_definedness_2(true)
    assert not test_definedness_3(false)
    assert test_definedness_3(true)
    assert not test_definedness_4(false)
    assert test_definedness_4(true)
    assert not test_definedness_if1(false, false, false)
    assert test_definedness_if1(false, false, true)
    assert not test_definedness_if1(false, true, true)
    assert test_definedness_if1(true, false, false)
    assert not test_definedness_if2(false, false, false)
    assert test_definedness_if2(false, false, true)
    assert not test_definedness_if2(true, true, true)
    assert not test_definedness_if3(false, false, true)
    assert test_definedness_if3(true, false, false)
    assert test_definedness_if4(false, false, true)
    assert test_definedness_if4(true, false, false)
    assert not test_definedness_if5(false, -3)
    assert not test_definedness_if5(false, -2)
    assert test_definedness_if5(false, -1)
    assert not test_definedness_if5(false, 0)
    assert not test_definedness_if5(false, 1)
    assert test_definedness_if5(false, 2)
    assert not test_definedness_if5(false, 10)
    assert test_definedness_if5(false, 11)
    assert test_definedness_if5(true, 123)
    assert test_definedness_if6(0, -1) == 789
    assert test_definedness_if6(1, -456) == 456
    assert test_definedness_if6(1, 0) == 123
    assert test_definedness_if6(1, 1) == -1
    assert test_definedness_if6(2, 1) == -2
    assert test_definedness_if6(5, 1) == 5
    assert test_definedness_block(0, 1) == 11
    assert test_definedness_block(0, 2) == 2
    assert test_definedness_block(0, 3) == 0
    assert test_definedness_goto1(1, false) == 1
    assert test_definedness_goto1(1, true) == 20
    assert test_definedness_goto1(2, false) == 2
    assert test_definedness_goto1(2, true) == 20
    assert test_definedness_goto1(3, false) == 3
    assert test_definedness_goto1(3, true) == 30
    assert test_definedness_goto2(1, false) == 1
    assert test_definedness_goto2(2, false) == 2
    assert test_definedness_goto2(2, true) == 20
    assert test_definedness_goto2(3, false) == 3
    assert test_definedness_goto2(3, true) == 20
    assert test_definedness_goto2(4, true) == 30
    assert test_definedness_switch1(1) == 123
    assert test_definedness_switch1(2) == 456
    assert test_definedness_switch1(-1) == 789
    assert test_definedness_switch2(1, -5) == 5
    assert test_definedness_switch2(1, 0) == 111
    assert test_definedness_switch2(1, 1) == 222
    assert test_definedness_switch2(2, 0) == 333
    assert test_definedness_switch2(3, 0) == 444
    assert test_definedness_switch2(4, -234) == 4
    assert test_definedness_switch3(0, 0) == 789
    assert test_definedness_switch3(1, -4) == 4
    assert test_definedness_switch3(1, 0) == 123
    assert test_definedness_switch3(1, 1) == -1
    assert test_definedness_switch3(2, 1) == -2
    assert test_definedness_switch3(3, 1) == 789
    assert test_definedness_switch3(4, 321) == 4
    assert test_definedness_switch4(0, 2) == 789
    assert test_definedness_switch4(1, 10) == 123
    assert test_definedness_switch4(1, 11) == -1
    assert test_definedness_switch4(5, 11) == 5
    assert test_definedness_switch5(1, 10) == 123
    assert test_definedness_switch5(1, 11) == -1
    assert test_definedness_switch5(2, 11) == -2
    assert test_definedness_switch5(3, 11) == 789
    assert test_definedness_switch5(11, 0) == -2
    assert test_definedness_switch6(0, 0) == 3
    assert test_definedness_switch6(123, 0) == 3
    assert test_definedness_switch6(123, 456) == 3
    # FIXME this test always freezes or fails
    #assert test_definedness_nullability(none, none, 1)
    assert test_definedness_nullability_index(none) == 0
    var usize ind = 1
    assert test_definedness_nullability_index(refto ind) == 22
    ind = 2
    assert test_definedness_nullability_index(refto ind) == 33
    # TODO should the syntax use "refto" for functions?
    funcref (int x)->int r = definedness_testfunc
    assert test_definedness_nullability_funcref(
            none,
            definedness_testfunc,
            123
            ) == 99
    assert test_definedness_nullability_funcref(
            definedness_testfunc,
            definedness_testfunc,
            123
            ) == 99
    test_definedness_nullability_funcref(
            none,
            definedness_testfunc,
            123
            )
}