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

View File

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

View File

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