Move random_shuffle to tests.h.

This commit is contained in:
levlam 2023-01-02 14:29:03 +03:00
parent 4258030967
commit 8b763bedaf
6 changed files with 15 additions and 16 deletions

View File

@ -8,9 +8,6 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include "td/utils/Span.h"
#include <utility>
namespace td { namespace td {
@ -57,13 +54,4 @@ class Random {
}; };
}; };
template <class T, class R>
void random_shuffle(MutableSpan<T> v, R &rnd) {
for (size_t i = 1; i < v.size(); i++) {
auto pos = static_cast<size_t>(rnd()) % (i + 1);
using std::swap;
swap(v[i], v[pos]);
}
}
} // namespace td } // namespace td

View File

@ -12,6 +12,7 @@
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/port/sleep.h" #include "td/utils/port/sleep.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include "td/utils/Span.h"
#include "td/utils/Status.h" #include "td/utils/Status.h"
#include <atomic> #include <atomic>
@ -165,6 +166,15 @@ string rand_string(int from, int to, size_t len);
vector<string> rand_split(Slice str); vector<string> rand_split(Slice str);
template <class T, class R>
void rand_shuffle(MutableSpan<T> v, R &rnd) {
for (size_t i = 1; i < v.size(); i++) {
auto pos = static_cast<size_t>(rnd()) % (i + 1);
using std::swap;
swap(v[i], v[pos]);
}
}
template <class T1, class T2> template <class T1, class T2>
void assert_eq_impl(const T1 &expected, const T2 &got, const char *file, int line) { void assert_eq_impl(const T1 &expected, const T2 &got, const char *file, int line) {
LOG_CHECK(expected == got) << tag("expected", expected) << tag("got", got) << " in " << file << " at line " << line; LOG_CHECK(expected == got) << tag("expected", expected) << tag("got", got) << " in " << file << " at line " << line;

View File

@ -139,7 +139,7 @@ TEST(ChainScheduler, Stress) {
int chain_n = rnd.fast(1, ChainsN); int chain_n = rnd.fast(1, ChainsN);
td::vector<ChainId> chain_ids(ChainsN); td::vector<ChainId> chain_ids(ChainsN);
std::iota(chain_ids.begin(), chain_ids.end(), 0); std::iota(chain_ids.begin(), chain_ids.end(), 0);
td::random_shuffle(td::as_mutable_span(chain_ids), rnd); td::rand_shuffle(td::as_mutable_span(chain_ids), rnd);
chain_ids.resize(chain_n); chain_ids.resize(chain_n);
for (auto chain_id : chain_ids) { for (auto chain_id : chain_ids) {
chains[td::narrow_cast<size_t>(chain_id)].push_back(query); chains[td::narrow_cast<size_t>(chain_id)].push_back(query);

View File

@ -204,7 +204,7 @@ static void BM_Get(benchmark::State &state) {
} }
std::size_t key_i = 0; std::size_t key_i = 0;
td::random_shuffle(td::as_mutable_span(keys), rnd); td::rand_shuffle(td::as_mutable_span(keys), rnd);
auto next_key = [&] { auto next_key = [&] {
key_i++; key_i++;
if (key_i == data.size()) { if (key_i == data.size()) {

View File

@ -23,7 +23,7 @@ TEST(Heap, sort_random_perm) {
v[i] = i; v[i] = i;
} }
td::Random::Xorshift128plus rnd(123); td::Random::Xorshift128plus rnd(123);
td::random_shuffle(td::as_mutable_span(v), rnd); td::rand_shuffle(td::as_mutable_span(v), rnd);
td::vector<td::HeapNode> nodes(n); td::vector<td::HeapNode> nodes(n);
td::KHeap<int> kheap; td::KHeap<int> kheap;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {

View File

@ -23,6 +23,7 @@
#include "td/utils/port/signals.h" #include "td/utils/port/signals.h"
#include "td/utils/Promise.h" #include "td/utils/Promise.h"
#include "td/utils/Random.h" #include "td/utils/Random.h"
#include "td/utils/tests.h"
#include <iostream> #include <iostream>
#include <map> #include <map>
@ -437,7 +438,7 @@ class TestDownloadFile : public Task {
begin = end; begin = end;
} }
random_shuffle(as_mutable_span(ranges_), rnd); rand_shuffle(as_mutable_span(ranges_), rnd);
start_chunk(); start_chunk();
} }