File: test_read_byte_containers.cpp

package info (click to toggle)
reflect-cpp 0.21.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,128 kB
  • sloc: cpp: 50,336; python: 139; makefile: 30; sh: 3
file content (90 lines) | stat: -rw-r--r-- 2,370 bytes parent folder | download
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
#include <algorithm>
#include <array>
#include <rfl/cbor.hpp>

#include <gtest/gtest.h>

// Make sure things still compile when
// rfl.hpp is included after rfl/cbor.hpp.
#include <rfl.hpp>

namespace test_read_byte_containers
{

struct TestBox
{
    int length;
    int width;
    int height;
};

// TODO: Apparently the jsoncons trait is_byte_sequence<T> is not working for std::span.
// TEST(cbor, test_read_from_char_span)
// {

//    TestBox b = {
//        .length = 1,
//        .width = 2,
//        .height = 3,
//    };


//     std::vector<char> rfl_buffer = rfl::cbor::write(s);

//     // I don't want to be forced to copy to a std::vector<char> if that's not what data strucure I use.
//     // So, allow reading from a std::span
//     std::span<char> span(rfl_buffer.data(), rfl_buffer.size());

//     auto result = rfl::cbor::read<TestBox>(span);
//     EXPECT_TRUE(result);
//     EXPECT_EQ(result->one, 1);
//     EXPECT_EQ(result->two, 2);
// }

TEST(cbor, test_read_from_byte_view)
{
    TestBox b = {
        .length = 1,
        .width = 2,
        .height = 3,
    };

    // TODO: Write directly into desired container, once rfl::cbor::write supports it.
    std::vector<char> rfl_buffer = rfl::cbor::write(b);

    std::array<std::byte, 64> my_buffer;
    std::transform(rfl_buffer.begin(), rfl_buffer.end(), my_buffer.begin(),
        [](char c) { return static_cast<std::byte>(c); });

    std::basic_string_view<std::byte> byte_view(my_buffer.data(), rfl_buffer.size());

    auto result = rfl::cbor::read<TestBox>(byte_view);
    EXPECT_TRUE(result);
    EXPECT_EQ(result->length, 1);
    EXPECT_EQ(result->width, 2);
    EXPECT_EQ(result->height, 3);
}

TEST(cbor, test_read_from_uint8_array)
{
    TestBox b = {
        .length = 4,
        .width = 5,
        .height = 6,
    };

    // TODO: Write directly into desired container, once rfl::cbor::write supports it.
    std::vector<char> rfl_buffer = rfl::cbor::write(b);
    
    std::array<std::uint8_t, 64> my_buffer;
    std::transform(rfl_buffer.begin(), rfl_buffer.end(), my_buffer.begin(),
        [](char c) { return static_cast<std::uint8_t>(c); });

    auto result = rfl::cbor::read<TestBox>(my_buffer);
    EXPECT_TRUE(result);
    EXPECT_EQ(result->length, 4);
    EXPECT_EQ(result->width, 5);
    EXPECT_EQ(result->height, 6);
}

} // namespace test_read_byte_containers