1449SN/A// Copyright (c) 2009 The NetBSD Foundation, Inc.
24144Sjjg// All rights reserved.
3449SN/A//
4449SN/A// Redistribution and use in source and binary forms, with or without
5449SN/A// modification, are permitted provided that the following conditions
6449SN/A// are met:
7553SN/A// 1. Redistributions of source code must retain the above copyright
8449SN/A//    notice, this list of conditions and the following disclaimer.
9553SN/A// 2. Redistributions in binary form must reproduce the above copyright
10449SN/A//    notice, this list of conditions and the following disclaimer in the
11449SN/A//    documentation and/or other materials provided with the distribution.
12449SN/A//
13449SN/A// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14449SN/A// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15449SN/A// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16449SN/A// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17449SN/A// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18449SN/A// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19449SN/A// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20449SN/A// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21553SN/A// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22553SN/A// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23553SN/A// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24449SN/A// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25449SN/A
262872Sjjg#include "atf-c++/build.hpp"
27449SN/A
28449SN/A#include <cstring>
29449SN/A#include <iostream>
30449SN/A
31449SN/A#include <atf-c++.hpp>
32449SN/A
33449SN/Aextern "C" {
34449SN/A#include "atf-c/h_build.h"
35449SN/A}
363155Sjjg
37449SN/A#include "atf-c++/detail/env.hpp"
38449SN/A#include "atf-c++/detail/process.hpp"
39449SN/A
403155Sjjg// ------------------------------------------------------------------------
413155Sjjg// Auxiliary functions.
42449SN/A// ------------------------------------------------------------------------
43958SN/A
44449SN/Atemplate< class C >
453155Sjjgvoid
462837SN/Aprint_col(const char* prefix, const C& c)
472872Sjjg{
48449SN/A    std::cout << prefix << ":";
49449SN/A    for (typename C::const_iterator iter = c.begin(); iter != c.end();
503155Sjjg         iter++)
51449SN/A        std::cout << " '" << *iter << "'";
52449SN/A    std::cout << "\n";
533155Sjjg}
542601SN/A
552601SN/Astatic
56449SN/Avoid
57449SN/Aprint_array(const char* prefix, const char* const* a)
58449SN/A{
59449SN/A    std::cout << prefix << ":";
60449SN/A    for (; *a != NULL; a++)
61449SN/A        std::cout << " '" << *a << "'";
623155Sjjg    std::cout << "\n";
633155Sjjg}
643155Sjjg
653155Sjjgstatic
663155Sjjgvoid
673155Sjjgverbose_set_env(const char *var, const char *val)
683155Sjjg{
693155Sjjg    std::cout << "Setting " << var << " to '" << val << "'\n";
703155Sjjg    atf::env::set(var, val);
713155Sjjg}
72449SN/A
73580SN/Astatic
74580SN/Abool
75449SN/Aequal_argvs(const atf::process::argv_array& aa, const char* const* array)
76449SN/A{
77449SN/A    bool equal = true;
782721SN/A
793155Sjjg    atf::process::argv_array::size_type i = 0;
803155Sjjg    while (equal && (i < aa.size() && array[i] != NULL)) {
813155Sjjg        if (std::strcmp(aa[i], array[i]) != 0)
823155Sjjg            equal = false;
833155Sjjg        else
843155Sjjg            i++;
85449SN/A    }
86449SN/A
873155Sjjg    if (equal && (i < aa.size() || array[i] != NULL))
883155Sjjg        equal = false;
893155Sjjg
903155Sjjg    return equal;
913155Sjjg}
923155Sjjg
933155Sjjgstatic
943155Sjjgvoid
953155Sjjgcheck_equal_argvs(const atf::process::argv_array& aa, const char* const* array)
963155Sjjg{
973155Sjjg    print_array("Expected arguments", array);
983155Sjjg    print_col("Arguments returned", aa);
99449SN/A
1003155Sjjg    if (!equal_argvs(aa, array))
1013155Sjjg        ATF_FAIL("The constructed argv differs from the expected values");
1023155Sjjg}
1033155Sjjg
1043155Sjjg// ------------------------------------------------------------------------
1053155Sjjg// Internal test cases.
1063155Sjjg// ------------------------------------------------------------------------
1073155Sjjg
1083155SjjgATF_TEST_CASE(equal_argvs);
1093155SjjgATF_TEST_CASE_HEAD(equal_argvs)
1103155Sjjg{
1113155Sjjg    set_md_var("descr", "Tests the test case internal equal_argvs function");
1123392Sjjg}
1133155SjjgATF_TEST_CASE_BODY(equal_argvs)
1143155Sjjg{
1153155Sjjg    {
1163155Sjjg        const char* const array[] = { NULL };
1173155Sjjg        const char* const argv[] = { NULL };
1183155Sjjg
1193155Sjjg        ATF_REQUIRE(equal_argvs(atf::process::argv_array(argv), array));
1203155Sjjg    }
1213155Sjjg
1223155Sjjg    {
1233155Sjjg        const char* const array[] = { NULL };
1243155Sjjg        const char* const argv[] = { "foo", NULL };
1253155Sjjg
1263155Sjjg        ATF_REQUIRE(!equal_argvs(atf::process::argv_array(argv), array));
1273155Sjjg    }
1283155Sjjg
1293155Sjjg    {
1303155Sjjg        const char* const array[] = { "foo", NULL };
1313155Sjjg        const char* const argv[] = { NULL };
1323155Sjjg
1333155Sjjg        ATF_REQUIRE(!equal_argvs(atf::process::argv_array(argv), array));
1343155Sjjg    }
1353155Sjjg
1363155Sjjg    {
1373155Sjjg        const char* const array[] = { "foo", NULL };
1383155Sjjg        const char* const argv[] = { "foo", NULL };
139449SN/A
140449SN/A        ATF_REQUIRE(equal_argvs(atf::process::argv_array(argv), array));
141449SN/A    }
1423155Sjjg}
1433155Sjjg
1443155Sjjg// ------------------------------------------------------------------------
1453155Sjjg// Test cases for the free functions.
1463155Sjjg// ------------------------------------------------------------------------
1473155Sjjg
1483155SjjgATF_TEST_CASE(c_o);
1493155SjjgATF_TEST_CASE_HEAD(c_o)
1503155Sjjg{
1513155Sjjg    set_md_var("descr", "Tests the c_o function");
1523155Sjjg}
1534144SjjgATF_TEST_CASE_BODY(c_o)
154449SN/A{
1553155Sjjg    for (struct c_o_test* test = c_o_tests; test->expargv[0] != NULL;
1563155Sjjg         test++) {
1573155Sjjg        std::cout << "> Test: " << test->msg << "\n";
158449SN/A
159449SN/A        verbose_set_env("ATF_BUILD_CC", test->cc);
1603155Sjjg        verbose_set_env("ATF_BUILD_CFLAGS", test->cflags);
1613155Sjjg        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
1623155Sjjg
1633155Sjjg        atf::process::argv_array argv =
1643155Sjjg            atf::build::c_o(test->sfile, test->ofile,
1653155Sjjg                            atf::process::argv_array(test->optargs));
1663155Sjjg        check_equal_argvs(argv, test->expargv);
1673155Sjjg    }
1683155Sjjg}
1693155Sjjg
1703155SjjgATF_TEST_CASE(cpp);
1713155SjjgATF_TEST_CASE_HEAD(cpp)
1723155Sjjg{
1733155Sjjg    set_md_var("descr", "Tests the cpp function");
1743155Sjjg}
1753155SjjgATF_TEST_CASE_BODY(cpp)
1763155Sjjg{
1773155Sjjg    for (struct cpp_test* test = cpp_tests; test->expargv[0] != NULL;
1783155Sjjg         test++) {
1793155Sjjg        std::cout << "> Test: " << test->msg << "\n";
1803155Sjjg
1813155Sjjg        verbose_set_env("ATF_BUILD_CPP", test->cpp);
1823155Sjjg        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
1833155Sjjg
1843155Sjjg        atf::process::argv_array argv =
1853155Sjjg            atf::build::cpp(test->sfile, test->ofile,
1863155Sjjg                            atf::process::argv_array(test->optargs));
1873155Sjjg        check_equal_argvs(argv, test->expargv);
1883155Sjjg    }
1893155Sjjg}
1903155Sjjg
1913155SjjgATF_TEST_CASE(cxx_o);
1923155SjjgATF_TEST_CASE_HEAD(cxx_o)
1933155Sjjg{
1943155Sjjg    set_md_var("descr", "Tests the cxx_o function");
1953155Sjjg}
1963155SjjgATF_TEST_CASE_BODY(cxx_o)
1973155Sjjg{
1983155Sjjg    for (struct cxx_o_test* test = cxx_o_tests; test->expargv[0] != NULL;
1993155Sjjg         test++) {
2003155Sjjg        std::cout << "> Test: " << test->msg << "\n";
2013155Sjjg
2023155Sjjg        verbose_set_env("ATF_BUILD_CXX", test->cxx);
2033155Sjjg        verbose_set_env("ATF_BUILD_CXXFLAGS", test->cxxflags);
2043155Sjjg        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
2053155Sjjg
2063155Sjjg        atf::process::argv_array argv =
2073155Sjjg            atf::build::cxx_o(test->sfile, test->ofile,
2083155Sjjg                              atf::process::argv_array(test->optargs));
2092721SN/A        check_equal_argvs(argv, test->expargv);
2103155Sjjg    }
2112721SN/A}
2122721SN/A
2132721SN/A// ------------------------------------------------------------------------
2143155Sjjg// Main.
2153155Sjjg// ------------------------------------------------------------------------
2163155Sjjg
2173155SjjgATF_INIT_TEST_CASES(tcs)
2183155Sjjg{
2193155Sjjg    // Add the internal test cases.
2203155Sjjg    ATF_ADD_TEST_CASE(tcs, equal_argvs);
2213155Sjjg
2223155Sjjg    // Add the test cases for the free functions.
2233155Sjjg    ATF_ADD_TEST_CASE(tcs, c_o);
2244144Sjjg    ATF_ADD_TEST_CASE(tcs, cpp);
225449SN/A    ATF_ADD_TEST_CASE(tcs, cxx_o);
2263155Sjjg}
2273155Sjjg