diff options
Diffstat (limited to 'atf-c++')
-rw-r--r-- | atf-c++/Makefile.am.inc | 3 | ||||
-rw-r--r-- | atf-c++/atf-c++-api.3 | 236 | ||||
-rw-r--r-- | atf-c++/atf-c++.m4 | 48 | ||||
-rw-r--r-- | atf-c++/atf-c++.pc.in | 11 | ||||
-rw-r--r-- | atf-c++/check.hpp | 4 | ||||
-rw-r--r-- | atf-c++/check_test.cpp | 38 | ||||
-rw-r--r-- | atf-c++/detail/Atffile | 1 | ||||
-rw-r--r-- | atf-c++/detail/Kyuafile | 1 | ||||
-rw-r--r-- | atf-c++/detail/Makefile.am.inc | 5 | ||||
-rw-r--r-- | atf-c++/detail/auto_array.hpp | 179 | ||||
-rw-r--r-- | atf-c++/detail/auto_array_test.cpp | 304 | ||||
-rw-r--r-- | atf-c++/detail/process.cpp | 4 | ||||
-rw-r--r-- | atf-c++/detail/process.hpp | 5 | ||||
-rw-r--r-- | atf-c++/detail/test_helpers.cpp | 41 | ||||
-rw-r--r-- | atf-c++/detail/test_helpers.hpp | 2 | ||||
-rw-r--r-- | atf-c++/macros_test.cpp | 47 | ||||
-rw-r--r-- | atf-c++/noncopyable.hpp | 56 | ||||
-rw-r--r-- | atf-c++/tests.cpp | 7 | ||||
-rw-r--r-- | atf-c++/tests.hpp | 4 | ||||
-rw-r--r-- | atf-c++/utils.cpp | 104 | ||||
-rw-r--r-- | atf-c++/utils.hpp | 180 | ||||
-rw-r--r-- | atf-c++/utils_test.cpp | 567 |
22 files changed, 1394 insertions, 453 deletions
diff --git a/atf-c++/Makefile.am.inc b/atf-c++/Makefile.am.inc index 23d497dde2e05..2d968026b391f 100644 --- a/atf-c++/Makefile.am.inc +++ b/atf-c++/Makefile.am.inc @@ -38,8 +38,10 @@ libatf_c___la_SOURCES = atf-c++/build.cpp \ atf-c++/config.cpp \ atf-c++/config.hpp \ atf-c++/macros.hpp \ + atf-c++/noncopyable.hpp \ atf-c++/tests.cpp \ atf-c++/tests.hpp \ + atf-c++/utils.cpp \ atf-c++/utils.hpp libatf_c___la_LDFLAGS = -version-info 0:0:0 @@ -48,6 +50,7 @@ atf_c___HEADERS = atf-c++/build.hpp \ atf-c++/check.hpp \ atf-c++/config.hpp \ atf-c++/macros.hpp \ + atf-c++/noncopyable.hpp \ atf-c++/tests.hpp \ atf-c++/utils.hpp atf_c__dir = $(includedir)/atf-c++ diff --git a/atf-c++/atf-c++-api.3 b/atf-c++/atf-c++-api.3 index 5d6618b4eb32a..d0579a9a75441 100644 --- a/atf-c++/atf-c++-api.3 +++ b/atf-c++/atf-c++-api.3 @@ -26,10 +26,11 @@ .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd January 21, 2012 +.Dd November 30, 2012 .Dt ATF-C++-API 3 .Os .Sh NAME +.Nm atf-c++-api , .Nm ATF_ADD_TEST_CASE , .Nm ATF_CHECK_ERRNO , .Nm ATF_FAIL , @@ -52,6 +53,17 @@ .Nm ATF_TEST_CASE_USE , .Nm ATF_TEST_CASE_WITH_CLEANUP , .Nm ATF_TEST_CASE_WITHOUT_HEAD , +.Nm atf::utils::cat_file , +.Nm atf::utils::compare_file , +.Nm atf::utils::copy_file , +.Nm atf::utils::create_file , +.Nm atf::utils::file_exists , +.Nm atf::utils::fork , +.Nm atf::utils::grep_collection , +.Nm atf::utils::grep_file , +.Nm atf::utils::grep_string , +.Nm atf::utils::redirect , +.Nm atf::utils::wait .Nd C++ API to write ATF-based test programs .Sh SYNOPSIS .In atf-c++.hpp @@ -77,6 +89,61 @@ .Fn ATF_TEST_CASE_USE "name" .Fn ATF_TEST_CASE_WITH_CLEANUP "name" .Fn ATF_TEST_CASE_WITHOUT_HEAD "name" +.Ft void +.Fo atf::utils::cat_file +.Fa "const std::string& path" +.Fa "const std::string& prefix" +.Fc +.Ft bool +.Fo atf::utils::compare_file +.Fa "const std::string& path" +.Fa "const std::string& contents" +.Fc +.Ft void +.Fo atf::utils::copy_file +.Fa "const std::string& source" +.Fa "const std::string& destination" +.Fc +.Ft void +.Fo atf::utils::create_file +.Fa "const std::string& path" +.Fa "const std::string& contents" +.Fc +.Ft void +.Fo atf::utils::file_exists +.Fa "const std::string& path" +.Fc +.Ft pid_t +.Fo atf::utils::fork +.Fa "void" +.Fc +.Ft bool +.Fo atf::utils::grep_collection +.Fa "const std::string& regexp" +.Fa "const Collection& collection" +.Fc +.Ft bool +.Fo atf::utils::grep_file +.Fa "const std::string& regexp" +.Fa "const std::string& path" +.Fc +.Ft bool +.Fo atf::utils::grep_string +.Fa "const std::string& regexp" +.Fa "const std::string& path" +.Fc +.Ft void +.Fo atf::utils::redirect +.Fa "const int fd" +.Fa "const std::string& path" +.Fc +.Ft void +.Fo atf::utils::wait +.Fa "const pid_t pid" +.Fa "const int expected_exit_status" +.Fa "const std::string& expected_stdout" +.Fa "const std::string& expected_stderr" +.Fc .Sh DESCRIPTION ATF provides a mostly-macro-based programming interface to implement test programs in C or C++. @@ -205,7 +272,7 @@ The first parameter of this macro matches the name you provided in the former call. .Ss Header definitions The test case's header can define the meta-data by using the -.Fn set +.Fn set_md_var method, which takes two parameters: the first one specifies the meta-data variable to be set and the second one specifies its value. Both of them are strings. @@ -348,7 +415,7 @@ in the collection. .Fn ATF_REQUIRE_THROW takes the name of an exception and a statement and raises a failure if the statement does not throw the specified exception. -.Fn ATF_REQUIRE_THROW_EQ +.Fn ATF_REQUIRE_THROW_RE takes the name of an exception, a regular expresion and a statement and raises a failure if the statement does not throw the specified exception and if the message of the exception does not match the regular expression. @@ -362,6 +429,163 @@ variable and, second, a boolean expression that, if evaluates to true, means that a call failed and .Va errno has to be checked against the first value. +.Ss Utility functions +The following functions are provided as part of the +.Nm +API to simplify the creation of a variety of tests. +In particular, these are useful to write tests for command-line interfaces. +.Pp +.Ft void +.Fo atf::utils::cat_file +.Fa "const std::string& path" +.Fa "const std::string& prefix" +.Fc +.Bd -offset indent +Prints the contents of +.Fa path +to the standard output, prefixing every line with the string in +.Fa prefix . +.Ed +.Pp +.Ft bool +.Fo atf::utils::compare_file +.Fa "const std::string& path" +.Fa "const std::string& contents" +.Fc +.Bd -offset indent +Returns true if the given +.Fa path +matches exactly the expected inlined +.Fa contents . +.Ed +.Pp +.Ft void +.Fo atf::utils::copy_file +.Fa "const std::string& source" +.Fa "const std::string& destination" +.Fc +.Bd -offset indent +Copies the file +.Fa source +to +.Fa destination . +The permissions of the file are preserved during the code. +.Ed +.Pp +.Ft void +.Fo atf::utils::create_file +.Fa "const std::string& path" +.Fa "const std::string& contents" +.Fc +.Bd -offset indent +Creates +.Fa file +with the text given in +.Fa contents . +.Ed +.Pp +.Ft void +.Fo atf::utils::file_exists +.Fa "const std::string& path" +.Fc +.Bd -offset indent +Checks if +.Fa path +exists. +.Ed +.Pp +.Ft pid_t +.Fo atf::utils::fork +.Fa "void" +.Fc +.Bd -offset indent +Forks a process and redirects the standard output and standard error of the +child to files for later validation with +.Fn atf::utils::wait . +Fails the test case if the fork fails, so this does not return an error. +.Ed +.Pp +.Ft bool +.Fo atf::utils::grep_collection +.Fa "const std::string& regexp" +.Fa "const Collection& collection" +.Fc +.Bd -offset indent +Searches for the regular expression +.Fa regexp +in any of the strings contained in the +.Fa collection . +This is a template that accepts any one-dimensional container of strings. +.Ed +.Pp +.Ft bool +.Fo atf::utils::grep_file +.Fa "const std::string& regexp" +.Fa "const std::string& path" +.Fc +.Bd -offset indent +Searches for the regular expression +.Fa regexp +in the file +.Fa path . +The variable arguments are used to construct the regular expression. +.Ed +.Pp +.Ft bool +.Fo atf::utils::grep_string +.Fa "const std::string& regexp" +.Fa "const std::string& str" +.Fc +.Bd -offset indent +Searches for the regular expression +.Fa regexp +in the string +.Fa str . +.Ed +.Ft void +.Fo atf::utils::redirect +.Fa "const int fd" +.Fa "const std::string& path" +.Fc +.Bd -offset indent +Redirects the given file descriptor +.Fa fd +to the file +.Fa path . +This function exits the process in case of an error and does not properly mark +the test case as failed. +As a result, it should only be used in subprocesses of the test case; specially +those spawned by +.Fn atf::utils::fork . +.Ed +.Pp +.Ft void +.Fo atf::utils::wait +.Fa "const pid_t pid" +.Fa "const int expected_exit_status" +.Fa "const std::string& expected_stdout" +.Fa "const std::string& expected_stderr" +.Fc +.Bd -offset indent +Waits and validates the result of a subprocess spawned with +.Fn atf::utils::wait . +The validation involves checking that the subprocess exited cleanly and returned +the code specified in +.Fa expected_exit_status +and that its standard output and standard error match the strings given in +.Fa expected_stdout +and +.Fa expected_stderr . +.Pp +If any of the +.Fa expected_stdout +or +.Fa expected_stderr +strings are prefixed with +.Sq save: , +then they specify the name of the file into which to store the stdout or stderr +of the subprocess, and no comparison is performed. +.Ed .Sh EXAMPLES The following shows a complete test program with a single test case that validates the addition operator: @@ -371,7 +595,7 @@ validates the addition operator: ATF_TEST_CASE(addition); ATF_TEST_CASE_HEAD(addition) { - set("descr", "Sample tests for the addition operator"); + set_md_var("descr", "Sample tests for the addition operator"); } ATF_TEST_CASE_BODY(addition) { @@ -387,7 +611,7 @@ ATF_TEST_CASE_BODY(addition) ATF_TEST_CASE(open_failure); ATF_TEST_CASE_HEAD(open_failure) { - set("descr", "Sample tests for the open function"); + set_md_var("descr", "Sample tests for the open function"); } ATF_TEST_CASE_BODY(open_failure) { @@ -397,7 +621,7 @@ ATF_TEST_CASE_BODY(open_failure) ATF_TEST_CASE(known_bug); ATF_TEST_CASE_HEAD(known_bug) { - set("descr", "Reproduces a known bug"); + set_md_var("descr", "Reproduces a known bug"); } ATF_TEST_CASE_BODY(known_bug) { diff --git a/atf-c++/atf-c++.m4 b/atf-c++/atf-c++.m4 new file mode 100644 index 0000000000000..0763d04865bc8 --- /dev/null +++ b/atf-c++/atf-c++.m4 @@ -0,0 +1,48 @@ +dnl +dnl Automated Testing Framework (atf) +dnl +dnl Copyright 2011 Google Inc. +dnl All rights reserved. +dnl +dnl Redistribution and use in source and binary forms, with or without +dnl modification, are permitted provided that the following conditions are +dnl met: +dnl +dnl * Redistributions of source code must retain the above copyright +dnl notice, this list of conditions and the following disclaimer. +dnl * Redistributions in binary form must reproduce the above copyright +dnl notice, this list of conditions and the following disclaimer in the +dnl documentation and/or other materials provided with the distribution. +dnl * Neither the name of Google Inc. nor the names of its contributors +dnl may be used to endorse or promote products derived from this software +dnl without specific prior written permission. +dnl +dnl THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +dnl "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +dnl LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +dnl A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +dnl OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +dnl SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +dnl LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +dnl DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +dnl THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +dnl (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +dnl OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +dnl + +dnl ATF_CHECK_CXX([version-spec]) +dnl +dnl Checks if atf-c++ is present. If version-spec is provided, ensures that +dnl the installed version of atf-sh matches the required version. This +dnl argument must be something like '>= 0.14' and accepts any version +dnl specification supported by pkg-config. +dnl +dnl Defines and substitutes ATF_CXX_CFLAGS and ATF_CXX_LIBS with the compiler +dnl and linker flags need to build against atf-c++. +AC_DEFUN([ATF_CHECK_CXX], [ + spec="atf-c++[]m4_default_nblank([ $1], [])" + _ATF_CHECK_ARG_WITH( + [PKG_CHECK_MODULES([ATF_CXX], [${spec}], + [found=yes found_atf_cxx=yes], [found=no])], + [required ${spec} not found]) +]) diff --git a/atf-c++/atf-c++.pc.in b/atf-c++/atf-c++.pc.in new file mode 100644 index 0000000000000..f366bb016d04e --- /dev/null +++ b/atf-c++/atf-c++.pc.in @@ -0,0 +1,11 @@ +# ATF pkg-config file + +cxx=__CXX__ +includedir=__INCLUDEDIR__ +libdir=__LIBDIR__ + +Name: atf-c++ +Description: Automated Testing Framework (C++ binding) +Version: __ATF_VERSION__ +Cflags: -I${includedir} +Libs: -L${libdir} -latf-c++ -latf-c diff --git a/atf-c++/check.hpp b/atf-c++/check.hpp index 055dd4f7209fa..0623529c745a5 100644 --- a/atf-c++/check.hpp +++ b/atf-c++/check.hpp @@ -39,7 +39,7 @@ extern "C" { #include <string> #include <vector> -#include <atf-c++/utils.hpp> +#include <atf-c++/noncopyable.hpp> namespace atf { @@ -60,7 +60,7 @@ namespace check { //! of executing arbitrary command and manages files containing //! its output. //! -class check_result : utils::noncopyable { +class check_result : noncopyable { //! //! \brief Internal representation of a result. //! diff --git a/atf-c++/check_test.cpp b/atf-c++/check_test.cpp index fd528e9d31a76..8e5983fdc6375 100644 --- a/atf-c++/check_test.cpp +++ b/atf-c++/check_test.cpp @@ -193,15 +193,15 @@ ATF_TEST_CASE_BODY(build_c_o) { ATF_TEST_CASE_USE(h_build_c_o_ok); run_h_tc< ATF_TEST_CASE_NAME(h_build_c_o_ok) >(); - ATF_REQUIRE(grep_file("stdout", "-o test.o")); - ATF_REQUIRE(grep_file("stdout", "-c test.c")); + ATF_REQUIRE(atf::utils::grep_file("-o test.o", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("-c test.c", "stdout")); ATF_TEST_CASE_USE(h_build_c_o_fail); run_h_tc< ATF_TEST_CASE_NAME(h_build_c_o_fail) >(); - ATF_REQUIRE(grep_file("stdout", "-o test.o")); - ATF_REQUIRE(grep_file("stdout", "-c test.c")); - ATF_REQUIRE(grep_file("stderr", "test.c")); - ATF_REQUIRE(grep_file("stderr", "UNDEFINED_SYMBOL")); + ATF_REQUIRE(atf::utils::grep_file("-o test.o", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("-c test.c", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("test.c", "stderr")); + ATF_REQUIRE(atf::utils::grep_file("UNDEFINED_SYMBOL", "stderr")); } ATF_TEST_CASE(build_cpp); @@ -213,16 +213,16 @@ ATF_TEST_CASE_BODY(build_cpp) { ATF_TEST_CASE_USE(h_build_cpp_ok); run_h_tc< ATF_TEST_CASE_NAME(h_build_cpp_ok) >(); - ATF_REQUIRE(grep_file("stdout", "-o.*test.p")); - ATF_REQUIRE(grep_file("stdout", "test.c")); - ATF_REQUIRE(grep_file("test.p", "foo bar")); + ATF_REQUIRE(atf::utils::grep_file("-o.*test.p", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("test.c", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("foo bar", "test.p")); ATF_TEST_CASE_USE(h_build_cpp_fail); run_h_tc< ATF_TEST_CASE_NAME(h_build_cpp_fail) >(); - ATF_REQUIRE(grep_file("stdout", "-o test.p")); - ATF_REQUIRE(grep_file("stdout", "test.c")); - ATF_REQUIRE(grep_file("stderr", "test.c")); - ATF_REQUIRE(grep_file("stderr", "non-existent.h")); + ATF_REQUIRE(atf::utils::grep_file("-o test.p", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("test.c", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("test.c", "stderr")); + ATF_REQUIRE(atf::utils::grep_file("non-existent.h", "stderr")); } ATF_TEST_CASE(build_cxx_o); @@ -234,15 +234,15 @@ ATF_TEST_CASE_BODY(build_cxx_o) { ATF_TEST_CASE_USE(h_build_cxx_o_ok); run_h_tc< ATF_TEST_CASE_NAME(h_build_cxx_o_ok) >(); - ATF_REQUIRE(grep_file("stdout", "-o test.o")); - ATF_REQUIRE(grep_file("stdout", "-c test.cpp")); + ATF_REQUIRE(atf::utils::grep_file("-o test.o", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("-c test.cpp", "stdout")); ATF_TEST_CASE_USE(h_build_cxx_o_fail); run_h_tc< ATF_TEST_CASE_NAME(h_build_cxx_o_fail) >(); - ATF_REQUIRE(grep_file("stdout", "-o test.o")); - ATF_REQUIRE(grep_file("stdout", "-c test.cpp")); - ATF_REQUIRE(grep_file("stderr", "test.cpp")); - ATF_REQUIRE(grep_file("stderr", "UNDEFINED_SYMBOL")); + ATF_REQUIRE(atf::utils::grep_file("-o test.o", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("-c test.cpp", "stdout")); + ATF_REQUIRE(atf::utils::grep_file("test.cpp", "stderr")); + ATF_REQUIRE(atf::utils::grep_file("UNDEFINED_SYMBOL", "stderr")); } ATF_TEST_CASE(exec_cleanup); diff --git a/atf-c++/detail/Atffile b/atf-c++/detail/Atffile index ead6ec3dcac20..2d53514bdf7e9 100644 --- a/atf-c++/detail/Atffile +++ b/atf-c++/detail/Atffile @@ -3,6 +3,7 @@ Content-Type: application/X-atf-atffile; version="1" prop: test-suite = atf tp: application_test +tp: auto_array_test tp: env_test tp: exceptions_test tp: expand_test diff --git a/atf-c++/detail/Kyuafile b/atf-c++/detail/Kyuafile index 472c1229d79de..fd0d7abe85927 100644 --- a/atf-c++/detail/Kyuafile +++ b/atf-c++/detail/Kyuafile @@ -3,6 +3,7 @@ syntax("kyuafile", 1) test_suite("atf") atf_test_program{name="application_test"} +atf_test_program{name="auto_array_test"} atf_test_program{name="env_test"} atf_test_program{name="exceptions_test"} atf_test_program{name="expand_test"} diff --git a/atf-c++/detail/Makefile.am.inc b/atf-c++/detail/Makefile.am.inc index fcadd77db1feb..0a7bd151e6f74 100644 --- a/atf-c++/detail/Makefile.am.inc +++ b/atf-c++/detail/Makefile.am.inc @@ -29,6 +29,7 @@ libatf_c___la_SOURCES += atf-c++/detail/application.cpp \ atf-c++/detail/application.hpp \ + atf-c++/detail/auto_array.hpp \ atf-c++/detail/env.cpp \ atf-c++/detail/env.hpp \ atf-c++/detail/exceptions.cpp \ @@ -60,6 +61,10 @@ tests_atf_c___detail_PROGRAMS = atf-c++/detail/application_test atf_c___detail_application_test_SOURCES = atf-c++/detail/application_test.cpp atf_c___detail_application_test_LDADD = atf-c++/detail/libtest_helpers.la $(ATF_CXX_LIBS) +tests_atf_c___detail_PROGRAMS += atf-c++/detail/auto_array_test +atf_c___detail_auto_array_test_SOURCES = atf-c++/detail/auto_array_test.cpp +atf_c___detail_auto_array_test_LDADD = atf-c++/detail/libtest_helpers.la $(ATF_CXX_LIBS) + tests_atf_c___detail_PROGRAMS += atf-c++/detail/env_test atf_c___detail_env_test_SOURCES = atf-c++/detail/env_test.cpp atf_c___detail_env_test_LDADD = atf-c++/detail/libtest_helpers.la $(ATF_CXX_LIBS) diff --git a/atf-c++/detail/auto_array.hpp b/atf-c++/detail/auto_array.hpp new file mode 100644 index 0000000000000..1459284e3b674 --- /dev/null +++ b/atf-c++/detail/auto_array.hpp @@ -0,0 +1,179 @@ +// +// Automated Testing Framework (atf) +// +// Copyright (c) 2007 The NetBSD Foundation, Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND +// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// + +#if !defined(_ATF_CXX_AUTO_ARRAY_HPP_) +#define _ATF_CXX_AUTO_ARRAY_HPP_ + +#include <cstddef> + +namespace atf { + +// ------------------------------------------------------------------------ +// The "auto_array" class. +// ------------------------------------------------------------------------ + +template< class T > +struct auto_array_ref { + T* m_ptr; + + explicit auto_array_ref(T*); +}; + +template< class T > +auto_array_ref< T >::auto_array_ref(T* ptr) : + m_ptr(ptr) +{ +} + +template< class T > +class auto_array { + T* m_ptr; + +public: + auto_array(T* = NULL) throw(); + auto_array(auto_array< T >&) throw(); + auto_array(auto_array_ref< T >) throw(); + ~auto_array(void) throw(); + + T* get(void) throw(); + const T* get(void) const throw(); + T* release(void) throw(); + void reset(T* = NULL) throw(); + + auto_array< T >& operator=(auto_array< T >&) throw(); + auto_array< T >& operator=(auto_array_ref< T >) throw(); + + T& operator[](int) throw(); + operator auto_array_ref< T >(void) throw(); +}; + +template< class T > +auto_array< T >::auto_array(T* ptr) + throw() : + m_ptr(ptr) +{ +} + +template< class T > +auto_array< T >::auto_array(auto_array< T >& ptr) + throw() : + m_ptr(ptr.release()) +{ +} + +template< class T > +auto_array< T >::auto_array(auto_array_ref< T > ref) + throw() : + m_ptr(ref.m_ptr) +{ +} + +template< class T > +auto_array< T >::~auto_array(void) + throw() +{ + if (m_ptr != NULL) + delete [] m_ptr; +} + +template< class T > +T* +auto_array< T >::get(void) + throw() +{ + return m_ptr; +} + +template< class T > +const T* +auto_array< T >::get(void) + const throw() +{ + return m_ptr; +} + +template< class T > +T* +auto_array< T >::release(void) + throw() +{ + T* ptr = m_ptr; + m_ptr = NULL; + return ptr; +} + +template< class T > +void +auto_array< T >::reset(T* ptr) + throw() +{ + if (m_ptr != NULL) + delete [] m_ptr; + m_ptr = ptr; +} + +template< class T > +auto_array< T >& +auto_array< T >::operator=(auto_array< T >& ptr) + throw() +{ + reset(ptr.release()); + return *this; +} + +template< class T > +auto_array< T >& +auto_array< T >::operator=(auto_array_ref< T > ref) + throw() +{ + if (m_ptr != ref.m_ptr) { + delete [] m_ptr; + m_ptr = ref.m_ptr; + } + return *this; +} + +template< class T > +T& +auto_array< T >::operator[](int pos) + throw() +{ + return m_ptr[pos]; +} + +template< class T > +auto_array< T >::operator auto_array_ref< T >(void) + throw() +{ + return auto_array_ref< T >(release()); +} + +} // namespace atf + +#endif // !defined(_ATF_CXX_AUTO_ARRAY_HPP_) diff --git a/atf-c++/detail/auto_array_test.cpp b/atf-c++/detail/auto_array_test.cpp new file mode 100644 index 0000000000000..dcfe41551ce4e --- /dev/null +++ b/atf-c++/detail/auto_array_test.cpp @@ -0,0 +1,304 @@ +// +// Automated Testing Framework (atf) +// +// Copyright (c) 2007 The NetBSD Foundation, Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND +// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// + +extern "C" { +#include <sys/types.h> +} + +#include <iostream> + +#include "atf-c/defs.h" + +#include "../macros.hpp" + +#include "auto_array.hpp" + +// ------------------------------------------------------------------------ +// Tests for the "auto_array" class. +// ------------------------------------------------------------------------ + +class test_array { +public: + int m_value; + + static ssize_t m_nblocks; + + static + atf::auto_array< test_array > + do_copy(atf::auto_array< test_array >& ta) + { + return atf::auto_array< test_array >(ta); + } + + void* operator new(size_t size ATF_DEFS_ATTRIBUTE_UNUSED) + { + ATF_FAIL("New called but should have been new[]"); + return new int(5); + } + + void* operator new[](size_t size) + { + m_nblocks++; + void* mem = ::operator new(size); + std::cout << "Allocated 'test_array' object " << mem << "\n"; + return mem; + } + + void operator delete(void* mem ATF_DEFS_ATTRIBUTE_UNUSED) + { + ATF_FAIL("Delete called but should have been delete[]"); + } + + void operator delete[](void* mem) + { + std::cout << "Releasing 'test_array' object " << mem << "\n"; + if (m_nblocks == 0) + ATF_FAIL("Unbalanced delete[]"); + m_nblocks--; + ::operator delete(mem); + } +}; + +ssize_t test_array::m_nblocks = 0; + +ATF_TEST_CASE(auto_array_scope); +ATF_TEST_CASE_HEAD(auto_array_scope) +{ + set_md_var("descr", "Tests the automatic scope handling in the " + "auto_array smart pointer class"); +} +ATF_TEST_CASE_BODY(auto_array_scope) +{ + using atf::auto_array; + + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + { + auto_array< test_array > t(new test_array[10]); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); +} + +ATF_TEST_CASE(auto_array_copy); +ATF_TEST_CASE_HEAD(auto_array_copy) +{ + set_md_var("descr", "Tests the auto_array smart pointer class' copy " + "constructor"); +} +ATF_TEST_CASE_BODY(auto_array_copy) +{ + using atf::auto_array; + + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + { + auto_array< test_array > t1(new test_array[10]); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + + { + auto_array< test_array > t2(t1); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); +} + +ATF_TEST_CASE(auto_array_copy_ref); +ATF_TEST_CASE_HEAD(auto_array_copy_ref) +{ + set_md_var("descr", "Tests the auto_array smart pointer class' copy " + "constructor through the auxiliary auto_array_ref object"); +} +ATF_TEST_CASE_BODY(auto_array_copy_ref) +{ + using atf::auto_array; + + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + { + auto_array< test_array > t1(new test_array[10]); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + + { + auto_array< test_array > t2 = test_array::do_copy(t1); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); +} + +ATF_TEST_CASE(auto_array_get); +ATF_TEST_CASE_HEAD(auto_array_get) +{ + set_md_var("descr", "Tests the auto_array smart pointer class' get " + "method"); +} +ATF_TEST_CASE_BODY(auto_array_get) +{ + using atf::auto_array; + + test_array* ta = new test_array[10]; + auto_array< test_array > t(ta); + ATF_REQUIRE_EQ(t.get(), ta); +} + +ATF_TEST_CASE(auto_array_release); +ATF_TEST_CASE_HEAD(auto_array_release) +{ + set_md_var("descr", "Tests the auto_array smart pointer class' release " + "method"); +} +ATF_TEST_CASE_BODY(auto_array_release) +{ + using atf::auto_array; + + test_array* ta1 = new test_array[10]; + { + auto_array< test_array > t(ta1); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + test_array* ta2 = t.release(); + ATF_REQUIRE_EQ(ta2, ta1); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + delete [] ta1; +} + +ATF_TEST_CASE(auto_array_reset); +ATF_TEST_CASE_HEAD(auto_array_reset) +{ + set_md_var("descr", "Tests the auto_array smart pointer class' reset " + "method"); +} +ATF_TEST_CASE_BODY(auto_array_reset) +{ + using atf::auto_array; + + test_array* ta1 = new test_array[10]; + test_array* ta2 = new test_array[10]; + ATF_REQUIRE_EQ(test_array::m_nblocks, 2); + + { + auto_array< test_array > t(ta1); + ATF_REQUIRE_EQ(test_array::m_nblocks, 2); + t.reset(ta2); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + t.reset(); + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); +} + +ATF_TEST_CASE(auto_array_assign); +ATF_TEST_CASE_HEAD(auto_array_assign) +{ + set_md_var("descr", "Tests the auto_array smart pointer class' " + "assignment operator"); +} +ATF_TEST_CASE_BODY(auto_array_assign) +{ + using atf::auto_array; + + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + { + auto_array< test_array > t1(new test_array[10]); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + + { + auto_array< test_array > t2; + t2 = t1; + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); +} + +ATF_TEST_CASE(auto_array_assign_ref); +ATF_TEST_CASE_HEAD(auto_array_assign_ref) +{ + set_md_var("descr", "Tests the auto_array smart pointer class' " + "assignment operator through the auxiliary auto_array_ref " + "object"); +} +ATF_TEST_CASE_BODY(auto_array_assign_ref) +{ + using atf::auto_array; + + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + { + auto_array< test_array > t1(new test_array[10]); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + + { + auto_array< test_array > t2; + t2 = test_array::do_copy(t1); + ATF_REQUIRE_EQ(test_array::m_nblocks, 1); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + } + ATF_REQUIRE_EQ(test_array::m_nblocks, 0); +} + +ATF_TEST_CASE(auto_array_access); +ATF_TEST_CASE_HEAD(auto_array_access) +{ + set_md_var("descr", "Tests the auto_array smart pointer class' access " + "operator"); +} +ATF_TEST_CASE_BODY(auto_array_access) +{ + using atf::auto_array; + + auto_array< test_array > t(new test_array[10]); + + for (int i = 0; i < 10; i++) + t[i].m_value = i * 2; + + for (int i = 0; i < 10; i++) + ATF_REQUIRE_EQ(t[i].m_value, i * 2); +} + +// ------------------------------------------------------------------------ +// Main. +// ------------------------------------------------------------------------ + +ATF_INIT_TEST_CASES(tcs) +{ + // Add the test for the "auto_array" class. + ATF_ADD_TEST_CASE(tcs, auto_array_scope); + ATF_ADD_TEST_CASE(tcs, auto_array_copy); + ATF_ADD_TEST_CASE(tcs, auto_array_copy_ref); + ATF_ADD_TEST_CASE(tcs, auto_array_get); + ATF_ADD_TEST_CASE(tcs, auto_array_release); + ATF_ADD_TEST_CASE(tcs, auto_array_reset); + ATF_ADD_TEST_CASE(tcs, auto_array_assign); + ATF_ADD_TEST_CASE(tcs, auto_array_assign_ref); + ATF_ADD_TEST_CASE(tcs, auto_array_access); +} diff --git a/atf-c++/detail/process.cpp b/atf-c++/detail/process.cpp index deb1158bcf8d2..f7ae6d49de00d 100644 --- a/atf-c++/detail/process.cpp +++ b/atf-c++/detail/process.cpp @@ -50,10 +50,10 @@ namespace impl = atf::process; // ------------------------------------------------------------------------ template< class C > -atf::utils::auto_array< const char* > +atf::auto_array< const char* > collection_to_argv(const C& c) { - atf::utils::auto_array< const char* > argv(new const char*[c.size() + 1]); + atf::auto_array< const char* > argv(new const char*[c.size() + 1]); std::size_t pos = 0; for (typename C::const_iterator iter = c.begin(); iter != c.end(); diff --git a/atf-c++/detail/process.hpp b/atf-c++/detail/process.hpp index 6e33e0037d872..bc55a5721c74e 100644 --- a/atf-c++/detail/process.hpp +++ b/atf-c++/detail/process.hpp @@ -41,11 +41,10 @@ extern "C" { #include <string> #include <vector> +#include "auto_array.hpp" #include "exceptions.hpp" #include "fs.hpp" -#include "../utils.hpp" - namespace atf { namespace process { @@ -64,7 +63,7 @@ class argv_array { // std::tr1::shared_array instead when it becomes widely available. // The reason would be to remove all copy constructors and assignment // operators from this class. - utils::auto_array< const char* > m_exec_argv; + auto_array< const char* > m_exec_argv; void ctor_init_exec_argv(void); public: diff --git a/atf-c++/detail/test_helpers.cpp b/atf-c++/detail/test_helpers.cpp index 42bd711dc7d63..191649d03d598 100644 --- a/atf-c++/detail/test_helpers.cpp +++ b/atf-c++/detail/test_helpers.cpp @@ -27,10 +27,6 @@ // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // -extern "C" { -#include <regex.h> -} - #include <fstream> #include <iostream> #include <string> @@ -88,43 +84,6 @@ get_process_helpers_path(const atf::tests::tc& tc) ".." / "atf-c" / "detail" / "process_helpers"; } -bool -grep_file(const char* name, const char* regex) -{ - std::ifstream is(name); - ATF_REQUIRE(is); - - bool found = false; - - std::string line; - std::getline(is, line); - while (!found && is.good()) { - if (grep_string(line, regex)) - found = true; - else - std::getline(is, line); - } - - return found; -} - -bool -grep_string(const std::string& str, const char* regex) -{ - int res; - regex_t preg; - - std::cout << "Looking for '" << regex << "' in '" << str << "'\n"; - ATF_REQUIRE(::regcomp(&preg, regex, REG_EXTENDED) == 0); - - res = ::regexec(&preg, str.c_str(), 0, NULL, 0); - ATF_REQUIRE(res == 0 || res == REG_NOMATCH); - - ::regfree(&preg); - - return res == 0; -} - void test_helpers_detail::check_equal(const char* expected[], const string_vector& actual) diff --git a/atf-c++/detail/test_helpers.hpp b/atf-c++/detail/test_helpers.hpp index 059a0a5b4b7b8..4a39331de4d49 100644 --- a/atf-c++/detail/test_helpers.hpp +++ b/atf-c++/detail/test_helpers.hpp @@ -87,8 +87,6 @@ class tc; void header_check(const char*); void build_check_cxx_o(const atf::tests::tc&, const char*, const char*, bool); atf::fs::path get_process_helpers_path(const atf::tests::tc&); -bool grep_file(const char*, const char*); -bool grep_string(const std::string&, const char*); struct run_h_tc_data { const atf::tests::vars_map& m_config; diff --git a/atf-c++/macros_test.cpp b/atf-c++/macros_test.cpp index 57e19e0e0322a..67e41061c8b01 100644 --- a/atf-c++/macros_test.cpp +++ b/atf-c++/macros_test.cpp @@ -38,6 +38,7 @@ extern "C" { #include <stdexcept> #include "macros.hpp" +#include "utils.hpp" #include "detail/fs.hpp" #include "detail/process.hpp" @@ -291,7 +292,7 @@ ATF_TEST_CASE_BODY(pass) { ATF_TEST_CASE_USE(h_pass); run_h_tc< ATF_TEST_CASE_NAME(h_pass) >(); - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(atf::fs::path("before"))); ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after"))); } @@ -305,7 +306,7 @@ ATF_TEST_CASE_BODY(fail) { ATF_TEST_CASE_USE(h_fail); run_h_tc< ATF_TEST_CASE_NAME(h_fail) >(); - ATF_REQUIRE(grep_file("result", "^failed: Failed on purpose")); + ATF_REQUIRE(atf::utils::grep_file("^failed: Failed on purpose", "result")); ATF_REQUIRE(atf::fs::exists(atf::fs::path("before"))); ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after"))); } @@ -319,7 +320,8 @@ ATF_TEST_CASE_BODY(skip) { ATF_TEST_CASE_USE(h_skip); run_h_tc< ATF_TEST_CASE_NAME(h_skip) >(); - ATF_REQUIRE(grep_file("result", "^skipped: Skipped on purpose")); + ATF_REQUIRE(atf::utils::grep_file("^skipped: Skipped on purpose", + "result")); ATF_REQUIRE(atf::fs::exists(atf::fs::path("before"))); ATF_REQUIRE(!atf::fs::exists(atf::fs::path("after"))); } @@ -354,10 +356,11 @@ ATF_TEST_CASE_BODY(require) ATF_REQUIRE(atf::fs::exists(before)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(after)); } else { - ATF_REQUIRE(grep_file("result", "^failed: .*condition not met")); + ATF_REQUIRE(atf::utils::grep_file( + "^failed: .*condition not met", "result")); ATF_REQUIRE(!atf::fs::exists(after)); } @@ -403,10 +406,10 @@ ATF_TEST_CASE_BODY(require_eq) ATF_REQUIRE(atf::fs::exists(before)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(after)); } else { - ATF_REQUIRE(grep_file("result", "^failed: .*v1 != v2")); + ATF_REQUIRE(atf::utils::grep_file("^failed: .*v1 != v2", "result")); ATF_REQUIRE(!atf::fs::exists(after)); } @@ -448,10 +451,10 @@ ATF_TEST_CASE_BODY(require_in) ATF_REQUIRE(atf::fs::exists(before)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(after)); } else { - ATF_REQUIRE(grep_file("result", "^failed: ")); + ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result")); ATF_REQUIRE(!atf::fs::exists(after)); } @@ -495,10 +498,10 @@ ATF_TEST_CASE_BODY(require_match) ATF_REQUIRE(atf::fs::exists(before)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(after)); } else { - ATF_REQUIRE(grep_file("result", "^failed: ")); + ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result")); ATF_REQUIRE(!atf::fs::exists(after)); } @@ -540,10 +543,10 @@ ATF_TEST_CASE_BODY(require_not_in) ATF_REQUIRE(atf::fs::exists(before)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(after)); } else { - ATF_REQUIRE(grep_file("result", "^failed: ")); + ATF_REQUIRE(atf::utils::grep_file("^failed: ", "result")); ATF_REQUIRE(!atf::fs::exists(after)); } @@ -586,13 +589,13 @@ ATF_TEST_CASE_BODY(require_throw) ATF_REQUIRE(atf::fs::exists(before)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(after)); } else { std::cout << "Checking that message contains '" << t->msg << "'\n"; std::string exp_result = std::string("^failed: .*") + t->msg; - ATF_REQUIRE(grep_file("result", exp_result.c_str())); + ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result")); ATF_REQUIRE(!atf::fs::exists(after)); } @@ -638,13 +641,13 @@ ATF_TEST_CASE_BODY(require_throw_re) ATF_REQUIRE(atf::fs::exists(before)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(after)); } else { std::cout << "Checking that message contains '" << t->msg << "'\n"; std::string exp_result = std::string("^failed: .*") + t->msg; - ATF_REQUIRE(grep_file("result", exp_result.c_str())); + ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result")); ATF_REQUIRE(!atf::fs::exists(after)); } @@ -688,13 +691,13 @@ ATF_TEST_CASE_BODY(check_errno) ATF_REQUIRE(atf::fs::exists(after)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); } else { - ATF_REQUIRE(grep_file("result", "^failed")); + ATF_REQUIRE(atf::utils::grep_file("^failed", "result")); std::string exp_result = "macros_test.cpp:[0-9]+: " + std::string(t->msg) + "$"; - ATF_REQUIRE(grep_file("stderr", exp_result.c_str())); + ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "stderr")); } atf::fs::remove(before); @@ -734,12 +737,12 @@ ATF_TEST_CASE_BODY(require_errno) ATF_REQUIRE(atf::fs::exists(before)); if (t->ok) { - ATF_REQUIRE(grep_file("result", "^passed")); + ATF_REQUIRE(atf::utils::grep_file("^passed", "result")); ATF_REQUIRE(atf::fs::exists(after)); } else { std::string exp_result = "^failed: .*macros_test.cpp:[0-9]+: " + std::string(t->msg) + "$"; - ATF_REQUIRE(grep_file("result", exp_result.c_str())); + ATF_REQUIRE(atf::utils::grep_file(exp_result.c_str(), "result")); ATF_REQUIRE(!atf::fs::exists(after)); } diff --git a/atf-c++/noncopyable.hpp b/atf-c++/noncopyable.hpp new file mode 100644 index 0000000000000..a885a668cb12b --- /dev/null +++ b/atf-c++/noncopyable.hpp @@ -0,0 +1,56 @@ +// +// Automated Testing Framework (atf) +// +// Copyright (c) 2007 The NetBSD Foundation, Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND +// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// + +#if !defined(_ATF_CXX_NONCOPYABLE_HPP_) +#define _ATF_CXX_NONCOPYABLE_HPP_ + +namespace atf { + +// ------------------------------------------------------------------------ +// The "noncopyable" class. +// ------------------------------------------------------------------------ + +class noncopyable { + // The class cannot be empty; otherwise we get ABI-stability warnings + // during the build, which will break it due to strict checking. + int m_noncopyable_dummy; + + noncopyable(const noncopyable& nc); + noncopyable& operator=(const noncopyable& nc); + +protected: + // Explicitly needed to provide some non-private functions. Otherwise + // we also get some warnings during the build. + noncopyable(void) {} + ~noncopyable(void) {} +}; + +} // namespace atf + +#endif // !defined(_ATF_CXX_NONCOPYABLE_HPP_) diff --git a/atf-c++/tests.cpp b/atf-c++/tests.cpp index cdc0dfbad4f3e..2c351fcb3dbbb 100644 --- a/atf-c++/tests.cpp +++ b/atf-c++/tests.cpp @@ -55,9 +55,11 @@ extern "C" { #include "atf-c/utils.h" } +#include "noncopyable.hpp" #include "tests.hpp" #include "detail/application.hpp" +#include "detail/auto_array.hpp" #include "detail/env.hpp" #include "detail/exceptions.hpp" #include "detail/fs.hpp" @@ -127,7 +129,7 @@ detail::match(const std::string& regexp, const std::string& str) static std::map< atf_tc_t*, impl::tc* > wraps; static std::map< const atf_tc_t*, const impl::tc* > cwraps; -struct impl::tc_impl : atf::utils::noncopyable { +struct impl::tc_impl : atf::noncopyable { std::string m_ident; atf_tc_t m_tc; bool m_has_cleanup; @@ -190,8 +192,7 @@ impl::tc::init(const vars_map& config) { atf_error_t err; - utils::auto_array< const char * > array( - new const char*[(config.size() * 2) + 1]); + auto_array< const char * > array(new const char*[(config.size() * 2) + 1]); const char **ptr = array.get(); for (vars_map::const_iterator iter = config.begin(); iter != config.end(); iter++) { diff --git a/atf-c++/tests.hpp b/atf-c++/tests.hpp index af75229d3b83a..22a2a8706a9ac 100644 --- a/atf-c++/tests.hpp +++ b/atf-c++/tests.hpp @@ -38,7 +38,7 @@ extern "C" { #include <atf-c/defs.h> } -#include <atf-c++/utils.hpp> +#include <atf-c++/noncopyable.hpp> namespace atf { namespace tests { @@ -74,7 +74,7 @@ typedef std::map< std::string, std::string > vars_map; struct tc_impl; -class tc : utils::noncopyable { +class tc : noncopyable { std::auto_ptr< tc_impl > pimpl; protected: diff --git a/atf-c++/utils.cpp b/atf-c++/utils.cpp new file mode 100644 index 0000000000000..cc338bb892301 --- /dev/null +++ b/atf-c++/utils.cpp @@ -0,0 +1,104 @@ +// +// Automated Testing Framework (atf) +// +// Copyright (c) 2007 The NetBSD Foundation, Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND +// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// + +extern "C" { +#include "atf-c/utils.h" +} + +#include <cstdlib> +#include <iostream> + +#include "utils.hpp" + +void +atf::utils::cat_file(const std::string& path, const std::string& prefix) +{ + atf_utils_cat_file(path.c_str(), prefix.c_str()); +} + +void +atf::utils::copy_file(const std::string& source, const std::string& destination) +{ + atf_utils_copy_file(source.c_str(), destination.c_str()); +} + +bool +atf::utils::compare_file(const std::string& path, const std::string& contents) +{ + return atf_utils_compare_file(path.c_str(), contents.c_str()); +} + +void +atf::utils::create_file(const std::string& path, const std::string& contents) +{ + atf_utils_create_file(path.c_str(), "%s", contents.c_str()); +} + +bool +atf::utils::file_exists(const std::string& path) +{ + return atf_utils_file_exists(path.c_str()); +} + +pid_t +atf::utils::fork(void) +{ + std::cout.flush(); + std::cerr.flush(); + return atf_utils_fork(); +} + +bool +atf::utils::grep_file(const std::string& regex, const std::string& path) +{ + return atf_utils_grep_file("%s", path.c_str(), regex.c_str()); +} + +bool +atf::utils::grep_string(const std::string& regex, const std::string& str) +{ + return atf_utils_grep_string("%s", str.c_str(), regex.c_str()); +} + +void +atf::utils::redirect(const int fd, const std::string& path) +{ + if (fd == STDOUT_FILENO) + std::cout.flush(); + else if (fd == STDERR_FILENO) + std::cerr.flush(); + atf_utils_redirect(fd, path.c_str()); +} + +void +atf::utils::wait(const pid_t pid, const int exitstatus, + const std::string& expout, const std::string& experr) +{ + atf_utils_wait(pid, exitstatus, expout.c_str(), experr.c_str()); +} diff --git a/atf-c++/utils.hpp b/atf-c++/utils.hpp index 1858b7f8e1ed8..b8596aecdc0c5 100644 --- a/atf-c++/utils.hpp +++ b/atf-c++/utils.hpp @@ -30,170 +30,38 @@ #if !defined(_ATF_CXX_UTILS_HPP_) #define _ATF_CXX_UTILS_HPP_ -#include <cstddef> - -namespace atf { -namespace utils { - -// ------------------------------------------------------------------------ -// The "auto_array" class. -// ------------------------------------------------------------------------ - -template< class T > -struct auto_array_ref { - T* m_ptr; - - explicit auto_array_ref(T*); -}; - -template< class T > -auto_array_ref< T >::auto_array_ref(T* ptr) : - m_ptr(ptr) -{ -} - -template< class T > -class auto_array { - T* m_ptr; - -public: - auto_array(T* = NULL) throw(); - auto_array(auto_array< T >&) throw(); - auto_array(auto_array_ref< T >) throw(); - ~auto_array(void) throw(); - - T* get(void) throw(); - const T* get(void) const throw(); - T* release(void) throw(); - void reset(T* = NULL) throw(); - - auto_array< T >& operator=(auto_array< T >&) throw(); - auto_array< T >& operator=(auto_array_ref< T >) throw(); - - T& operator[](int) throw(); - operator auto_array_ref< T >(void) throw(); -}; - -template< class T > -auto_array< T >::auto_array(T* ptr) - throw() : - m_ptr(ptr) -{ -} - -template< class T > -auto_array< T >::auto_array(auto_array< T >& ptr) - throw() : - m_ptr(ptr.release()) -{ -} - -template< class T > -auto_array< T >::auto_array(auto_array_ref< T > ref) - throw() : - m_ptr(ref.m_ptr) -{ -} - -template< class T > -auto_array< T >::~auto_array(void) - throw() -{ - if (m_ptr != NULL) - delete [] m_ptr; +extern "C" { +#include <unistd.h> } -template< class T > -T* -auto_array< T >::get(void) - throw() -{ - return m_ptr; -} +#include <string> -template< class T > -const T* -auto_array< T >::get(void) - const throw() -{ - return m_ptr; -} - -template< class T > -T* -auto_array< T >::release(void) - throw() -{ - T* ptr = m_ptr; - m_ptr = NULL; - return ptr; -} - -template< class T > -void -auto_array< T >::reset(T* ptr) - throw() -{ - if (m_ptr != NULL) - delete [] m_ptr; - m_ptr = ptr; -} - -template< class T > -auto_array< T >& -auto_array< T >::operator=(auto_array< T >& ptr) - throw() -{ - reset(ptr.release()); - return *this; -} +namespace atf { +namespace utils { -template< class T > -auto_array< T >& -auto_array< T >::operator=(auto_array_ref< T > ref) - throw() +void cat_file(const std::string&, const std::string&); +bool compare_file(const std::string&, const std::string&); +void copy_file(const std::string&, const std::string&); +void create_file(const std::string&, const std::string&); +bool file_exists(const std::string&); +pid_t fork(void); +bool grep_file(const std::string&, const std::string&); +bool grep_string(const std::string&, const std::string&); +void redirect(const int, const std::string&); +void wait(const pid_t, const int, const std::string&, const std::string&); + +template< typename Collection > +bool +grep_collection(const std::string& regexp, const Collection& collection) { - if (m_ptr != ref.m_ptr) { - delete [] m_ptr; - m_ptr = ref.m_ptr; + for (typename Collection::const_iterator iter = collection.begin(); + iter != collection.end(); ++iter) { + if (grep_string(regexp, *iter)) + return true; } - return *this; -} - -template< class T > -T& -auto_array< T >::operator[](int pos) - throw() -{ - return m_ptr[pos]; -} - -template< class T > -auto_array< T >::operator auto_array_ref< T >(void) - throw() -{ - return auto_array_ref< T >(release()); + return false; } -// ------------------------------------------------------------------------ -// The "noncopyable" class. -// ------------------------------------------------------------------------ - -class noncopyable { - // The class cannot be empty; otherwise we get ABI-stability warnings - // during the build, which will break it due to strict checking. - int m_noncopyable_dummy; - - noncopyable(const noncopyable& nc); - noncopyable& operator=(const noncopyable& nc); - -protected: - // Explicitly needed to provide some non-private functions. Otherwise - // we also get some warnings during the build. - noncopyable(void) {} - ~noncopyable(void) {} -}; - } // namespace utils } // namespace atf diff --git a/atf-c++/utils_test.cpp b/atf-c++/utils_test.cpp index f75e0a7a1edb6..37457ff2b8389 100644 --- a/atf-c++/utils_test.cpp +++ b/atf-c++/utils_test.cpp @@ -27,259 +27,409 @@ // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // -#include <iostream> +extern "C" { +#include <sys/stat.h> +#include <sys/wait.h> + +#include <fcntl.h> +#include <unistd.h> +} -#include "atf-c/defs.h" +#include <cstdlib> +#include <iostream> +#include <set> +#include <string> +#include <vector> #include "macros.hpp" #include "utils.hpp" #include "detail/test_helpers.hpp" +static std::string +read_file(const char *path) +{ + char buffer[1024]; + + const int fd = open(path, O_RDONLY); + if (fd == -1) + ATF_FAIL("Cannot open " + std::string(path)); + const ssize_t length = read(fd, buffer, sizeof(buffer) - 1); + close(fd); + ATF_REQUIRE(length != -1); + if (length == sizeof(buffer) - 1) + ATF_FAIL("Internal buffer not long enough to read temporary file"); + ((char *)buffer)[length] = '\0'; + + return buffer; +} + // ------------------------------------------------------------------------ -// Tests for the "auto_array" class. +// Tests cases for the free functions. // ------------------------------------------------------------------------ -class test_array { -public: - int m_value; +ATF_TEST_CASE_WITHOUT_HEAD(cat_file__empty); +ATF_TEST_CASE_BODY(cat_file__empty) +{ + atf::utils::create_file("file.txt", ""); + atf::utils::redirect(STDOUT_FILENO, "captured.txt"); + atf::utils::cat_file("file.txt", "PREFIX"); + std::cout.flush(); + close(STDOUT_FILENO); - static ssize_t m_nblocks; + ATF_REQUIRE_EQ("", read_file("captured.txt")); +} - static - atf::utils::auto_array< test_array > - do_copy(atf::utils::auto_array< test_array >& ta) - { - return atf::utils::auto_array< test_array >(ta); - } +ATF_TEST_CASE_WITHOUT_HEAD(cat_file__one_line); +ATF_TEST_CASE_BODY(cat_file__one_line) +{ + atf::utils::create_file("file.txt", "This is a single line\n"); + atf::utils::redirect(STDOUT_FILENO, "captured.txt"); + atf::utils::cat_file("file.txt", "PREFIX"); + std::cout.flush(); + close(STDOUT_FILENO); - void* operator new(size_t size ATF_DEFS_ATTRIBUTE_UNUSED) - { - ATF_FAIL("New called but should have been new[]"); - return new int(5); - } + ATF_REQUIRE_EQ("PREFIXThis is a single line\n", read_file("captured.txt")); +} - void* operator new[](size_t size) - { - m_nblocks++; - void* mem = ::operator new(size); - std::cout << "Allocated 'test_array' object " << mem << "\n"; - return mem; - } +ATF_TEST_CASE_WITHOUT_HEAD(cat_file__several_lines); +ATF_TEST_CASE_BODY(cat_file__several_lines) +{ + atf::utils::create_file("file.txt", "First\nSecond line\nAnd third\n"); + atf::utils::redirect(STDOUT_FILENO, "captured.txt"); + atf::utils::cat_file("file.txt", ">"); + std::cout.flush(); + close(STDOUT_FILENO); + + ATF_REQUIRE_EQ(">First\n>Second line\n>And third\n", + read_file("captured.txt")); +} - void operator delete(void* mem ATF_DEFS_ATTRIBUTE_UNUSED) - { - ATF_FAIL("Delete called but should have been delete[]"); - } +ATF_TEST_CASE_WITHOUT_HEAD(cat_file__no_newline_eof); +ATF_TEST_CASE_BODY(cat_file__no_newline_eof) +{ + atf::utils::create_file("file.txt", "Foo\n bar baz"); + atf::utils::redirect(STDOUT_FILENO, "captured.txt"); + atf::utils::cat_file("file.txt", "PREFIX"); + std::cout.flush(); + close(STDOUT_FILENO); - void operator delete[](void* mem) - { - std::cout << "Releasing 'test_array' object " << mem << "\n"; - if (m_nblocks == 0) - ATF_FAIL("Unbalanced delete[]"); - m_nblocks--; - ::operator delete(mem); - } -}; + ATF_REQUIRE_EQ("PREFIXFoo\nPREFIX bar baz", read_file("captured.txt")); +} -ssize_t test_array::m_nblocks = 0; +ATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__match); +ATF_TEST_CASE_BODY(compare_file__empty__match) +{ + atf::utils::create_file("test.txt", ""); + ATF_REQUIRE(atf::utils::compare_file("test.txt", "")); +} + +ATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__not_match); +ATF_TEST_CASE_BODY(compare_file__empty__not_match) +{ + atf::utils::create_file("test.txt", ""); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n")); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "foo")); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", " ")); +} -ATF_TEST_CASE(auto_array_scope); -ATF_TEST_CASE_HEAD(auto_array_scope) +ATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__match); +ATF_TEST_CASE_BODY(compare_file__short__match) { - set_md_var("descr", "Tests the automatic scope handling in the " - "auto_array smart pointer class"); + atf::utils::create_file("test.txt", "this is a short file"); + ATF_REQUIRE(atf::utils::compare_file("test.txt", "this is a short file")); } -ATF_TEST_CASE_BODY(auto_array_scope) + +ATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__not_match); +ATF_TEST_CASE_BODY(compare_file__short__not_match) { - using atf::utils::auto_array; + atf::utils::create_file("test.txt", "this is a short file"); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "")); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n")); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a Short file")); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short fil")); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short file ")); +} - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); - { - auto_array< test_array > t(new test_array[10]); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); +ATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__match); +ATF_TEST_CASE_BODY(compare_file__long__match) +{ + char long_contents[3456]; + size_t i = 0; + for (; i < sizeof(long_contents) - 1; i++) + long_contents[i] = '0' + (i % 10); + long_contents[i] = '\0'; + atf::utils::create_file("test.txt", long_contents); + + ATF_REQUIRE(atf::utils::compare_file("test.txt", long_contents)); } -ATF_TEST_CASE(auto_array_copy); -ATF_TEST_CASE_HEAD(auto_array_copy) +ATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__not_match); +ATF_TEST_CASE_BODY(compare_file__long__not_match) { - set_md_var("descr", "Tests the auto_array smart pointer class' copy " - "constructor"); + char long_contents[3456]; + size_t i = 0; + for (; i < sizeof(long_contents) - 1; i++) + long_contents[i] = '0' + (i % 10); + long_contents[i] = '\0'; + atf::utils::create_file("test.txt", long_contents); + + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "")); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n")); + ATF_REQUIRE(!atf::utils::compare_file("test.txt", "0123456789")); + long_contents[i - 1] = 'Z'; + ATF_REQUIRE(!atf::utils::compare_file("test.txt", long_contents)); } -ATF_TEST_CASE_BODY(auto_array_copy) + +ATF_TEST_CASE_WITHOUT_HEAD(copy_file__empty); +ATF_TEST_CASE_BODY(copy_file__empty) { - using atf::utils::auto_array; - - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); - { - auto_array< test_array > t1(new test_array[10]); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - - { - auto_array< test_array > t2(t1); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); - } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + atf::utils::create_file("src.txt", ""); + ATF_REQUIRE(chmod("src.txt", 0520) != -1); + + atf::utils::copy_file("src.txt", "dest.txt"); + ATF_REQUIRE(atf::utils::compare_file("dest.txt", "")); + struct stat sb; + ATF_REQUIRE(stat("dest.txt", &sb) != -1); + ATF_REQUIRE_EQ(0520, sb.st_mode & 0xfff); +} + +ATF_TEST_CASE_WITHOUT_HEAD(copy_file__some_contents); +ATF_TEST_CASE_BODY(copy_file__some_contents) +{ + atf::utils::create_file("src.txt", "This is a\ntest file\n"); + atf::utils::copy_file("src.txt", "dest.txt"); + ATF_REQUIRE(atf::utils::compare_file("dest.txt", "This is a\ntest file\n")); +} + +ATF_TEST_CASE_WITHOUT_HEAD(create_file); +ATF_TEST_CASE_BODY(create_file) +{ + atf::utils::create_file("test.txt", "This is a %d test"); + + ATF_REQUIRE_EQ("This is a %d test", read_file("test.txt")); } -ATF_TEST_CASE(auto_array_copy_ref); -ATF_TEST_CASE_HEAD(auto_array_copy_ref) +ATF_TEST_CASE_WITHOUT_HEAD(file_exists); +ATF_TEST_CASE_BODY(file_exists) { - set_md_var("descr", "Tests the auto_array smart pointer class' copy " - "constructor through the auxiliary auto_array_ref object"); + atf::utils::create_file("test.txt", "foo"); + + ATF_REQUIRE( atf::utils::file_exists("test.txt")); + ATF_REQUIRE( atf::utils::file_exists("./test.txt")); + ATF_REQUIRE(!atf::utils::file_exists("./test.tx")); + ATF_REQUIRE(!atf::utils::file_exists("test.txt2")); } -ATF_TEST_CASE_BODY(auto_array_copy_ref) + +ATF_TEST_CASE_WITHOUT_HEAD(fork); +ATF_TEST_CASE_BODY(fork) { - using atf::utils::auto_array; - - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); - { - auto_array< test_array > t1(new test_array[10]); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - - { - auto_array< test_array > t2 = test_array::do_copy(t1); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + std::cout << "Should not get into child\n"; + std::cerr << "Should not get into child\n"; + pid_t pid = atf::utils::fork(); + if (pid == 0) { + std::cout << "Child stdout\n"; + std::cerr << "Child stderr\n"; + exit(EXIT_SUCCESS); } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + + int status; + ATF_REQUIRE(waitpid(pid, &status, 0) != -1); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); + + ATF_REQUIRE_EQ("Child stdout\n", read_file("atf_utils_fork_out.txt")); + ATF_REQUIRE_EQ("Child stderr\n", read_file("atf_utils_fork_err.txt")); } -ATF_TEST_CASE(auto_array_get); -ATF_TEST_CASE_HEAD(auto_array_get) +ATF_TEST_CASE_WITHOUT_HEAD(grep_collection__set); +ATF_TEST_CASE_BODY(grep_collection__set) { - set_md_var("descr", "Tests the auto_array smart pointer class' get " - "method"); + std::set< std::string > strings; + strings.insert("First"); + strings.insert("Second"); + + ATF_REQUIRE( atf::utils::grep_collection("irs", strings)); + ATF_REQUIRE( atf::utils::grep_collection("cond", strings)); + ATF_REQUIRE(!atf::utils::grep_collection("Third", strings)); } -ATF_TEST_CASE_BODY(auto_array_get) + +ATF_TEST_CASE_WITHOUT_HEAD(grep_collection__vector); +ATF_TEST_CASE_BODY(grep_collection__vector) { - using atf::utils::auto_array; + std::vector< std::string > strings; + strings.push_back("First"); + strings.push_back("Second"); - test_array* ta = new test_array[10]; - auto_array< test_array > t(ta); - ATF_REQUIRE_EQ(t.get(), ta); + ATF_REQUIRE( atf::utils::grep_collection("irs", strings)); + ATF_REQUIRE( atf::utils::grep_collection("cond", strings)); + ATF_REQUIRE(!atf::utils::grep_collection("Third", strings)); } -ATF_TEST_CASE(auto_array_release); -ATF_TEST_CASE_HEAD(auto_array_release) +ATF_TEST_CASE_WITHOUT_HEAD(grep_file); +ATF_TEST_CASE_BODY(grep_file) { - set_md_var("descr", "Tests the auto_array smart pointer class' release " - "method"); + atf::utils::create_file("test.txt", "line1\nthe second line\naaaabbbb\n"); + + ATF_REQUIRE(atf::utils::grep_file("line1", "test.txt")); + ATF_REQUIRE(atf::utils::grep_file("second line", "test.txt")); + ATF_REQUIRE(atf::utils::grep_file("aa.*bb", "test.txt")); + ATF_REQUIRE(!atf::utils::grep_file("foo", "test.txt")); + ATF_REQUIRE(!atf::utils::grep_file("bar", "test.txt")); + ATF_REQUIRE(!atf::utils::grep_file("aaaaa", "test.txt")); } -ATF_TEST_CASE_BODY(auto_array_release) + +ATF_TEST_CASE_WITHOUT_HEAD(grep_string); +ATF_TEST_CASE_BODY(grep_string) { - using atf::utils::auto_array; - - test_array* ta1 = new test_array[10]; - { - auto_array< test_array > t(ta1); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - test_array* ta2 = t.release(); - ATF_REQUIRE_EQ(ta2, ta1); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - } - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - delete [] ta1; + const char *str = "a string - aaaabbbb"; + ATF_REQUIRE(atf::utils::grep_string("a string", str)); + ATF_REQUIRE(atf::utils::grep_string("^a string", str)); + ATF_REQUIRE(atf::utils::grep_string("aaaabbbb$", str)); + ATF_REQUIRE(atf::utils::grep_string("aa.*bb", str)); + ATF_REQUIRE(!atf::utils::grep_string("foo", str)); + ATF_REQUIRE(!atf::utils::grep_string("bar", str)); + ATF_REQUIRE(!atf::utils::grep_string("aaaaa", str)); } -ATF_TEST_CASE(auto_array_reset); -ATF_TEST_CASE_HEAD(auto_array_reset) +ATF_TEST_CASE_WITHOUT_HEAD(redirect__stdout); +ATF_TEST_CASE_BODY(redirect__stdout) { - set_md_var("descr", "Tests the auto_array smart pointer class' reset " - "method"); + std::cout << "Buffer this"; + atf::utils::redirect(STDOUT_FILENO, "captured.txt"); + std::cout << "The printed message"; + std::cout.flush(); + + ATF_REQUIRE_EQ("The printed message", read_file("captured.txt")); } -ATF_TEST_CASE_BODY(auto_array_reset) + +ATF_TEST_CASE_WITHOUT_HEAD(redirect__stderr); +ATF_TEST_CASE_BODY(redirect__stderr) { - using atf::utils::auto_array; - - test_array* ta1 = new test_array[10]; - test_array* ta2 = new test_array[10]; - ATF_REQUIRE_EQ(test_array::m_nblocks, 2); - - { - auto_array< test_array > t(ta1); - ATF_REQUIRE_EQ(test_array::m_nblocks, 2); - t.reset(ta2); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - t.reset(); - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); - } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + std::cerr << "Buffer this"; + atf::utils::redirect(STDERR_FILENO, "captured.txt"); + std::cerr << "The printed message"; + std::cerr.flush(); + + ATF_REQUIRE_EQ("The printed message", read_file("captured.txt")); } -ATF_TEST_CASE(auto_array_assign); -ATF_TEST_CASE_HEAD(auto_array_assign) +ATF_TEST_CASE_WITHOUT_HEAD(redirect__other); +ATF_TEST_CASE_BODY(redirect__other) { - set_md_var("descr", "Tests the auto_array smart pointer class' " - "assignment operator"); + const std::string message = "Foo bar\nbaz\n"; + atf::utils::redirect(15, "captured.txt"); + ATF_REQUIRE(write(15, message.c_str(), message.length()) != -1); + close(15); + + ATF_REQUIRE_EQ(message, read_file("captured.txt")); } -ATF_TEST_CASE_BODY(auto_array_assign) + +static void +fork_and_wait(const int exitstatus, const char* expout, const char* experr) { - using atf::utils::auto_array; - - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); - { - auto_array< test_array > t1(new test_array[10]); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - - { - auto_array< test_array > t2; - t2 = t1; - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + const pid_t pid = atf::utils::fork(); + if (pid == 0) { + std::cout << "Some output\n"; + std::cerr << "Some error\n"; + exit(123); } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + atf::utils::wait(pid, exitstatus, expout, experr); + exit(EXIT_SUCCESS); } -ATF_TEST_CASE(auto_array_assign_ref); -ATF_TEST_CASE_HEAD(auto_array_assign_ref) +ATF_TEST_CASE_WITHOUT_HEAD(wait__ok); +ATF_TEST_CASE_BODY(wait__ok) { - set_md_var("descr", "Tests the auto_array smart pointer class' " - "assignment operator through the auxiliary auto_array_ref " - "object"); + const pid_t control = fork(); + ATF_REQUIRE(control != -1); + if (control == 0) + fork_and_wait(123, "Some output\n", "Some error\n"); + else { + int status; + ATF_REQUIRE(waitpid(control, &status, 0) != -1); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); + } } -ATF_TEST_CASE_BODY(auto_array_assign_ref) + +ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_exitstatus); +ATF_TEST_CASE_BODY(wait__invalid_exitstatus) { - using atf::utils::auto_array; - - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); - { - auto_array< test_array > t1(new test_array[10]); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - - { - auto_array< test_array > t2; - t2 = test_array::do_copy(t1); - ATF_REQUIRE_EQ(test_array::m_nblocks, 1); - } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); + const pid_t control = fork(); + ATF_REQUIRE(control != -1); + if (control == 0) + fork_and_wait(120, "Some output\n", "Some error\n"); + else { + int status; + ATF_REQUIRE(waitpid(control, &status, 0) != -1); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status)); } - ATF_REQUIRE_EQ(test_array::m_nblocks, 0); } -ATF_TEST_CASE(auto_array_access); -ATF_TEST_CASE_HEAD(auto_array_access) +ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stdout); +ATF_TEST_CASE_BODY(wait__invalid_stdout) { - set_md_var("descr", "Tests the auto_array smart pointer class' access " - "operator"); + const pid_t control = fork(); + ATF_REQUIRE(control != -1); + if (control == 0) + fork_and_wait(123, "Some output foo\n", "Some error\n"); + else { + int status; + ATF_REQUIRE(waitpid(control, &status, 0) != -1); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status)); + } } -ATF_TEST_CASE_BODY(auto_array_access) -{ - using atf::utils::auto_array; - auto_array< test_array > t(new test_array[10]); +ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stderr); +ATF_TEST_CASE_BODY(wait__invalid_stderr) +{ + const pid_t control = fork(); + ATF_REQUIRE(control != -1); + if (control == 0) + fork_and_wait(123, "Some output\n", "Some error foo\n"); + else { + int status; + ATF_REQUIRE(waitpid(control, &status, 0) != -1); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status)); + } +} - for (int i = 0; i < 10; i++) - t[i].m_value = i * 2; +ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stdout); +ATF_TEST_CASE_BODY(wait__save_stdout) +{ + const pid_t control = fork(); + ATF_REQUIRE(control != -1); + if (control == 0) + fork_and_wait(123, "save:my-output.txt", "Some error\n"); + else { + int status; + ATF_REQUIRE(waitpid(control, &status, 0) != -1); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); + + ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some output\n")); + } +} - for (int i = 0; i < 10; i++) - ATF_REQUIRE_EQ(t[i].m_value, i * 2); +ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stderr); +ATF_TEST_CASE_BODY(wait__save_stderr) +{ + const pid_t control = fork(); + ATF_REQUIRE(control != -1); + if (control == 0) + fork_and_wait(123, "Some output\n", "save:my-output.txt"); + else { + int status; + ATF_REQUIRE(waitpid(control, &status, 0) != -1); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); + + ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some error\n")); + } } // ------------------------------------------------------------------------ @@ -294,16 +444,43 @@ HEADER_TC(include, "atf-c++/utils.hpp"); ATF_INIT_TEST_CASES(tcs) { - // Add the test for the "auto_array" class. - ATF_ADD_TEST_CASE(tcs, auto_array_scope); - ATF_ADD_TEST_CASE(tcs, auto_array_copy); - ATF_ADD_TEST_CASE(tcs, auto_array_copy_ref); - ATF_ADD_TEST_CASE(tcs, auto_array_get); - ATF_ADD_TEST_CASE(tcs, auto_array_release); - ATF_ADD_TEST_CASE(tcs, auto_array_reset); - ATF_ADD_TEST_CASE(tcs, auto_array_assign); - ATF_ADD_TEST_CASE(tcs, auto_array_assign_ref); - ATF_ADD_TEST_CASE(tcs, auto_array_access); + // Add the test for the free functions. + ATF_ADD_TEST_CASE(tcs, cat_file__empty); + ATF_ADD_TEST_CASE(tcs, cat_file__one_line); + ATF_ADD_TEST_CASE(tcs, cat_file__several_lines); + ATF_ADD_TEST_CASE(tcs, cat_file__no_newline_eof); + + ATF_ADD_TEST_CASE(tcs, compare_file__empty__match); + ATF_ADD_TEST_CASE(tcs, compare_file__empty__not_match); + ATF_ADD_TEST_CASE(tcs, compare_file__short__match); + ATF_ADD_TEST_CASE(tcs, compare_file__short__not_match); + ATF_ADD_TEST_CASE(tcs, compare_file__long__match); + ATF_ADD_TEST_CASE(tcs, compare_file__long__not_match); + + ATF_ADD_TEST_CASE(tcs, copy_file__empty); + ATF_ADD_TEST_CASE(tcs, copy_file__some_contents); + + ATF_ADD_TEST_CASE(tcs, create_file); + + ATF_ADD_TEST_CASE(tcs, file_exists); + + ATF_ADD_TEST_CASE(tcs, fork); + + ATF_ADD_TEST_CASE(tcs, grep_collection__set); + ATF_ADD_TEST_CASE(tcs, grep_collection__vector); + ATF_ADD_TEST_CASE(tcs, grep_file); + ATF_ADD_TEST_CASE(tcs, grep_string); + + ATF_ADD_TEST_CASE(tcs, redirect__stdout); + ATF_ADD_TEST_CASE(tcs, redirect__stderr); + ATF_ADD_TEST_CASE(tcs, redirect__other); + + ATF_ADD_TEST_CASE(tcs, wait__ok); + ATF_ADD_TEST_CASE(tcs, wait__invalid_exitstatus); + ATF_ADD_TEST_CASE(tcs, wait__invalid_stdout); + ATF_ADD_TEST_CASE(tcs, wait__invalid_stderr); + ATF_ADD_TEST_CASE(tcs, wait__save_stdout); + ATF_ADD_TEST_CASE(tcs, wait__save_stderr); // Add the test cases for the header file. ATF_ADD_TEST_CASE(tcs, include); |