xref: /minix3/external/bsd/atf/dist/atf-c/build_test.c (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 <stdio.h>
31*11be35a1SLionel Sambuc #include <stdlib.h>
32*11be35a1SLionel Sambuc #include <string.h>
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <atf-c.h>
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc #include "atf-c/build.h"
37*11be35a1SLionel Sambuc #include "atf-c/config.h"
38*11be35a1SLionel Sambuc #include "atf-c/utils.h"
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc #include "detail/env.h"
41*11be35a1SLionel Sambuc #include "detail/test_helpers.h"
42*11be35a1SLionel Sambuc #include "h_build.h"
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
45*11be35a1SLionel Sambuc  * Auxiliary functions.
46*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc void __atf_config_reinit(void);
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc static
51*11be35a1SLionel Sambuc bool
equal_arrays(const char * const * exp_array,char ** actual_array)52*11be35a1SLionel Sambuc equal_arrays(const char *const *exp_array, char **actual_array)
53*11be35a1SLionel Sambuc {
54*11be35a1SLionel Sambuc     bool equal;
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc     if (*exp_array == NULL && *actual_array == NULL)
57*11be35a1SLionel Sambuc         equal = true;
58*11be35a1SLionel Sambuc     else if (*exp_array == NULL || *actual_array == NULL)
59*11be35a1SLionel Sambuc         equal = false;
60*11be35a1SLionel Sambuc     else {
61*11be35a1SLionel Sambuc         equal = true;
62*11be35a1SLionel Sambuc         while (*actual_array != NULL) {
63*11be35a1SLionel Sambuc             if (*exp_array == NULL || strcmp(*exp_array, *actual_array) != 0) {
64*11be35a1SLionel Sambuc                 equal = false;
65*11be35a1SLionel Sambuc                 break;
66*11be35a1SLionel Sambuc             }
67*11be35a1SLionel Sambuc             exp_array++;
68*11be35a1SLionel Sambuc             actual_array++;
69*11be35a1SLionel Sambuc         }
70*11be35a1SLionel Sambuc     }
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc     return equal;
73*11be35a1SLionel Sambuc }
74*11be35a1SLionel Sambuc 
75*11be35a1SLionel Sambuc static
76*11be35a1SLionel Sambuc void
check_equal_array(const char * const * exp_array,char ** actual_array)77*11be35a1SLionel Sambuc check_equal_array(const char *const *exp_array, char **actual_array)
78*11be35a1SLionel Sambuc {
79*11be35a1SLionel Sambuc     {
80*11be35a1SLionel Sambuc         const char *const *exp_ptr;
81*11be35a1SLionel Sambuc         printf("Expected arguments:");
82*11be35a1SLionel Sambuc         for (exp_ptr = exp_array; *exp_ptr != NULL; exp_ptr++)
83*11be35a1SLionel Sambuc             printf(" '%s'", *exp_ptr);
84*11be35a1SLionel Sambuc         printf("\n");
85*11be35a1SLionel Sambuc     }
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc     {
88*11be35a1SLionel Sambuc         char **actual_ptr;
89*11be35a1SLionel Sambuc         printf("Returned arguments:");
90*11be35a1SLionel Sambuc         for (actual_ptr = actual_array; *actual_ptr != NULL; actual_ptr++)
91*11be35a1SLionel Sambuc             printf(" '%s'", *actual_ptr);
92*11be35a1SLionel Sambuc         printf("\n");
93*11be35a1SLionel Sambuc     }
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc     if (!equal_arrays(exp_array, actual_array))
96*11be35a1SLionel Sambuc         atf_tc_fail_nonfatal("The constructed argv differs from the "
97*11be35a1SLionel Sambuc                              "expected values");
98*11be35a1SLionel Sambuc }
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc static
101*11be35a1SLionel Sambuc void
verbose_set_env(const char * var,const char * val)102*11be35a1SLionel Sambuc verbose_set_env(const char *var, const char *val)
103*11be35a1SLionel Sambuc {
104*11be35a1SLionel Sambuc     printf("Setting %s to '%s'\n", var, val);
105*11be35a1SLionel Sambuc     RE(atf_env_set(var, val));
106*11be35a1SLionel Sambuc }
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
109*11be35a1SLionel Sambuc  * Internal test cases.
110*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc ATF_TC(equal_arrays);
ATF_TC_HEAD(equal_arrays,tc)113*11be35a1SLionel Sambuc ATF_TC_HEAD(equal_arrays, tc)
114*11be35a1SLionel Sambuc {
115*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the test case internal "
116*11be35a1SLionel Sambuc                       "equal_arrays function");
117*11be35a1SLionel Sambuc }
ATF_TC_BODY(equal_arrays,tc)118*11be35a1SLionel Sambuc ATF_TC_BODY(equal_arrays, tc)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc     {
121*11be35a1SLionel Sambuc         const char *const exp[] = { NULL };
122*11be35a1SLionel Sambuc         char *actual[] = { NULL };
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc         ATF_CHECK(equal_arrays(exp, actual));
125*11be35a1SLionel Sambuc     }
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc     {
128*11be35a1SLionel Sambuc         const char *const exp[] = { NULL };
129*11be35a1SLionel Sambuc         char *actual[2] = { strdup("foo"), NULL };
130*11be35a1SLionel Sambuc 
131*11be35a1SLionel Sambuc         ATF_CHECK(!equal_arrays(exp, actual));
132*11be35a1SLionel Sambuc         free(actual[0]);
133*11be35a1SLionel Sambuc     }
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc     {
136*11be35a1SLionel Sambuc         const char *const exp[] = { "foo", NULL };
137*11be35a1SLionel Sambuc         char *actual[] = { NULL };
138*11be35a1SLionel Sambuc 
139*11be35a1SLionel Sambuc         ATF_CHECK(!equal_arrays(exp, actual));
140*11be35a1SLionel Sambuc     }
141*11be35a1SLionel Sambuc 
142*11be35a1SLionel Sambuc     {
143*11be35a1SLionel Sambuc         const char *const exp[] = { "foo", NULL };
144*11be35a1SLionel Sambuc         char *actual[2] = { strdup("foo"), NULL };
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc         ATF_CHECK(equal_arrays(exp, actual));
147*11be35a1SLionel Sambuc         free(actual[0]);
148*11be35a1SLionel Sambuc     }
149*11be35a1SLionel Sambuc }
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
152*11be35a1SLionel Sambuc  * Test cases for the free functions.
153*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
154*11be35a1SLionel Sambuc 
155*11be35a1SLionel Sambuc ATF_TC(c_o);
ATF_TC_HEAD(c_o,tc)156*11be35a1SLionel Sambuc ATF_TC_HEAD(c_o, tc)
157*11be35a1SLionel Sambuc {
158*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the atf_build_c_o function");
159*11be35a1SLionel Sambuc }
ATF_TC_BODY(c_o,tc)160*11be35a1SLionel Sambuc ATF_TC_BODY(c_o, tc)
161*11be35a1SLionel Sambuc {
162*11be35a1SLionel Sambuc     struct c_o_test *test;
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc     for (test = c_o_tests; test->expargv[0] != NULL; test++) {
165*11be35a1SLionel Sambuc         printf("> Test: %s\n", test->msg);
166*11be35a1SLionel Sambuc 
167*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CC", test->cc);
168*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CFLAGS", test->cflags);
169*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
170*11be35a1SLionel Sambuc         __atf_config_reinit();
171*11be35a1SLionel Sambuc 
172*11be35a1SLionel Sambuc         {
173*11be35a1SLionel Sambuc             char **argv;
174*11be35a1SLionel Sambuc             if (test->hasoptargs)
175*11be35a1SLionel Sambuc                 RE(atf_build_c_o(test->sfile, test->ofile, test->optargs,
176*11be35a1SLionel Sambuc                                  &argv));
177*11be35a1SLionel Sambuc             else
178*11be35a1SLionel Sambuc                 RE(atf_build_c_o(test->sfile, test->ofile, NULL, &argv));
179*11be35a1SLionel Sambuc             check_equal_array(test->expargv, argv);
180*11be35a1SLionel Sambuc             atf_utils_free_charpp(argv);
181*11be35a1SLionel Sambuc         }
182*11be35a1SLionel Sambuc     }
183*11be35a1SLionel Sambuc }
184*11be35a1SLionel Sambuc 
185*11be35a1SLionel Sambuc ATF_TC(cpp);
ATF_TC_HEAD(cpp,tc)186*11be35a1SLionel Sambuc ATF_TC_HEAD(cpp, tc)
187*11be35a1SLionel Sambuc {
188*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cpp function");
189*11be35a1SLionel Sambuc }
ATF_TC_BODY(cpp,tc)190*11be35a1SLionel Sambuc ATF_TC_BODY(cpp, tc)
191*11be35a1SLionel Sambuc {
192*11be35a1SLionel Sambuc     struct cpp_test *test;
193*11be35a1SLionel Sambuc 
194*11be35a1SLionel Sambuc     for (test = cpp_tests; test->expargv[0] != NULL; test++) {
195*11be35a1SLionel Sambuc         printf("> Test: %s\n", test->msg);
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CPP", test->cpp);
198*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
199*11be35a1SLionel Sambuc         __atf_config_reinit();
200*11be35a1SLionel Sambuc 
201*11be35a1SLionel Sambuc         {
202*11be35a1SLionel Sambuc             char **argv;
203*11be35a1SLionel Sambuc             if (test->hasoptargs)
204*11be35a1SLionel Sambuc                 RE(atf_build_cpp(test->sfile, test->ofile, test->optargs,
205*11be35a1SLionel Sambuc                                  &argv));
206*11be35a1SLionel Sambuc             else
207*11be35a1SLionel Sambuc                 RE(atf_build_cpp(test->sfile, test->ofile, NULL, &argv));
208*11be35a1SLionel Sambuc             check_equal_array(test->expargv, argv);
209*11be35a1SLionel Sambuc             atf_utils_free_charpp(argv);
210*11be35a1SLionel Sambuc         }
211*11be35a1SLionel Sambuc     }
212*11be35a1SLionel Sambuc }
213*11be35a1SLionel Sambuc 
214*11be35a1SLionel Sambuc ATF_TC(cxx_o);
ATF_TC_HEAD(cxx_o,tc)215*11be35a1SLionel Sambuc ATF_TC_HEAD(cxx_o, tc)
216*11be35a1SLionel Sambuc {
217*11be35a1SLionel Sambuc     atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cxx_o function");
218*11be35a1SLionel Sambuc }
ATF_TC_BODY(cxx_o,tc)219*11be35a1SLionel Sambuc ATF_TC_BODY(cxx_o, tc)
220*11be35a1SLionel Sambuc {
221*11be35a1SLionel Sambuc     struct cxx_o_test *test;
222*11be35a1SLionel Sambuc 
223*11be35a1SLionel Sambuc     for (test = cxx_o_tests; test->expargv[0] != NULL; test++) {
224*11be35a1SLionel Sambuc         printf("> Test: %s\n", test->msg);
225*11be35a1SLionel Sambuc 
226*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CXX", test->cxx);
227*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CXXFLAGS", test->cxxflags);
228*11be35a1SLionel Sambuc         verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
229*11be35a1SLionel Sambuc         __atf_config_reinit();
230*11be35a1SLionel Sambuc 
231*11be35a1SLionel Sambuc         {
232*11be35a1SLionel Sambuc             char **argv;
233*11be35a1SLionel Sambuc             if (test->hasoptargs)
234*11be35a1SLionel Sambuc                 RE(atf_build_cxx_o(test->sfile, test->ofile, test->optargs,
235*11be35a1SLionel Sambuc                                    &argv));
236*11be35a1SLionel Sambuc             else
237*11be35a1SLionel Sambuc                 RE(atf_build_cxx_o(test->sfile, test->ofile, NULL, &argv));
238*11be35a1SLionel Sambuc             check_equal_array(test->expargv, argv);
239*11be35a1SLionel Sambuc             atf_utils_free_charpp(argv);
240*11be35a1SLionel Sambuc         }
241*11be35a1SLionel Sambuc     }
242*11be35a1SLionel Sambuc }
243*11be35a1SLionel Sambuc 
244*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
245*11be35a1SLionel Sambuc  * Tests cases for the header file.
246*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
247*11be35a1SLionel Sambuc 
248*11be35a1SLionel Sambuc HEADER_TC(include, "atf-c/build.h");
249*11be35a1SLionel Sambuc 
250*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
251*11be35a1SLionel Sambuc  * Main.
252*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
253*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)254*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
255*11be35a1SLionel Sambuc {
256*11be35a1SLionel Sambuc     /* Add the internal test cases. */
257*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, equal_arrays);
258*11be35a1SLionel Sambuc 
259*11be35a1SLionel Sambuc     /* Add the test cases for the free functions. */
260*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, c_o);
261*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cpp);
262*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, cxx_o);
263*11be35a1SLionel Sambuc 
264*11be35a1SLionel Sambuc     /* Add the test cases for the header file. */
265*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, include);
266*11be35a1SLionel Sambuc 
267*11be35a1SLionel Sambuc     return atf_no_error();
268*11be35a1SLionel Sambuc }
269