| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include "proton/map.hpp" |
| 22 |
#include "test_bits.hpp" |
| 23 |
|
| 24 |
#include <string> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
namespace { |
| 28 |
|
| 29 |
using namespace std; |
| 30 |
using namespace proton; |
| 31 |
|
| 32 |
void test_empty() { |
| 33 |
proton::map<string, scalar> m; |
| 34 |
ASSERT_EQUAL(0U, m.size()); |
| 35 |
ASSERT(m.empty()); |
| 36 |
ASSERT_EQUAL(0U, m.erase("x")); |
| 37 |
ASSERT(!m.exists("x")); |
| 38 |
|
| 39 |
std::map<string, scalar> sm; |
| 40 |
proton::get(m, sm); |
| 41 |
ASSERT(sm.empty()); |
| 42 |
} |
| 43 |
|
| 44 |
void test_use() { |
| 45 |
proton::map<string, scalar> m; |
| 46 |
|
| 47 |
m.put("x", "y"); |
| 48 |
ASSERT_EQUAL(scalar("y"), m.get("x")); |
| 49 |
ASSERT(!m.empty()); |
| 50 |
ASSERT(m.exists("x")); |
| 51 |
ASSERT_EQUAL(1U, m.size()); |
| 52 |
|
| 53 |
m.put("a", "b"); |
| 54 |
ASSERT_EQUAL(scalar("b"), m.get("a")); |
| 55 |
ASSERT_EQUAL(2U, m.size()); |
| 56 |
|
| 57 |
ASSERT_EQUAL(1U, m.erase("x")); |
| 58 |
ASSERT_EQUAL(1U, m.size()); |
| 59 |
ASSERT(!m.exists("x")); |
| 60 |
ASSERT_EQUAL(scalar("b"), m.get("a")); |
| 61 |
|
| 62 |
m.clear(); |
| 63 |
ASSERT(m.empty()); |
| 64 |
} |
| 65 |
|
| 66 |
void test_cppmap() { |
| 67 |
std::map<string, scalar> sm; |
| 68 |
sm["a"] = 2; |
| 69 |
sm["b"] = 3; |
| 70 |
proton::map<string, scalar> m; |
| 71 |
m = sm; |
| 72 |
ASSERT_EQUAL(scalar(2), m.get("a")); |
| 73 |
ASSERT_EQUAL(scalar(3), m.get("b")); |
| 74 |
ASSERT_EQUAL(2U, m.size()); |
| 75 |
|
| 76 |
std::map<string, scalar> sm2; |
| 77 |
proton::get(m, sm2); |
| 78 |
ASSERT_EQUAL(2U, sm2.size()); |
| 79 |
ASSERT_EQUAL(scalar(2), sm2["a"]); |
| 80 |
ASSERT_EQUAL(scalar(3), sm2["b"]); |
| 81 |
|
| 82 |
// Round trip: |
| 83 |
value v = m.value(); |
| 84 |
proton::map<string, scalar> m2; |
| 85 |
m2.value(v); |
| 86 |
|
| 87 |
// Use a vector as map storage |
| 88 |
vector<pair<string, scalar> > vm; |
| 89 |
|
| 90 |
vm.push_back(std::make_pair(string("x"), 8)); |
| 91 |
vm.push_back(std::make_pair(string("y"), 9)); |
| 92 |
m.value(vm); // Can't use type-safe op=, not enabled |
| 93 |
ASSERT_EQUAL(scalar(8), m.get("x")); |
| 94 |
ASSERT_EQUAL(scalar(9), m.get("y")); |
| 95 |
ASSERT_EQUAL(2U, m.size()); |
| 96 |
|
| 97 |
vm.clear(); |
| 98 |
proton::get(m, vm); |
| 99 |
ASSERT_EQUAL(2U, vm.size()); |
| 100 |
ASSERT_EQUAL(string("x"), vm[0].first); |
| 101 |
ASSERT_EQUAL(scalar(8), vm[0].second); |
| 102 |
} |
| 103 |
|
| 104 |
void test_value() { |
| 105 |
proton::map<string, scalar> m; |
| 106 |
value v; |
| 107 |
v = "foo"; |
| 108 |
ASSERT_THROWS(conversion_error, m.value(v)); |
| 109 |
std::map<int, float> bad; |
| 110 |
// Wrong type of map. |
| 111 |
// Note we can't detect an empty map of bad type because AMQP maps allow |
| 112 |
// mixed types, so there must be data to object to. |
| 113 |
bad[1]=1.0; |
| 114 |
ASSERT_THROWS(conversion_error, m.value(bad)); |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
int main(int, char**) { |
| 120 |
int failed = 0; |
| 121 |
RUN_TEST(failed, test_empty()); |
| 122 |
RUN_TEST(failed, test_use()); |
| 123 |
RUN_TEST(failed, test_cppmap()); |
| 124 |
RUN_TEST(failed, test_value()); |
| 125 |
return failed; |
| 126 |
} |