145147a9marcel// Copyright (c) 2009 The NetBSD Foundation, Inc.
245147a9marcel// All rights reserved.
345147a9marcel//
445147a9marcel// Redistribution and use in source and binary forms, with or without
545147a9marcel// modification, are permitted provided that the following conditions
645147a9marcel// are met:
745147a9marcel// 1. Redistributions of source code must retain the above copyright
845147a9marcel//    notice, this list of conditions and the following disclaimer.
945147a9marcel// 2. Redistributions in binary form must reproduce the above copyright
1045147a9marcel//    notice, this list of conditions and the following disclaimer in the
1145147a9marcel//    documentation and/or other materials provided with the distribution.
1245147a9marcel//
1345147a9marcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1445147a9marcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1545147a9marcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1645147a9marcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1745147a9marcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
1845147a9marcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1945147a9marcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2045147a9marcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2145147a9marcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2245147a9marcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2345147a9marcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2445147a9marcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2545147a9marcel
263b6cf63jmmv#if defined(ATF_CXX_DETAIL_TEST_HELPERS_H)
2745147a9marcel#   error "Cannot include test_helpers.hpp more than once."
2845147a9marcel#else
293b6cf63jmmv#   define ATF_CXX_DETAIL_TEST_HELPERS_H
3045147a9marcel#endif
3145147a9marcel
3245147a9marcel#include <cstdlib>
3345147a9marcel#include <iostream>
3445147a9marcel#include <sstream>
3545147a9marcel#include <utility>
3645147a9marcel
373b6cf63jmmv#include <atf-c++.hpp>
383b6cf63jmmv
393b6cf63jmmv#include <atf-c++/detail/process.hpp>
4045147a9marcel
4145147a9marcel#define HEADER_TC(name, hdrname) \
4245147a9marcel    ATF_TEST_CASE(name); \
4345147a9marcel    ATF_TEST_CASE_HEAD(name) \
4445147a9marcel    { \
4545147a9marcel        set_md_var("descr", "Tests that the " hdrname " file can be " \
4645147a9marcel            "included on its own, without any prerequisites"); \
4745147a9marcel    } \
4845147a9marcel    ATF_TEST_CASE_BODY(name) \
4945147a9marcel    { \
5045147a9marcel        header_check(hdrname); \
5145147a9marcel    }
5245147a9marcel
5345147a9marcel#define BUILD_TC(name, sfile, descr, failmsg) \
5445147a9marcel    ATF_TEST_CASE(name); \
5545147a9marcel    ATF_TEST_CASE_HEAD(name) \
5645147a9marcel    { \
5745147a9marcel        set_md_var("descr", descr); \
5845147a9marcel    } \
5945147a9marcel    ATF_TEST_CASE_BODY(name) \
6045147a9marcel    { \
61a817576jmmv        if (!build_check_cxx_o_srcdir(*this, sfile)) \
62a817576jmmv            ATF_FAIL(failmsg); \
6345147a9marcel    }
6445147a9marcel
6545147a9marcelnamespace atf {
6645147a9marcelnamespace tests {
6745147a9marcelclass tc;
6845147a9marcel}
6945147a9marcel}
7045147a9marcel
7145147a9marcelvoid header_check(const char*);
72a817576jmmvbool build_check_cxx_o(const char*);
73a817576jmmvbool build_check_cxx_o_srcdir(const atf::tests::tc&, const char*);
74a817576jmmvatf::fs::path get_process_helpers_path(const atf::tests::tc&, bool);
7545147a9marcel
7645147a9marcelstruct run_h_tc_data {
7745147a9marcel    const atf::tests::vars_map& m_config;
7845147a9marcel
7945147a9marcel    run_h_tc_data(const atf::tests::vars_map& config) :
8045147a9marcel        m_config(config) {}
8145147a9marcel};
8245147a9marcel
8345147a9marceltemplate< class TestCase >
8445147a9marcelvoid
8545147a9marcelrun_h_tc_child(void* v)
8645147a9marcel{
8745147a9marcel    run_h_tc_data* data = static_cast< run_h_tc_data* >(v);
8845147a9marcel
8945147a9marcel    TestCase tc;
9045147a9marcel    tc.init(data->m_config);
9145147a9marcel    tc.run("result");
9245147a9marcel    std::exit(EXIT_SUCCESS);
9345147a9marcel}
9445147a9marcel
9545147a9marceltemplate< class TestCase >
9645147a9marcelvoid
9745147a9marcelrun_h_tc(atf::tests::vars_map config = atf::tests::vars_map())
9845147a9marcel{
9945147a9marcel    run_h_tc_data data(config);
10045147a9marcel    atf::process::child c = atf::process::fork(
10145147a9marcel        run_h_tc_child< TestCase >,
10245147a9marcel        atf::process::stream_redirect_path(atf::fs::path("stdout")),
10345147a9marcel        atf::process::stream_redirect_path(atf::fs::path("stderr")),
10445147a9marcel        &data);
10545147a9marcel    const atf::process::status s = c.wait();
10645147a9marcel    ATF_REQUIRE(s.exited());
10745147a9marcel}
108