summaryrefslogtreecommitdiff
path: root/cpp/src/tests/amqp_0_10/Map.cpp
blob: ffb235829e6043e383cee27bff005bb154b82c09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 *
 * 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 "amqp_0_10/unit_test.h"
#include "qpid/amqp_0_10/Map.h"
#include "qpid/amqp_0_10/Array.h"
#include "qpid/amqp_0_10/Struct32.h"
#include "qpid/amqp_0_10/UnknownType.h"
#include "qpid/amqp_0_10/Codec.h"
#include <iostream>

using namespace qpid::amqp_0_10;
using namespace std;

QPID_AUTO_TEST_SUITE(MapTestSuite)

 QPID_AUTO_TEST_CASE(testGetSet) {
    MapValue v;
    v = Str8("foo");
    BOOST_CHECK(v.get<Str8>());
    BOOST_CHECK(!v.get<uint8_t>());
    BOOST_CHECK_EQUAL(*v.get<Str8>(), "foo");
    
    v = uint8_t(42);
    BOOST_CHECK(!v.get<Str8>());
    BOOST_CHECK(v.get<uint8_t>());
    BOOST_CHECK_EQUAL(*v.get<uint8_t>(), 42);

    v = uint16_t(12);
    BOOST_CHECK(v.get<uint16_t>());
    BOOST_CHECK_EQUAL(*v.get<uint16_t>(), 12);
}

template <class R> struct TestVisitor : public MapValue::Visitor<R> {
    template <class T> R operator()(const T&) const { throw MapValue::BadTypeException(); }
    R operator()(const R& r) const { return r; }
};
    
QPID_AUTO_TEST_CASE(testVisit) {
    MapValue v;
    v = Str8("foo");
    BOOST_CHECK_EQUAL(v.apply_visitor(TestVisitor<Str8>()), "foo");
    v = Uint16(42);
    BOOST_CHECK_EQUAL(v.apply_visitor(TestVisitor<Uint16>()), 42);
    try {
        v.apply_visitor(TestVisitor<bool>());
        BOOST_FAIL("Expecting exception");
    }
    catch(const MapValue::BadTypeException&) {}
}


QPID_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<Str8>());
    BOOST_CHECK_EQUAL(*mv2.get<Str8>(), "hello");
}

QPID_AUTO_TEST_CASE(testEncode) {
    Map map;
    std::string data;
    map["A"] = true;
    map["b"] = Str8("hello");
    Codec::encode(back_inserter(data))(map);
    BOOST_CHECK_EQUAL(Codec::size(map), data.size());
    Map map2;
    Codec::decode(data.begin())(map2);
    BOOST_CHECK_EQUAL(map.size(), 2u);
    BOOST_CHECK(map["A"].get<bool>());
    BOOST_CHECK_EQUAL(*map["b"].get<Str8>(), "hello");
}


QPID_AUTO_TEST_SUITE_END()