| 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 "proton/codec/decoder.hpp" |
| 21 |
#include "proton/codec/encoder.hpp" |
| 22 |
#include "proton/error.hpp" |
| 23 |
#include "proton/value.hpp" |
| 24 |
#include "test_bits.hpp" |
| 25 |
#include <string> |
| 26 |
#include <sstream> |
| 27 |
#include <fstream> |
| 28 |
#include <streambuf> |
| 29 |
#include <iosfwd> |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
using namespace std; |
| 34 |
using namespace proton; |
| 35 |
|
| 36 |
using proton::codec::encoder; |
| 37 |
using proton::codec::decoder; |
| 38 |
|
| 39 |
using proton::internal::data; |
| 40 |
|
| 41 |
using test::str; |
| 42 |
|
| 43 |
std::string tests_dir; |
| 44 |
|
| 45 |
string read(string filename) { |
| 46 |
filename = tests_dir+string("/interop/")+filename+string(".amqp"); |
| 47 |
ifstream ifs(filename.c_str()); |
| 48 |
if (!ifs.good()) FAIL("Can't open " << filename); |
| 49 |
return string(istreambuf_iterator<char>(ifs), istreambuf_iterator<char>()); |
| 50 |
} |
| 51 |
|
| 52 |
// Test data ostream operator |
| 53 |
void test_data_ostream() { |
| 54 |
data dt(data::create()); |
| 55 |
decoder d(dt); |
| 56 |
d.decode(read("primitives")); |
| 57 |
ASSERT_EQUAL("true, false, 42, 42, -42, 12345, -12345, 12345, -12345, 0.125, 0.125", str(dt)); |
| 58 |
} |
| 59 |
|
| 60 |
// Test extracting to exact AMQP types works correctly, extracting to invalid types fails. |
| 61 |
void test_decoder_primitives_exact() { |
| 62 |
value dv; |
| 63 |
decoder d(dv); |
| 64 |
d.decode(read("primitives")); |
| 65 |
ASSERT(d.more()); |
| 66 |
try { get< ::int8_t>(d); FAIL("got bool as byte"); } catch(const conversion_error&){} |
| 67 |
ASSERT_EQUAL(true, get<bool>(d)); |
| 68 |
ASSERT_EQUAL(false, get<bool>(d)); |
| 69 |
try { get< ::int8_t>(d); FAIL("got ubyte as byte"); } catch(const conversion_error&){} |
| 70 |
ASSERT_EQUAL(42, get< ::uint8_t>(d)); |
| 71 |
try { get< ::int32_t>(d); FAIL("got uint as ushort"); } catch(const conversion_error&){} |
| 72 |
ASSERT_EQUAL(42, get< ::uint16_t>(d)); |
| 73 |
try { get< ::uint16_t>(d); FAIL("got short as ushort"); } catch(const conversion_error&){} |
| 74 |
ASSERT_EQUAL(-42, get< ::int16_t>(d)); |
| 75 |
ASSERT_EQUAL(12345u, get< ::uint32_t>(d)); |
| 76 |
ASSERT_EQUAL(-12345, get< ::int32_t>(d)); |
| 77 |
ASSERT_EQUAL(12345u, get< ::uint64_t>(d)); |
| 78 |
ASSERT_EQUAL(-12345, get< ::int64_t>(d)); |
| 79 |
try { get<double>(d); FAIL("got float as double"); } catch(const conversion_error&){} |
| 80 |
ASSERT_EQUAL(0.125f, get<float>(d)); |
| 81 |
try { get<float>(d); FAIL("got double as float"); } catch(const conversion_error&){} |
| 82 |
ASSERT_EQUAL(0.125, get<double>(d)); |
| 83 |
ASSERT(!d.more()); |
| 84 |
} |
| 85 |
|
| 86 |
// Test inserting primitive sand encoding as AMQP. |
| 87 |
void test_encoder_primitives() { |
| 88 |
value dv; |
| 89 |
encoder e(dv); |
| 90 |
e << true << false; |
| 91 |
e << ::uint8_t(42); |
| 92 |
e << ::uint16_t(42) << ::int16_t(-42); |
| 93 |
e << ::uint32_t(12345) << ::int32_t(-12345); |
| 94 |
e << ::uint64_t(12345) << ::int64_t(-12345); |
| 95 |
e << float(0.125) << double(0.125); |
| 96 |
ASSERT_EQUAL("true, false, 42, 42, -42, 12345, -12345, 12345, -12345, 0.125, 0.125", str(e)); |
| 97 |
std::string data = e.encode(); |
| 98 |
ASSERT_EQUAL(read("primitives"), data); |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
int main(int argc, char** argv) { |
| 104 |
int failed = 0; |
| 105 |
if (argc != 2) { |
| 106 |
cerr << "Usage: " << argv[0] << " tests-dir" << endl; |
| 107 |
return 1; |
| 108 |
} |
| 109 |
tests_dir = argv[1]; |
| 110 |
|
| 111 |
RUN_TEST(failed, test_data_ostream()); |
| 112 |
RUN_TEST(failed, test_decoder_primitives_exact()); |
| 113 |
RUN_TEST(failed, test_encoder_primitives()); |
| 114 |
return failed; |
| 115 |
} |