Explicitly specify namespace td in tdutils tests.

This commit is contained in:
levlam 2021-10-14 15:08:35 +03:00
parent c7ce933520
commit e78a5fbecf
3 changed files with 48 additions and 51 deletions

View File

@ -9,47 +9,45 @@
#include "td/utils/buffer.h"
#include "td/utils/Random.h"
using namespace td;
TEST(Buffer, buffer_builder) {
{
BufferBuilder builder;
td::BufferBuilder builder;
builder.append("b");
builder.prepend("a");
builder.append("c");
ASSERT_EQ(builder.extract().as_slice(), "abc");
}
{
BufferBuilder builder{"hello", 0, 0};
td::BufferBuilder builder{"hello", 0, 0};
ASSERT_EQ(builder.extract().as_slice(), "hello");
}
{
BufferBuilder builder{"hello", 1, 1};
td::BufferBuilder builder{"hello", 1, 1};
builder.prepend("A ");
builder.append(" B");
ASSERT_EQ(builder.extract().as_slice(), "A hello B");
}
{
std::string str = rand_string('a', 'z', 10000);
auto splitted_str = rand_split(str);
auto str = td::rand_string('a', 'z', 10000);
auto splitted_str = td::rand_split(str);
int l = Random::fast(0, static_cast<int32>(splitted_str.size() - 1));
int l = td::Random::fast(0, static_cast<int>(splitted_str.size() - 1));
int r = l;
BufferBuilder builder(splitted_str[l], 123, 1000);
while (l != 0 || r != static_cast<int32>(splitted_str.size()) - 1) {
if (l == 0 || (Random::fast_bool() && r != static_cast<int32>(splitted_str.size() - 1))) {
td::BufferBuilder builder(splitted_str[l], 123, 1000);
while (l != 0 || r != static_cast<int>(splitted_str.size()) - 1) {
if (l == 0 || (td::Random::fast_bool() && r != static_cast<int>(splitted_str.size() - 1))) {
r++;
if (Random::fast_bool()) {
if (td::Random::fast_bool()) {
builder.append(splitted_str[r]);
} else {
builder.append(BufferSlice(splitted_str[r]));
builder.append(td::BufferSlice(splitted_str[r]));
}
} else {
l--;
if (Random::fast_bool()) {
if (td::Random::fast_bool()) {
builder.prepend(splitted_str[l]);
} else {
builder.prepend(BufferSlice(splitted_str[l]));
builder.prepend(td::BufferSlice(splitted_str[l]));
}
}
}

View File

@ -12,17 +12,15 @@
#include <utility>
using namespace td;
static void decode_encode(string str, string result = "") {
static void decode_encode(td::string str, td::string result = "") {
auto str_copy = str;
auto r_value = json_decode(str_copy);
auto r_value = td::json_decode(str_copy);
ASSERT_TRUE(r_value.is_ok());
if (r_value.is_error()) {
LOG(INFO) << r_value.error();
return;
}
auto new_str = json_encode<string>(r_value.ok());
auto new_str = td::json_encode<td::string>(r_value.ok());
if (result.empty()) {
result = str;
}
@ -31,18 +29,19 @@ static void decode_encode(string str, string result = "") {
TEST(JSON, array) {
char tmp[1000];
StringBuilder sb(MutableSlice{tmp, sizeof(tmp)});
JsonBuilder jb(std::move(sb));
td::StringBuilder sb(td::MutableSlice{tmp, sizeof(tmp)});
td::JsonBuilder jb(std::move(sb));
jb.enter_value().enter_array() << "Hello" << -123;
ASSERT_EQ(jb.string_builder().is_error(), false);
auto encoded = jb.string_builder().as_cslice().str();
ASSERT_EQ("[\"Hello\",-123]", encoded);
decode_encode(encoded);
}
TEST(JSON, object) {
char tmp[1000];
StringBuilder sb(MutableSlice{tmp, sizeof(tmp)});
JsonBuilder jb(std::move(sb));
td::StringBuilder sb(td::MutableSlice{tmp, sizeof(tmp)});
td::JsonBuilder jb(std::move(sb));
auto c = jb.enter_object();
c("key", "value");
c("1", 2);
@ -55,8 +54,8 @@ TEST(JSON, object) {
TEST(JSON, nested) {
char tmp[1000];
StringBuilder sb(MutableSlice{tmp, sizeof(tmp)});
JsonBuilder jb(std::move(sb));
td::StringBuilder sb(td::MutableSlice{tmp, sizeof(tmp)});
td::JsonBuilder jb(std::move(sb));
{
auto a = jb.enter_array();
a << 1;

View File

@ -18,11 +18,9 @@
#include <limits>
#include <utility>
using namespace td;
#if TD_HAVE_OPENSSL
static bool is_prime(uint64 x) {
for (uint64 d = 2; d < x && d * d <= x; d++) {
static bool is_prime(td::uint64 x) {
for (td::uint64 d = 2; d < x && d * d <= x; d++) {
if (x % d == 0) {
return false;
}
@ -30,8 +28,8 @@ static bool is_prime(uint64 x) {
return true;
}
static std::vector<uint64> gen_primes(uint64 L, uint64 R, int limit = 0) {
std::vector<uint64> res;
static td::vector<td::uint64> gen_primes(td::uint64 L, td::uint64 R, int limit = 0) {
td::vector<td::uint64> res;
for (auto x = L; x <= R && (limit <= 0 || res.size() < static_cast<std::size_t>(limit)); x++) {
if (is_prime(x)) {
res.push_back(x);
@ -40,21 +38,23 @@ static std::vector<uint64> gen_primes(uint64 L, uint64 R, int limit = 0) {
return res;
}
static std::vector<uint64> gen_primes() {
std::vector<uint64> result;
append(result, gen_primes(1, 100));
append(result, gen_primes((1ull << 31) - 500000, std::numeric_limits<uint64>::max(), 5));
append(result, gen_primes((1ull << 32) - 500000, std::numeric_limits<uint64>::max(), 5));
append(result, gen_primes((1ull << 39) - 500000, std::numeric_limits<uint64>::max(), 1));
static td::vector<td::uint64> gen_primes() {
td::vector<td::uint64> result;
td::append(result, gen_primes(1, 100));
td::append(result, gen_primes((1ull << 31) - 500000, std::numeric_limits<td::uint64>::max(), 5));
td::append(result, gen_primes((1ull << 32) - 500000, std::numeric_limits<td::uint64>::max(), 5));
td::append(result, gen_primes((1ull << 39) - 500000, std::numeric_limits<td::uint64>::max(), 1));
return result;
}
using PqQuery = std::pair<uint64, uint64>;
using PqQuery = std::pair<td::uint64, td::uint64>;
static bool cmp(const PqQuery &a, const PqQuery &b) {
return a.first * a.second < b.first * b.second;
}
static std::vector<PqQuery> gen_pq_queries() {
std::vector<PqQuery> res;
static td::vector<PqQuery> gen_pq_queries() {
td::vector<PqQuery> res;
auto primes = gen_primes();
for (auto q : primes) {
for (auto p : primes) {
@ -68,21 +68,21 @@ static std::vector<PqQuery> gen_pq_queries() {
return res;
}
static void test_pq(uint64 first, uint64 second) {
BigNum p = BigNum::from_decimal(PSLICE() << first).move_as_ok();
BigNum q = BigNum::from_decimal(PSLICE() << second).move_as_ok();
static void test_pq(td::uint64 first, td::uint64 second) {
td::BigNum p = td::BigNum::from_decimal(PSLICE() << first).move_as_ok();
td::BigNum q = td::BigNum::from_decimal(PSLICE() << second).move_as_ok();
BigNum pq;
BigNumContext context;
BigNum::mul(pq, p, q, context);
std::string pq_str = pq.to_binary();
td::BigNum pq;
td::BigNumContext context;
td::BigNum::mul(pq, p, q, context);
td::string pq_str = pq.to_binary();
std::string p_str, q_str;
td::string p_str, q_str;
int err = td::pq_factorize(pq_str, &p_str, &q_str);
LOG_CHECK(err == 0) << first << " * " << second;
BigNum p_res = BigNum::from_binary(p_str);
BigNum q_res = BigNum::from_binary(q_str);
td::BigNum p_res = td::BigNum::from_binary(p_str);
td::BigNum q_res = td::BigNum::from_binary(q_str);
LOG_CHECK(p_str == p.to_binary()) << td::tag("got", p_res.to_decimal()) << td::tag("expected", first);
LOG_CHECK(q_str == q.to_binary()) << td::tag("got", q_res.to_decimal()) << td::tag("expected", second);