File: size_equal_only.cpp

package info (click to toggle)
msgpack-cxx 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,520 kB
  • sloc: cpp: 87,413; ansic: 3,571; sh: 56; makefile: 39
file content (226 lines) | stat: -rw-r--r-- 6,866 bytes parent folder | download | duplicates (3)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <sstream>
#include <msgpack.hpp>

#define BOOST_TEST_MODULE size_equal_only
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(array)
{
    std::stringstream ss;
    int buf[3] = { 1, 2, 3 };
    msgpack::type::size_equal_only<int[3]> seo(buf);

    msgpack::pack(ss, seo);
    std::string const& str = ss.str();
    msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());

    int ret_buf1[3];
    oh.get().convert(ret_buf1);
    BOOST_CHECK_EQUAL(buf[0], ret_buf1[0]);
    BOOST_CHECK_EQUAL(buf[1], ret_buf1[1]);
    BOOST_CHECK_EQUAL(buf[2], ret_buf1[2]);

    int ret_buf2[4];
    oh.get().convert(ret_buf2);
    BOOST_CHECK_EQUAL(buf[0], ret_buf2[0]);
    BOOST_CHECK_EQUAL(buf[1], ret_buf2[1]);
    BOOST_CHECK_EQUAL(buf[2], ret_buf2[2]);

    int ret_buf3[3];
    msgpack::type::size_equal_only<int[3]> ret_seo3(ret_buf3);
    oh.get().convert(ret_seo3);
    BOOST_CHECK_EQUAL(buf[0], ret_buf3[0]);
    BOOST_CHECK_EQUAL(buf[1], ret_buf3[1]);
    BOOST_CHECK_EQUAL(buf[2], ret_buf3[2]);

    int ret_buf4[4];
    msgpack::type::size_equal_only<int[4]> ret_seo4(ret_buf4);
    try {
        oh.get().convert(ret_seo4);
        BOOST_CHECK(false);
    }
    catch (msgpack::type_error const&) {
        BOOST_CHECK(true);
    }
}

BOOST_AUTO_TEST_CASE(vector)
{
    std::stringstream ss;
    std::vector<int> buf;
    buf.push_back(1);
    buf.push_back(2);
    buf.push_back(3);

    msgpack::type::size_equal_only<std::vector<int> > seo(buf);

    msgpack::pack(ss, seo);
    std::string const& str = ss.str();
    msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());

    std::vector<int> ret_buf1;
    oh.get().convert(ret_buf1);
    BOOST_CHECK(buf == ret_buf1);


    std::vector<int> ret_buf2;
    ret_buf2.resize(3);
    msgpack::type::size_equal_only<std::vector<int> > ret_seo2(ret_buf2);
    oh.get().convert(ret_seo2);
    BOOST_CHECK(buf == ret_buf2);

    std::vector<int> ret_buf3;
    ret_buf2.resize(4);
    msgpack::type::size_equal_only<std::vector<int> > ret_seo3(ret_buf3);
    try {
        oh.get().convert(ret_seo3);
        BOOST_CHECK(false);
    }
    catch (msgpack::type_error const&) {
        BOOST_CHECK(true);
    }
}

BOOST_AUTO_TEST_CASE(msgpack_tuple)
{
    std::stringstream ss;
    msgpack::type::tuple<int, bool, std::string> buf(1, false, "ABC");

    msgpack::type::size_equal_only<msgpack::type::tuple<int, bool, std::string> > seo(buf);

    msgpack::pack(ss, seo);
    std::string const& str = ss.str();
    msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());

    msgpack::type::tuple<int, bool, std::string> ret_buf1;
    oh.get().convert(ret_buf1);
    BOOST_CHECK_EQUAL(buf.get<0>(), ret_buf1.get<0>());
    BOOST_CHECK_EQUAL(buf.get<1>(), ret_buf1.get<1>());
    BOOST_CHECK_EQUAL(buf.get<2>(), ret_buf1.get<2>());

    msgpack::type::tuple<int, bool, std::string> ret_buf2;
    msgpack::type::size_equal_only<msgpack::type::tuple<int, bool, std::string> > ret_seo2(ret_buf2);
    oh.get().convert(ret_seo2);
    BOOST_CHECK_EQUAL(buf.get<0>(), ret_buf2.get<0>());
    BOOST_CHECK_EQUAL(buf.get<1>(), ret_buf2.get<1>());
    BOOST_CHECK_EQUAL(buf.get<2>(), ret_buf2.get<2>());

    msgpack::type::tuple<int, bool, std::string, int> ret_buf3;
    oh.get().convert(ret_buf3);
    BOOST_CHECK_EQUAL(buf.get<0>(), ret_buf3.get<0>());
    BOOST_CHECK_EQUAL(buf.get<1>(), ret_buf3.get<1>());
    BOOST_CHECK_EQUAL(buf.get<2>(), ret_buf3.get<2>());

    msgpack::type::tuple<int, bool, std::string, int> ret_buf4;
    msgpack::type::size_equal_only<msgpack::type::tuple<int, bool, std::string, int> > ret_seo4(ret_buf4);
    try {
        oh.get().convert(ret_seo4);
        BOOST_CHECK(false);
    }
    catch (msgpack::type_error const&) {
        BOOST_CHECK(true);
    }

    msgpack::type::tuple<int, bool, std::string> ret_buf5;
    oh.get().convert(ret_buf5);
    BOOST_CHECK_EQUAL(buf.get<0>(), ret_buf5.get<0>());
    BOOST_CHECK_EQUAL(buf.get<1>(), ret_buf5.get<1>());

    msgpack::type::tuple<int, bool, std::string, int> ret_buf6;
    msgpack::type::size_equal_only<msgpack::type::tuple<int, bool, std::string, int> > ret_seo6(ret_buf6);
    try {
        oh.get().convert(ret_seo6);
        BOOST_CHECK(false);
    }
    catch (msgpack::type_error const&) {
        BOOST_CHECK(true);
    }
}

#if !defined(MSGPACK_USE_CPP03)

BOOST_AUTO_TEST_CASE(tuple)
{
    std::stringstream ss;
    std::tuple<int, bool, std::string> buf(1, false, "ABC");

    auto seo = msgpack::type::make_size_equal_only(buf);

    msgpack::pack(ss, seo);
    std::string const& str = ss.str();
    msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());

    std::tuple<int, bool, std::string> ret_buf1;
    oh.get().convert(ret_buf1);
    BOOST_CHECK(buf == ret_buf1);

    std::tuple<int, bool, std::string> ret_buf2;
    auto ret_seo2 = msgpack::type::make_size_equal_only(ret_buf2);
    oh.get().convert(ret_seo2);
    BOOST_CHECK(buf == ret_buf2);

    std::tuple<int, bool, std::string, int> ret_buf3;
    oh.get().convert(ret_buf3);
    BOOST_CHECK_EQUAL(std::get<0>(buf), std::get<0>(ret_buf3));
    BOOST_CHECK_EQUAL(std::get<1>(buf), std::get<1>(ret_buf3));
    BOOST_CHECK_EQUAL(std::get<2>(buf), std::get<2>(ret_buf3));

    std::tuple<int, bool, std::string, int> ret_buf4;
    auto ret_seo4 = msgpack::type::make_size_equal_only(ret_buf4);
    try {
        oh.get().convert(ret_seo4);
        BOOST_CHECK(false);
    }
    catch (msgpack::type_error const&) {
        BOOST_CHECK(true);
    }

    std::tuple<int, bool, std::string> ret_buf5;
    oh.get().convert(ret_buf5);
    BOOST_CHECK_EQUAL(std::get<0>(buf), std::get<0>(ret_buf5));
    BOOST_CHECK_EQUAL(std::get<1>(buf), std::get<1>(ret_buf5));

    std::tuple<int, bool, std::string, int> ret_buf6;
    auto ret_seo6 = msgpack::type::make_size_equal_only(ret_buf6);
    try {
        oh.get().convert(ret_seo6);
        BOOST_CHECK(false);
    }
    catch (msgpack::type_error const&) {
        BOOST_CHECK(true);
    }
}

struct foo1 {
    foo1(int i, bool b):t(i, b), seo(t) {}
    std::tuple<int, bool> t;
    msgpack::type::size_equal_only<std::tuple<int, bool> > seo;
    MSGPACK_DEFINE(seo);
};

struct foo2 {
    foo2(int i, bool b, std::string const& s):t(i, b, s), seo(t) {}
    std::tuple<int, bool, std::string> t;
    msgpack::type::size_equal_only<std::tuple<int, bool, std::string> > seo;
    MSGPACK_DEFINE(seo);
};

BOOST_AUTO_TEST_CASE(custom_class)
{
    std::stringstream ss;
    foo1 f1(42, true);
    msgpack::pack(ss, f1);
    std::string const& str = ss.str();
    msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());

    foo2 f2(123, false, "ABC");
    try {
        oh.get().convert(f2);
        BOOST_CHECK(false);
    }
    catch (msgpack::type_error const&) {
        BOOST_CHECK(true);
    }
}

#endif //  !defined(MSGPACK_USE_CPP03)