Path: blob/main/contrib/atf/atf-c++/detail/test_helpers.hpp
39563 views
// Copyright (c) 2009 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#if defined(ATF_CXX_DETAIL_TEST_HELPERS_H)26# error "Cannot include test_helpers.hpp more than once."27#else28# define ATF_CXX_DETAIL_TEST_HELPERS_H29#endif3031#include <cstdlib>32#include <iostream>33#include <sstream>34#include <utility>3536#include <atf-c++.hpp>3738#include <atf-c++/detail/env.hpp>39#include <atf-c++/detail/process.hpp>4041#define HEADER_TC(name, hdrname) \42ATF_TEST_CASE(name); \43ATF_TEST_CASE_HEAD(name) \44{ \45set_md_var("descr", "Tests that the " hdrname " file can be " \46"included on its own, without any prerequisites"); \47const std::string cxx = atf::env::get("ATF_BUILD_CXX", ATF_BUILD_CXX); \48set_md_var("require.progs", cxx); \49} \50ATF_TEST_CASE_BODY(name) \51{ \52header_check(hdrname); \53}5455#define BUILD_TC(name, sfile, descr, failmsg) \56ATF_TEST_CASE(name); \57ATF_TEST_CASE_HEAD(name) \58{ \59set_md_var("descr", descr); \60const std::string cxx = atf::env::get("ATF_BUILD_CXX", ATF_BUILD_CXX); \61set_md_var("require.progs", cxx); \62} \63ATF_TEST_CASE_BODY(name) \64{ \65if (!build_check_cxx_o_srcdir(*this, sfile)) \66ATF_FAIL(failmsg); \67}6869namespace atf {70namespace tests {71class tc;72}73}7475void header_check(const char*);76bool build_check_cxx_o(const char*);77bool build_check_cxx_o_srcdir(const atf::tests::tc&, const char*);78atf::fs::path get_process_helpers_path(const atf::tests::tc&, bool);7980struct run_h_tc_data {81const atf::tests::vars_map& m_config;8283run_h_tc_data(const atf::tests::vars_map& config) :84m_config(config) {}85};8687template< class TestCase >88void89run_h_tc_child(void* v)90{91run_h_tc_data* data = static_cast< run_h_tc_data* >(v);9293TestCase tc;94tc.init(data->m_config);95tc.run("result");96std::exit(EXIT_SUCCESS);97}9899template< class TestCase >100void101run_h_tc(atf::tests::vars_map config = atf::tests::vars_map())102{103run_h_tc_data data(config);104atf::process::child c = atf::process::fork(105run_h_tc_child< TestCase >,106atf::process::stream_redirect_path(atf::fs::path("stdout")),107atf::process::stream_redirect_path(atf::fs::path("stderr")),108&data);109const atf::process::status s = c.wait();110ATF_REQUIRE(s.exited());111}112113114