1c243e490SMarcel Moolenaar // Copyright (c) 2009 The NetBSD Foundation, Inc.
2c243e490SMarcel Moolenaar // All rights reserved.
3c243e490SMarcel Moolenaar //
4c243e490SMarcel Moolenaar // Redistribution and use in source and binary forms, with or without
5c243e490SMarcel Moolenaar // modification, are permitted provided that the following conditions
6c243e490SMarcel Moolenaar // are met:
7c243e490SMarcel Moolenaar // 1. Redistributions of source code must retain the above copyright
8c243e490SMarcel Moolenaar // notice, this list of conditions and the following disclaimer.
9c243e490SMarcel Moolenaar // 2. Redistributions in binary form must reproduce the above copyright
10c243e490SMarcel Moolenaar // notice, this list of conditions and the following disclaimer in the
11c243e490SMarcel Moolenaar // documentation and/or other materials provided with the distribution.
12c243e490SMarcel Moolenaar //
13c243e490SMarcel Moolenaar // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14c243e490SMarcel Moolenaar // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15c243e490SMarcel Moolenaar // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16c243e490SMarcel Moolenaar // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17c243e490SMarcel Moolenaar // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18c243e490SMarcel Moolenaar // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c243e490SMarcel Moolenaar // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20c243e490SMarcel Moolenaar // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21c243e490SMarcel Moolenaar // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22c243e490SMarcel Moolenaar // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23c243e490SMarcel Moolenaar // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24c243e490SMarcel Moolenaar // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c243e490SMarcel Moolenaar
260677dfd1SJulio Merino #if defined(ATF_CXX_DETAIL_TEST_HELPERS_H)
27c243e490SMarcel Moolenaar # error "Cannot include test_helpers.hpp more than once."
28c243e490SMarcel Moolenaar #else
290677dfd1SJulio Merino # define ATF_CXX_DETAIL_TEST_HELPERS_H
30c243e490SMarcel Moolenaar #endif
31c243e490SMarcel Moolenaar
32c243e490SMarcel Moolenaar #include <cstdlib>
33c243e490SMarcel Moolenaar #include <iostream>
34c243e490SMarcel Moolenaar #include <sstream>
35c243e490SMarcel Moolenaar #include <utility>
36c243e490SMarcel Moolenaar
370677dfd1SJulio Merino #include <atf-c++.hpp>
380677dfd1SJulio Merino
39*c203bd70SAlex Richardson #include <atf-c++/detail/env.hpp>
400677dfd1SJulio Merino #include <atf-c++/detail/process.hpp>
41c243e490SMarcel Moolenaar
42c243e490SMarcel Moolenaar #define HEADER_TC(name, hdrname) \
43c243e490SMarcel Moolenaar ATF_TEST_CASE(name); \
44c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(name) \
45c243e490SMarcel Moolenaar { \
46c243e490SMarcel Moolenaar set_md_var("descr", "Tests that the " hdrname " file can be " \
47c243e490SMarcel Moolenaar "included on its own, without any prerequisites"); \
48*c203bd70SAlex Richardson const std::string cxx = atf::env::get("ATF_BUILD_CXX", ATF_BUILD_CXX); \
49*c203bd70SAlex Richardson set_md_var("require.progs", cxx); \
50c243e490SMarcel Moolenaar } \
51c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(name) \
52c243e490SMarcel Moolenaar { \
53c243e490SMarcel Moolenaar header_check(hdrname); \
54c243e490SMarcel Moolenaar }
55c243e490SMarcel Moolenaar
56c243e490SMarcel Moolenaar #define BUILD_TC(name, sfile, descr, failmsg) \
57c243e490SMarcel Moolenaar ATF_TEST_CASE(name); \
58c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(name) \
59c243e490SMarcel Moolenaar { \
60c243e490SMarcel Moolenaar set_md_var("descr", descr); \
61*c203bd70SAlex Richardson const std::string cxx = atf::env::get("ATF_BUILD_CXX", ATF_BUILD_CXX); \
62*c203bd70SAlex Richardson set_md_var("require.progs", cxx); \
63c243e490SMarcel Moolenaar } \
64c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(name) \
65c243e490SMarcel Moolenaar { \
661a61beb0SJulio Merino if (!build_check_cxx_o_srcdir(*this, sfile)) \
671a61beb0SJulio Merino ATF_FAIL(failmsg); \
68c243e490SMarcel Moolenaar }
69c243e490SMarcel Moolenaar
70c243e490SMarcel Moolenaar namespace atf {
71c243e490SMarcel Moolenaar namespace tests {
72c243e490SMarcel Moolenaar class tc;
73c243e490SMarcel Moolenaar }
74c243e490SMarcel Moolenaar }
75c243e490SMarcel Moolenaar
76c243e490SMarcel Moolenaar void header_check(const char*);
771a61beb0SJulio Merino bool build_check_cxx_o(const char*);
781a61beb0SJulio Merino bool build_check_cxx_o_srcdir(const atf::tests::tc&, const char*);
795686c6c3SMarcel Moolenaar atf::fs::path get_process_helpers_path(const atf::tests::tc&, bool);
80c243e490SMarcel Moolenaar
81c243e490SMarcel Moolenaar struct run_h_tc_data {
82c243e490SMarcel Moolenaar const atf::tests::vars_map& m_config;
83c243e490SMarcel Moolenaar
run_h_tc_datarun_h_tc_data84c243e490SMarcel Moolenaar run_h_tc_data(const atf::tests::vars_map& config) :
85c243e490SMarcel Moolenaar m_config(config) {}
86c243e490SMarcel Moolenaar };
87c243e490SMarcel Moolenaar
88c243e490SMarcel Moolenaar template< class TestCase >
89c243e490SMarcel Moolenaar void
run_h_tc_child(void * v)90c243e490SMarcel Moolenaar run_h_tc_child(void* v)
91c243e490SMarcel Moolenaar {
92c243e490SMarcel Moolenaar run_h_tc_data* data = static_cast< run_h_tc_data* >(v);
93c243e490SMarcel Moolenaar
94c243e490SMarcel Moolenaar TestCase tc;
95c243e490SMarcel Moolenaar tc.init(data->m_config);
96c243e490SMarcel Moolenaar tc.run("result");
97c243e490SMarcel Moolenaar std::exit(EXIT_SUCCESS);
98c243e490SMarcel Moolenaar }
99c243e490SMarcel Moolenaar
100c243e490SMarcel Moolenaar template< class TestCase >
101c243e490SMarcel Moolenaar void
run_h_tc(atf::tests::vars_map config=atf::tests::vars_map ())102c243e490SMarcel Moolenaar run_h_tc(atf::tests::vars_map config = atf::tests::vars_map())
103c243e490SMarcel Moolenaar {
104c243e490SMarcel Moolenaar run_h_tc_data data(config);
105c243e490SMarcel Moolenaar atf::process::child c = atf::process::fork(
106c243e490SMarcel Moolenaar run_h_tc_child< TestCase >,
107c243e490SMarcel Moolenaar atf::process::stream_redirect_path(atf::fs::path("stdout")),
108c243e490SMarcel Moolenaar atf::process::stream_redirect_path(atf::fs::path("stderr")),
109c243e490SMarcel Moolenaar &data);
110c243e490SMarcel Moolenaar const atf::process::status s = c.wait();
111c243e490SMarcel Moolenaar ATF_REQUIRE(s.exited());
112c243e490SMarcel Moolenaar }
113