]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/atf/atf-c++/detail/test_helpers.cpp
MFC various fixes for the ATF tests.
[FreeBSD/stable/10.git] / contrib / atf / atf-c++ / detail / test_helpers.cpp
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 // Path to the directory containing the libatf-c tests, used to locate the
44 // process_helpers program.  If NULL (the default), the code will use a
45 // relative path.  Otherwise, the provided path will be used; this is so
46 // that we can locate the helpers binary if the installation uses a
47 // different layout than the one we provide (as is the case in FreeBSD).
48 #if defined(ATF_C_TESTS_BASE)
49 static const char* atf_c_tests_base = ATF_C_TESTS_BASE;
50 #else
51 static const char* atf_c_tests_base = NULL;
52 #endif
53 #undef ATF_C_TESTS_BASE
54
55 void
56 build_check_cxx_o_aux(const atf::fs::path& sfile, const char* failmsg,
57                       const bool expect_pass)
58 {
59     std::vector< std::string > optargs;
60     optargs.push_back("-I" + atf::config::get("atf_includedir"));
61     optargs.push_back("-Wall");
62     optargs.push_back("-Werror");
63
64     const bool result = atf::check::build_cxx_o(
65         sfile.str(), "test.o", atf::process::argv_array(optargs));
66     if ((expect_pass && !result) || (!expect_pass && result))
67         ATF_FAIL(failmsg);
68 }
69
70 void
71 build_check_cxx_o(const atf::tests::tc& tc, const char* sfile,
72                   const char* failmsg, const bool expect_pass)
73 {
74     const atf::fs::path sfilepath =
75         atf::fs::path(tc.get_config_var("srcdir")) / sfile;
76     build_check_cxx_o_aux(sfilepath, failmsg, expect_pass);
77 }
78
79 void
80 header_check(const char *hdrname)
81 {
82     std::ofstream srcfile("test.cpp");
83     ATF_REQUIRE(srcfile);
84     srcfile << "#include <" << hdrname << ">\n";
85     srcfile.close();
86
87     const std::string failmsg = std::string("Header check failed; ") +
88         hdrname + " is not self-contained";
89     build_check_cxx_o_aux(atf::fs::path("test.cpp"), failmsg.c_str(), true);
90 }
91
92 atf::fs::path
93 get_process_helpers_path(const atf::tests::tc& tc, bool is_detail)
94 {
95     const char* helper = "detail/process_helpers";
96     if (atf_c_tests_base == NULL) {
97         if (is_detail)
98             return atf::fs::path(tc.get_config_var("srcdir")) /
99                    ".." / ".." / "atf-c" / helper;
100         else
101             return atf::fs::path(tc.get_config_var("srcdir")) /
102                    ".." / "atf-c" / helper;
103     } else {
104         return atf::fs::path(atf_c_tests_base) / helper;
105     }
106 }
107
108 void
109 test_helpers_detail::check_equal(const char* expected[],
110                                  const string_vector& actual)
111 {
112     const char** expected_iter = expected;
113     string_vector::const_iterator actual_iter = actual.begin();
114
115     bool equals = true;
116     while (equals && *expected_iter != NULL && actual_iter != actual.end()) {
117         if (*expected_iter != *actual_iter) {
118             equals = false;
119         } else {
120             expected_iter++;
121             actual_iter++;
122         }
123     }
124     if (equals && ((*expected_iter == NULL && actual_iter != actual.end()) ||
125                    (*expected_iter != NULL && actual_iter == actual.end())))
126         equals = false;
127
128     if (!equals) {
129         std::cerr << "EXPECTED:\n";
130         for (expected_iter = expected; *expected_iter != NULL; expected_iter++)
131             std::cerr << *expected_iter << "\n";
132
133         std::cerr << "ACTUAL:\n";
134         for (actual_iter = actual.begin(); actual_iter != actual.end();
135              actual_iter++)
136             std::cerr << *actual_iter << "\n";
137
138         ATF_FAIL("Expected results differ to actual values");
139     }
140 }