memgraph/tests/unit/chunked_decoder.cpp

69 lines
1.6 KiB
C++
Raw Normal View History

2016-08-03 00:05:10 +08:00
#include <array>
2016-08-02 05:14:09 +08:00
#include <cassert>
#include <cstring>
2016-08-03 00:05:10 +08:00
#include <deque>
#include <iostream>
2016-08-02 05:14:09 +08:00
#include <vector>
2016-12-22 22:51:16 +08:00
#include "gtest/gtest.h"
#include "communication/bolt/v1/transport/chunked_decoder.hpp"
2016-08-02 05:14:09 +08:00
using byte = unsigned char;
2016-08-03 00:05:10 +08:00
void print_hex(byte x) { printf("%02X ", static_cast<byte>(x)); }
2016-08-02 05:14:09 +08:00
2016-12-22 22:51:16 +08:00
struct DummyStream
2016-08-02 05:14:09 +08:00
{
2016-08-03 00:05:10 +08:00
void write(const byte *values, size_t n)
2016-08-02 05:14:09 +08:00
{
data.insert(data.end(), values, values + n);
}
std::vector<byte> data;
};
using Decoder = bolt::ChunkedDecoder<DummyStream>;
std::vector<byte> chunks[] = {
2016-08-03 00:05:10 +08:00
{0x00, 0x08, 'A', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 0x00, 0x06, 'b', 'r',
'o', 'w', 'n', ' '},
{0x00, 0x0A, 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' '},
{0x00, 0x07, 'o', 'v', 'e', 'r', ' ', 'a', ' '},
{0x00, 0x08, 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g', 0x00, 0x00}};
2016-08-02 05:14:09 +08:00
static constexpr size_t N = std::extent<decltype(chunks)>::value;
std::string decoded = "A quick brown fox jumps over a lazy dog";
2016-12-22 22:51:16 +08:00
TEST(ChunkedDecoderTest, WriteString)
2016-08-02 05:14:09 +08:00
{
2016-12-22 22:51:16 +08:00
DummyStream stream;
Decoder decoder(stream);
2016-08-02 05:14:09 +08:00
2016-12-22 22:51:16 +08:00
for(size_t i = 0; i < N; ++i)
{
auto & chunk = chunks[i];
logging::info("Chunk size: {}", chunk.size());
2016-08-02 05:14:09 +08:00
2016-12-22 22:51:16 +08:00
const byte* start = chunk.data();
auto finished = decoder.decode(start, chunk.size());
2016-08-02 05:14:09 +08:00
2016-12-22 22:51:16 +08:00
// break early if finished
if(finished)
break;
}
2016-08-02 05:14:09 +08:00
2016-12-22 22:51:16 +08:00
// check validity
ASSERT_EQ(decoded.size(), stream.data.size());
for(size_t i = 0; i < decoded.size(); ++i)
ASSERT_EQ(decoded[i], stream.data[i]);
}
2016-08-02 05:14:09 +08:00
2016-12-22 22:51:16 +08:00
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
2016-08-02 05:14:09 +08:00
}
2016-12-22 22:51:16 +08:00