]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/atf/atf-c++/detail/test_helpers.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 extern "C" {
31 #include <regex.h>
32 }
33
34 #include <fstream>
35 #include <iostream>
36 #include <string>
37 #include <vector>
38
39 #include "../check.hpp"
40 #include "../config.hpp"
41 #include "../macros.hpp"
42
43 #include "fs.hpp"
44 #include "process.hpp"
45 #include "test_helpers.hpp"
46
47 void
48 build_check_cxx_o_aux(const atf::fs::path& sfile, const char* failmsg,
49                       const bool expect_pass)
50 {
51     std::vector< std::string > optargs;
52     optargs.push_back("-I" + atf::config::get("atf_includedir"));
53     optargs.push_back("-Wall");
54     optargs.push_back("-Werror");
55
56     const bool result = atf::check::build_cxx_o(
57         sfile.str(), "test.o", atf::process::argv_array(optargs));
58     if ((expect_pass && !result) || (!expect_pass && result))
59         ATF_FAIL(failmsg);
60 }
61
62 void
63 build_check_cxx_o(const atf::tests::tc& tc, const char* sfile,
64                   const char* failmsg, const bool expect_pass)
65 {
66     const atf::fs::path sfilepath =
67         atf::fs::path(tc.get_config_var("srcdir")) / sfile;
68     build_check_cxx_o_aux(sfilepath, failmsg, expect_pass);
69 }
70
71 void
72 header_check(const char *hdrname)
73 {
74     std::ofstream srcfile("test.c");
75     ATF_REQUIRE(srcfile);
76     srcfile << "#include <" << hdrname << ">\n";
77     srcfile.close();
78
79     const std::string failmsg = std::string("Header check failed; ") +
80         hdrname + " is not self-contained";
81     build_check_cxx_o_aux(atf::fs::path("test.c"), failmsg.c_str(), true);
82 }
83
84 atf::fs::path
85 get_process_helpers_path(const atf::tests::tc& tc, bool is_detail)
86 {
87     if (is_detail)
88         return atf::fs::path(tc.get_config_var("srcdir")) /
89                ".." / ".." / "atf-c" / "detail" / "process_helpers";
90     else
91         return atf::fs::path(tc.get_config_var("srcdir")) /
92                ".." / "atf-c" / "detail" / "process_helpers";
93 }
94
95 bool
96 grep_file(const char* name, const char* regex)
97 {
98     std::ifstream is(name);
99     ATF_REQUIRE(is);
100
101     bool found = false;
102
103     std::string line;
104     std::getline(is, line);
105     while (!found && is.good()) {
106         if (grep_string(line, regex))
107             found = true;
108         else
109             std::getline(is, line);
110     }
111
112     return found;
113 }
114
115 bool
116 grep_string(const std::string& str, const char* regex)
117 {
118     int res;
119     regex_t preg;
120
121     std::cout << "Looking for '" << regex << "' in '" << str << "'\n";
122     ATF_REQUIRE(::regcomp(&preg, regex, REG_EXTENDED) == 0);
123
124     res = ::regexec(&preg, str.c_str(), 0, NULL, 0);
125     ATF_REQUIRE(res == 0 || res == REG_NOMATCH);
126
127     ::regfree(&preg);
128
129     return res == 0;
130 }
131
132 void
133 test_helpers_detail::check_equal(const char* expected[],
134                                  const string_vector& actual)
135 {
136     const char** expected_iter = expected;
137     string_vector::const_iterator actual_iter = actual.begin();
138
139     bool equals = true;
140     while (equals && *expected_iter != NULL && actual_iter != actual.end()) {
141         if (*expected_iter != *actual_iter) {
142             equals = false;
143         } else {
144             expected_iter++;
145             actual_iter++;
146         }
147     }
148     if (equals && ((*expected_iter == NULL && actual_iter != actual.end()) ||
149                    (*expected_iter != NULL && actual_iter == actual.end())))
150         equals = false;
151
152     if (!equals) {
153         std::cerr << "EXPECTED:\n";
154         for (expected_iter = expected; *expected_iter != NULL; expected_iter++)
155             std::cerr << *expected_iter << "\n";
156
157         std::cerr << "ACTUAL:\n";
158         for (actual_iter = actual.begin(); actual_iter != actual.end();
159              actual_iter++)
160             std::cerr << *actual_iter << "\n";
161
162         ATF_FAIL("Expected results differ to actual values");
163     }
164 }