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