Path: blob/main/contrib/atf/atf-c++/build_test.cpp
39507 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#include "atf-c++/build.hpp"2627#include <cstring>28#include <iostream>2930#include <atf-c++.hpp>3132extern "C" {33#include "atf-c/h_build.h"34}3536#include "atf-c++/detail/env.hpp"37#include "atf-c++/detail/process.hpp"3839// ------------------------------------------------------------------------40// Auxiliary functions.41// ------------------------------------------------------------------------4243template< class C >44void45print_col(const char* prefix, const C& c)46{47std::cout << prefix << ":";48for (typename C::const_iterator iter = c.begin(); iter != c.end();49iter++)50std::cout << " '" << *iter << "'";51std::cout << "\n";52}5354static55void56print_array(const char* prefix, const char* const* a)57{58std::cout << prefix << ":";59for (; *a != NULL; a++)60std::cout << " '" << *a << "'";61std::cout << "\n";62}6364static65void66verbose_set_env(const char *var, const char *val)67{68std::cout << "Setting " << var << " to '" << val << "'\n";69atf::env::set(var, val);70}7172static73bool74equal_argvs(const atf::process::argv_array& aa, const char* const* array)75{76bool equal = true;7778atf::process::argv_array::size_type i = 0;79while (equal && (i < aa.size() && array[i] != NULL)) {80if (std::strcmp(aa[i], array[i]) != 0)81equal = false;82else83i++;84}8586if (equal && (i < aa.size() || array[i] != NULL))87equal = false;8889return equal;90}9192static93void94check_equal_argvs(const atf::process::argv_array& aa, const char* const* array)95{96print_array("Expected arguments", array);97print_col("Arguments returned", aa);9899if (!equal_argvs(aa, array))100ATF_FAIL("The constructed argv differs from the expected values");101}102103// ------------------------------------------------------------------------104// Internal test cases.105// ------------------------------------------------------------------------106107ATF_TEST_CASE(equal_argvs);108ATF_TEST_CASE_HEAD(equal_argvs)109{110set_md_var("descr", "Tests the test case internal equal_argvs function");111}112ATF_TEST_CASE_BODY(equal_argvs)113{114{115const char* const array[] = { NULL };116const char* const argv[] = { NULL };117118ATF_REQUIRE(equal_argvs(atf::process::argv_array(argv), array));119}120121{122const char* const array[] = { NULL };123const char* const argv[] = { "foo", NULL };124125ATF_REQUIRE(!equal_argvs(atf::process::argv_array(argv), array));126}127128{129const char* const array[] = { "foo", NULL };130const char* const argv[] = { NULL };131132ATF_REQUIRE(!equal_argvs(atf::process::argv_array(argv), array));133}134135{136const char* const array[] = { "foo", NULL };137const char* const argv[] = { "foo", NULL };138139ATF_REQUIRE(equal_argvs(atf::process::argv_array(argv), array));140}141}142143// ------------------------------------------------------------------------144// Test cases for the free functions.145// ------------------------------------------------------------------------146147ATF_TEST_CASE(c_o);148ATF_TEST_CASE_HEAD(c_o)149{150set_md_var("descr", "Tests the c_o function");151}152ATF_TEST_CASE_BODY(c_o)153{154for (struct c_o_test* test = c_o_tests; test->expargv[0] != NULL;155test++) {156std::cout << "> Test: " << test->msg << "\n";157158verbose_set_env("ATF_BUILD_CC", test->cc);159verbose_set_env("ATF_BUILD_CFLAGS", test->cflags);160verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);161162atf::process::argv_array argv =163atf::build::c_o(test->sfile, test->ofile,164atf::process::argv_array(test->optargs));165check_equal_argvs(argv, test->expargv);166}167}168169ATF_TEST_CASE(cpp);170ATF_TEST_CASE_HEAD(cpp)171{172set_md_var("descr", "Tests the cpp function");173}174ATF_TEST_CASE_BODY(cpp)175{176for (struct cpp_test* test = cpp_tests; test->expargv[0] != NULL;177test++) {178std::cout << "> Test: " << test->msg << "\n";179180verbose_set_env("ATF_BUILD_CPP", test->cpp);181verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);182183atf::process::argv_array argv =184atf::build::cpp(test->sfile, test->ofile,185atf::process::argv_array(test->optargs));186check_equal_argvs(argv, test->expargv);187}188}189190ATF_TEST_CASE(cxx_o);191ATF_TEST_CASE_HEAD(cxx_o)192{193set_md_var("descr", "Tests the cxx_o function");194}195ATF_TEST_CASE_BODY(cxx_o)196{197for (struct cxx_o_test* test = cxx_o_tests; test->expargv[0] != NULL;198test++) {199std::cout << "> Test: " << test->msg << "\n";200201verbose_set_env("ATF_BUILD_CXX", test->cxx);202verbose_set_env("ATF_BUILD_CXXFLAGS", test->cxxflags);203verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);204205atf::process::argv_array argv =206atf::build::cxx_o(test->sfile, test->ofile,207atf::process::argv_array(test->optargs));208check_equal_argvs(argv, test->expargv);209}210}211212// ------------------------------------------------------------------------213// Main.214// ------------------------------------------------------------------------215216ATF_INIT_TEST_CASES(tcs)217{218// Add the internal test cases.219ATF_ADD_TEST_CASE(tcs, equal_argvs);220221// Add the test cases for the free functions.222ATF_ADD_TEST_CASE(tcs, c_o);223ATF_ADD_TEST_CASE(tcs, cpp);224ATF_ADD_TEST_CASE(tcs, cxx_o);225}226227228