#!/bin/bash
###############################################################################
# @用例ID: 20230525-154056-556766015
# @用例名称: glibc-test
# @用例级别: 3
# @用例标签:
# @用例类型: 功能
###############################################################################
[ -z "$TST_TS_TOPDIR" ] && {
    TST_TS_TOPDIR="$(realpath "$(dirname "$0")/..")"
    export TST_TS_TOPDIR
}
source "${TST_TS_TOPDIR}/tst_common/lib/common.sh" || exit 1
###############################################################################

g_tmpdir="$(mktemp -d)"

tc_setup() {
    msg "this is tc_setup"
    # @预置条件:glibc已提前安装

    assert_true command -v gcc
    return 0
}

do_test() {
    msg "this is do_test"

    # @测试步骤:1: 编写测试程序文件
    cat >test.c <<EOF
#include <stdio.h>
#include <stdlib.h>

int main() {
    // 使用 printf 打印 Hello, World!
    printf("Hello, World!\\n");

    // 使用 malloc 分配内存
    int* arr = malloc(5 * sizeof(int));
    if (arr == NULL) {
        printf("Failed to allocate memory\\n");
        return 1;
    }

    // 释放内存
    free(arr);

    return 0;
}
EOF

    # @测试步骤:2: 编译测试程序
    assert_true gcc -o test test.c

    # @测试步骤:3: 执行测试程序
    # @预期结果:3: 执行成功
    assert_true ./test

    return 0
}

tc_teardown() {
    msg "this is tc_teardown"

    # 删除 test.c 文件
    rm -f test.c

    # 删除 test 文件
    rm -f test

    rm -rfv "$g_tmpdir" || return 1
    return 0
}

###############################################################################
tst_main "$@"
###############################################################################