aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/expressions.sh
blob: bb0743b8068ec1df8206f86c71e5c01832819781 (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
# logical operators: short-circuit evaluation
cat <<'EOF' > expected
foo
EOF
test_diff <<'EOF'
int printf();

int foo() {
    printf("foo\n");
    return 0;
}

int bar() {
    printf("bar\n");
    return 1;
}

int main() {
    if (foo() && bar()) {
        printf("baz\n");
    }

    return 0;
}
EOF

cat <<'EOF' > expected
foo
bar
baz
EOF
test_diff <<'EOF'
int printf();

int foo() {
    printf("foo\n");
    return 0;
}

int bar() {
    printf("bar\n");
    return 1;
}

int main() {
    if (foo() || bar()) {
        printf("baz\n");
    }

    return 0;
}
EOF

# cast with typedef
cat <<'EOF' > expected
Result: -42
Result: 0
EOF
test_diff <<'EOF'
int printf(const char*, ...);

typedef int foo;

int main() {
    int a = 42;
    int b = -(int)a;
    int c = !(foo)a;
    printf("Result: %d\n", b);
    printf("Result: %d\n", c);
}
EOF

# comma operator in for loop
cat <<'EOF' > expected
0 0
1 1
2 2
3 3
4 4
EOF

test_diff <<'EOF'
int printf();

int main() {
    int i = 1000;
    int j = 1000;
    for (i = 0, j = 0; i < 5; i++, j++) {
        printf("%d %d\n", i, j);
    }
}
EOF

# void cast
cat <<'EOF' > expected
42
EOF
test_diff <<'EOF'
int printf(const char*, ...);

int f() {
    return printf("42\n");
}

int main() {
    (void)123;
    (void)(5 + 6 + 7);
    (void)f();
}
EOF