From 582206d641056cf4efa619ee3b7fabe073720de6 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 8 Oct 2020 12:56:08 +0300 Subject: [PATCH] Use Random::fast_bool. GitOrigin-RevId: 42da8a237c6dfbb6e72bc5b21482891b6c9d58b5 --- td/telegram/SecretChatsManager.cpp | 2 +- td/telegram/cli.cpp | 13 ++++++------- tdactor/test/actors_main.cpp | 4 ++-- tdutils/td/utils/tests.cpp | 2 +- tdutils/test/EpochBasedMemoryReclamation.cpp | 2 +- tdutils/test/HazardPointers.cpp | 2 +- tdutils/test/OrderedEventsProcessor.cpp | 2 +- tdutils/test/buffer.cpp | 6 +++--- test/http.cpp | 4 ++-- test/message_entities.cpp | 2 +- test/secret.cpp | 2 +- test/tdclient.cpp | 10 +++++----- 12 files changed, 25 insertions(+), 26 deletions(-) diff --git a/td/telegram/SecretChatsManager.cpp b/td/telegram/SecretChatsManager.cpp index 6c63fb68f..94ab4aede 100644 --- a/td/telegram/SecretChatsManager.cpp +++ b/td/telegram/SecretChatsManager.cpp @@ -114,7 +114,7 @@ void SecretChatsManager::cancel_chat(SecretChatId secret_chat_id, Promise<> prom void SecretChatsManager::send_message(SecretChatId secret_chat_id, tl_object_ptr message, tl_object_ptr file, Promise<> promise) { - // message->message_ = Random::fast(0, 1) ? string(1, static_cast(0x80)) : "a"; + // message->message_ = Random::fast_bool() ? string(1, static_cast(0x80)) : "a"; auto actor = get_chat_actor(secret_chat_id.get()); auto safe_promise = SafePromise<>(std::move(promise), Status::Error(400, "Can't find secret chat")); send_closure(actor, &SecretChatActor::send_message, std::move(message), std::move(file), std::move(safe_promise)); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 95533a81c..9ea42c604 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -1108,7 +1108,7 @@ class CliClient final : public Actor { std::tie(excluded_chat_ids, filter) = split(filter); auto rand_bool = [] { - return Random::fast(0, 1) == 1; + return Random::fast_bool(); }; return td_api::make_object( @@ -2748,7 +2748,7 @@ class CliClient final : public Actor { auto chat = as_chat_id(chat_id); send_request(td_api::make_object( chat, as_chat_id(from_chat_id), as_message_ids(message_ids), default_message_send_options(), op[0] == 'c', - Random::fast(0, 1) == 1)); + Random::fast_bool())); } else if (op == "resend") { string chat_id; string message_ids; @@ -2772,7 +2772,7 @@ class CliClient final : public Actor { } else if (op == "cc" || op == "CreateCall") { send_request(td_api::make_object( as_user_id(args), td_api::make_object(true, true, 65, 65, vector{"2.6", "3.0"}), - Random::fast(0, 1) == 1)); + Random::fast_bool())); } else if (op == "ac" || op == "AcceptCall") { send_request(td_api::make_object( as_call_id(args), @@ -2785,7 +2785,7 @@ class CliClient final : public Actor { std::tie(call_id, is_disconnected) = split(args); send_request(td_api::make_object(as_call_id(call_id), as_bool(is_disconnected), 0, - Random::fast(0, 1) == 1, 0)); + Random::fast_bool(), 0)); } else if (op == "scr" || op == "SendCallRating") { string call_id; string rating; @@ -3280,8 +3280,7 @@ class CliClient final : public Actor { td_api::object_ptr copy_options; if (op == "scopy") { - copy_options = - td_api::make_object(true, Random::fast(0, 1) == 0, as_caption("_as_d")); + copy_options = td_api::make_object(true, Random::fast_bool(), as_caption("_as_d")); } send_message(chat_id, @@ -4348,7 +4347,7 @@ class CliClient final : public Actor { } BufferSlice block(it->part_size); FileFd::open(it->source, FileFd::Flags::Read).move_as_ok().pread(block.as_slice(), it->local_size).ensure(); - if (Random::fast(0, 1) == 0) { + if (Random::fast_bool()) { auto open_flags = FileFd::Flags::Write | (it->local_size ? 0 : FileFd::Flags::Truncate | FileFd::Flags::Create); FileFd::open(it->destination, open_flags).move_as_ok().pwrite(block.as_slice(), it->local_size).ensure(); } else { diff --git a/tdactor/test/actors_main.cpp b/tdactor/test/actors_main.cpp index f9f085361..e98f338fe 100644 --- a/tdactor/test/actors_main.cpp +++ b/tdactor/test/actors_main.cpp @@ -261,7 +261,7 @@ class MainQueryActor final : public Actor { void wakeup() override { int cnt = 100000; while (out_cnt_ < in_cnt_ + 100 && out_cnt_ < cnt) { - if (Random::fast(0, 1)) { + if (Random::fast_bool()) { send_closure(rand_elem(actors_), &QueryActor::query, create_query()); } else { send_closure_later(rand_elem(actors_), &QueryActor::query, create_query()); @@ -306,7 +306,7 @@ class SimpleActor final : public Actor { return; } q_++; - p_ = Random::fast(0, 1) ? 1 : 10000; + p_ = Random::fast_bool() ? 1 : 10000; auto future = Random::fast(0, 3) == 0 ? send_promise(worker_, &Worker::query, q_, p_) : send_promise(worker_, &Worker::query, q_, p_); if (future.is_ready()) { diff --git a/tdutils/td/utils/tests.cpp b/tdutils/td/utils/tests.cpp index f036918f0..a4337341b 100644 --- a/tdutils/td/utils/tests.cpp +++ b/tdutils/td/utils/tests.cpp @@ -34,7 +34,7 @@ vector rand_split(Slice str) { size_t pos = 0; while (pos < str.size()) { size_t len; - if (Random::fast(0, 1) == 1) { + if (Random::fast_bool()) { len = Random::fast(1, 10); } else { len = Random::fast(100, 200); diff --git a/tdutils/test/EpochBasedMemoryReclamation.cpp b/tdutils/test/EpochBasedMemoryReclamation.cpp index e55556eab..1be94215b 100644 --- a/tdutils/test/EpochBasedMemoryReclamation.cpp +++ b/tdutils/test/EpochBasedMemoryReclamation.cpp @@ -42,7 +42,7 @@ TEST(EpochBaseMemoryReclamation, stress) { locker.retire(); } if (td::Random::fast(0, 5) == 0) { - std::string *new_str = new std::string(td::Random::fast(0, 1) == 0 ? "one" : "twotwo"); + std::string *new_str = new std::string(td::Random::fast_bool() ? "one" : "twotwo"); if (node.name_.compare_exchange_strong(str, new_str, std::memory_order_acq_rel)) { locker.retire(str); } else { diff --git a/tdutils/test/HazardPointers.cpp b/tdutils/test/HazardPointers.cpp index 4009b7367..a85da290e 100644 --- a/tdutils/test/HazardPointers.cpp +++ b/tdutils/test/HazardPointers.cpp @@ -36,7 +36,7 @@ TEST(HazardPointers, stress) { } holder.clear(); if (td::Random::fast(0, 5) == 0) { - std::string *new_str = new std::string(td::Random::fast(0, 1) == 0 ? "one" : "twotwo"); + std::string *new_str = new std::string(td::Random::fast_bool() ? "one" : "twotwo"); if (node.name_.compare_exchange_strong(str, new_str, std::memory_order_acq_rel)) { hazard_pointers.retire(thread_id, str); } else { diff --git a/tdutils/test/OrderedEventsProcessor.cpp b/tdutils/test/OrderedEventsProcessor.cpp index 26660e9c0..fcbecd1ab 100644 --- a/tdutils/test/OrderedEventsProcessor.cpp +++ b/tdutils/test/OrderedEventsProcessor.cpp @@ -18,7 +18,7 @@ TEST(OrderedEventsProcessor, random) { int offset = 1000000; std::vector> v; for (int i = 0; i < n; i++) { - auto shift = td::Random::fast(0, 1) ? td::Random::fast(0, d) : td::Random::fast(0, 1) * d; + auto shift = td::Random::fast_bool() ? td::Random::fast(0, d) : td::Random::fast(0, 1) * d; v.push_back({i + shift, i + offset}); } std::sort(v.begin(), v.end()); diff --git a/tdutils/test/buffer.cpp b/tdutils/test/buffer.cpp index 34ef876a3..ee2b5e1e3 100644 --- a/tdutils/test/buffer.cpp +++ b/tdutils/test/buffer.cpp @@ -37,16 +37,16 @@ TEST(Buffer, buffer_builder) { int r = l; BufferBuilder builder(splitted_str[l], 123, 1000); while (l != 0 || r != static_cast(splitted_str.size()) - 1) { - if (l == 0 || (Random::fast(0, 1) == 1 && r != static_cast(splitted_str.size() - 1))) { + if (l == 0 || (Random::fast_bool() && r != static_cast(splitted_str.size() - 1))) { r++; - if (Random::fast(0, 1) == 1) { + if (Random::fast_bool()) { builder.append(splitted_str[r]); } else { builder.append(BufferSlice(splitted_str[r])); } } else { l--; - if (Random::fast(0, 1) == 1) { + if (Random::fast_bool()) { builder.prepend(splitted_str[l]); } else { builder.prepend(BufferSlice(splitted_str[l])); diff --git a/test/http.cpp b/test/http.cpp index 74e17cfec..182c483c1 100644 --- a/test/http.cpp +++ b/test/http.cpp @@ -99,8 +99,8 @@ static string make_http_query(string content, bool is_chunked, bool is_gzip, dou } static string rand_http_query(string content) { - bool is_chunked = Random::fast(0, 1) == 0; - bool is_gzip = Random::fast(0, 1) == 0; + bool is_chunked = Random::fast_bool(); + bool is_gzip = Random::fast_bool(); return make_http_query(std::move(content), is_chunked, is_gzip); } diff --git a/test/message_entities.cpp b/test/message_entities.cpp index 8b5e659ab..cf07fb59d 100644 --- a/test/message_entities.cpp +++ b/test/message_entities.cpp @@ -964,7 +964,7 @@ TEST(MessageEntities, fix_formatted_text) { {td::MessageEntity::Type::Italic, 2, 11}}); for (size_t test_n = 0; test_n < 100000; test_n++) { - bool is_url = td::Random::fast(0, 1) == 1; + bool is_url = td::Random::fast_bool(); td::int32 url_offset = 0; td::int32 url_end = 0; if (is_url) { diff --git a/test/secret.cpp b/test/secret.cpp index d639c2a47..acb61089d 100644 --- a/test/secret.cpp +++ b/test/secret.cpp @@ -786,7 +786,7 @@ class Master : public Actor { return false; } void send_net_query(NetQueryPtr query, ActorShared callback, bool ordered) { - if (can_fail(query) && Random::fast(0, 1) == 0) { + if (can_fail(query) && Random::fast_bool()) { LOG(INFO) << "Fail query " << query; auto resend_promise = PromiseCreator::lambda([id = actor_shared(this, get_link_token()), callback_actor = callback.get(), diff --git a/test/tdclient.cpp b/test/tdclient.cpp index 6ab0d533d..b20cd037e 100644 --- a/test/tdclient.cpp +++ b/test/tdclient.cpp @@ -861,7 +861,7 @@ TEST(Client, SimpleMulti) { for (size_t i = 0; i < clients.size(); i++) { clients[i].send({i + 2, td::make_tl_object(3)}); - if (Random::fast(0, 1) == 1) { + if (Random::fast_bool()) { clients[i].send({1, td::make_tl_object()}); } } @@ -974,7 +974,7 @@ TEST(Client, Close) { can_stop_receive = true; }); - auto max_continue_send = td::Random::fast(0, 1) ? 0 : 1000; + auto max_continue_send = td::Random::fast_bool() ? 0 : 1000; td::thread receive_thread([&] { while (true) { auto response = client.receive(100.0); @@ -1000,7 +1000,7 @@ TEST(Client, Close) { } }); - td::usleep_for((td::Random::fast(0, 1) ? 0 : 1000) * (td::Random::fast(0, 1) ? 1 : 50)); + td::usleep_for((td::Random::fast_bool() ? 0 : 1000) * (td::Random::fast_bool() ? 1 : 50)); client.send({1, td::make_tl_object()}); send_thread.join(); @@ -1033,7 +1033,7 @@ TEST(Client, ManagerClose) { can_stop_receive = true; }); - auto max_continue_send = td::Random::fast(0, 1) ? 0 : 1000; + auto max_continue_send = td::Random::fast_bool() ? 0 : 1000; td::thread receive_thread([&] { while (true) { auto response = client_manager.receive(100.0); @@ -1059,7 +1059,7 @@ TEST(Client, ManagerClose) { } }); - td::usleep_for((td::Random::fast(0, 1) ? 0 : 1000) * (td::Random::fast(0, 1) ? 1 : 50)); + td::usleep_for((td::Random::fast_bool() ? 0 : 1000) * (td::Random::fast_bool() ? 1 : 50)); client_manager.send(client_id, 1, td::make_tl_object()); send_thread.join();