File: Item.cpp

package info (click to toggle)
odil 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 55,982; python: 3,947; javascript: 460; xml: 182; makefile: 99; sh: 36
file content (128 lines) | stat: -rw-r--r-- 3,709 bytes parent folder | download | duplicates (6)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#define BOOST_TEST_MODULE pdu::Item
#include <boost/test/unit_test.hpp>

#include <cstdint>

#include "odil/pdu/Item.h"
#include "odil/Exception.h"

BOOST_AUTO_TEST_CASE(FieldUInt8)
{
    odil::pdu::Item::Field const field(uint8_t(123));

    BOOST_REQUIRE(
        field.get_type() == odil::pdu::Item::Field::Type::unsigned_int_8);
    BOOST_REQUIRE_EQUAL(field.as_unsigned_int_8(), 123);
}

BOOST_AUTO_TEST_CASE(FieldUInt16)
{
    odil::pdu::Item::Field const field(uint16_t(1234));

    BOOST_REQUIRE(
        field.get_type() == odil::pdu::Item::Field::Type::unsigned_int_16);
    BOOST_REQUIRE_EQUAL(field.as_unsigned_int_16(), 1234);
}

BOOST_AUTO_TEST_CASE(FieldUInt32)
{
    odil::pdu::Item::Field const field(uint32_t(123456));

    BOOST_REQUIRE(
        field.get_type() == odil::pdu::Item::Field::Type::unsigned_int_32);
    BOOST_REQUIRE_EQUAL(field.as_unsigned_int_32(), 123456);
}

BOOST_AUTO_TEST_CASE(FieldString)
{
    odil::pdu::Item::Field const field("abcdef");

    BOOST_REQUIRE(field.get_type() == odil::pdu::Item::Field::Type::string);
    BOOST_REQUIRE_EQUAL(field.as_string(), "abcdef");
}

BOOST_AUTO_TEST_CASE(FieldItems)
{
    odil::pdu::Item const item_1({{"foo", uint8_t(123)}});
    odil::pdu::Item const item_2({{"bar", std::string("abcdef")}});
    odil::pdu::Item::Field const field(
        std::vector<odil::pdu::Item>({item_1, item_2}));

    BOOST_REQUIRE(field.get_type() == odil::pdu::Item::Field::Type::items);
    BOOST_REQUIRE_EQUAL(field.as_items().size(), 2);
}

BOOST_AUTO_TEST_CASE(DefaultConstructor)
{
    odil::pdu::Item const item;
    BOOST_REQUIRE(item.empty());
    BOOST_REQUIRE_EQUAL(item.size(), 0);
}

BOOST_AUTO_TEST_CASE(FieldsConstructor)
{
    odil::pdu::Item const item({
        {std::string("foo"), uint8_t(123)},
        {std::string("bar"), std::string("abcd")}
    });
    BOOST_REQUIRE_EQUAL(item.size(), 2);
    BOOST_REQUIRE_EQUAL(item.as_unsigned_int_8("foo"), 123);
    BOOST_REQUIRE_EQUAL(item.as_string("bar"), "abcd");
}

BOOST_AUTO_TEST_CASE(UInt8)
{
    odil::pdu::Item item;
    item.add("foo", uint8_t(123));
    BOOST_REQUIRE_EQUAL(item.size(), 1);
    BOOST_REQUIRE_EQUAL(item.has_field("foo"), true);
    BOOST_REQUIRE_EQUAL(item.as_unsigned_int_8("foo"), 123);
    BOOST_REQUIRE_THROW(item.as_unsigned_int_16("foo"), odil::Exception);
}

BOOST_AUTO_TEST_CASE(UInt16)
{
    odil::pdu::Item item;
    item.add("foo", uint16_t(1234));
    BOOST_REQUIRE_EQUAL(item.size(), 1);
    BOOST_REQUIRE_EQUAL(item.as_unsigned_int_16("foo"), 1234);
    BOOST_REQUIRE_THROW(item.as_unsigned_int_32("foo"), odil::Exception);
}

BOOST_AUTO_TEST_CASE(UInt32)
{
    odil::pdu::Item item;
    item.add("foo", uint32_t(123456));
    BOOST_REQUIRE_EQUAL(item.size(), 1);
    BOOST_REQUIRE_EQUAL(item.as_unsigned_int_32("foo"), 123456);
    BOOST_REQUIRE_THROW(item.as_string("foo"), odil::Exception);
}

BOOST_AUTO_TEST_CASE(String)
{
    odil::pdu::Item item;
    item.add("foo", std::string("abcd"));
    BOOST_REQUIRE_EQUAL(item.size(), 1);
    BOOST_REQUIRE_EQUAL(item.as_string("foo"), "abcd");
    BOOST_REQUIRE_THROW(item.as_items("foo"), odil::Exception);
}

BOOST_AUTO_TEST_CASE(Items)
{
    odil::pdu::Item const sub_item(
        {{"bar", std::string("abcdef")}});
    odil::pdu::Item item;
    item.add("foo", std::vector<odil::pdu::Item>({sub_item}));
    BOOST_REQUIRE_EQUAL(item.size(), 1);
    BOOST_REQUIRE_EQUAL(item.as_items("foo").size(), 1);
    BOOST_REQUIRE_THROW(item.as_unsigned_int_8("foo"), odil::Exception);
}

BOOST_AUTO_TEST_CASE(FieldAccessor)
{
    odil::pdu::Item item;
    item.add("foo", std::string("abcd"));
    BOOST_REQUIRE_NO_THROW(item["foo"]);
    BOOST_REQUIRE_THROW(item["bar"], odil::Exception);

}