File: iterator_cpp11.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 (71 lines) | stat: -rw-r--r-- 1,868 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
#include <msgpack.hpp>

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

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if !defined(MSGPACK_USE_CPP03)
#include <iterator>

// C++11

constexpr unsigned int VECTOR_SIZE = 100;
constexpr unsigned int MAP_SIZE    = 100;

BOOST_AUTO_TEST_CASE(vector)
{
    using vec_type = std::vector<unsigned>;
    vec_type vec;
    vec.reserve(VECTOR_SIZE);
    for (unsigned int i = 0; i < VECTOR_SIZE; i++) {
        vec.push_back(static_cast<unsigned>(rand()));
    }
    msgpack::sbuffer sbuf;
    msgpack::pack(sbuf, vec);

    msgpack::object_handle oh =
        msgpack::unpack(sbuf.data(), sbuf.size());

    auto const& msgarr = oh.get().via.array;
    auto dist = std::distance(begin(msgarr), end(msgarr));
    auto vecSize = vec.size();
    BOOST_CHECK_EQUAL(static_cast<size_t>(dist), vecSize);

    vec_type::const_iterator correct = std::begin(vec);
    for (auto const& obj : msgarr) {
        auto u64 = *correct;
        BOOST_CHECK_EQUAL(obj.as<unsigned>(), u64);
        ++correct;
    }
}

BOOST_AUTO_TEST_CASE(map)
{
    using map_type = std::map<unsigned, unsigned>;
    map_type map;
    for (unsigned i = 0; i < MAP_SIZE; i++) {
        map[static_cast<unsigned>(rand())] = static_cast<unsigned>(rand());
    }
    msgpack::sbuffer sbuf;
    msgpack::pack(sbuf, map);

    msgpack::object_handle oh =
        msgpack::unpack(sbuf.data(), sbuf.size());

    auto const& msgmap = oh.get().via.map;
    auto dist = std::distance(begin(msgmap), end(msgmap));
    auto mapSize = map.size();
    BOOST_CHECK_EQUAL(static_cast<size_t>(dist), mapSize);

    for (auto const& kv : msgmap) {
        auto key = kv.key.as<unsigned>();
        auto val = kv.val.as<unsigned>();
        auto correct = map[key];
        BOOST_CHECK_EQUAL(val, correct);
    }
}

#endif // !defined(MSGPACK_USE_CPP03