xref: /minix3/external/bsd/atf/dist/atf-c++/build_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc //
2*11be35a1SLionel Sambuc // Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Copyright (c) 2009 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc // All rights reserved.
6*11be35a1SLionel Sambuc //
7*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc // are met:
10*11be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc //
16*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc //
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #include <cstring>
31*11be35a1SLionel Sambuc #include <iostream>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include "../atf-c/h_build.h"
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include "build.hpp"
36*11be35a1SLionel Sambuc #include "config.hpp"
37*11be35a1SLionel Sambuc #include "macros.hpp"
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include "detail/env.hpp"
40*11be35a1SLionel Sambuc #include "detail/process.hpp"
41*11be35a1SLionel Sambuc #include "detail/test_helpers.hpp"
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
44*11be35a1SLionel Sambuc // Auxiliary functions.
45*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc namespace atf {
48*11be35a1SLionel Sambuc     namespace config {
49*11be35a1SLionel Sambuc         void __reinit(void);
50*11be35a1SLionel Sambuc     }
51*11be35a1SLionel Sambuc }
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc template< class C >
54*11be35a1SLionel Sambuc void
print_col(const char * prefix,const C & c)55*11be35a1SLionel Sambuc print_col(const char* prefix, const C& c)
56*11be35a1SLionel Sambuc {
57*11be35a1SLionel Sambuc     std::cout << prefix << ":";
58*11be35a1SLionel Sambuc     for (typename C::const_iterator iter = c.begin(); iter != c.end();
59*11be35a1SLionel Sambuc          iter++)
60*11be35a1SLionel Sambuc         std::cout << " '" << *iter << "'";
61*11be35a1SLionel Sambuc     std::cout << "\n";
62*11be35a1SLionel Sambuc }
63*11be35a1SLionel Sambuc 
64*11be35a1SLionel Sambuc static
65*11be35a1SLionel Sambuc void
print_array(const char * prefix,const char * const * a)66*11be35a1SLionel Sambuc print_array(const char* prefix, const char* const* a)
67*11be35a1SLionel Sambuc {
68*11be35a1SLionel Sambuc     std::cout << prefix << ":";
69*11be35a1SLionel Sambuc     for (; *a != NULL; a++)
70*11be35a1SLionel Sambuc         std::cout << " '" << *a << "'";
71*11be35a1SLionel Sambuc     std::cout << "\n";
72*11be35a1SLionel Sambuc }
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc static
75*11be35a1SLionel Sambuc void
verbose_set_env(const char * var,const char * val)76*11be35a1SLionel Sambuc verbose_set_env(const char *var, const char *val)
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc     std::cout << "Setting " << var << " to '" << val << "'\n";
79*11be35a1SLionel Sambuc     atf::env::set(var, val);
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc static
83*11be35a1SLionel Sambuc bool
equal_argvs(const atf::process::argv_array & aa,const char * const * array)84*11be35a1SLionel Sambuc equal_argvs(const atf::process::argv_array& aa, const char* const* array)
85*11be35a1SLionel Sambuc {
86*11be35a1SLionel Sambuc     bool equal = true;
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc     atf::process::argv_array::size_type i = 0;
89*11be35a1SLionel Sambuc     while (equal && (i < aa.size() && array[i] != NULL)) {
90*11be35a1SLionel Sambuc         if (std::strcmp(aa[i], array[i]) != 0)
91*11be35a1SLionel Sambuc             equal = false;
92*11be35a1SLionel Sambuc         else
93*11be35a1SLionel Sambuc             i++;
94*11be35a1SLionel Sambuc     }
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc     if (equal && (i < aa.size() || array[i] != NULL))
97*11be35a1SLionel Sambuc         equal = false;
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc     return equal;
100*11be35a1SLionel Sambuc }
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc static
103*11be35a1SLionel Sambuc void
check_equal_argvs(const atf::process::argv_array & aa,const char * const * array)104*11be35a1SLionel Sambuc check_equal_argvs(const atf::process::argv_array& aa, const char* const* array)
105*11be35a1SLionel Sambuc {
106*11be35a1SLionel Sambuc     print_array("Expected arguments", array);
107*11be35a1SLionel Sambuc     print_col("Arguments returned", aa);
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc     if (!equal_argvs(aa, array))
110*11be35a1SLionel Sambuc         ATF_FAIL("The constructed argv differs from the expected values");
111*11be35a1SLionel Sambuc }
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
114*11be35a1SLionel Sambuc // Internal test cases.
115*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc ATF_TEST_CASE(equal_argvs);
ATF_TEST_CASE_HEAD(equal_argvs)118*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(equal_argvs)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the test case internal equal_argvs function");
121*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(equal_argvs)122*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(equal_argvs)
123*11be35a1SLionel Sambuc {
124*11be35a1SLionel Sambuc     {
125*11be35a1SLionel Sambuc         const char* const array[] = { NULL };
126*11be35a1SLionel Sambuc         const char* const argv[] = { NULL };
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc         ATF_REQUIRE(equal_argvs(atf::process::argv_array(argv), array));
129*11be35a1SLionel Sambuc     }
130*11be35a1SLionel Sambuc 
131*11be35a1SLionel Sambuc     {
132*11be35a1SLionel Sambuc         const char* const array[] = { NULL };
133*11be35a1SLionel Sambuc         const char* const argv[] = { "foo", NULL };
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc         ATF_REQUIRE(!equal_argvs(atf::process::argv_array(argv), array));
136*11be35a1SLionel Sambuc     }
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc     {
139*11be35a1SLionel Sambuc         const char* const array[] = { "foo", NULL };
140*11be35a1SLionel Sambuc         const char* const argv[] = { NULL };
141*11be35a1SLionel Sambuc 
142*11be35a1SLionel Sambuc         ATF_REQUIRE(!equal_argvs(atf::process::argv_array(argv), array));
143*11be35a1SLionel Sambuc     }
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc     {
146*11be35a1SLionel Sambuc         const char* const array[] = { "foo", NULL };
147*11be35a1SLionel Sambuc         const char* const argv[] = { "foo", NULL };
148*11be35a1SLionel Sambuc 
149*11be35a1SLionel Sambuc         ATF_REQUIRE(equal_argvs(atf::process::argv_array(argv), array));
150*11be35a1SLionel Sambuc     }
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc 
153*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
154*11be35a1SLionel Sambuc // Test cases for the free functions.
155*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
156*11be35a1SLionel Sambuc 
157*11be35a1SLionel Sambuc ATF_TEST_CASE(c_o);
ATF_TEST_CASE_HEAD(c_o)158*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(c_o)
159*11be35a1SLionel Sambuc {
160*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the c_o function");
161*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(c_o)162*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(c_o)
163*11be35a1SLionel Sambuc {
164*11be35a1SLionel Sambuc     for (struct c_o_test* test = c_o_tests; test->expargv[0] != NULL;
165*11be35a1SLionel Sambuc          test++) {
166*11be35a1SLionel Sambuc         std::cout << "> Test: " << test->msg << "\n";
167*11be35a1SLionel Sambuc 
168*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CC", test->cc);
169*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CFLAGS", test->cflags);
170*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
171*11be35a1SLionel Sambuc         atf::config::__reinit();
172*11be35a1SLionel Sambuc 
173*11be35a1SLionel Sambuc         atf::process::argv_array argv =
174*11be35a1SLionel Sambuc             atf::build::c_o(test->sfile, test->ofile,
175*11be35a1SLionel Sambuc                             atf::process::argv_array(test->optargs));
176*11be35a1SLionel Sambuc         check_equal_argvs(argv, test->expargv);
177*11be35a1SLionel Sambuc     }
178*11be35a1SLionel Sambuc }
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc ATF_TEST_CASE(cpp);
ATF_TEST_CASE_HEAD(cpp)181*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(cpp)
182*11be35a1SLionel Sambuc {
183*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the cpp function");
184*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(cpp)185*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(cpp)
186*11be35a1SLionel Sambuc {
187*11be35a1SLionel Sambuc     for (struct cpp_test* test = cpp_tests; test->expargv[0] != NULL;
188*11be35a1SLionel Sambuc          test++) {
189*11be35a1SLionel Sambuc         std::cout << "> Test: " << test->msg << "\n";
190*11be35a1SLionel Sambuc 
191*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CPP", test->cpp);
192*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
193*11be35a1SLionel Sambuc         atf::config::__reinit();
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc         atf::process::argv_array argv =
196*11be35a1SLionel Sambuc             atf::build::cpp(test->sfile, test->ofile,
197*11be35a1SLionel Sambuc                             atf::process::argv_array(test->optargs));
198*11be35a1SLionel Sambuc         check_equal_argvs(argv, test->expargv);
199*11be35a1SLionel Sambuc     }
200*11be35a1SLionel Sambuc }
201*11be35a1SLionel Sambuc 
202*11be35a1SLionel Sambuc ATF_TEST_CASE(cxx_o);
ATF_TEST_CASE_HEAD(cxx_o)203*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(cxx_o)
204*11be35a1SLionel Sambuc {
205*11be35a1SLionel Sambuc     set_md_var("descr", "Tests the cxx_o function");
206*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(cxx_o)207*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(cxx_o)
208*11be35a1SLionel Sambuc {
209*11be35a1SLionel Sambuc     for (struct cxx_o_test* test = cxx_o_tests; test->expargv[0] != NULL;
210*11be35a1SLionel Sambuc          test++) {
211*11be35a1SLionel Sambuc         std::cout << "> Test: " << test->msg << "\n";
212*11be35a1SLionel Sambuc 
213*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CXX", test->cxx);
214*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CXXFLAGS", test->cxxflags);
215*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
216*11be35a1SLionel Sambuc         atf::config::__reinit();
217*11be35a1SLionel Sambuc 
218*11be35a1SLionel Sambuc         atf::process::argv_array argv =
219*11be35a1SLionel Sambuc             atf::build::cxx_o(test->sfile, test->ofile,
220*11be35a1SLionel Sambuc                               atf::process::argv_array(test->optargs));
221*11be35a1SLionel Sambuc         check_equal_argvs(argv, test->expargv);
222*11be35a1SLionel Sambuc     }
223*11be35a1SLionel Sambuc }
224*11be35a1SLionel Sambuc 
225*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
226*11be35a1SLionel Sambuc // Tests cases for the header file.
227*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
228*11be35a1SLionel Sambuc 
229*11be35a1SLionel Sambuc HEADER_TC(include, "atf-c++/build.hpp");
230*11be35a1SLionel Sambuc 
231*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
232*11be35a1SLionel Sambuc // Main.
233*11be35a1SLionel Sambuc // ------------------------------------------------------------------------
234*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)235*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
236*11be35a1SLionel Sambuc {
237*11be35a1SLionel Sambuc     // Add the internal test cases.
238*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, equal_argvs);
239*11be35a1SLionel Sambuc 
240*11be35a1SLionel Sambuc     // Add the test cases for the free functions.
241*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, c_o);
242*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, cpp);
243*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, cxx_o);
244*11be35a1SLionel Sambuc 
245*11be35a1SLionel Sambuc     // Add the test cases for the header file.
246*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, include);
247*11be35a1SLionel Sambuc }
248