test_helpers.cpp revision 303975
154359Sroberto// Copyright (c) 2009 The NetBSD Foundation, Inc.
254359Sroberto// All rights reserved.
354359Sroberto//
454359Sroberto// Redistribution and use in source and binary forms, with or without
554359Sroberto// modification, are permitted provided that the following conditions
654359Sroberto// are met:
754359Sroberto// 1. Redistributions of source code must retain the above copyright
854359Sroberto//    notice, this list of conditions and the following disclaimer.
954359Sroberto// 2. Redistributions in binary form must reproduce the above copyright
1054359Sroberto//    notice, this list of conditions and the following disclaimer in the
1154359Sroberto//    documentation and/or other materials provided with the distribution.
1254359Sroberto//
1354359Sroberto// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1454359Sroberto// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1554359Sroberto// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1654359Sroberto// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1754359Sroberto// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
1854359Sroberto// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1954359Sroberto// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2054359Sroberto// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2154359Sroberto// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2254359Sroberto// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2354359Sroberto// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2454359Sroberto// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2554359Sroberto
2654359Sroberto#include "atf-c++/detail/test_helpers.hpp"
2754359Sroberto
2854359Sroberto#include <fstream>
2954359Sroberto#include <iostream>
3054359Sroberto#include <string>
3154359Sroberto#include <vector>
3254359Sroberto
3354359Sroberto#include <atf-c++.hpp>
3454359Sroberto
3554359Sroberto#include "atf-c++/check.hpp"
3654359Sroberto#include "atf-c++/detail/env.hpp"
3754359Sroberto#include "atf-c++/detail/fs.hpp"
3854359Sroberto#include "atf-c++/detail/process.hpp"
3954359Sroberto
4054359Sroberto// Path to the directory containing the libatf-c tests, used to locate the
4154359Sroberto// process_helpers program.  If NULL (the default), the code will use a
4254359Sroberto// relative path.  Otherwise, the provided path will be used; this is so
4354359Sroberto// that we can locate the helpers binary if the installation uses a
4454359Sroberto// different layout than the one we provide (as is the case in FreeBSD).
4556746Sroberto#if defined(ATF_C_TESTS_BASE)
4656746Srobertostatic const char* atf_c_tests_base = ATF_C_TESTS_BASE;
4756746Sroberto#else
4856746Srobertostatic const char* atf_c_tests_base = NULL;
4956746Sroberto#endif
5056746Sroberto#undef ATF_C_TESTS_BASE
5156746Sroberto
5254359Srobertobool
5354359Srobertobuild_check_cxx_o(const char* sfile)
5454359Sroberto{
5554359Sroberto    std::vector< std::string > optargs;
5654359Sroberto    optargs.push_back("-I" + atf::env::get("ATF_INCLUDEDIR", ATF_INCLUDEDIR));
5754359Sroberto    optargs.push_back("-Wall");
5854359Sroberto    optargs.push_back("-Werror");
5954359Sroberto
6054359Sroberto    return atf::check::build_cxx_o(sfile, "test.o",
6154359Sroberto                                   atf::process::argv_array(optargs));
6254359Sroberto}
6354359Sroberto
6454359Srobertobool
6554359Srobertobuild_check_cxx_o_srcdir(const atf::tests::tc& tc, const char* sfile)
6654359Sroberto{
6754359Sroberto    const atf::fs::path sfilepath =
6854359Sroberto        atf::fs::path(tc.get_config_var("srcdir")) / sfile;
6954359Sroberto    return build_check_cxx_o(sfilepath.c_str());
7054359Sroberto}
7154359Sroberto
7254359Srobertovoid
7354359Srobertoheader_check(const char *hdrname)
7454359Sroberto{
7554359Sroberto    std::ofstream srcfile("test.cpp");
7654359Sroberto    ATF_REQUIRE(srcfile);
7754359Sroberto    srcfile << "#include <" << hdrname << ">\n";
7854359Sroberto    srcfile.close();
7954359Sroberto
8054359Sroberto    const std::string failmsg = std::string("Header check failed; ") +
8154359Sroberto        hdrname + " is not self-contained";
8254359Sroberto    if (!build_check_cxx_o("test.cpp"))
8354359Sroberto        ATF_FAIL(failmsg);
8454359Sroberto}
8554359Sroberto
8654359Srobertoatf::fs::path
8754359Srobertoget_process_helpers_path(const atf::tests::tc& tc, bool is_detail)
8854359Sroberto{
8954359Sroberto    const char* helper = "detail/process_helpers";
9054359Sroberto    if (atf_c_tests_base == NULL) {
9154359Sroberto        if (is_detail)
9254359Sroberto            return atf::fs::path(tc.get_config_var("srcdir")) /
9354359Sroberto                   ".." / ".." / "atf-c" / helper;
9454359Sroberto        else
9554359Sroberto            return atf::fs::path(tc.get_config_var("srcdir")) /
9654359Sroberto                   ".." / "atf-c" / helper;
9754359Sroberto    } else {
9854359Sroberto        return atf::fs::path(atf_c_tests_base) / helper;
9954359Sroberto    }
10054359Sroberto}
10154359Sroberto