aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/int_comparison_elision.zig
blob: 32c670fff425f0396ab76be7aac219c9fb82c71b (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
const std = @import("std");
const minInt = std.math.minInt;
const maxInt = std.math.maxInt;
const builtin = @import("builtin");

test "int comparison elision" {
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    testIntEdges(u0);
    testIntEdges(i0);
    testIntEdges(u1);
    testIntEdges(i1);
    testIntEdges(u4);
    testIntEdges(i4);

    // TODO: support int types > 128 bits wide in other backends
    if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO

    // TODO: panic: integer overflow with int types > 65528 bits wide
    // TODO: LLVM generates too many parameters for wasmtime when splitting up int > 64000 bits wide
    testIntEdges(u64000);
    testIntEdges(i64000);
}

// All comparisons in this test have a guaranteed result,
// so one branch of each 'if' should never be analyzed.
fn testIntEdges(comptime T: type) void {
    const min = minInt(T);
    const max = maxInt(T);

    var runtime_val: T = undefined;

    if (min > runtime_val) @compileError("analyzed impossible branch");
    if (min <= runtime_val) {} else @compileError("analyzed impossible branch");
    if (runtime_val < min) @compileError("analyzed impossible branch");
    if (runtime_val >= min) {} else @compileError("analyzed impossible branch");

    if (min - 1 > runtime_val) @compileError("analyzed impossible branch");
    if (min - 1 >= runtime_val) @compileError("analyzed impossible branch");
    if (min - 1 < runtime_val) {} else @compileError("analyzed impossible branch");
    if (min - 1 <= runtime_val) {} else @compileError("analyzed impossible branch");
    if (min - 1 == runtime_val) @compileError("analyzed impossible branch");
    if (min - 1 != runtime_val) {} else @compileError("analyzed impossible branch");
    if (runtime_val < min - 1) @compileError("analyzed impossible branch");
    if (runtime_val <= min - 1) @compileError("analyzed impossible branch");
    if (runtime_val > min - 1) {} else @compileError("analyzed impossible branch");
    if (runtime_val >= min - 1) {} else @compileError("analyzed impossible branch");
    if (runtime_val == min - 1) @compileError("analyzed impossible branch");
    if (runtime_val != min - 1) {} else @compileError("analyzed impossible branch");

    if (max >= runtime_val) {} else @compileError("analyzed impossible branch");
    if (max < runtime_val) @compileError("analyzed impossible branch");
    if (runtime_val <= max) {} else @compileError("analyzed impossible branch");
    if (runtime_val > max) @compileError("analyzed impossible branch");

    if (max + 1 > runtime_val) {} else @compileError("analyzed impossible branch");
    if (max + 1 >= runtime_val) {} else @compileError("analyzed impossible branch");
    if (max + 1 < runtime_val) @compileError("analyzed impossible branch");
    if (max + 1 <= runtime_val) @compileError("analyzed impossible branch");
    if (max + 1 == runtime_val) @compileError("analyzed impossible branch");
    if (max + 1 != runtime_val) {} else @compileError("analyzed impossible branch");
    if (runtime_val < max + 1) {} else @compileError("analyzed impossible branch");
    if (runtime_val <= max + 1) {} else @compileError("analyzed impossible branch");
    if (runtime_val > max + 1) @compileError("analyzed impossible branch");
    if (runtime_val >= max + 1) @compileError("analyzed impossible branch");
    if (runtime_val == max + 1) @compileError("analyzed impossible branch");
    if (runtime_val != max + 1) {} else @compileError("analyzed impossible branch");

    const undef_const: T = undefined;

    if (min > undef_const) @compileError("analyzed impossible branch");
    if (min <= undef_const) {} else @compileError("analyzed impossible branch");
    if (undef_const < min) @compileError("analyzed impossible branch");
    if (undef_const >= min) {} else @compileError("analyzed impossible branch");

    if (min - 1 > undef_const) @compileError("analyzed impossible branch");
    if (min - 1 >= undef_const) @compileError("analyzed impossible branch");
    if (min - 1 < undef_const) {} else @compileError("analyzed impossible branch");
    if (min - 1 <= undef_const) {} else @compileError("analyzed impossible branch");
    if (min - 1 == undef_const) @compileError("analyzed impossible branch");
    if (min - 1 != undef_const) {} else @compileError("analyzed impossible branch");
    if (undef_const < min - 1) @compileError("analyzed impossible branch");
    if (undef_const <= min - 1) @compileError("analyzed impossible branch");
    if (undef_const > min - 1) {} else @compileError("analyzed impossible branch");
    if (undef_const >= min - 1) {} else @compileError("analyzed impossible branch");
    if (undef_const == min - 1) @compileError("analyzed impossible branch");
    if (undef_const != min - 1) {} else @compileError("analyzed impossible branch");

    if (max >= undef_const) {} else @compileError("analyzed impossible branch");
    if (max < undef_const) @compileError("analyzed impossible branch");
    if (undef_const <= max) {} else @compileError("analyzed impossible branch");
    if (undef_const > max) @compileError("analyzed impossible branch");

    if (max + 1 > undef_const) {} else @compileError("analyzed impossible branch");
    if (max + 1 >= undef_const) {} else @compileError("analyzed impossible branch");
    if (max + 1 < undef_const) @compileError("analyzed impossible branch");
    if (max + 1 <= undef_const) @compileError("analyzed impossible branch");
    if (max + 1 == undef_const) @compileError("analyzed impossible branch");
    if (max + 1 != undef_const) {} else @compileError("analyzed impossible branch");
    if (undef_const < max + 1) {} else @compileError("analyzed impossible branch");
    if (undef_const <= max + 1) {} else @compileError("analyzed impossible branch");
    if (undef_const > max + 1) @compileError("analyzed impossible branch");
    if (undef_const >= max + 1) @compileError("analyzed impossible branch");
    if (undef_const == max + 1) @compileError("analyzed impossible branch");
    if (undef_const != max + 1) {} else @compileError("analyzed impossible branch");
}