From c6bd8565ecc054fa3a393f7df04b1d44836c3a79 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Wed, 2 Apr 2008 17:56:14 +0000 Subject: Encoding/decoding for new types: amqp_0_10::Map, amqp_0_10:UnknownType git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk@643995 13f79535-47bb-0310-9956-ffa450edef68 --- qpid/cpp/src/tests/Makefile.am | 3 +- qpid/cpp/src/tests/amqp_0_10/Map.cpp | 99 ++++++++++++++++++++++++++++++++++++ qpid/cpp/src/tests/serialize.cpp | 15 +++--- 3 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 qpid/cpp/src/tests/amqp_0_10/Map.cpp (limited to 'qpid/cpp/src/tests') diff --git a/qpid/cpp/src/tests/Makefile.am b/qpid/cpp/src/tests/Makefile.am index ca25ced5e0..ae47241e67 100644 --- a/qpid/cpp/src/tests/Makefile.am +++ b/qpid/cpp/src/tests/Makefile.am @@ -41,7 +41,8 @@ unit_test_SOURCES= unit_test.cpp unit_test.h \ SequenceSet.cpp \ serialize.cpp \ ProxyTemplate.cpp apply.cpp BoundedIterator.cpp \ - IncompleteMessageList.cpp + IncompleteMessageList.cpp \ + amqp_0_10/Map.cpp check_LTLIBRARIES += libshlibtest.la libshlibtest_la_LDFLAGS = -module -rpath $(abs_builddir) diff --git a/qpid/cpp/src/tests/amqp_0_10/Map.cpp b/qpid/cpp/src/tests/amqp_0_10/Map.cpp new file mode 100644 index 0000000000..958470f6d7 --- /dev/null +++ b/qpid/cpp/src/tests/amqp_0_10/Map.cpp @@ -0,0 +1,99 @@ +/* + * + * 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 "unit_test.h" +#include "qpid/amqp_0_10/Map.h" +#include "qpid/amqp_0_10/Codec.h" +#include + +using namespace qpid::amqp_0_10; +using namespace std; + +namespace std { +// Dummy += for back inserters so we can use them with the decoder. +template back_insert_iterator& operator+=(back_insert_iterator& bi, size_t) { return bi; } +} + +QPID_AUTO_TEST_SUITE(MapTestSuite) + + BOOST_AUTO_TEST_CASE(testGetSet) { + MapValue v; + v = Str8("foo"); + BOOST_CHECK(v.get()); + BOOST_CHECK(!v.get()); + BOOST_CHECK_EQUAL(*v.get(), "foo"); + + v = uint8_t(42); + BOOST_CHECK(!v.get()); + BOOST_CHECK(v.get()); + BOOST_CHECK_EQUAL(*v.get(), 42); + + v = uint16_t(12); + BOOST_CHECK(v.get()); + BOOST_CHECK_EQUAL(*v.get(), 12); +} + +template struct TestVisitor : public MapValue::Visitor { + template R operator()(const T&) const { throw MapValue::BadTypeException(); } + R operator()(const R& r) const { return r; } +}; + +BOOST_AUTO_TEST_CASE(testVisit) { + MapValue v; + v = Str8("foo"); + BOOST_CHECK_EQUAL(v.apply_visitor(TestVisitor()), "foo"); + v = Uint16(42); + BOOST_CHECK_EQUAL(v.apply_visitor(TestVisitor()), 42); + try { + v.apply_visitor(TestVisitor()); + BOOST_FAIL("Expecting exception"); + } + catch(const MapValue::BadTypeException&) {} +} + + +BOOST_AUTO_TEST_CASE(testEncodeMapValue) { + MapValue mv; + std::string data; + mv = Str8("hello"); + Codec::encode(back_inserter(data))(mv); + BOOST_CHECK_EQUAL(data.size(), Codec::size(mv)); + MapValue mv2; + Codec::decode(data.begin())(mv2); + BOOST_CHECK_EQUAL(mv2.getCode(), 0x85); + BOOST_REQUIRE(mv2.get()); + BOOST_CHECK_EQUAL(*mv2.get(), "hello"); +} + +BOOST_AUTO_TEST_CASE(testEncode) { + Map map; + std::string data; + map["A"] = true; + map["b"] = Str8("hello"); + Codec::encode(back_inserter(data))(map); + Map map2; + Codec::decode(data.begin())(map2); + BOOST_CHECK_EQUAL(map.size(), 2u); + BOOST_CHECK(map["A"].get()); + BOOST_CHECK_EQUAL(*map["b"].get(), "hello"); +} + + +QPID_AUTO_TEST_SUITE_END() diff --git a/qpid/cpp/src/tests/serialize.cpp b/qpid/cpp/src/tests/serialize.cpp index 228a4b6e3a..da71917cbd 100644 --- a/qpid/cpp/src/tests/serialize.cpp +++ b/qpid/cpp/src/tests/serialize.cpp @@ -25,6 +25,7 @@ #include "qpid/amqp_0_10/specification.h" #include "qpid/amqp_0_10/ControlHolder.h" #include "qpid/amqp_0_10/Frame.h" +#include "qpid/amqp_0_10/Map.h" #include #include @@ -76,16 +77,13 @@ template struct concat2 { typedef typename mpl::copy struct concat3 { typedef typename concat2::type>::type type; }; template struct concat4 { typedef typename concat2::type>::type type; }; -typedef mpl::vector::type IntegralTypes; +typedef mpl::vector::type IntegralTypes; typedef mpl::vector::type BinTypes; -// FIXME aconway 2008-03-07: float encoding typedef mpl::vector::type FloatTypes; typedef mpl::vector FixedSizeClassTypes; -typedef mpl::vector VariableSizeTypes; +typedef mpl::vector VariableSizeTypes; - -// FIXME aconway 2008-03-07: float encoding -typedef concat3::type FixedSizeTypes; +typedef concat4::type FixedSizeTypes; typedef concat2::type AllTypes; // TODO aconway 2008-02-20: should test 64 bit integrals for order also. @@ -107,19 +105,22 @@ BOOST_AUTO_TEST_CASE(testNetworkByteOrder) { // Assign test values to the various types. void testValue(bool& b) { b = true; } +void testValue(Bit&) { } template typename boost::enable_if >::type testValue(T& n) { n=42; } +void testValue(CharUtf32& c) { c = 43; } void testValue(long long& l) { l = 0x012345; } void testValue(Datetime& dt) { dt = qpid::sys::now(); } void testValue(Uuid& uuid) { uuid=Uuid(true); } template void testValue(Decimal& d) { d.exponent=2; d.mantissa=0x1122; } void testValue(SequenceNo& s) { s = 42; } template void testValue(Bin& a) { a.assign(42); } -template void testValue(SerializableString& s) { +template void testValue(SerializableString& s) { char msg[]="foobar"; s.assign(msg, msg+sizeof(msg)); } void testValue(Str16& s) { s = "the quick brown fox jumped over the lazy dog"; } void testValue(Str8& s) { s = "foobar"; } +void testValue(Map& m) { m["s"] = Str8("foobar"); m["b"] = true; m["c"] = uint16_t(42); } //typedef mpl::vector::type TestTypes; BOOST_AUTO_TEST_CASE_TEMPLATE(testEncodeDecode, T, AllTypes) -- cgit v1.2.1