forked from basiliscos/cpp-bredis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-fragmented-buffer.cpp
More file actions
75 lines (62 loc) · 2.78 KB
/
Copy path06-fragmented-buffer.cpp
File metadata and controls
75 lines (62 loc) · 2.78 KB
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
#include <boost/asio/buffer.hpp>
#include <boost/asio/streambuf.hpp>
#include <sstream>
#include <vector>
#include "bredis/MarkerHelpers.hpp"
#include "bredis/Protocol.hpp"
#include "catch.hpp"
namespace r = bredis;
namespace asio = boost::asio;
TEST_CASE("right consumption", "[protocol]") {
using Buffer = std::vector<asio::const_buffers_1>;
using Iterator = boost::asio::buffers_iterator<Buffer, char>;
using Policy = r::parsing_policy::keep_result;
using positive_result_t = r::parse_result_mapper_t<Iterator, Policy>;
std::string full_message =
"*3\r\n$7\r\nmessage\r\n$13\r\nsome-channel1\r\n$10\r\nmessage-a1\r\n";
Buffer buff;
for (auto i = 0; i < full_message.size(); i++) {
asio::const_buffers_1 v(full_message.c_str() + i, 1);
buff.push_back(v);
}
auto b_from = Iterator::begin(buff), b_to = Iterator::end(buff);
auto parsed_result = r::Protocol::parse(b_from, b_to);
auto positive_parse_result = boost::get<positive_result_t>(parsed_result);
REQUIRE(positive_parse_result.consumed);
REQUIRE(positive_parse_result.consumed == full_message.size());
auto *array = boost::get<r::markers::array_holder_t<Iterator>>(
&positive_parse_result.result);
REQUIRE(array != nullptr);
REQUIRE(array->elements.size() == 3);
REQUIRE(boost::apply_visitor(
r::marker_helpers::equality<Iterator>("message"), array->elements[0]));
REQUIRE(boost::apply_visitor(
r::marker_helpers::equality<Iterator>("some-channel1"),
array->elements[1]));
REQUIRE(boost::apply_visitor(
r::marker_helpers::equality<Iterator>("message-a1"),
array->elements[2]));
REQUIRE(boost::get<r::markers::string_t<Iterator>>(&array->elements[0]) !=
nullptr);
REQUIRE(boost::get<r::markers::string_t<Iterator>>(&array->elements[1]) !=
nullptr);
REQUIRE(boost::get<r::markers::string_t<Iterator>>(&array->elements[2]) !=
nullptr);
}
TEST_CASE("using strembuff", "[protocol]") {
using Buffer = boost::asio::streambuf;
using Iterator = typename r::to_iterator<Buffer>::iterator_t;
using Policy = r::parsing_policy::keep_result;
using positive_result_t = r::parse_result_mapper_t<Iterator, Policy>;
std::string ok = "+OK\r\n";
Buffer buff;
std::ostream os(&buff);
os.write(ok.c_str(), ok.size());
auto const_buff = buff.data();
auto from = Iterator::begin(const_buff), to = Iterator::end(const_buff);
auto parsed_result = r::Protocol::parse(from, to);
auto positive_parse_result = boost::get<positive_result_t>(parsed_result);
REQUIRE(positive_parse_result.consumed == ok.size());
REQUIRE(boost::apply_visitor(r::marker_helpers::equality<Iterator>("OK"),
positive_parse_result.result));
}