build_test.c revision 256281
1186647Srwatson/* 2186647Srwatson * Automated Testing Framework (atf) 3186647Srwatson * 4186647Srwatson * Copyright (c) 2009 The NetBSD Foundation, Inc. 5186647Srwatson * All rights reserved. 6186647Srwatson * 7186647Srwatson * Redistribution and use in source and binary forms, with or without 8186647Srwatson * modification, are permitted provided that the following conditions 9186647Srwatson * are met: 10186647Srwatson * 1. Redistributions of source code must retain the above copyright 11186647Srwatson * notice, this list of conditions and the following disclaimer. 12186647Srwatson * 2. Redistributions in binary form must reproduce the above copyright 13186647Srwatson * notice, this list of conditions and the following disclaimer in the 14186647Srwatson * documentation and/or other materials provided with the distribution. 15186647Srwatson * 16186647Srwatson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17186647Srwatson * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18186647Srwatson * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19186647Srwatson * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20186647Srwatson * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21186647Srwatson * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22186647Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23186647Srwatson * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24186647Srwatson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25186647Srwatson * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26186647Srwatson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27186647Srwatson * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28186647Srwatson */ 29187214Srwatson 30186647Srwatson#include <stdio.h> 31186647Srwatson#include <stdlib.h> 32186647Srwatson#include <string.h> 33186647Srwatson 34186647Srwatson#include <atf-c.h> 35186647Srwatson 36186647Srwatson#include "atf-c/build.h" 37186647Srwatson#include "atf-c/config.h" 38186647Srwatson#include "atf-c/utils.h" 39186647Srwatson 40186647Srwatson#include "detail/env.h" 41186647Srwatson#include "detail/test_helpers.h" 42186647Srwatson#include "h_build.h" 43186647Srwatson 44186647Srwatson/* --------------------------------------------------------------------- 45187214Srwatson * Auxiliary functions. 46187214Srwatson * --------------------------------------------------------------------- */ 47187214Srwatson 48187214Srwatsonvoid __atf_config_reinit(void); 49187214Srwatson 50187214Srwatsonstatic 51187214Srwatsonbool 52187214Srwatsonequal_arrays(const char *const *exp_array, char **actual_array) 53187214Srwatson{ 54187214Srwatson bool equal; 55187214Srwatson 56187214Srwatson if (*exp_array == NULL && *actual_array == NULL) 57187214Srwatson equal = true; 58187214Srwatson else if (*exp_array == NULL || *actual_array == NULL) 59187214Srwatson equal = false; 60187214Srwatson else { 61187214Srwatson equal = true; 62187214Srwatson while (*actual_array != NULL) { 63187214Srwatson if (*exp_array == NULL || strcmp(*exp_array, *actual_array) != 0) { 64187214Srwatson equal = false; 65187214Srwatson break; 66187214Srwatson } 67187214Srwatson exp_array++; 68187214Srwatson actual_array++; 69187214Srwatson } 70187214Srwatson } 71187214Srwatson 72187214Srwatson return equal; 73187214Srwatson} 74187214Srwatson 75187214Srwatsonstatic 76187214Srwatsonvoid 77187214Srwatsoncheck_equal_array(const char *const *exp_array, char **actual_array) 78187214Srwatson{ 79187214Srwatson { 80187214Srwatson const char *const *exp_ptr; 81187214Srwatson printf("Expected arguments:"); 82187214Srwatson for (exp_ptr = exp_array; *exp_ptr != NULL; exp_ptr++) 83187214Srwatson printf(" '%s'", *exp_ptr); 84187214Srwatson printf("\n"); 85187214Srwatson } 86187214Srwatson 87187214Srwatson { 88187214Srwatson char **actual_ptr; 89187214Srwatson printf("Returned arguments:"); 90187214Srwatson for (actual_ptr = actual_array; *actual_ptr != NULL; actual_ptr++) 91187214Srwatson printf(" '%s'", *actual_ptr); 92187214Srwatson printf("\n"); 93187214Srwatson } 94187214Srwatson 95187214Srwatson if (!equal_arrays(exp_array, actual_array)) 96187214Srwatson atf_tc_fail_nonfatal("The constructed argv differs from the " 97187214Srwatson "expected values"); 98187214Srwatson} 99187214Srwatson 100187214Srwatsonstatic 101187214Srwatsonvoid 102187214Srwatsonverbose_set_env(const char *var, const char *val) 103187214Srwatson{ 104187214Srwatson printf("Setting %s to '%s'\n", var, val); 105187214Srwatson RE(atf_env_set(var, val)); 106187214Srwatson} 107187214Srwatson 108187214Srwatson/* --------------------------------------------------------------------- 109187214Srwatson * Internal test cases. 110187214Srwatson * --------------------------------------------------------------------- */ 111187214Srwatson 112187214SrwatsonATF_TC(equal_arrays); 113187214SrwatsonATF_TC_HEAD(equal_arrays, tc) 114187214Srwatson{ 115187214Srwatson atf_tc_set_md_var(tc, "descr", "Tests the test case internal " 116187214Srwatson "equal_arrays function"); 117187214Srwatson} 118187214SrwatsonATF_TC_BODY(equal_arrays, tc) 119187214Srwatson{ 120187214Srwatson { 121187214Srwatson const char *const exp[] = { NULL }; 122187214Srwatson char *actual[] = { NULL }; 123187214Srwatson 124187214Srwatson ATF_CHECK(equal_arrays(exp, actual)); 125187214Srwatson } 126187214Srwatson 127187214Srwatson { 128187214Srwatson const char *const exp[] = { NULL }; 129187214Srwatson char *actual[2] = { strdup("foo"), NULL }; 130187214Srwatson 131187214Srwatson ATF_CHECK(!equal_arrays(exp, actual)); 132187214Srwatson free(actual[0]); 133187214Srwatson } 134187214Srwatson 135187214Srwatson { 136187214Srwatson const char *const exp[] = { "foo", NULL }; 137187214Srwatson char *actual[] = { NULL }; 138187214Srwatson 139187214Srwatson ATF_CHECK(!equal_arrays(exp, actual)); 140187214Srwatson } 141187214Srwatson 142187214Srwatson { 143187214Srwatson const char *const exp[] = { "foo", NULL }; 144187214Srwatson char *actual[2] = { strdup("foo"), NULL }; 145187214Srwatson 146187214Srwatson ATF_CHECK(equal_arrays(exp, actual)); 147187214Srwatson free(actual[0]); 148187214Srwatson } 149187214Srwatson} 150187214Srwatson 151187214Srwatson/* --------------------------------------------------------------------- 152187214Srwatson * Test cases for the free functions. 153187214Srwatson * --------------------------------------------------------------------- */ 154187214Srwatson 155187214SrwatsonATF_TC(c_o); 156187214SrwatsonATF_TC_HEAD(c_o, tc) 157187214Srwatson{ 158187214Srwatson atf_tc_set_md_var(tc, "descr", "Tests the atf_build_c_o function"); 159187214Srwatson} 160187214SrwatsonATF_TC_BODY(c_o, tc) 161187214Srwatson{ 162187214Srwatson struct c_o_test *test; 163187214Srwatson 164187214Srwatson for (test = c_o_tests; test->expargv[0] != NULL; test++) { 165187214Srwatson printf("> Test: %s\n", test->msg); 166187214Srwatson 167186647Srwatson verbose_set_env("ATF_BUILD_CC", test->cc); 168186647Srwatson verbose_set_env("ATF_BUILD_CFLAGS", test->cflags); 169186647Srwatson verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags); 170186647Srwatson __atf_config_reinit(); 171186647Srwatson 172186647Srwatson { 173186647Srwatson char **argv; 174186647Srwatson if (test->hasoptargs) 175187214Srwatson RE(atf_build_c_o(test->sfile, test->ofile, test->optargs, 176187214Srwatson &argv)); 177187214Srwatson else 178187214Srwatson RE(atf_build_c_o(test->sfile, test->ofile, NULL, &argv)); 179187214Srwatson check_equal_array(test->expargv, argv); 180187214Srwatson atf_utils_free_charpp(argv); 181187214Srwatson } 182187214Srwatson } 183187214Srwatson} 184187214Srwatson 185187214SrwatsonATF_TC(cpp); 186187214SrwatsonATF_TC_HEAD(cpp, tc) 187187214Srwatson{ 188187214Srwatson atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cpp function"); 189187214Srwatson} 190187214SrwatsonATF_TC_BODY(cpp, tc) 191187214Srwatson{ 192187214Srwatson struct cpp_test *test; 193187214Srwatson 194187214Srwatson for (test = cpp_tests; test->expargv[0] != NULL; test++) { 195187214Srwatson printf("> Test: %s\n", test->msg); 196187214Srwatson 197187214Srwatson verbose_set_env("ATF_BUILD_CPP", test->cpp); 198187214Srwatson verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags); 199187214Srwatson __atf_config_reinit(); 200187214Srwatson 201187214Srwatson { 202187214Srwatson char **argv; 203187214Srwatson if (test->hasoptargs) 204187214Srwatson RE(atf_build_cpp(test->sfile, test->ofile, test->optargs, 205187214Srwatson &argv)); 206187214Srwatson else 207187214Srwatson RE(atf_build_cpp(test->sfile, test->ofile, NULL, &argv)); 208219128Srwatson check_equal_array(test->expargv, argv); 209219128Srwatson atf_utils_free_charpp(argv); 210186647Srwatson } 211186647Srwatson } 212186647Srwatson} 213186647Srwatson 214186647SrwatsonATF_TC(cxx_o); 215187214SrwatsonATF_TC_HEAD(cxx_o, tc) 216186647Srwatson{ 217186647Srwatson atf_tc_set_md_var(tc, "descr", "Tests the atf_build_cxx_o function"); 218} 219ATF_TC_BODY(cxx_o, tc) 220{ 221 struct cxx_o_test *test; 222 223 for (test = cxx_o_tests; test->expargv[0] != NULL; test++) { 224 printf("> Test: %s\n", test->msg); 225 226 verbose_set_env("ATF_BUILD_CXX", test->cxx); 227 verbose_set_env("ATF_BUILD_CXXFLAGS", test->cxxflags); 228 verbose_set_env("ATF_BUILD_CPPFLAGS", test->cppflags); 229 __atf_config_reinit(); 230 231 { 232 char **argv; 233 if (test->hasoptargs) 234 RE(atf_build_cxx_o(test->sfile, test->ofile, test->optargs, 235 &argv)); 236 else 237 RE(atf_build_cxx_o(test->sfile, test->ofile, NULL, &argv)); 238 check_equal_array(test->expargv, argv); 239 atf_utils_free_charpp(argv); 240 } 241 } 242} 243 244/* --------------------------------------------------------------------- 245 * Tests cases for the header file. 246 * --------------------------------------------------------------------- */ 247 248HEADER_TC(include, "atf-c/build.h"); 249 250/* --------------------------------------------------------------------- 251 * Main. 252 * --------------------------------------------------------------------- */ 253 254ATF_TP_ADD_TCS(tp) 255{ 256 /* Add the internal test cases. */ 257 ATF_TP_ADD_TC(tp, equal_arrays); 258 259 /* Add the test cases for the free functions. */ 260 ATF_TP_ADD_TC(tp, c_o); 261 ATF_TP_ADD_TC(tp, cpp); 262 ATF_TP_ADD_TC(tp, cxx_o); 263 264 /* Add the test cases for the header file. */ 265 ATF_TP_ADD_TC(tp, include); 266 267 return atf_no_error(); 268} 269