1275988Sngie/* Copyright (c) 2009 The NetBSD Foundation, Inc.
2240116Smarcel * All rights reserved.
3240116Smarcel *
4240116Smarcel * Redistribution and use in source and binary forms, with or without
5240116Smarcel * modification, are permitted provided that the following conditions
6240116Smarcel * are met:
7240116Smarcel * 1. Redistributions of source code must retain the above copyright
8240116Smarcel *    notice, this list of conditions and the following disclaimer.
9240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
10240116Smarcel *    notice, this list of conditions and the following disclaimer in the
11240116Smarcel *    documentation and/or other materials provided with the distribution.
12240116Smarcel *
13240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24275988Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25240116Smarcel
26275988Sngie#include "atf-c/build.h"
27275988Sngie
28240116Smarcel#include <stdio.h>
29240116Smarcel#include <stdlib.h>
30240116Smarcel#include <string.h>
31240116Smarcel
32240116Smarcel#include <atf-c.h>
33240116Smarcel
34275988Sngie#include "atf-c/detail/env.h"
35275988Sngie#include "atf-c/detail/test_helpers.h"
36275988Sngie#include "atf-c/h_build.h"
37240116Smarcel#include "atf-c/utils.h"
38240116Smarcel
39240116Smarcel/* ---------------------------------------------------------------------
40240116Smarcel * Auxiliary functions.
41240116Smarcel * --------------------------------------------------------------------- */
42240116Smarcel
43240116Smarcelstatic
44240116Smarcelbool
45240116Smarcelequal_arrays(const char *const *exp_array, char **actual_array)
46240116Smarcel{
47240116Smarcel    bool equal;
48240116Smarcel
49240116Smarcel    if (*exp_array == NULL && *actual_array == NULL)
50240116Smarcel        equal = true;
51240116Smarcel    else if (*exp_array == NULL || *actual_array == NULL)
52240116Smarcel        equal = false;
53240116Smarcel    else {
54240116Smarcel        equal = true;
55240116Smarcel        while (*actual_array != NULL) {
56240116Smarcel            if (*exp_array == NULL || strcmp(*exp_array, *actual_array) != 0) {
57240116Smarcel                equal = false;
58240116Smarcel                break;
59240116Smarcel            }
60240116Smarcel            exp_array++;
61240116Smarcel            actual_array++;
62240116Smarcel        }
63240116Smarcel    }
64240116Smarcel
65240116Smarcel    return equal;
66240116Smarcel}
67240116Smarcel
68240116Smarcelstatic
69240116Smarcelvoid
70240116Smarcelcheck_equal_array(const char *const *exp_array, char **actual_array)
71240116Smarcel{
72240116Smarcel    {
73240116Smarcel        const char *const *exp_ptr;
74240116Smarcel        printf("Expected arguments:");
75240116Smarcel        for (exp_ptr = exp_array; *exp_ptr != NULL; exp_ptr++)
76240116Smarcel            printf(" '%s'", *exp_ptr);
77240116Smarcel        printf("\n");
78240116Smarcel    }
79240116Smarcel
80240116Smarcel    {
81240116Smarcel        char **actual_ptr;
82240116Smarcel        printf("Returned arguments:");
83240116Smarcel        for (actual_ptr = actual_array; *actual_ptr != NULL; actual_ptr++)
84240116Smarcel            printf(" '%s'", *actual_ptr);
85240116Smarcel        printf("\n");
86240116Smarcel    }
87240116Smarcel
88240116Smarcel    if (!equal_arrays(exp_array, actual_array))
89240116Smarcel        atf_tc_fail_nonfatal("The constructed argv differs from the "
90240116Smarcel                             "expected values");
91240116Smarcel}
92240116Smarcel
93240116Smarcelstatic
94240116Smarcelvoid
95240116Smarcelverbose_set_env(const char *var, const char *val)
96240116Smarcel{
97240116Smarcel    printf("Setting %s to '%s'\n", var, val);
98240116Smarcel    RE(atf_env_set(var, val));
99240116Smarcel}
100240116Smarcel
101240116Smarcel/* ---------------------------------------------------------------------
102240116Smarcel * Internal test cases.
103240116Smarcel * --------------------------------------------------------------------- */
104240116Smarcel
105240116SmarcelATF_TC(equal_arrays);
106240116SmarcelATF_TC_HEAD(equal_arrays, tc)
107240116Smarcel{
108240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the test case internal "
109240116Smarcel                      "equal_arrays function");
110240116Smarcel}
111240116SmarcelATF_TC_BODY(equal_arrays, tc)
112240116Smarcel{
113240116Smarcel    {
114240116Smarcel        const char *const exp[] = { NULL };
115240116Smarcel        char *actual[] = { NULL };
116240116Smarcel
117240116Smarcel        ATF_CHECK(equal_arrays(exp, actual));
118240116Smarcel    }
119240116Smarcel
120240116Smarcel    {
121240116Smarcel        const char *const exp[] = { NULL };
122240116Smarcel        char *actual[2] = { strdup("foo"), NULL };
123240116Smarcel
124240116Smarcel        ATF_CHECK(!equal_arrays(exp, actual));
125240116Smarcel        free(actual[0]);
126240116Smarcel    }
127240116Smarcel
128240116Smarcel    {
129240116Smarcel        const char *const exp[] = { "foo", NULL };
130240116Smarcel        char *actual[] = { NULL };
131240116Smarcel
132240116Smarcel        ATF_CHECK(!equal_arrays(exp, actual));
133240116Smarcel    }
134240116Smarcel
135240116Smarcel    {
136240116Smarcel        const char *const exp[] = { "foo", NULL };
137240116Smarcel        char *actual[2] = { strdup("foo"), NULL };
138240116Smarcel
139240116Smarcel        ATF_CHECK(equal_arrays(exp, actual));
140240116Smarcel        free(actual[0]);
141240116Smarcel    }
142240116Smarcel}
143240116Smarcel
144240116Smarcel/* ---------------------------------------------------------------------
145240116Smarcel * Test cases for the free functions.
146240116Smarcel * --------------------------------------------------------------------- */
147240116Smarcel
148240116SmarcelATF_TC(c_o);
149240116SmarcelATF_TC_HEAD(c_o, tc)
150240116Smarcel{
151240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_build_c_o function");
152240116Smarcel}
153240116SmarcelATF_TC_BODY(c_o, tc)
154240116Smarcel{
155240116Smarcel    struct c_o_test *test;
156240116Smarcel
157240116Smarcel    for (test = c_o_tests; test->expargv[0] != NULL; test++) {
158240116Smarcel        printf("> Test: %s\n", test->msg);
159240116Smarcel
160240116Smarcel        verbose_set_env("ATF_BUILD_CC", test->cc);
161240116Smarcel        verbose_set_env("ATF_BUILD_CFLAGS", test->cflags);
162240116Smarcel        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
163240116Smarcel
164240116Smarcel        {
165240116Smarcel            char **argv;
166240116Smarcel            if (test->hasoptargs)
167240116Smarcel                RE(atf_build_c_o(test->sfile, test->ofile, test->optargs,
168240116Smarcel                                 &argv));
169240116Smarcel            else
170240116Smarcel                RE(atf_build_c_o(test->sfile, test->ofile, NULL, &argv));
171240116Smarcel            check_equal_array(test->expargv, argv);
172240116Smarcel            atf_utils_free_charpp(argv);
173240116Smarcel        }
174240116Smarcel    }
175240116Smarcel}
176240116Smarcel
177240116SmarcelATF_TC(cpp);
178240116SmarcelATF_TC_HEAD(cpp, tc)
179240116Smarcel{
180240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cpp function");
181240116Smarcel}
182240116SmarcelATF_TC_BODY(cpp, tc)
183240116Smarcel{
184240116Smarcel    struct cpp_test *test;
185240116Smarcel
186240116Smarcel    for (test = cpp_tests; test->expargv[0] != NULL; test++) {
187240116Smarcel        printf("> Test: %s\n", test->msg);
188240116Smarcel
189240116Smarcel        verbose_set_env("ATF_BUILD_CPP", test->cpp);
190240116Smarcel        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
191240116Smarcel
192240116Smarcel        {
193240116Smarcel            char **argv;
194240116Smarcel            if (test->hasoptargs)
195240116Smarcel                RE(atf_build_cpp(test->sfile, test->ofile, test->optargs,
196240116Smarcel                                 &argv));
197240116Smarcel            else
198240116Smarcel                RE(atf_build_cpp(test->sfile, test->ofile, NULL, &argv));
199240116Smarcel            check_equal_array(test->expargv, argv);
200240116Smarcel            atf_utils_free_charpp(argv);
201240116Smarcel        }
202240116Smarcel    }
203240116Smarcel}
204240116Smarcel
205240116SmarcelATF_TC(cxx_o);
206240116SmarcelATF_TC_HEAD(cxx_o, tc)
207240116Smarcel{
208240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cxx_o function");
209240116Smarcel}
210240116SmarcelATF_TC_BODY(cxx_o, tc)
211240116Smarcel{
212240116Smarcel    struct cxx_o_test *test;
213240116Smarcel
214240116Smarcel    for (test = cxx_o_tests; test->expargv[0] != NULL; test++) {
215240116Smarcel        printf("> Test: %s\n", test->msg);
216240116Smarcel
217240116Smarcel        verbose_set_env("ATF_BUILD_CXX", test->cxx);
218240116Smarcel        verbose_set_env("ATF_BUILD_CXXFLAGS", test->cxxflags);
219240116Smarcel        verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags);
220240116Smarcel
221240116Smarcel        {
222240116Smarcel            char **argv;
223240116Smarcel            if (test->hasoptargs)
224240116Smarcel                RE(atf_build_cxx_o(test->sfile, test->ofile, test->optargs,
225240116Smarcel                                   &argv));
226240116Smarcel            else
227240116Smarcel                RE(atf_build_cxx_o(test->sfile, test->ofile, NULL, &argv));
228240116Smarcel            check_equal_array(test->expargv, argv);
229240116Smarcel            atf_utils_free_charpp(argv);
230240116Smarcel        }
231240116Smarcel    }
232240116Smarcel}
233240116Smarcel
234240116Smarcel/* ---------------------------------------------------------------------
235240116Smarcel * Main.
236240116Smarcel * --------------------------------------------------------------------- */
237240116Smarcel
238240116SmarcelATF_TP_ADD_TCS(tp)
239240116Smarcel{
240240116Smarcel    /* Add the internal test cases. */
241240116Smarcel    ATF_TP_ADD_TC(tp, equal_arrays);
242240116Smarcel
243240116Smarcel    /* Add the test cases for the free functions. */
244240116Smarcel    ATF_TP_ADD_TC(tp, c_o);
245240116Smarcel    ATF_TP_ADD_TC(tp, cpp);
246240116Smarcel    ATF_TP_ADD_TC(tp, cxx_o);
247240116Smarcel
248240116Smarcel    return atf_no_error();
249240116Smarcel}
250