| 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 |
#include "scalar_test.hpp" |
| 21 |
|
| 22 |
namespace { |
| 23 |
|
| 24 |
using namespace std; |
| 25 |
using namespace proton; |
| 26 |
|
| 27 |
using test::many; |
| 28 |
using test::scalar_test_group; |
| 29 |
|
| 30 |
// Inserting and extracting arrays from a container T of type U |
| 31 |
template <class T> void sequence_test( |
| 32 |
type_id tid, const many<typename T::value_type>& values, const string& s) |
| 33 |
{ |
| 34 |
T x(values.begin(), values.end()); |
| 35 |
|
| 36 |
value vx(x); // construct |
| 37 |
ASSERT_EQUAL(tid, vx.type()); |
| 38 |
ASSERT_EQUAL(x, get<T>(vx)); |
| 39 |
ASSERT_EQUAL(x, coerce<T>(vx)); |
| 40 |
{ |
| 41 |
T y; |
| 42 |
get(vx, y); // Two argument get. |
| 43 |
ASSERT_EQUAL(x, y); |
| 44 |
} |
| 45 |
{ |
| 46 |
T y; |
| 47 |
coerce(vx, y); // Two argument coerce. |
| 48 |
ASSERT_EQUAL(x, y); |
| 49 |
} |
| 50 |
value v2; // assign |
| 51 |
v2 = x; |
| 52 |
ASSERT_EQUAL(tid, v2.type()); |
| 53 |
ASSERT_EQUAL(x, get<T>(v2)); |
| 54 |
ASSERT_EQUAL(x, coerce<T>(v2)); |
| 55 |
ASSERT_EQUAL(vx, v2); |
| 56 |
|
| 57 |
T y(x); |
| 58 |
typename T::iterator it = y.begin(); |
| 59 |
*y.begin() = *(++it); // Second element is bigger so y is lexicographically bigger than x |
| 60 |
value vy(y); |
| 61 |
ASSERT(vx != vy); |
| 62 |
ASSERT(vx < vy); |
| 63 |
ASSERT(vy > vx); |
| 64 |
|
| 65 |
ASSERT_EQUAL(s, to_string(vx)); |
| 66 |
} |
| 67 |
|
| 68 |
template <class T, class U> void map_test(const U& values, const string& s) { |
| 69 |
T m(values.begin(), values.end()); |
| 70 |
value v(m); |
| 71 |
ASSERT_EQUAL(MAP, v.type()); |
| 72 |
T m2(get<T>(v)); |
| 73 |
ASSERT_EQUAL(m.size(), m2.size()); |
| 74 |
ASSERT_EQUAL(m, m2); |
| 75 |
if (!s.empty()) |
| 76 |
ASSERT_EQUAL(s, to_string(v)); |
| 77 |
} |
| 78 |
|
| 79 |
void null_test() { |
| 80 |
proton::null n; |
| 81 |
ASSERT_EQUAL("null", to_string(n)); |
| 82 |
|
| 83 |
std::vector<proton::value> nulls(2, n); |
| 84 |
ASSERT_EQUAL("[null, null]", to_string(nulls)); |
| 85 |
|
| 86 |
std::vector<proton::null> nulls1(2, n); |
| 87 |
ASSERT_EQUAL("@PN_NULL[null, null]", to_string(nulls1)); |
| 88 |
|
| 89 |
std::vector<proton::value> vs; |
| 90 |
vs.push_back(n); |
| 91 |
vs.push_back(nulls); |
| 92 |
vs.push_back(nulls1); |
| 93 |
ASSERT_EQUAL("[null, [null, null], @PN_NULL[null, null]]", to_string(vs)); |
| 94 |
|
| 95 |
std::map<proton::value, proton::value> vm; |
| 96 |
vm[n] = 1; |
| 97 |
vm[nulls1] = 2; |
| 98 |
vm[nulls] = 3; |
| 99 |
// Different types compare by type-id, so NULL < ARRAY < LIST |
| 100 |
ASSERT_EQUAL("{null=1, @PN_NULL[null, null]=2, [null, null]=3}", to_string(vm)); |
| 101 |
|
| 102 |
std::map<proton::scalar, proton::scalar> vm2; |
| 103 |
vm2[n] = 1; // Different types compare by type-id, NULL is smallest |
| 104 |
vm2[2] = n; |
| 105 |
ASSERT_EQUAL("{null=1, 2=null}", to_string(vm2)); |
| 106 |
|
| 107 |
proton::value nn = nullptr; |
| 108 |
ASSERT(n == nn); // Don't use ASSERT_EQUAL, it will try to print |
| 109 |
ASSERT_EQUAL("null", to_string(nn)); |
| 110 |
std::vector<std::nullptr_t> nulls2 {nullptr, nullptr}; |
| 111 |
ASSERT_EQUAL("@PN_NULL[null, null]", to_string(nulls2)); |
| 112 |
std::map<proton::scalar, proton::scalar> m {{nullptr, nullptr}}; |
| 113 |
ASSERT_EQUAL("{null=null}", to_string(m)); |
| 114 |
std::map<proton::value, proton::value> m2 {{nullptr, nullptr}}; |
| 115 |
ASSERT_EQUAL("{null=null}", to_string(m2)); |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
int main(int, char**) { |
| 121 |
try { |
| 122 |
int failed = 0; |
| 123 |
scalar_test_group<value>(failed); |
| 124 |
|
| 125 |
// Sequence tests |
| 126 |
RUN_TEST(failed, sequence_test<list<bool> >( |
| 127 |
ARRAY, many<bool>() + false + true, "@PN_BOOL[false, true]")); |
| 128 |
RUN_TEST(failed, sequence_test<vector<int> >( |
| 129 |
ARRAY, many<int>() + -1 + 2, "@PN_INT[-1, 2]")); |
| 130 |
RUN_TEST(failed, sequence_test<deque<string> >( |
| 131 |
ARRAY, many<string>() + "a" + "b", "@PN_STRING[\"a\", \"b\"]")); |
| 132 |
RUN_TEST(failed, sequence_test<deque<symbol> >( |
| 133 |
ARRAY, many<symbol>() + "a" + "b", "@PN_SYMBOL[:a, :b]")); |
| 134 |
RUN_TEST(failed, sequence_test<vector<value> >( |
| 135 |
LIST, many<value>() + value(0) + value("a"), "[0, \"a\"]")); |
| 136 |
RUN_TEST(failed, sequence_test<vector<scalar> >( |
| 137 |
LIST, many<scalar>() + scalar(0) + scalar("a"), "[0, \"a\"]")); |
| 138 |
|
| 139 |
// // Map tests |
| 140 |
typedef pair<string, uint64_t> si_pair; |
| 141 |
many<si_pair> si_pairs; |
| 142 |
si_pairs << si_pair("a", 0) << si_pair("b", 1) << si_pair("c", 2); |
| 143 |
|
| 144 |
RUN_TEST(failed, (map_test<map<string, uint64_t> >( |
| 145 |
si_pairs, "{\"a\"=0, \"b\"=1, \"c\"=2}"))); |
| 146 |
RUN_TEST(failed, (map_test<vector<si_pair> >( |
| 147 |
si_pairs, "{\"a\"=0, \"b\"=1, \"c\"=2}"))); |
| 148 |
|
| 149 |
many<std::pair<value,value> > value_pairs(si_pairs); |
| 150 |
RUN_TEST(failed, (map_test<map<value, value> >( |
| 151 |
value_pairs, "{\"a\"=0, \"b\"=1, \"c\"=2}"))); |
| 152 |
|
| 153 |
many<pair<scalar,scalar> > scalar_pairs(si_pairs); |
| 154 |
RUN_TEST(failed, (map_test<map<scalar, scalar> >( |
| 155 |
scalar_pairs, "{\"a\"=0, \"b\"=1, \"c\"=2}"))); |
| 156 |
|
| 157 |
annotation_key ak(si_pairs[0].first); |
| 158 |
pair<annotation_key, message_id> p(si_pairs[0]); |
| 159 |
many<pair<annotation_key, message_id> > restricted_pairs(si_pairs); |
| 160 |
RUN_TEST(failed, (map_test<map<annotation_key, message_id> >( |
| 161 |
restricted_pairs, "{:a=0, :b=1, :c=2}"))); |
| 162 |
RUN_TEST(failed, null_test()); |
| 163 |
|
| 164 |
RUN_TEST(failed, sequence_test<forward_list<binary> >( |
| 165 |
ARRAY, many<binary>() + binary("xx") + binary("yy"), "@PN_BINARY[b\"xx\", b\"yy\"]")); |
| 166 |
RUN_TEST(failed, (map_test<unordered_map<string, uint64_t> >(si_pairs, ""))); |
| 167 |
|
| 168 |
return failed; |
| 169 |
} catch (const std::exception& e) { |
| 170 |
std::cout << "ERROR in main(): " << e.what() << std::endl; |
| 171 |
} |
| 172 |
} |