macros_test.c revision 0da7c95546ec1c76ca52f334a68dd69e4fa4dcde
/*
* Automated Testing Framework (atf)
*
* Copyright (c) 2008, 2009, 2010 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
*
* 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.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <atf-c.h>
#include "detail/test_helpers.h"
/* ---------------------------------------------------------------------
* Auxiliary functions.
* --------------------------------------------------------------------- */
static
void
{
atf_fs_path_fini(&p);
}
static
bool
exists(const char *p)
{
bool b;
return b;
}
static
void
{
atf_tc_fini(&tc);
}
/* ---------------------------------------------------------------------
* Helper test cases.
* --------------------------------------------------------------------- */
{ \
} \
{ \
macro; \
}
#define H_CHECK_EQ_MSG_HEAD_NAME(id) \
#define H_CHECK_EQ_MSG_BODY_NAME(id) \
#define H_CHECK_STREQ_MSG_HEAD_NAME(id) \
#define H_CHECK_STREQ_MSG_BODY_NAME(id) \
#define H_REQUIRE_EQ_MSG_HEAD_NAME(id) \
#define H_REQUIRE_EQ_MSG_BODY_NAME(id) \
#define H_REQUIRE_STREQ_MSG_HEAD_NAME(id) \
#define H_REQUIRE_STREQ_MSG_BODY_NAME(id) \
/* ---------------------------------------------------------------------
* Test cases for the ATF_{CHECK,REQUIRE}_ERRNO macros.
* --------------------------------------------------------------------- */
static int
errno_fail_stub(const int raised_errno)
{
return -1;
}
static int
errno_ok_stub(void)
{
return 0;
}
{
}
{
struct test {
bool ok;
const char *exp_regex;
} *t, tests[] = {
false, "Expected true value in errno_ok_stub\\(\\) == -1" },
true, NULL },
false, "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
};
if (t->ok) {
} else {
t->exp_regex));
}
}
}
{
}
{
struct test {
bool ok;
const char *exp_regex;
} *t, tests[] = {
false, "Expected true value in errno_ok_stub\\(\\) == -1" },
true, NULL },
false, "Expected errno 3, got 4, in errno_fail_stub\\(4\\) == -1" },
};
if (t->ok) {
} else {
"%s$", t->exp_regex));
}
if (t->ok)
}
}
/* ---------------------------------------------------------------------
* Test cases for the ATF_CHECK and ATF_CHECK_MSG macros.
* --------------------------------------------------------------------- */
H_CHECK(0, 0);
H_CHECK_MSG(0, 0, "expected a false value");
{
"ATF_CHECK_MSG macros");
}
{
struct test {
bool value;
const char *msg;
bool ok;
} *t, tests[] = {
{ H_CHECK_HEAD_NAME(0), H_CHECK_BODY_NAME(0), 0,
"0 not met", false },
"1 not met", true },
{ H_CHECK_MSG_HEAD_NAME(0), H_CHECK_MSG_BODY_NAME(0), 0,
"expected a false value", false },
"expected a true value", true },
};
if (t->ok) {
} else {
"macros_test.c:[0-9]+: %s$", t->msg));
}
}
}
/* ---------------------------------------------------------------------
* Test cases for the ATF_CHECK_*EQ_ macros.
* --------------------------------------------------------------------- */
struct check_eq_test {
const char *v1;
const char *v2;
const char *msg;
bool ok;
};
static
void
{
const struct check_eq_test *t;
if (t->ok) {
} else {
"macros_test.c:[0-9]+: %s$", t->msg));
}
}
}
{
"ATF_CHECK_EQ_MSG macros");
}
{
struct check_eq_test tests[] = {
"1", "1", "1 != 1", true },
"1", "2", "1 != 2", false },
"2", "1", "2 != 1", false },
"2", "2", "2 != 2", true },
"1", "1", "1 != 1: 1 does not match 1", true },
"1", "2", "1 != 2: 1 does not match 2", false },
"2", "1", "2 != 1: 2 does not match 1", false },
"2", "2", "2 != 2: 2 does not match 2", true },
};
}
#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;
{
"ATF_CHECK_STREQ_MSG macros");
}
{
struct check_eq_test tests[] = {
"1", "1", "\"1\" != \"1\" \\(1 != 1\\)", true },
"1", "2", "\"1\" != \"2\" \\(1 != 2\\)", false },
"2", "1", "\"2\" != \"1\" \\(2 != 1\\)", false },
"2", "2", "\"2\" != \"2\" \\(2 != 2\\)", true },
"1", "1", "\"1\" != \"1\" \\(1 != 1\\): 1 does not match 1", true },
"1", "2", "\"1\" != \"2\" \\(1 != 2\\): 1 does not match 2", false },
"2", "1", "\"2\" != \"1\" \\(2 != 1\\): 2 does not match 1", false },
"2", "2", "\"2\" != \"2\" \\(2 != 2\\): 2 does not match 2", true },
"check_streq_var1 != check_streq_var2 \\("
};
}
/* ---------------------------------------------------------------------
* Test cases for the ATF_REQUIRE and ATF_REQUIRE_MSG macros.
* --------------------------------------------------------------------- */
H_REQUIRE(0, 0);
H_REQUIRE_MSG(0, 0, "expected a false value");
{
"ATF_REQUIRE_MSG macros");
}
{
struct test {
bool value;
const char *msg;
bool ok;
} *t, tests[] = {
{ H_REQUIRE_HEAD_NAME(0), H_REQUIRE_BODY_NAME(0), 0,
"0 not met", false },
"1 not met", true },
{ H_REQUIRE_MSG_HEAD_NAME(0), H_REQUIRE_MSG_BODY_NAME(0), 0,
"expected a false value", false },
"expected a true value", true },
};
if (t->ok) {
} else {
"%s$", t->msg));
}
if (t->ok)
}
}
/* ---------------------------------------------------------------------
* Test cases for the ATF_REQUIRE_*EQ_ macros.
* --------------------------------------------------------------------- */
struct require_eq_test {
const char *v1;
const char *v2;
const char *msg;
bool ok;
};
static
void
{
const struct require_eq_test *t;
if (t->ok) {
} else {
":[0-9]+: %s$", t->msg));
}
if (t->ok)
}
}
{
"ATF_REQUIRE_EQ_MSG macros");
}
{
struct require_eq_test tests[] = {
"1", "1", "1 != 1", true },
"1", "2", "1 != 2", false },
"2", "1", "2 != 1", false },
"2", "2", "2 != 2", true },
"1", "1", "1 != 1: 1 does not match 1", true },
"1", "2", "1 != 2: 1 does not match 2", false },
"2", "1", "2 != 1: 2 does not match 1", false },
"2", "2", "2 != 2: 2 does not match 2", true },
};
}
#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;
{
"ATF_REQUIRE_STREQ_MSG macros");
}
{
struct require_eq_test tests[] = {
"1", "1", "\"1\" != \"1\" \\(1 != 1\\)", true },
"1", "2", "\"1\" != \"2\" \\(1 != 2\\)", false },
"2", "1", "\"2\" != \"1\" \\(2 != 1\\)", false },
"2", "2", "\"2\" != \"2\" \\(2 != 2\\)", true },
"1", "1", "\"1\" != \"1\" \\(1 != 1\\): 1 does not match 1", true },
"1", "2", "\"1\" != \"2\" \\(1 != 2\\): 1 does not match 2", false },
"2", "1", "\"2\" != \"1\" \\(2 != 1\\): 2 does not match 1", false },
"2", "2", "\"2\" != \"2\" \\(2 != 2\\): 2 does not match 2", true },
"require_streq_var1 != require_streq_var2 \\("
};
}
/* ---------------------------------------------------------------------
* Miscellaneous test cases covering several macros.
* --------------------------------------------------------------------- */
static
bool
{
return false;
}
static
const char *
{
return "foo";
}
{
"as part of the automatically-generated messages "
"do not get expanded");
}
{
struct test {
bool fatal;
const char *msg;
} *t, tests[] = {
"aux_bool\\(\"%d\"\\) not met" },
"aux_bool\\(\"%d\"\\) not met" },
"aux_str\\(\"%d\"\\) != \"\" \\(foo != \\)" },
"aux_str\\(\"%d\"\\) != \"\" \\(foo != \\)" },
};
if (t->fatal) {
bool matched =
"%s$", t->msg);
} else {
"macros_test.c:[0-9]+: %s$", t->msg);
}
}
}
/* ---------------------------------------------------------------------
* Tests cases for the header file.
* --------------------------------------------------------------------- */
"do not cause syntax errors when used",
"Build of macros_h_test.c failed; some macros in atf-c/macros.h "
"are broken");
/* ---------------------------------------------------------------------
* Main.
* --------------------------------------------------------------------- */
{
/* Add the test cases for the header file. */
ATF_TP_ADD_TC(tp, include);
return atf_no_error();
}