/*
* Automated Testing Framework (atf)
*
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* ---------------------------------------------------------------------
* Test cases for the ATF_{CHECK,REQUIRE}_ERRNO macros.
* --------------------------------------------------------------------- */
static int
errno_fail_stub(const int raised_errno)
{
errno = raised_errno;
return -1;
}
ATF_REQUIRE(unlink("before") != -1);
if (t->ok)
ATF_REQUIRE(unlink("after") != -1);
}
}
/* ---------------------------------------------------------------------
* Test cases for the ATF_CHECK and ATF_CHECK_MSG macros.
* --------------------------------------------------------------------- */
H_CHECK(0, 0);
H_CHECK(1, 1);
H_CHECK_MSG(0, 0, "expected a false value");
H_CHECK_MSG(1, 1, "expected a true value");
/* ---------------------------------------------------------------------
* Test cases for the ATF_CHECK_*EQ_ macros.
* --------------------------------------------------------------------- */
H_CHECK_EQ(1_1, 1, 1);
H_CHECK_EQ(1_2, 1, 2);
H_CHECK_EQ(2_1, 2, 1);
H_CHECK_EQ(2_2, 2, 2);
H_CHECK_EQ_MSG(1_1, 1, 1, "1 does not match 1");
H_CHECK_EQ_MSG(1_2, 1, 2, "1 does not match 2");
H_CHECK_EQ_MSG(2_1, 2, 1, "2 does not match 1");
H_CHECK_EQ_MSG(2_2, 2, 2, "2 does not match 2");
ATF_TC(check_eq);
ATF_TC_HEAD(check_eq, tc)
{
atf_tc_set_md_var(tc, "descr", "Tests the ATF_CHECK_EQ and "
"ATF_CHECK_EQ_MSG macros");
}
ATF_TC_BODY(check_eq, tc)
{
struct check_eq_test tests[] = {
{ H_CHECK_EQ_HEAD_NAME(1_1), H_CHECK_EQ_BODY_NAME(1_1),
"1", "1", "1 != 1", true },
{ H_CHECK_EQ_HEAD_NAME(1_2), H_CHECK_EQ_BODY_NAME(1_2),
"1", "2", "1 != 2", false },
{ H_CHECK_EQ_HEAD_NAME(2_1), H_CHECK_EQ_BODY_NAME(2_1),
"2", "1", "2 != 1", false },
{ H_CHECK_EQ_HEAD_NAME(2_2), H_CHECK_EQ_BODY_NAME(2_2),
"2", "2", "2 != 2", true },
{ H_CHECK_EQ_MSG_HEAD_NAME(1_1), H_CHECK_EQ_MSG_BODY_NAME(1_1),
"1", "1", "1 != 1: 1 does not match 1", true },
{ H_CHECK_EQ_MSG_HEAD_NAME(1_2), H_CHECK_EQ_MSG_BODY_NAME(1_2),
"1", "2", "1 != 2: 1 does not match 2", false },
{ H_CHECK_EQ_MSG_HEAD_NAME(2_1), H_CHECK_EQ_MSG_BODY_NAME(2_1),
"2", "1", "2 != 1: 2 does not match 1", false },
{ H_CHECK_EQ_MSG_HEAD_NAME(2_2), H_CHECK_EQ_MSG_BODY_NAME(2_2),
"2", "2", "2 != 2: 2 does not match 2", true },
{ NULL, NULL, 0, 0, "", false }
};
do_check_eq_tests(tests);
}
H_CHECK_STREQ(1_1, "1", "1");
H_CHECK_STREQ(1_2, "1", "2");
H_CHECK_STREQ(2_1, "2", "1");
H_CHECK_STREQ(2_2, "2", "2");
H_CHECK_STREQ_MSG(1_1, "1", "1", "1 does not match 1");
H_CHECK_STREQ_MSG(1_2, "1", "2", "1 does not match 2");
H_CHECK_STREQ_MSG(2_1, "2", "1", "2 does not match 1");
H_CHECK_STREQ_MSG(2_2, "2", "2", "2 does not match 2");
#define CHECK_STREQ_VAR1 "5"
#define CHECK_STREQ_VAR2 "9"
const char *check_streq_var1 = CHECK_STREQ_VAR1;
const char *check_streq_var2 = CHECK_STREQ_VAR2;
H_CHECK_STREQ(vars, check_streq_var1, check_streq_var2);
/* ---------------------------------------------------------------------
* Test cases for the ATF_CHECK_MATCH and ATF_CHECK_MATCH_MSG macros.
* --------------------------------------------------------------------- */
/* ---------------------------------------------------------------------
* Test cases for the ATF_REQUIRE and ATF_REQUIRE_MSG macros.
* --------------------------------------------------------------------- */
H_REQUIRE(0, 0);
H_REQUIRE(1, 1);
H_REQUIRE_MSG(0, 0, "expected a false value");
H_REQUIRE_MSG(1, 1, "expected a true value");
ATF_REQUIRE(unlink("before") != -1);
if (t->ok)
ATF_REQUIRE(unlink("after") != -1);
}
}
/* ---------------------------------------------------------------------
* Test cases for the ATF_REQUIRE_*EQ_ macros.
* --------------------------------------------------------------------- */
ATF_REQUIRE(unlink("before") != -1);
if (t->ok)
ATF_REQUIRE(unlink("after") != -1);
}
}
H_REQUIRE_EQ(1_1, 1, 1);
H_REQUIRE_EQ(1_2, 1, 2);
H_REQUIRE_EQ(2_1, 2, 1);
H_REQUIRE_EQ(2_2, 2, 2);
H_REQUIRE_EQ_MSG(1_1, 1, 1, "1 does not match 1");
H_REQUIRE_EQ_MSG(1_2, 1, 2, "1 does not match 2");
H_REQUIRE_EQ_MSG(2_1, 2, 1, "2 does not match 1");
H_REQUIRE_EQ_MSG(2_2, 2, 2, "2 does not match 2");
ATF_TC(require_eq);
ATF_TC_HEAD(require_eq, tc)
{
atf_tc_set_md_var(tc, "descr", "Tests the ATF_REQUIRE_EQ and "
"ATF_REQUIRE_EQ_MSG macros");
}
ATF_TC_BODY(require_eq, tc)
{
struct require_eq_test tests[] = {
{ H_REQUIRE_EQ_HEAD_NAME(1_1), H_REQUIRE_EQ_BODY_NAME(1_1),
"1", "1", "1 != 1", true },
{ H_REQUIRE_EQ_HEAD_NAME(1_2), H_REQUIRE_EQ_BODY_NAME(1_2),
"1", "2", "1 != 2", false },
{ H_REQUIRE_EQ_HEAD_NAME(2_1), H_REQUIRE_EQ_BODY_NAME(2_1),
"2", "1", "2 != 1", false },
{ H_REQUIRE_EQ_HEAD_NAME(2_2), H_REQUIRE_EQ_BODY_NAME(2_2),
"2", "2", "2 != 2", true },
{ H_REQUIRE_EQ_MSG_HEAD_NAME(1_1), H_REQUIRE_EQ_MSG_BODY_NAME(1_1),
"1", "1", "1 != 1: 1 does not match 1", true },
{ H_REQUIRE_EQ_MSG_HEAD_NAME(1_2), H_REQUIRE_EQ_MSG_BODY_NAME(1_2),
"1", "2", "1 != 2: 1 does not match 2", false },
{ H_REQUIRE_EQ_MSG_HEAD_NAME(2_1), H_REQUIRE_EQ_MSG_BODY_NAME(2_1),
"2", "1", "2 != 1: 2 does not match 1", false },
{ H_REQUIRE_EQ_MSG_HEAD_NAME(2_2), H_REQUIRE_EQ_MSG_BODY_NAME(2_2),
"2", "2", "2 != 2: 2 does not match 2", true },
{ NULL, NULL, 0, 0, "", false }
};
do_require_eq_tests(tests);
}
H_REQUIRE_STREQ(1_1, "1", "1");
H_REQUIRE_STREQ(1_2, "1", "2");
H_REQUIRE_STREQ(2_1, "2", "1");
H_REQUIRE_STREQ(2_2, "2", "2");
H_REQUIRE_STREQ_MSG(1_1, "1", "1", "1 does not match 1");
H_REQUIRE_STREQ_MSG(1_2, "1", "2", "1 does not match 2");
H_REQUIRE_STREQ_MSG(2_1, "2", "1", "2 does not match 1");
H_REQUIRE_STREQ_MSG(2_2, "2", "2", "2 does not match 2");
#define REQUIRE_STREQ_VAR1 "5"
#define REQUIRE_STREQ_VAR2 "9"
const char *require_streq_var1 = REQUIRE_STREQ_VAR1;
const char *require_streq_var2 = REQUIRE_STREQ_VAR2;
H_REQUIRE_STREQ(vars, require_streq_var1, require_streq_var2);
/* ---------------------------------------------------------------------
* Test cases for the ATF_REQUIRE_MATCH and ATF_REQUIRE_MATCH_MSG macros.
* --------------------------------------------------------------------- */
/* ---------------------------------------------------------------------
* Tests cases for the header file.
* --------------------------------------------------------------------- */
HEADER_TC(include, "atf-c/macros.h");
BUILD_TC(use, "macros_h_test.c",
"Tests that the macros provided by the atf-c/macros.h file "
"do not cause syntax errors when used",
"Build of macros_h_test.c failed; some macros in atf-c/macros.h "
"are broken");
ATF_TC(detect_unused_tests);
ATF_TC_HEAD(detect_unused_tests, tc)
{
atf_tc_set_md_var(tc, "descr",
"Tests that defining an unused test case raises a "
"warning (and thus an error)");
}
ATF_TC_BODY(detect_unused_tests, tc)
{
const char* validate_compiler =
"struct test_struct { int dummy; };\n"
"#define define_unused static struct test_struct unused\n"
"define_unused;\n";
atf_utils_create_file("compiler_test.c", "%s", validate_compiler);
if (build_check_c_o("compiler_test.c"))
atf_tc_expect_fail("Compiler does not raise a warning on an unused "
"static global variable declared by a macro");