Repository: qpid-proton Updated Branches: refs/heads/master bf7e19309 -> 8a35409a9
PROTON-1065: c++: add test to verify PROTON-1065, add as_xxx() members to proton::value. Tested on linux and windows VS 9. Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/8a35409a Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/8a35409a Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/8a35409a Branch: refs/heads/master Commit: 8a35409a90a676e0b31acc32f7ebc1cd191f4a56 Parents: bf7e193 Author: Alan Conway <[email protected]> Authored: Wed Dec 30 16:50:56 2015 -0500 Committer: Alan Conway <[email protected]> Committed: Wed Dec 30 17:02:18 2015 -0500 ---------------------------------------------------------------------- proton-c/bindings/cpp/CMakeLists.txt | 2 +- proton-c/bindings/cpp/include/proton/value.hpp | 9 ++ .../bindings/cpp/src/encode_decode_test.cpp | 93 -------------------- proton-c/bindings/cpp/src/message_test.cpp | 14 +++ proton-c/bindings/cpp/src/value.cpp | 6 ++ proton-c/bindings/cpp/src/value_test.cpp | 93 ++++++++++++++++++++ 6 files changed, 123 insertions(+), 94 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8a35409a/proton-c/bindings/cpp/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/CMakeLists.txt b/proton-c/bindings/cpp/CMakeLists.txt index 10d40f8..732a8c7 100644 --- a/proton-c/bindings/cpp/CMakeLists.txt +++ b/proton-c/bindings/cpp/CMakeLists.txt @@ -169,5 +169,5 @@ endmacro(add_cpp_test) add_cpp_test(interop_test ${CMAKE_SOURCE_DIR}/tests) add_cpp_test(message_test) -add_cpp_test(encode_decode_test) +add_cpp_test(value_test) add_cpp_test(scalar_test) http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8a35409a/proton-c/bindings/cpp/include/proton/value.hpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/include/proton/value.hpp b/proton-c/bindings/cpp/include/proton/value.hpp index 1ecaaa5..3c4cecd 100644 --- a/proton-c/bindings/cpp/include/proton/value.hpp +++ b/proton-c/bindings/cpp/include/proton/value.hpp @@ -73,6 +73,15 @@ class value { /** Get the value. */ template<class T> T get() const { T t; get(t); return t; } + ///@name as_ methods do "loose" conversion, they will convert the scalar + ///value to the requested type if possible, else throw type_error + ///@{ + PN_CPP_EXTERN int64_t as_int() const; ///< Allowed if type_id_is_integral(type()) + PN_CPP_EXTERN uint64_t as_uint() const; ///< Allowed if type_id_is_integral(type()) + PN_CPP_EXTERN double as_double() const; ///< Allowed if type_id_is_floating_point(type()) + PN_CPP_EXTERN std::string as_string() const; ///< Allowed if type_id_is_string_like(type()) + ///@} + PN_CPP_EXTERN void swap(value& v); friend PN_CPP_EXTERN bool operator==(const value& x, const value& y); http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8a35409a/proton-c/bindings/cpp/src/encode_decode_test.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/encode_decode_test.cpp b/proton-c/bindings/cpp/src/encode_decode_test.cpp deleted file mode 100644 index 8c285ac..0000000 --- a/proton-c/bindings/cpp/src/encode_decode_test.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "test_bits.hpp" - -#include <proton/value.hpp> -#include <algorithm> -#include <iostream> -#include <iterator> -#include <map> -#include <sstream> -#include <vector> - -using namespace std; -using namespace proton; - -// Inserting and extracting simple C++ values. -template <class T> void value_test(T x, type_id tid, const std::string& s, T y) { - value v(x); - ASSERT_EQUAL(tid, v.type()); - ASSERT_EQUAL(x, v.get<T>()); - - value v2; - v2 = x; - ASSERT_EQUAL(tid, v2.type()); - ASSERT_EQUAL(x, v2.get<T>()); - - ASSERT_EQUAL(v, v2); - ASSERT_EQUAL(s, str(v)); - ASSERT(x != y); - ASSERT(x < y); - ASSERT(y > x); -} - -// Map values -void map_test() { - std::map<string, int> m; - m["a"] = 1; - m["b"] = 2; - m["c"] = 3; - value v = m; - ASSERT_EQUAL("{\"a\"=1, \"b\"=2, \"c\"=3}", str(v)); - std::map<value, value> mv; - v.get(mv); - ASSERT_EQUAL(mv["a"], value(amqp_int(1))); - mv["b"] = amqp_binary("xyz"); - mv.erase("c"); - v = value(mv); - ASSERT_EQUAL("{\"a\"=1, \"b\"=b\"xyz\"}", str(v)); - - std::vector<std::pair<string, value> > vec; - v.get(to_pairs(vec)); - ASSERT_EQUAL(2, vec.size()); - ASSERT_EQUAL(std::make_pair(std::string("a"), value(1)), vec[0]); - ASSERT_EQUAL(std::make_pair(std::string("b"), value(amqp_binary("xyz"))), vec[1]); -} - -int main(int, char**) { - int failed = 0; - RUN_TEST(failed, value_test(false, BOOLEAN, "false", true)); - RUN_TEST(failed, value_test(amqp_ubyte(42), UBYTE, "42", amqp_ubyte(50))); - RUN_TEST(failed, value_test(amqp_byte(-42), BYTE, "-42", amqp_byte(-40))); - RUN_TEST(failed, value_test(amqp_ushort(4242), USHORT, "4242", amqp_ushort(5252))); - RUN_TEST(failed, value_test(amqp_short(-4242), SHORT, "-4242", amqp_short(3))); - RUN_TEST(failed, value_test(amqp_uint(4242), UINT, "4242", amqp_uint(5252))); - RUN_TEST(failed, value_test(amqp_int(-4242), INT, "-4242", amqp_int(3))); - RUN_TEST(failed, value_test(amqp_ulong(4242), ULONG, "4242", amqp_ulong(5252))); - RUN_TEST(failed, value_test(amqp_long(-4242), LONG, "-4242", amqp_long(3))); - RUN_TEST(failed, value_test(amqp_float(1.234), FLOAT, "1.234", amqp_float(2.345))); - RUN_TEST(failed, value_test(amqp_double(11.2233), DOUBLE, "11.2233", amqp_double(12))); - RUN_TEST(failed, value_test(amqp_string("aaa"), STRING, "aaa", amqp_string("aaaa"))); - RUN_TEST(failed, value_test(std::string("xxx"), STRING, "xxx", std::string("yyy"))); - RUN_TEST(failed, value_test(amqp_symbol("aaa"), SYMBOL, "aaa", amqp_symbol("aaaa"))); - RUN_TEST(failed, value_test(amqp_binary("aaa"), BINARY, "b\"aaa\"", amqp_binary("aaaa"))); - RUN_TEST(failed, map_test()); - return failed; -} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8a35409a/proton-c/bindings/cpp/src/message_test.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/message_test.cpp b/proton-c/bindings/cpp/src/message_test.cpp index 9f05d1c..d3509aa 100644 --- a/proton-c/bindings/cpp/src/message_test.cpp +++ b/proton-c/bindings/cpp/src/message_test.cpp @@ -88,6 +88,19 @@ void test_message_properties() { ASSERT_EQUAL(4242, m.creation_time().milliseconds); } +void test_message_body() { + std::string s("hello"); + message m1(s.c_str()); + ASSERT_EQUAL(s, m1.body().get<std::string>()); + message m2(s); + ASSERT_EQUAL(s, m2.body().as_string()); + message m3; + m3.body(s); + ASSERT_EQUAL(s, m3.body().as_string()); + ASSERT_EQUAL(5, message(5).body().as_int()); + ASSERT_EQUAL(3.1, message(3.1).body().as_double()); +} + void test_message_maps() { message m; @@ -126,6 +139,7 @@ void test_message_maps() { int main(int argc, char** argv) { int failed = 0; RUN_TEST(failed, test_message_properties()); + RUN_TEST(failed, test_message_body()); RUN_TEST(failed, test_message_maps()); return failed; } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8a35409a/proton-c/bindings/cpp/src/value.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/value.cpp b/proton-c/bindings/cpp/src/value.cpp index 9e8448c..bdceacc 100644 --- a/proton-c/bindings/cpp/src/value.cpp +++ b/proton-c/bindings/cpp/src/value.cpp @@ -21,6 +21,7 @@ #include "proton/data.hpp" #include "proton/value.hpp" +#include "proton/scalar.hpp" #include <ostream> @@ -71,4 +72,9 @@ std::ostream& operator<<(std::ostream& o, const value& v) { } } +int64_t value::as_int() const { return get<scalar>().as_int(); } +uint64_t value::as_uint() const { return get<scalar>().as_uint(); } +double value::as_double() const { return get<scalar>().as_double(); } +std::string value::as_string() const { return get<scalar>().as_string(); } + } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/8a35409a/proton-c/bindings/cpp/src/value_test.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/value_test.cpp b/proton-c/bindings/cpp/src/value_test.cpp new file mode 100644 index 0000000..8c285ac --- /dev/null +++ b/proton-c/bindings/cpp/src/value_test.cpp @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "test_bits.hpp" + +#include <proton/value.hpp> +#include <algorithm> +#include <iostream> +#include <iterator> +#include <map> +#include <sstream> +#include <vector> + +using namespace std; +using namespace proton; + +// Inserting and extracting simple C++ values. +template <class T> void value_test(T x, type_id tid, const std::string& s, T y) { + value v(x); + ASSERT_EQUAL(tid, v.type()); + ASSERT_EQUAL(x, v.get<T>()); + + value v2; + v2 = x; + ASSERT_EQUAL(tid, v2.type()); + ASSERT_EQUAL(x, v2.get<T>()); + + ASSERT_EQUAL(v, v2); + ASSERT_EQUAL(s, str(v)); + ASSERT(x != y); + ASSERT(x < y); + ASSERT(y > x); +} + +// Map values +void map_test() { + std::map<string, int> m; + m["a"] = 1; + m["b"] = 2; + m["c"] = 3; + value v = m; + ASSERT_EQUAL("{\"a\"=1, \"b\"=2, \"c\"=3}", str(v)); + std::map<value, value> mv; + v.get(mv); + ASSERT_EQUAL(mv["a"], value(amqp_int(1))); + mv["b"] = amqp_binary("xyz"); + mv.erase("c"); + v = value(mv); + ASSERT_EQUAL("{\"a\"=1, \"b\"=b\"xyz\"}", str(v)); + + std::vector<std::pair<string, value> > vec; + v.get(to_pairs(vec)); + ASSERT_EQUAL(2, vec.size()); + ASSERT_EQUAL(std::make_pair(std::string("a"), value(1)), vec[0]); + ASSERT_EQUAL(std::make_pair(std::string("b"), value(amqp_binary("xyz"))), vec[1]); +} + +int main(int, char**) { + int failed = 0; + RUN_TEST(failed, value_test(false, BOOLEAN, "false", true)); + RUN_TEST(failed, value_test(amqp_ubyte(42), UBYTE, "42", amqp_ubyte(50))); + RUN_TEST(failed, value_test(amqp_byte(-42), BYTE, "-42", amqp_byte(-40))); + RUN_TEST(failed, value_test(amqp_ushort(4242), USHORT, "4242", amqp_ushort(5252))); + RUN_TEST(failed, value_test(amqp_short(-4242), SHORT, "-4242", amqp_short(3))); + RUN_TEST(failed, value_test(amqp_uint(4242), UINT, "4242", amqp_uint(5252))); + RUN_TEST(failed, value_test(amqp_int(-4242), INT, "-4242", amqp_int(3))); + RUN_TEST(failed, value_test(amqp_ulong(4242), ULONG, "4242", amqp_ulong(5252))); + RUN_TEST(failed, value_test(amqp_long(-4242), LONG, "-4242", amqp_long(3))); + RUN_TEST(failed, value_test(amqp_float(1.234), FLOAT, "1.234", amqp_float(2.345))); + RUN_TEST(failed, value_test(amqp_double(11.2233), DOUBLE, "11.2233", amqp_double(12))); + RUN_TEST(failed, value_test(amqp_string("aaa"), STRING, "aaa", amqp_string("aaaa"))); + RUN_TEST(failed, value_test(std::string("xxx"), STRING, "xxx", std::string("yyy"))); + RUN_TEST(failed, value_test(amqp_symbol("aaa"), SYMBOL, "aaa", amqp_symbol("aaaa"))); + RUN_TEST(failed, value_test(amqp_binary("aaa"), BINARY, "b\"aaa\"", amqp_binary("aaaa"))); + RUN_TEST(failed, map_test()); + return failed; +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
