From: Brian Aker Date: Tue, 11 Oct 2011 20:04:15 +0000 (-0700) Subject: Streamline how we are handling the printing of error types. X-Git-Tag: 1.0.2~6^2^2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=419e8e264c48ed4543674dd67380bddf37354771;p=m6w6%2Flibmemcached Streamline how we are handling the printing of error types. --- diff --git a/libmemcached-1.0/memcached.hpp b/libmemcached-1.0/memcached.hpp index 66d62524..33dcdc1f 100644 --- a/libmemcached-1.0/memcached.hpp +++ b/libmemcached-1.0/memcached.hpp @@ -215,7 +215,7 @@ public: // Actual value, null terminated ret_val.reserve(memcached_result_length(result) +1); - ret_val.assign(memcached_result_value(result), + ret_val.assign(memcached_result_value(result), memcached_result_value(result) +memcached_result_length(result)); // Misc @@ -255,7 +255,7 @@ public: if (value != NULL && ret_val.empty()) { ret_val.reserve(value_length); - ret_val.assign(value, value + value_length); + ret_val.assign(value, value +value_length); free(value); return true; } @@ -290,7 +290,7 @@ public: if (value) { ret_val.reserve(value_length); - ret_val.assign(value, value + value_length); + ret_val.assign(value, value +value_length); free(value); return true; } diff --git a/libmemcachedutil-1.0/include.am b/libmemcachedutil-1.0/include.am index c5576058..39a15937 100644 --- a/libmemcachedutil-1.0/include.am +++ b/libmemcachedutil-1.0/include.am @@ -5,6 +5,7 @@ nobase_include_HEADERS+= \ libmemcachedutil-1.0/flush.h \ + libmemcachedutil-1.0/ostream.hpp \ libmemcachedutil-1.0/pid.h \ libmemcachedutil-1.0/ping.h \ libmemcachedutil-1.0/pool.h \ diff --git a/libmemcachedutil-1.0/ostream.hpp b/libmemcachedutil-1.0/ostream.hpp new file mode 100644 index 00000000..41134140 --- /dev/null +++ b/libmemcachedutil-1.0/ostream.hpp @@ -0,0 +1,43 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached library + * + * Copyright (C) 2011 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 + +static inline std::ostream& operator<<(std::ostream& output, const enum memcached_return_t &arg) +{ + output << memcached_strerror(NULL, arg); + return output; +} diff --git a/libtest/comparison.hpp b/libtest/comparison.hpp index eb240d47..33cfa5a5 100644 --- a/libtest/comparison.hpp +++ b/libtest/comparison.hpp @@ -24,7 +24,8 @@ #include #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED -#include +#include +#include #endif #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN @@ -46,43 +47,11 @@ bool _compare_truth_hint(const char *file, int line, const char *func, T_compara } template -bool _compare(const char *file, int line, const char *func, const T1_comparable __expected, const T2_comparable __actual) +bool _compare(const char *file, int line, const char *func, const T1_comparable& __expected, const T2_comparable& __actual) { if (__expected != __actual) { - if (typeid(__expected) == typeid(test_return_t)) - { - const char *expected_str= test_strerror(test_return_t(__expected)); - const char *got_str= test_strerror(test_return_t(__actual)); - - libtest::stream::make_cerr(file, line, func) << "Expected \"" - << expected_str - << "\" got \"" - << got_str - << "\""; - } -#if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED - else if (typeid(__expected) == typeid(memcached_return_t)) - { - libtest::stream::make_cerr(file, line, func) << "Expected \"" - << memcached_strerror(NULL, memcached_return_t(__expected)) - << "\" got \"" - << memcached_strerror(NULL, memcached_return_t(__actual)) << "\""; - } -#endif -#if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN - else if (typeid(__expected) == typeid(gearman_return_t)) - { - libtest::stream::make_cerr(file, line, func) << "Expected \"" - << gearman_strerror(gearman_return_t(__expected)) - << "\" got \"" - << gearman_strerror(gearman_return_t(__actual)) << "\""; - } -#endif - else - { - libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\""; - } + libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\""; return false; } @@ -118,43 +87,8 @@ bool _compare_hint(const char *file, int line, const char *func, T1_comparable _ { if (__expected != __actual) { - if (typeid(__expected) == typeid(test_return_t)) - { - const char *expected_str= test_strerror(test_return_t(__expected)); - const char *got_str= test_strerror(test_return_t(__actual)); - - libtest::stream::make_cerr(file, line, func) << "Expected \"" - << expected_str - << "\" got \"" - << got_str - << "\"" - << " Additionally: \"" << __hint << "\""; - } -#if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED - else if (typeid(__expected) == typeid(memcached_return_t)) - { - libtest::stream::make_cerr(file, line, func) << "Expected \"" - << memcached_strerror(NULL, memcached_return_t(__expected)) - << "\" got \"" - << memcached_strerror(NULL, memcached_return_t(__actual)) << "\"" - << " Additionally: \"" << __hint << "\""; - } -#endif -#if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN - else if (typeid(__expected) == typeid(gearman_return_t)) - { - libtest::stream::make_cerr(file, line, func) << "Expected \"" - << gearman_strerror(gearman_return_t(__expected)) - << "\" got \"" - << gearman_strerror(gearman_return_t(__actual)) << "\"" - << " Additionally: \"" << __hint << "\""; - } -#endif - else - { - libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\"" - << " Additionally: \"" << __hint << "\""; - } + libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\"" << " Additionally: \"" << __hint << "\""; + return false; } diff --git a/libtest/strerror.cc b/libtest/strerror.cc index 74440f51..e4f0cf50 100644 --- a/libtest/strerror.cc +++ b/libtest/strerror.cc @@ -46,3 +46,16 @@ const char *test_strerror(test_return_t code) } } // namespace libtest + + +std::ostream& operator<<(std::ostream& output, const enum test_return_t &arg) +{ + output << libtest::test_strerror(arg); + return output; +} + +std::ostream& operator<<(std::ostream& output, const std::vector &arg) +{ + output << "std::vector:" << arg.size(); + return output; +} diff --git a/libtest/strerror.h b/libtest/strerror.h index 861c84b6..7dc3cf5e 100644 --- a/libtest/strerror.h +++ b/libtest/strerror.h @@ -21,6 +21,9 @@ #pragma once +#include +#include + namespace libtest { /** @@ -30,3 +33,7 @@ LIBTEST_API const char *test_strerror(test_return_t code); } // namespace libtest + +std::ostream& operator<<(std::ostream& output, const enum test_return_t &arg); +std::ostream& operator<<(std::ostream& output, const std::vector &arg); + diff --git a/support/libmemcached.spec.in b/support/libmemcached.spec.in index 449ee0b3..e610adaa 100644 --- a/support/libmemcached.spec.in +++ b/support/libmemcached.spec.in @@ -134,6 +134,7 @@ you will need to install %{name}-devel. %{_includedir}/libmemcachedutil-1.0/flush.h %{_includedir}/libmemcachedutil-1.0/pid.h %{_includedir}/libmemcachedutil-1.0/ping.h +%{_includedir}/libmemcachedutil-1.0/ostream.hpp %{_includedir}/libmemcachedutil-1.0/pool.h %{_includedir}/libmemcachedutil-1.0/version.h diff --git a/tests/plus.cpp b/tests/plus.cpp index e020cdd9..7de8164f 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -86,6 +86,7 @@ static test_return_t basic_test(memcached_st *memc) test_true(foo.set("mine", value, 0, 0)); test_true(foo.get("mine", test_value)); + test_compare(test_value.size(), value.size()); test_memcmp(&test_value[0], &value[0], test_value.size()); test_false(foo.set("", value, 0, 0));