2020-06-09 17:40:36 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2020-06-09 17:40:36 +02: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)
|
|
|
|
//
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/List.h"
|
2020-06-10 02:01:23 +02:00
|
|
|
#include "td/utils/MovableValue.h"
|
2020-06-10 00:38:00 +02:00
|
|
|
#include "td/utils/port/thread.h"
|
2020-06-09 17:40:36 +02:00
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
#include "td/utils/TsList.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
2020-06-10 02:42:00 +02:00
|
|
|
#include <mutex>
|
2020-06-09 17:40:36 +02:00
|
|
|
#include <set>
|
2020-06-10 00:38:00 +02:00
|
|
|
#include <utility>
|
2020-06-09 17:40:36 +02:00
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
struct ListData {
|
2020-06-10 02:01:23 +02:00
|
|
|
td::MovableValue<td::uint64> value;
|
|
|
|
td::MovableValue<bool> in_list;
|
2020-06-09 17:40:36 +02:00
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
ListData() = default;
|
|
|
|
ListData(td::uint64 value, bool in_list) : value(value), in_list(in_list) {
|
2020-06-09 17:40:36 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
struct Node final : public td::ListNode {
|
2020-06-09 17:40:36 +02:00
|
|
|
Node() = default;
|
2020-06-12 02:53:04 +02:00
|
|
|
explicit Node(ListData data) : data(std::move(data)) {
|
2020-06-09 17:40:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
ListData data;
|
2020-06-09 17:40:36 +02:00
|
|
|
};
|
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
static ListData &get_data(Node &node) {
|
2020-06-09 17:40:36 +02:00
|
|
|
return node.data;
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
static ListData &get_data(td::TsListNode<ListData> &node) {
|
2020-06-09 17:40:36 +02:00
|
|
|
return node.get_data_unsafe();
|
|
|
|
}
|
|
|
|
|
2020-06-10 02:01:23 +02:00
|
|
|
static std::unique_lock<std::mutex> lock(td::ListNode &node) {
|
2020-06-09 17:40:36 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:53:04 +02:00
|
|
|
static std::unique_lock<std::mutex> lock(td::TsListNode<ListData> &node) {
|
2020-06-09 17:40:36 +02:00
|
|
|
return node.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ListNodeT, class ListRootT, class NodeT>
|
2020-06-10 02:01:23 +02:00
|
|
|
static void do_run_list_test(ListRootT &root, std::atomic<td::uint64> &id) {
|
2020-06-09 17:40:36 +02:00
|
|
|
td::vector<NodeT> nodes;
|
|
|
|
|
|
|
|
td::Random::Xorshift128plus rnd(123);
|
|
|
|
|
|
|
|
auto next_id = [&] {
|
|
|
|
return ++id;
|
|
|
|
};
|
|
|
|
auto add_node = [&] {
|
|
|
|
if (nodes.size() >= 20) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nodes.emplace_back(NodeT({next_id(), false}));
|
|
|
|
};
|
|
|
|
auto pop_node = [&] {
|
2020-06-13 04:13:11 +02:00
|
|
|
if (nodes.empty()) {
|
2020-06-09 17:40:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
nodes.pop_back();
|
|
|
|
};
|
2020-06-13 04:13:11 +02:00
|
|
|
auto random_node_index = [&] {
|
|
|
|
CHECK(!nodes.empty());
|
|
|
|
return rnd.fast(0, static_cast<int>(nodes.size()) - 1);
|
|
|
|
};
|
2020-06-09 17:40:36 +02:00
|
|
|
|
|
|
|
auto link_node = [&] {
|
|
|
|
if (nodes.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-13 04:13:11 +02:00
|
|
|
auto i = random_node_index();
|
2020-06-09 17:40:36 +02:00
|
|
|
nodes[i].remove();
|
2020-06-12 02:53:04 +02:00
|
|
|
get_data(nodes[i]) = ListData(next_id(), true);
|
2020-06-09 17:40:36 +02:00
|
|
|
root.put(&nodes[i]);
|
|
|
|
};
|
|
|
|
auto unlink_node = [&] {
|
|
|
|
if (nodes.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-13 04:13:11 +02:00
|
|
|
auto i = random_node_index();
|
2020-06-09 17:40:36 +02:00
|
|
|
nodes[i].remove();
|
|
|
|
get_data(nodes[i]).in_list = false;
|
|
|
|
};
|
|
|
|
auto swap_nodes = [&] {
|
|
|
|
if (nodes.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-13 04:13:11 +02:00
|
|
|
auto i = random_node_index();
|
|
|
|
auto j = random_node_index();
|
2020-06-09 17:40:36 +02:00
|
|
|
std::swap(nodes[i], nodes[j]);
|
|
|
|
};
|
|
|
|
auto set_node = [&] {
|
|
|
|
if (nodes.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-13 04:13:11 +02:00
|
|
|
auto i = random_node_index();
|
|
|
|
auto j = random_node_index();
|
2020-06-09 17:40:36 +02:00
|
|
|
nodes[i] = std::move(nodes[j]);
|
|
|
|
};
|
|
|
|
auto validate = [&] {
|
2021-10-21 11:51:16 +02:00
|
|
|
std::multiset<td::uint64> in_list;
|
|
|
|
std::multiset<td::uint64> not_in_list;
|
2020-06-09 17:40:36 +02:00
|
|
|
for (auto &node : nodes) {
|
2020-06-10 02:01:23 +02:00
|
|
|
if (get_data(node).in_list.get()) {
|
|
|
|
in_list.insert(get_data(node).value.get());
|
2020-06-09 17:40:36 +02:00
|
|
|
} else {
|
2020-06-10 02:01:23 +02:00
|
|
|
not_in_list.insert(get_data(node).value.get());
|
2020-06-09 17:40:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auto guard = lock(root);
|
|
|
|
for (auto *begin = root.begin(), *end = root.end(); begin != end; begin = begin->get_next()) {
|
|
|
|
auto &data = get_data(*static_cast<NodeT *>(begin));
|
2020-06-10 02:01:23 +02:00
|
|
|
CHECK(data.in_list.get());
|
|
|
|
CHECK(data.value.get() != 0);
|
|
|
|
auto it = in_list.find(data.value.get());
|
2020-06-09 17:40:36 +02:00
|
|
|
if (it != in_list.end()) {
|
|
|
|
in_list.erase(it);
|
|
|
|
} else {
|
2020-06-10 02:01:23 +02:00
|
|
|
ASSERT_EQ(0u, not_in_list.count(data.value.get()));
|
2020-06-09 17:40:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_EQ(0u, in_list.size());
|
|
|
|
};
|
|
|
|
td::RandomSteps steps(
|
|
|
|
{{add_node, 3}, {pop_node, 1}, {unlink_node, 1}, {link_node, 3}, {swap_nodes, 1}, {set_node, 1}, {validate, 1}});
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
|
|
|
steps.step(rnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Misc, List) {
|
|
|
|
td::ListNode root;
|
|
|
|
std::atomic<td::uint64> id{0};
|
|
|
|
for (std::size_t i = 0; i < 4; i++) {
|
|
|
|
do_run_list_test<td::ListNode, td::ListNode, Node>(root, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Misc, TsList) {
|
2020-06-12 02:53:04 +02:00
|
|
|
td::TsList<ListData> root;
|
2020-06-09 17:40:36 +02:00
|
|
|
std::atomic<td::uint64> id{0};
|
|
|
|
for (std::size_t i = 0; i < 4; i++) {
|
2020-06-12 02:53:04 +02:00
|
|
|
do_run_list_test<td::TsListNode<ListData>, td::TsList<ListData>, td::TsListNode<ListData>>(root, id);
|
2020-06-09 17:40:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Misc, TsListConcurrent) {
|
2020-06-12 02:53:04 +02:00
|
|
|
td::TsList<ListData> root;
|
2020-06-09 17:40:36 +02:00
|
|
|
td::vector<td::thread> threads;
|
|
|
|
std::atomic<td::uint64> id{0};
|
|
|
|
for (std::size_t i = 0; i < 4; i++) {
|
|
|
|
threads.emplace_back(
|
2020-06-12 02:53:04 +02:00
|
|
|
[&] { do_run_list_test<td::TsListNode<ListData>, td::TsList<ListData>, td::TsListNode<ListData>>(root, id); });
|
2020-06-09 17:40:36 +02:00
|
|
|
}
|
|
|
|
}
|