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