/* * * 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/built_in_types.h" #include "qpid/amqp_0_10/Codec.h" #include #include #include #include #include #include #include #include #include #include #include // Missing operators needed for tests. namespace boost { template std::ostream& operator<<(std::ostream& out, const array& a) { std::ostream_iterator o(out, " "); std::copy(a.begin(), a.end(), o); return out; } } // boost namespace qpid { namespace sys { std::ostream& operator<<(std::ostream& out, const AbsTime& t) { return out << t.timeValue(); } } namespace amqp_0_10 { template std::ostream& operator<<(std::ostream& out, const CodableString& str) { std::ostream_iterator o(out, " "); std::copy(str.begin(), str.end(), o); return out; } } } // qpid QPID_AUTO_TEST_SUITE(SerializeTestSuite) using namespace std; namespace mpl=boost::mpl; using namespace qpid::amqp_0_10; template struct concat2 { typedef typename mpl::copy >::type type; }; template 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 BinTypes; typedef mpl::vector::type FloatTypes; typedef mpl::vector FixedSizeClassTypes; typedef mpl::vector VariableSizeTypes; typedef concat4::type FixedSizeTypes; typedef concat2::type AllTypes; // TODO aconway 2008-02-20: should test 64 bit integrals for order also. BOOST_AUTO_TEST_CASE(testNetworkByteOrder) { string data; uint32_t l = 1234567890; Codec::encode(std::back_inserter(data), l); uint32_t enc=reinterpret_cast(*data.data()); uint32_t l2 = ntohl(enc); BOOST_CHECK_EQUAL(l, l2); data.clear(); uint16_t s = 12345; Codec::encode(std::back_inserter(data), s); uint32_t s2 = ntohs(*reinterpret_cast(data.data())); BOOST_CHECK_EQUAL(s, s2); } void testValue(bool& b) { b = true; } template typename boost::enable_if >::type testValue(T& n) { n=42; } void testValue(long long& l) { l = 12345; } 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=1234; } void testValue(SequenceNo& s) { s = 42; } template void testValue(boost::array& a) { a.assign(42); } template void testValue(CodableString& s) { char msg[]="foobar"; s.assign(msg, msg+sizeof(msg)); } // FIXME aconway 2008-02-20: test AllTypes BOOST_AUTO_TEST_CASE_TEMPLATE(testEncodeDecode, T, FixedSizeTypes) { string data; T t; testValue(t); Codec::encode(std::back_inserter(data), t); BOOST_CHECK_EQUAL(Codec::size(t), data.size()); T t2; Codec::decode(data.begin(), t2); BOOST_CHECK_EQUAL(t,t2); } QPID_AUTO_TEST_SUITE_END()