2018-08-13 19:15:09 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/Random.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
TEST(Buffer, buffer_builder) {
|
|
|
|
{
|
2021-10-14 14:08:35 +02:00
|
|
|
td::BufferBuilder builder;
|
2018-08-13 19:15:09 +02:00
|
|
|
builder.append("b");
|
|
|
|
builder.prepend("a");
|
|
|
|
builder.append("c");
|
|
|
|
ASSERT_EQ(builder.extract().as_slice(), "abc");
|
|
|
|
}
|
|
|
|
{
|
2021-10-14 14:08:35 +02:00
|
|
|
td::BufferBuilder builder{"hello", 0, 0};
|
2018-08-13 19:15:09 +02:00
|
|
|
ASSERT_EQ(builder.extract().as_slice(), "hello");
|
|
|
|
}
|
|
|
|
{
|
2021-10-14 14:08:35 +02:00
|
|
|
td::BufferBuilder builder{"hello", 1, 1};
|
2018-08-13 19:15:09 +02:00
|
|
|
builder.prepend("A ");
|
|
|
|
builder.append(" B");
|
|
|
|
ASSERT_EQ(builder.extract().as_slice(), "A hello B");
|
|
|
|
}
|
|
|
|
{
|
2021-10-14 14:08:35 +02:00
|
|
|
auto str = td::rand_string('a', 'z', 10000);
|
2021-12-23 12:18:32 +01:00
|
|
|
auto split_str = td::rand_split(str);
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2021-12-23 12:18:32 +01:00
|
|
|
int l = td::Random::fast(0, static_cast<int>(split_str.size() - 1));
|
2018-08-13 19:15:09 +02:00
|
|
|
int r = l;
|
2021-12-23 12:18:32 +01:00
|
|
|
td::BufferBuilder builder(split_str[l], 123, 1000);
|
|
|
|
while (l != 0 || r != static_cast<int>(split_str.size()) - 1) {
|
|
|
|
if (l == 0 || (td::Random::fast_bool() && r != static_cast<int>(split_str.size() - 1))) {
|
2018-08-13 19:15:09 +02:00
|
|
|
r++;
|
2021-10-14 14:08:35 +02:00
|
|
|
if (td::Random::fast_bool()) {
|
2021-12-23 12:18:32 +01:00
|
|
|
builder.append(split_str[r]);
|
2018-08-13 19:15:09 +02:00
|
|
|
} else {
|
2021-12-23 12:18:32 +01:00
|
|
|
builder.append(td::BufferSlice(split_str[r]));
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
l--;
|
2021-10-14 14:08:35 +02:00
|
|
|
if (td::Random::fast_bool()) {
|
2021-12-23 12:18:32 +01:00
|
|
|
builder.prepend(split_str[l]);
|
2018-08-13 19:15:09 +02:00
|
|
|
} else {
|
2021-12-23 12:18:32 +01:00
|
|
|
builder.prepend(td::BufferSlice(split_str[l]));
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_EQ(builder.extract().as_slice(), str);
|
|
|
|
}
|
|
|
|
}
|