From: Brian Aker Date: Sun, 21 Apr 2013 01:17:49 +0000 (-0400) Subject: Clean up exception classes. X-Git-Tag: 1.0.18~27^2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=321fb333c1f00bec9c70a8f58187eb6a500a7f62;p=awesomized%2Flibmemcached Clean up exception classes. --- diff --git a/.bzrignore b/.bzrignore index 23517686..3db7c5f1 100644 --- a/.bzrignore +++ b/.bzrignore @@ -72,6 +72,7 @@ docs/text example/memcached_light example/t/memcached_light libhashkit-1.0/configure.h +libhashkit/hashkitcon.h libmemcached-1.0/configure.h libmemcached-1.0/t/c_sasl_test libmemcached-1.0/t/c_test @@ -90,11 +91,13 @@ libmemcached/memcached_configure.h libtest/.hg/ libtest/.hgignore libtest/abort +libtest/backtrace libtest/core-count libtest/skiptest libtest/unittest libtest/version.h libtest/wait +libtest/yatlcon.h libtool m4/libtool.m4 m4/libtool.m4 @@ -142,6 +145,7 @@ tests/memdump tests/memerror tests/memexist tests/memflush +tests/memping tests/memplus tests/memrm tests/memslap @@ -158,5 +162,3 @@ tests/testudp tests/var/ tmp_chroot unittests/unittests -libhashkit/hashkitcon.h -libtest/yatlcon.h diff --git a/libtest/cmdline.cc b/libtest/cmdline.cc index fd9e510d..3ecf63df 100644 --- a/libtest/cmdline.cc +++ b/libtest/cmdline.cc @@ -59,6 +59,7 @@ using namespace libtest; #include #include +#include #ifndef __USE_GNU static char **environ= NULL; diff --git a/libtest/exception.cc b/libtest/exception.cc new file mode 100644 index 00000000..0b18f777 --- /dev/null +++ b/libtest/exception.cc @@ -0,0 +1,108 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Data Differential YATL (i.e. libtest) library + * + * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * 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. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT + * OWNER 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. + * + */ + +#include "libtest/yatlcon.h" +#include +#include + +namespace libtest { + +exception::exception(const char *file_arg, int line_arg, const char *func_arg): + std::exception(), + _line(line_arg), + _file(file_arg), + _func(func_arg), + _error_message(NULL), + _error_message_size(0) +{ +} + +#ifndef __INTEL_COMPILER +# pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif +void exception::init(va_list args_) +{ + const char *format= va_arg(args_, const char *); + int error_message_length= vasprintf(&_error_message, format, args_); + assert(error_message_length != -1); + if (error_message_length > 0) + { + _error_message_size= error_message_length +1; + } +} + +exception::~exception() throw() +{ + if (_error_message) + { + free(_error_message); + } +} + +void exception::what(size_t length_, const char* message_) +{ + if (length_ > 0 and message_) + { + char *ptr= (char*) realloc(_error_message, length_ +1); + if (ptr) + { + _error_message= ptr; + memcpy(_error_message, message_, length_); + _error_message[length_]= 0; + } + } +} + +exception::exception(const exception& other) : + std::exception(), + _line(other._line), + _file(other._file), + _func(other._func), + _error_message_size(0) +{ + if (other.length() > 0) + { + _error_message= (char*) malloc(other.length() +1); + if (_error_message) + { + memcpy(_error_message, other._error_message, other.length()); + _error_message_size= other.length(); + } + } +} + +} // namespace libtest + diff --git a/libtest/exception.hpp b/libtest/exception.hpp new file mode 100644 index 00000000..3d20e8ba --- /dev/null +++ b/libtest/exception.hpp @@ -0,0 +1,94 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Data Differential YATL (i.e. libtest) library + * + * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * 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. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT + * OWNER 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. + * + */ + +#pragma once + +namespace libtest { + +class exception : public std::exception +{ +public: + exception(const char *file, int line, const char *func); + + exception( const exception& ); + + virtual ~exception() throw(); + + virtual const char* what() const throw() + { + if (_error_message) + { + return _error_message; + } + + return ""; + } + + void what(size_t, const char*); + + size_t length() const + { + return _error_message_size; + } + + int line() const + { + return _line; + } + + const char* file() const + { + return _file; + } + + const char* func() const + { + return _func; + } + +protected: + void init(va_list); + +private: + int _line; + const char* _file; + const char* _func; + char* _error_message; + size_t _error_message_size; +}; + +} // namespace libtest + diff --git a/libtest/exception/disconnected.hpp b/libtest/exception/disconnected.hpp new file mode 100644 index 00000000..fb3a2adb --- /dev/null +++ b/libtest/exception/disconnected.hpp @@ -0,0 +1,62 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Data Differential YATL (i.e. libtest) library + * + * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * 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. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT + * OWNER 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. + * + */ + +#pragma once + +#include "libtest/exception.hpp" + +namespace libtest { + +class disconnected : public libtest::exception +{ +public: + disconnected(const char *file, int line, const char *func, const std::string&, const in_port_t port, ...); + + disconnected(const disconnected&); + + // The following are just for unittesting the exception class + static bool is_disabled(); + static void disable(); + static void enable(); + static uint32_t disabled_counter(); + static void increment_disabled_counter(); + +private: + in_port_t _port; + char _instance[BUFSIZ]; +}; + +} // namespace libtest diff --git a/libtest/exception/fatal.cc b/libtest/exception/fatal.cc new file mode 100644 index 00000000..21a8ca79 --- /dev/null +++ b/libtest/exception/fatal.cc @@ -0,0 +1,119 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Data Differential YATL (i.e. libtest) library + * + * Copyright (C) 2012 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * 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. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT + * OWNER 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. + * + */ + +#include "libtest/yatlcon.h" +#include +#include "libtest/exception.hpp" +#include + +namespace libtest { + +#pragma GCC diagnostic ignored "-Wformat-nonliteral" + +fatal::fatal(const char *file_arg, int line_arg, const char *func_arg, ...) : + libtest::exception(file_arg, line_arg, func_arg) +{ + va_list args; + va_start(args, func_arg); + init(args); + va_end(args); +} + +fatal::fatal( const fatal& other ) : + libtest::exception(other) +{ +} + +static bool _disabled= false; +static uint32_t _counter= 0; + +bool fatal::is_disabled() throw() +{ + return _disabled; +} + +void fatal::disable() throw() +{ + _counter= 0; + _disabled= true; +} + +void fatal::enable() throw() +{ + _counter= 0; + _disabled= false; +} + +uint32_t fatal::disabled_counter() throw() +{ + return _counter; +} + +void fatal::increment_disabled_counter() throw() +{ + _counter++; +} + +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +disconnected::disconnected(const char *file_arg, int line_arg, const char *func_arg, + const std::string& instance, const in_port_t port, ...) : + libtest::exception(file_arg, line_arg, func_arg), + _port(port) +{ + va_list args; + va_start(args, port); + const char *format= va_arg(args, const char *); + char last_error[BUFSIZ]; + (void)vsnprintf(last_error, sizeof(last_error), format, args); + va_end(args); + + char buffer_error[BUFSIZ]; + int error_length= snprintf(buffer_error, sizeof(buffer_error), "%s:%u %s", instance.c_str(), uint32_t(port), last_error); + + if (error_length > 0) + { + what(size_t(error_length), buffer_error); + } +} + +disconnected::disconnected(const disconnected& other): + libtest::exception(other), + _port(other._port) +{ + strncpy(_instance, other._instance, BUFSIZ); +} + +} // namespace libtest diff --git a/libtest/exception/fatal.hpp b/libtest/exception/fatal.hpp new file mode 100644 index 00000000..5ee1aac5 --- /dev/null +++ b/libtest/exception/fatal.hpp @@ -0,0 +1,87 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Data Differential YATL (i.e. libtest) library + * + * Copyright (C) 2012 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * 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. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT + * OWNER 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. + * + */ + +#pragma once + +namespace libtest { + +class fatal : public libtest::exception +{ +public: + fatal(const char *file, int line, const char *func, ...); + + fatal(const fatal&); + + // The following are just for unittesting the exception class + static bool is_disabled() throw(); + static void disable() throw(); + static void enable() throw(); + static uint32_t disabled_counter() throw(); + static void increment_disabled_counter() throw(); + + test_return_t return_code() const + { + return TEST_SKIPPED; + } + +private: +}; + +} // namespace libtest + +#define FATAL(...) \ +do \ +{ \ + throw libtest::fatal(LIBYATL_DEFAULT_PARAM, __VA_ARGS__); \ +} while (0) + +#define FATAL_IF(__expression, ...) \ +do \ +{ \ + if ((__expression)) { \ + throw libtest::fatal(LIBYATL_DEFAULT_PARAM, (#__expression)); \ + } \ +} while (0) + +#define FATAL_IF_(__expression, ...) \ +do \ +{ \ + if ((__expression)) { \ + throw libtest::fatal(LIBYATL_DEFAULT_PARAM, __VA_ARGS__); \ + } \ +} while (0) + +#define fatal_assert(__assert) if((__assert)) {} else { throw libtest::fatal(LIBYATL_DEFAULT_PARAM, #__assert); } diff --git a/libtest/failed.h b/libtest/failed.h deleted file mode 100644 index bc459660..00000000 --- a/libtest/failed.h +++ /dev/null @@ -1,51 +0,0 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Data Differential YATL (i.e. libtest) library - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * 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. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT - * OWNER 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. - * - */ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -LIBTEST_INTERNAL_API - void push_failed_test(const char *collection, const char *test); - -LIBTEST_INTERNAL_API - void print_failed_test(void); - -#ifdef __cplusplus -} -#endif diff --git a/libtest/fatal.cc b/libtest/fatal.cc deleted file mode 100644 index 0ed06c2c..00000000 --- a/libtest/fatal.cc +++ /dev/null @@ -1,119 +0,0 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Data Differential YATL (i.e. libtest) library - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * 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. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT - * OWNER 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. - * - */ - -#include "libtest/yatlcon.h" -#include -#include - -namespace libtest { - -#pragma GCC diagnostic ignored "-Wformat-nonliteral" - -fatal::fatal(const char *file_arg, int line_arg, const char *func_arg, ...) : - __test_result(file_arg, line_arg, func_arg) -{ - va_list args; - va_start(args, func_arg); - init(args); - va_end(args); -} - -fatal::fatal( const fatal& other ) : - __test_result(other) -{ -} - -static bool _disabled= false; -static uint32_t _counter= 0; - -bool fatal::is_disabled() throw() -{ - return _disabled; -} - -void fatal::disable() throw() -{ - _counter= 0; - _disabled= true; -} - -void fatal::enable() throw() -{ - _counter= 0; - _disabled= false; -} - -uint32_t fatal::disabled_counter() throw() -{ - return _counter; -} - -void fatal::increment_disabled_counter() throw() -{ - _counter++; -} - -#pragma GCC diagnostic ignored "-Wformat-nonliteral" -disconnected::disconnected(const char *file_arg, int line_arg, const char *func_arg, - const std::string& instance, const in_port_t port, ...) : - std::runtime_error(func_arg), - _port(port), - _line(line_arg), - _file(file_arg), - _func(func_arg) -{ - va_list args; - va_start(args, port); - const char *format= va_arg(args, const char *); - char last_error[BUFSIZ]; - (void)vsnprintf(last_error, sizeof(last_error), format, args); - va_end(args); - - snprintf(_error_message, sizeof(_error_message), "%s:%u %s", instance.c_str(), uint32_t(port), last_error); -} - -disconnected::disconnected(const disconnected& other): - std::runtime_error(other._func), - _port(other._port), - _line(other._line), - _file(other._file), - _func(other._func) -{ - strncpy(_error_message, other._error_message, BUFSIZ); - strncpy(_instance, other._instance, BUFSIZ); -} - -} // namespace libtest diff --git a/libtest/fatal.hpp b/libtest/fatal.hpp deleted file mode 100644 index c679f9b0..00000000 --- a/libtest/fatal.hpp +++ /dev/null @@ -1,86 +0,0 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Data Differential YATL (i.e. libtest) library - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * 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. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT - * OWNER 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. - * - */ - -#pragma once - -#include - -namespace libtest { - -class disconnected : public std::runtime_error -{ -public: - disconnected(const char *file, int line, const char *func, const std::string&, const in_port_t port, ...); - - const char* what() const throw() - { - return _error_message; - } - - disconnected(const disconnected&); - - // The following are just for unittesting the exception class - static bool is_disabled(); - static void disable(); - static void enable(); - static uint32_t disabled_counter(); - static void increment_disabled_counter(); - - int line() const - { - return _line; - } - - const char* file() const - { - return _file; - } - - const char* func() const - { - return _func; - } - -private: - char _error_message[BUFSIZ]; - in_port_t _port; - char _instance[BUFSIZ]; - int _line; - const char* _file; - const char* _func; -}; - -} // namespace libtest diff --git a/libtest/has.cc b/libtest/has.cc index a7a09cb4..fd25550e 100644 --- a/libtest/has.cc +++ b/libtest/has.cc @@ -143,7 +143,7 @@ static char memcached_binary_path[FILENAME_MAX]; static void initialize_curl_startup() { - memcached_binary_path[0]= NULL; + memcached_binary_path[0]= 0; #if defined(HAVE_MEMCACHED_BINARY) && HAVE_MEMCACHED_BINARY if (HAVE_MEMCACHED_BINARY) diff --git a/libtest/include.am b/libtest/include.am index c9368c4f..9ef6d0ba 100644 --- a/libtest/include.am +++ b/libtest/include.am @@ -67,8 +67,9 @@ noinst_HEADERS+= libtest/comparison.hpp noinst_HEADERS+= libtest/core.h noinst_HEADERS+= libtest/dream.h noinst_HEADERS+= libtest/error.h -noinst_HEADERS+= libtest/failed.h -noinst_HEADERS+= libtest/fatal.hpp +noinst_HEADERS+= libtest/exception.hpp +noinst_HEADERS+= libtest/exception/disconnected.hpp +noinst_HEADERS+= libtest/exception/fatal.hpp noinst_HEADERS+= libtest/framework.h noinst_HEADERS+= libtest/gearmand.h noinst_HEADERS+= libtest/drizzled.h @@ -86,7 +87,6 @@ noinst_HEADERS+= libtest/port.h noinst_HEADERS+= libtest/result.hpp noinst_HEADERS+= libtest/result/base.hpp noinst_HEADERS+= libtest/result/fail.hpp -noinst_HEADERS+= libtest/result/fatal.hpp noinst_HEADERS+= libtest/result/skip.hpp noinst_HEADERS+= libtest/result/success.hpp noinst_HEADERS+= libtest/runner.h @@ -132,7 +132,8 @@ libtest_libtest_la_SOURCES+= libtest/cpu.cc libtest_libtest_la_SOURCES+= libtest/dns.cc libtest_libtest_la_SOURCES+= libtest/dream.cc libtest_libtest_la_SOURCES+= libtest/drizzled.cc -libtest_libtest_la_SOURCES+= libtest/fatal.cc +libtest_libtest_la_SOURCES+= libtest/exception.cc +libtest_libtest_la_SOURCES+= libtest/exception/fatal.cc libtest_libtest_la_SOURCES+= libtest/formatter.cc libtest_libtest_la_SOURCES+= libtest/client.cc libtest_libtest_la_SOURCES+= libtest/framework.cc diff --git a/libtest/result.cc b/libtest/result.cc index a102b4f7..3e4523d3 100644 --- a/libtest/result.cc +++ b/libtest/result.cc @@ -41,53 +41,9 @@ namespace libtest { __test_result::__test_result(const char *file_arg, int line_arg, const char *func_arg): - _line(line_arg), - _file(file_arg), - _func(func_arg), - _error_message(NULL), - _error_message_size(0) -{ -} - -#ifndef __INTEL_COMPILER -# pragma GCC diagnostic ignored "-Wformat-nonliteral" -#endif -void __test_result::init(va_list args_) -{ - const char *format= va_arg(args_, const char *); - _error_message_size= vasprintf(&_error_message, format, args_); - assert(_error_message_size != -1); - if (_error_message_size > 0) + libtest::exception(file_arg, line_arg, func_arg) { - _error_message_size++; } -} - -__test_result::~__test_result() throw() -{ - free(_error_message); -} - -__test_result::__test_result(const __test_result& other) : - std::exception(), - _line(other._line), - _file(other._file), - _func(other._func), - _error_message_size(other._error_message_size) -{ - if (_error_message_size > 0) - { - _error_message= (char*) malloc(_error_message_size); - if (_error_message) - { - memcpy(_error_message, other._error_message, _error_message_size); - } - else - { - _error_message_size= -1; - } - } -} __success::__success(const char *file_arg, int line_arg, const char *func_arg): __test_result(file_arg, line_arg, func_arg) diff --git a/libtest/result.hpp b/libtest/result.hpp index 0c78b998..79acbb40 100644 --- a/libtest/result.hpp +++ b/libtest/result.hpp @@ -36,10 +36,8 @@ #pragma once -#include #include #include -#include #include #include @@ -56,27 +54,3 @@ do \ { \ throw libtest::__failure(LIBYATL_DEFAULT_PARAM, __VA_ARGS__); \ } while (0) - -#define FATAL(...) \ -do \ -{ \ - throw libtest::fatal(LIBYATL_DEFAULT_PARAM, __VA_ARGS__); \ -} while (0) - -#define FATAL_IF(__expression, ...) \ -do \ -{ \ - if ((__expression)) { \ - throw libtest::fatal(LIBYATL_DEFAULT_PARAM, (#__expression)); \ - } \ -} while (0) - -#define FATAL_IF_(__expression, ...) \ -do \ -{ \ - if ((__expression)) { \ - throw libtest::fatal(LIBYATL_DEFAULT_PARAM, __VA_ARGS__); \ - } \ -} while (0) - -#define fatal_assert(__assert) if((__assert)) {} else { throw libtest::fatal(LIBYATL_DEFAULT_PARAM, #__assert); } diff --git a/libtest/result/base.hpp b/libtest/result/base.hpp index 51ee995d..5f093f83 100644 --- a/libtest/result/base.hpp +++ b/libtest/result/base.hpp @@ -36,51 +36,19 @@ #pragma once +#include "libtest/exception.hpp" +#include "libtest/error.h" + namespace libtest { -class __test_result : public std::exception +class __test_result : public libtest::exception { public: __test_result(const char *file, int line, const char *func); - __test_result( const __test_result& ); - - virtual ~__test_result() throw(); - - virtual const char* what() const throw() - { - if (_error_message) - { - return _error_message; - } - - return ""; - } - - int line() const - { - return _line; - } - - const char* file() const - { - return _file; - } - - const char* func() const - { - return _func; - } - -protected: - void init(va_list); + virtual test_return_t return_code() const= 0; private: - int _line; - const char* _file; - const char* _func; - char* _error_message; - int _error_message_size; }; } // namespace libtest diff --git a/libtest/result/fail.hpp b/libtest/result/fail.hpp index fd8766a9..3aec9c1e 100644 --- a/libtest/result/fail.hpp +++ b/libtest/result/fail.hpp @@ -45,6 +45,11 @@ public: __failure(const __failure&); + test_return_t return_code() const + { + return TEST_FAILURE; + } + private: }; diff --git a/libtest/result/fatal.hpp b/libtest/result/fatal.hpp deleted file mode 100644 index 8e6d1346..00000000 --- a/libtest/result/fatal.hpp +++ /dev/null @@ -1,58 +0,0 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Data Differential YATL (i.e. libtest) library - * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * 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. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT - * OWNER 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. - * - */ - -#pragma once - -namespace libtest { - -class fatal : public __test_result -{ -public: - fatal(const char *file, int line, const char *func, ...); - - fatal(const fatal&); - - // The following are just for unittesting the exception class - static bool is_disabled() throw(); - static void disable() throw(); - static void enable() throw(); - static uint32_t disabled_counter() throw(); - static void increment_disabled_counter() throw(); - -private: -}; - -} // namespace libtest diff --git a/libtest/result/skip.hpp b/libtest/result/skip.hpp index d0226e9f..7409d718 100644 --- a/libtest/result/skip.hpp +++ b/libtest/result/skip.hpp @@ -44,6 +44,11 @@ public: __skipped(const char *file, int line, const char *func, ...); __skipped(const __skipped&); + + test_return_t return_code() const + { + return TEST_SKIPPED; + } }; } // namespace libtest diff --git a/libtest/result/success.hpp b/libtest/result/success.hpp index 2931f2d0..a34e677e 100644 --- a/libtest/result/success.hpp +++ b/libtest/result/success.hpp @@ -48,6 +48,11 @@ public: return "SUCCESS"; } + test_return_t return_code() const + { + return TEST_SUCCESS; + } + private: }; diff --git a/libtest/test.hpp b/libtest/test.hpp index 7c18d6bc..d9040700 100644 --- a/libtest/test.hpp +++ b/libtest/test.hpp @@ -65,7 +65,10 @@ #include #include -#include +#include +#include +#include +#include #include #include