#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# (C) 2022 Domenico Andreoli
# Author: Domenico Andreoli <cavok@debian.org>

readonly SOME_BINARY=/usr/bin/pahole

failures=0

run_test() {
    local test_name=$1
    shift

    local output_file="${AUTOPKGTEST_ARTIFACTS}/${test_name}.log"

    echo "Running: ${test_name}"

    if "$@" >"${output_file}" 2>&1; then
        echo "PASS: ${test_name}"
    else
        local exit_code=$?
        echo "FAIL: ${test_name} (exit code: ${exit_code})"
        failures=$((failures + 1))
    fi

}

# smoke tested in btf-struct-with-hole
# btfdiff

run_test "codiff" codiff $SOME_BINARY $SOME_BINARY

# not trivial to test (see README.ctracer)
# ctracer

run_test "dtagnames" dtagnames $SOME_BINARY

# smoke tested in btf-struct-with-hole
# fullcircle

# tested in btf-struct-with-hole
# pahole

run_test "fullcircle" fullcircle $SOME_BINARY
run_test "pdwtags" pdwtags $SOME_BINARY
run_test "pfunct" pfunct $SOME_BINARY
run_test "pglobal" pglobal $SOME_BINARY
run_test "prefcnt" prefcnt $SOME_BINARY
run_test "scncopy" scncopy -s data -o output $SOME_BINARY
run_test "syscse" syscse $SOME_BINARY

if (( failures > 0 )); then
    echo "${failures} test(s) failed."
    exit 1
fi

echo "All tests passed."
exit 0
