aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/test_helpers.sh
blob: 9169c241299dfa31993c8ee7d43dc5e09c4e82c5 (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
function test_exit_code() {
    set +e
    cat > main.c

    "$ducc" main.c > main.s
    if [[ $? -ne 0 ]]; then
        set -e
        exit 1
    fi
    gcc -o a.out main.s
    ./a.out
    exit_code=$?

    expected=$1

    if [[ $exit_code -ne $expected ]]; then
        echo "invalid exit code: expected $expected, but got $exit_code" >&2
        set -e
        exit 1
    fi
    set -e
}

function test_diff() {
    cat > main.c

    "$ducc" main.c > main.s
    if [[ $? -ne 0 ]]; then
        exit 1
    fi
    gcc -o a.out main.s
    if [[ ! -f input ]]; then
        touch input
    fi
    ./a.out "$@" < input > output
    exit_code=$?

    if [[ $exit_code -ne 0 ]]; then
        echo "invalid exit code: $exit_code" >&2
        exit 1
    fi

    diff -u expected output
}

function test_compile_error() {
    set +e
    cat > main.c

    "$ducc" main.c > /dev/null 2> output
    if [[ $? -eq 0 ]]; then
        "expected to fail"
        set -e
        exit 1
    fi

    diff -u expected output
    set -e
}

function test_example() {
    filename="../../../examples/$1.c"

    "$ducc" "$filename" > main.s
    if [[ $? -ne 0 ]]; then
        exit 1
    fi
    gcc -o a.out main.s
    if [[ ! -f input ]]; then
        touch input
    fi
    ./a.out "$@" < input > output
    exit_code=$?

    if [[ $exit_code -ne 0 ]]; then
        echo "invalid exit code: $exit_code" >&2
        exit 1
    fi

    diff -u expected output
}