xref: /minix3/external/bsd/bind/dist/unit/atf-src/atf-c++/detail/test_helpers.cpp (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
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 <fstream>
31 #include <iostream>
32 #include <string>
33 #include <vector>
34 
35 #include "../check.hpp"
36 #include "../config.hpp"
37 #include "../macros.hpp"
38 
39 #include "fs.hpp"
40 #include "process.hpp"
41 #include "test_helpers.hpp"
42 
43 void
build_check_cxx_o_aux(const atf::fs::path & sfile,const char * failmsg,const bool expect_pass)44 build_check_cxx_o_aux(const atf::fs::path& sfile, const char* failmsg,
45                       const bool expect_pass)
46 {
47     std::vector< std::string > optargs;
48     optargs.push_back("-I" + atf::config::get("atf_includedir"));
49     optargs.push_back("-Wall");
50     optargs.push_back("-Werror");
51 
52     const bool result = atf::check::build_cxx_o(
53         sfile.str(), "test.o", atf::process::argv_array(optargs));
54     if ((expect_pass && !result) || (!expect_pass && result))
55         ATF_FAIL(failmsg);
56 }
57 
58 void
build_check_cxx_o(const atf::tests::tc & tc,const char * sfile,const char * failmsg,const bool expect_pass)59 build_check_cxx_o(const atf::tests::tc& tc, const char* sfile,
60                   const char* failmsg, const bool expect_pass)
61 {
62     const atf::fs::path sfilepath =
63         atf::fs::path(tc.get_config_var("srcdir")) / sfile;
64     build_check_cxx_o_aux(sfilepath, failmsg, expect_pass);
65 }
66 
67 void
header_check(const char * hdrname)68 header_check(const char *hdrname)
69 {
70     std::ofstream srcfile("test.c");
71     ATF_REQUIRE(srcfile);
72     srcfile << "#include <" << hdrname << ">\n";
73     srcfile.close();
74 
75     const std::string failmsg = std::string("Header check failed; ") +
76         hdrname + " is not self-contained";
77     build_check_cxx_o_aux(atf::fs::path("test.c"), failmsg.c_str(), true);
78 }
79 
80 atf::fs::path
get_process_helpers_path(const atf::tests::tc & tc)81 get_process_helpers_path(const atf::tests::tc& tc)
82 {
83     return atf::fs::path(tc.get_config_var("srcdir")) /
84            ".." / "atf-c" / "detail" / "process_helpers";
85 }
86 
87 void
check_equal(const char * expected[],const string_vector & actual)88 test_helpers_detail::check_equal(const char* expected[],
89                                  const string_vector& actual)
90 {
91     const char** expected_iter = expected;
92     string_vector::const_iterator actual_iter = actual.begin();
93 
94     bool equals = true;
95     while (equals && *expected_iter != NULL && actual_iter != actual.end()) {
96         if (*expected_iter != *actual_iter) {
97             equals = false;
98         } else {
99             expected_iter++;
100             actual_iter++;
101         }
102     }
103     if (equals && ((*expected_iter == NULL && actual_iter != actual.end()) ||
104                    (*expected_iter != NULL && actual_iter == actual.end())))
105         equals = false;
106 
107     if (!equals) {
108         std::cerr << "EXPECTED:\n";
109         for (expected_iter = expected; *expected_iter != NULL; expected_iter++)
110             std::cerr << *expected_iter << "\n";
111 
112         std::cerr << "ACTUAL:\n";
113         for (actual_iter = actual.begin(); actual_iter != actual.end();
114              actual_iter++)
115             std::cerr << *actual_iter << "\n";
116 
117         ATF_FAIL("Expected results differ to actual values");
118     }
119 }
120