2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01: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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-12-19 15:48:39 +01:00
|
|
|
#include "td/utils/Context.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2022-07-14 14:15:17 +02:00
|
|
|
#include "td/utils/port/sleep.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2023-01-02 12:29:03 +01:00
|
|
|
#include "td/utils/Span.h"
|
2018-12-19 15:48:39 +01:00
|
|
|
#include "td/utils/Status.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <atomic>
|
2022-07-14 17:38:48 +02:00
|
|
|
#include <condition_variable>
|
2020-06-09 04:17:39 +02:00
|
|
|
#include <functional>
|
2022-07-14 14:15:17 +02:00
|
|
|
#include <mutex>
|
2018-12-19 21:35:13 +01:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
class RandomSteps {
|
|
|
|
public:
|
|
|
|
struct Step {
|
|
|
|
std::function<void()> func;
|
2020-06-26 01:24:13 +02:00
|
|
|
uint32 weight;
|
2020-06-24 13:47:36 +02:00
|
|
|
};
|
2020-06-26 01:24:13 +02:00
|
|
|
|
|
|
|
explicit RandomSteps(vector<Step> steps) : steps_(std::move(steps)) {
|
|
|
|
for (const auto &step : steps_) {
|
2020-06-24 13:47:36 +02:00
|
|
|
steps_sum_ += step.weight;
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
template <class Random>
|
2020-06-26 01:24:13 +02:00
|
|
|
void step(Random &rnd) const {
|
2020-06-24 13:47:36 +02:00
|
|
|
auto w = rnd() % steps_sum_;
|
2020-06-26 01:24:13 +02:00
|
|
|
for (const auto &step : steps_) {
|
2020-06-24 13:47:36 +02:00
|
|
|
if (w < step.weight) {
|
|
|
|
step.func();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
w -= step.weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-06-26 01:24:13 +02:00
|
|
|
vector<Step> steps_;
|
|
|
|
int32 steps_sum_ = 0;
|
2020-06-24 13:47:36 +02:00
|
|
|
};
|
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
class RegressionTester {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2018-12-19 15:48:39 +01:00
|
|
|
virtual ~RegressionTester() = default;
|
|
|
|
static void destroy(CSlice db_path);
|
2018-12-19 21:35:13 +01:00
|
|
|
static unique_ptr<RegressionTester> create(string db_path, string db_cache_dir = "");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
virtual Status verify_test(Slice name, Slice result) = 0;
|
|
|
|
virtual void save_db() = 0;
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
class Test {
|
|
|
|
public:
|
|
|
|
virtual ~Test() = default;
|
|
|
|
virtual void run() {
|
|
|
|
while (step()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 15:48:39 +01:00
|
|
|
virtual bool step() {
|
|
|
|
run();
|
|
|
|
return false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-19 15:48:39 +01:00
|
|
|
Test() = default;
|
|
|
|
Test(const Test &) = delete;
|
|
|
|
Test &operator=(const Test &) = delete;
|
|
|
|
Test(Test &&) = delete;
|
|
|
|
Test &operator=(Test &&) = delete;
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
class TestContext : public Context<TestContext> {
|
|
|
|
public:
|
|
|
|
virtual ~TestContext() = default;
|
|
|
|
virtual Slice name() = 0;
|
|
|
|
virtual Status verify(Slice data) = 0;
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class TestsRunner final : public TestContext {
|
2018-12-19 15:48:39 +01:00
|
|
|
public:
|
|
|
|
static TestsRunner &get_default();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-08-04 14:22:16 +02:00
|
|
|
void add_test(string name, std::function<unique_ptr<Test>()> test);
|
2018-12-19 21:35:13 +01:00
|
|
|
void add_substr_filter(string str);
|
2018-12-19 15:48:39 +01:00
|
|
|
void set_stress_flag(bool flag);
|
|
|
|
void run_all();
|
|
|
|
bool run_all_step();
|
2018-12-19 21:35:13 +01:00
|
|
|
void set_regression_tester(unique_ptr<RegressionTester> regression_tester);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct State {
|
2018-12-19 15:48:39 +01:00
|
|
|
size_t it{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_running = false;
|
2018-12-19 15:48:39 +01:00
|
|
|
double start{0};
|
2020-06-24 13:47:36 +02:00
|
|
|
double start_unadjusted{0};
|
2018-12-19 15:48:39 +01:00
|
|
|
size_t end{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-12-19 15:48:39 +01:00
|
|
|
bool stress_flag_{false};
|
2018-12-19 21:35:13 +01:00
|
|
|
vector<string> substr_filters_;
|
2020-08-04 14:22:16 +02:00
|
|
|
struct TestInfo {
|
|
|
|
std::function<unique_ptr<Test>()> creator;
|
|
|
|
unique_ptr<Test> test;
|
|
|
|
};
|
|
|
|
vector<std::pair<string, TestInfo>> tests_;
|
2018-12-19 15:48:39 +01:00
|
|
|
State state_;
|
2018-12-19 21:35:13 +01:00
|
|
|
unique_ptr<RegressionTester> regression_tester_;
|
2018-12-19 15:48:39 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Slice name() final;
|
|
|
|
Status verify(Slice data) final;
|
2018-12-19 15:48:39 +01:00
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
template <class T>
|
|
|
|
class RegisterTest {
|
|
|
|
public:
|
2020-06-09 04:17:39 +02:00
|
|
|
explicit RegisterTest(string name, TestsRunner &runner = TestsRunner::get_default()) {
|
2021-10-18 18:26:14 +02:00
|
|
|
runner.add_test(std::move(name), [] { return make_unique<T>(); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-14 14:15:17 +02:00
|
|
|
class StageWait {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
void wait(uint64 need) {
|
|
|
|
value_.fetch_add(1, std::memory_order_release);
|
|
|
|
while (value_.load(std::memory_order_acquire) < need) {
|
2022-07-14 14:15:17 +02:00
|
|
|
usleep_for(1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::atomic<uint64> value_{0};
|
|
|
|
};
|
|
|
|
|
2022-07-14 14:15:17 +02:00
|
|
|
class StageMutex {
|
|
|
|
public:
|
|
|
|
void wait(uint64 need) {
|
|
|
|
std::unique_lock<std::mutex> lock{mutex_};
|
|
|
|
value_++;
|
|
|
|
if (value_ == need) {
|
|
|
|
cond_.notify_all();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cond_.wait(lock, [&] { return value_ >= need; });
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex mutex_;
|
|
|
|
std::condition_variable cond_;
|
|
|
|
uint64 value_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
using Stage = StageMutex;
|
|
|
|
|
2020-09-27 19:38:23 +02:00
|
|
|
string rand_string(int from, int to, size_t len);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-09-27 19:38:23 +02:00
|
|
|
vector<string> rand_split(Slice str);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-02 12:29:03 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T1, class T2>
|
2023-03-02 15:35:57 +01:00
|
|
|
void assert_eq_impl(const T1 &expected, const T2 &received, const char *file, int line) {
|
|
|
|
LOG_CHECK(expected == received) << tag("expected", expected) << tag("received", received) << " in " << file
|
|
|
|
<< " at line " << line;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2023-03-02 15:35:57 +01:00
|
|
|
void assert_true_impl(const T &received, const char *file, int line) {
|
|
|
|
LOG_CHECK(received) << "Expected true in " << file << " at line " << line;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|
|
|
|
|
2023-03-02 15:35:57 +01:00
|
|
|
#define ASSERT_EQ(expected, received) ::td::assert_eq_impl((expected), (received), __FILE__, __LINE__)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-03-02 15:35:57 +01:00
|
|
|
#define ASSERT_TRUE(received) ::td::assert_true_impl((received), __FILE__, __LINE__)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-03-02 15:35:57 +01:00
|
|
|
#define ASSERT_STREQ(expected, received) \
|
|
|
|
::td::assert_eq_impl(::td::Slice((expected)), ::td::Slice((received)), __FILE__, __LINE__)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
#define REGRESSION_VERIFY(data) ::td::TestContext::get()->verify(data).ensure()
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#define TEST_NAME(test_case_name, test_name) \
|
|
|
|
TD_CONCAT(Test, TD_CONCAT(_, TD_CONCAT(test_case_name, TD_CONCAT(_, test_name))))
|
|
|
|
|
|
|
|
#define TEST(test_case_name, test_name) TEST_IMPL(TEST_NAME(test_case_name, test_name))
|
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
#define TEST_IMPL(test_name) \
|
2021-07-04 04:58:54 +02:00
|
|
|
class test_name final : public ::td::Test { \
|
2018-12-19 15:48:39 +01:00
|
|
|
public: \
|
|
|
|
using Test::Test; \
|
|
|
|
void run() final; \
|
|
|
|
}; \
|
|
|
|
::td::RegisterTest<test_name> TD_CONCAT(test_instance_, TD_CONCAT(test_name, __LINE__))(TD_DEFINE_STR(test_name)); \
|
2018-12-31 20:04:05 +01:00
|
|
|
void test_name::run()
|