1240116Smarcel// Copyright (c) 2009 The NetBSD Foundation, Inc.
2240116Smarcel// All rights reserved.
3240116Smarcel//
4240116Smarcel// Redistribution and use in source and binary forms, with or without
5240116Smarcel// modification, are permitted provided that the following conditions
6240116Smarcel// are met:
7240116Smarcel// 1. Redistributions of source code must retain the above copyright
8240116Smarcel//    notice, this list of conditions and the following disclaimer.
9240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright
10240116Smarcel//    notice, this list of conditions and the following disclaimer in the
11240116Smarcel//    documentation and/or other materials provided with the distribution.
12240116Smarcel//
13240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25240116Smarcel
26273929Sjmmv#if defined(ATF_CXX_DETAIL_TEST_HELPERS_H)
27240116Smarcel#   error "Cannot include test_helpers.hpp more than once."
28240116Smarcel#else
29273929Sjmmv#   define ATF_CXX_DETAIL_TEST_HELPERS_H
30240116Smarcel#endif
31240116Smarcel
32240116Smarcel#include <cstdlib>
33240116Smarcel#include <iostream>
34240116Smarcel#include <sstream>
35240116Smarcel#include <utility>
36240116Smarcel
37273929Sjmmv#include <atf-c++.hpp>
38240116Smarcel
39273929Sjmmv#include <atf-c++/detail/process.hpp>
40273929Sjmmv
41240116Smarcel#define HEADER_TC(name, hdrname) \
42240116Smarcel    ATF_TEST_CASE(name); \
43240116Smarcel    ATF_TEST_CASE_HEAD(name) \
44240116Smarcel    { \
45240116Smarcel        set_md_var("descr", "Tests that the " hdrname " file can be " \
46240116Smarcel            "included on its own, without any prerequisites"); \
47240116Smarcel    } \
48240116Smarcel    ATF_TEST_CASE_BODY(name) \
49240116Smarcel    { \
50240116Smarcel        header_check(hdrname); \
51240116Smarcel    }
52240116Smarcel
53240116Smarcel#define BUILD_TC(name, sfile, descr, failmsg) \
54240116Smarcel    ATF_TEST_CASE(name); \
55240116Smarcel    ATF_TEST_CASE_HEAD(name) \
56240116Smarcel    { \
57240116Smarcel        set_md_var("descr", descr); \
58240116Smarcel    } \
59240116Smarcel    ATF_TEST_CASE_BODY(name) \
60240116Smarcel    { \
61261897Sjmmv        if (!build_check_cxx_o_srcdir(*this, sfile)) \
62261897Sjmmv            ATF_FAIL(failmsg); \
63240116Smarcel    }
64240116Smarcel
65240116Smarcelnamespace atf {
66240116Smarcelnamespace tests {
67240116Smarcelclass tc;
68240116Smarcel}
69240116Smarcel}
70240116Smarcel
71240116Smarcelvoid header_check(const char*);
72261897Sjmmvbool build_check_cxx_o(const char*);
73261897Sjmmvbool build_check_cxx_o_srcdir(const atf::tests::tc&, const char*);
74251108Smarcelatf::fs::path get_process_helpers_path(const atf::tests::tc&, bool);
75240116Smarcel
76240116Smarcelstruct run_h_tc_data {
77240116Smarcel    const atf::tests::vars_map& m_config;
78240116Smarcel
79240116Smarcel    run_h_tc_data(const atf::tests::vars_map& config) :
80240116Smarcel        m_config(config) {}
81240116Smarcel};
82240116Smarcel
83240116Smarceltemplate< class TestCase >
84240116Smarcelvoid
85240116Smarcelrun_h_tc_child(void* v)
86240116Smarcel{
87240116Smarcel    run_h_tc_data* data = static_cast< run_h_tc_data* >(v);
88240116Smarcel
89240116Smarcel    TestCase tc;
90240116Smarcel    tc.init(data->m_config);
91240116Smarcel    tc.run("result");
92240116Smarcel    std::exit(EXIT_SUCCESS);
93240116Smarcel}
94240116Smarcel
95240116Smarceltemplate< class TestCase >
96240116Smarcelvoid
97240116Smarcelrun_h_tc(atf::tests::vars_map config = atf::tests::vars_map())
98240116Smarcel{
99240116Smarcel    run_h_tc_data data(config);
100240116Smarcel    atf::process::child c = atf::process::fork(
101240116Smarcel        run_h_tc_child< TestCase >,
102240116Smarcel        atf::process::stream_redirect_path(atf::fs::path("stdout")),
103240116Smarcel        atf::process::stream_redirect_path(atf::fs::path("stderr")),
104240116Smarcel        &data);
105240116Smarcel    const atf::process::status s = c.wait();
106240116Smarcel    ATF_REQUIRE(s.exited());
107240116Smarcel}
108