Simplify List test.
GitOrigin-RevId: 7306a169794ecb1e24e3b256bed600dd7d1a698c
This commit is contained in:
parent
c921bcc5ed
commit
bed0bb69f0
@ -18,8 +18,10 @@ class MovableValue {
|
|||||||
other.clear();
|
other.clear();
|
||||||
}
|
}
|
||||||
MovableValue &operator=(MovableValue &&other) {
|
MovableValue &operator=(MovableValue &&other) {
|
||||||
|
if (this != &other) {
|
||||||
val_ = other.val_;
|
val_ = other.val_;
|
||||||
other.clear();
|
other.clear();
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
MovableValue(const MovableValue &) = delete;
|
MovableValue(const MovableValue &) = delete;
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
//
|
//
|
||||||
#include "td/utils/common.h"
|
#include "td/utils/common.h"
|
||||||
#include "td/utils/List.h"
|
#include "td/utils/List.h"
|
||||||
|
#include "td/utils/MovableValue.h"
|
||||||
#include "td/utils/port/thread.h"
|
#include "td/utils/port/thread.h"
|
||||||
#include "td/utils/Random.h"
|
#include "td/utils/Random.h"
|
||||||
#include "td/utils/tests.h"
|
#include "td/utils/tests.h"
|
||||||
@ -16,28 +17,12 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
struct Data {
|
struct Data {
|
||||||
td::uint64 value{0};
|
td::MovableValue<td::uint64> value;
|
||||||
bool in_list{false};
|
td::MovableValue<bool> in_list;
|
||||||
|
|
||||||
Data() = default;
|
Data() = default;
|
||||||
Data(td::uint64 value, bool in_list) : value(value), in_list(in_list) {
|
Data(td::uint64 value, bool in_list) : value(value), in_list(in_list) {
|
||||||
}
|
}
|
||||||
Data(const Data &from) = delete;
|
|
||||||
Data &operator=(const Data &from) = delete;
|
|
||||||
Data(Data &&from) {
|
|
||||||
*this = std::move(from);
|
|
||||||
}
|
|
||||||
Data &operator=(Data &&from) {
|
|
||||||
if (this == &from) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
value = from.value;
|
|
||||||
in_list = from.in_list;
|
|
||||||
from.value = 0;
|
|
||||||
from.in_list = false;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
~Data() = default;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Node : public td::ListNode {
|
struct Node : public td::ListNode {
|
||||||
@ -48,24 +33,24 @@ struct Node : public td::ListNode {
|
|||||||
Data data;
|
Data data;
|
||||||
};
|
};
|
||||||
|
|
||||||
Data &get_data(Node &node) {
|
static Data &get_data(Node &node) {
|
||||||
return node.data;
|
return node.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
Data &get_data(td::TsListNode<Data> &node) {
|
static Data &get_data(td::TsListNode<Data> &node) {
|
||||||
return node.get_data_unsafe();
|
return node.get_data_unsafe();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(td::ListNode &node) {
|
static std::unique_lock<std::mutex> lock(td::ListNode &node) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(td::TsListNode<Data> &node) {
|
static std::unique_lock<std::mutex> lock(td::TsListNode<Data> &node) {
|
||||||
return node.lock();
|
return node.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ListNodeT, class ListRootT, class NodeT>
|
template <class ListNodeT, class ListRootT, class NodeT>
|
||||||
void do_run_list_test(ListRootT &root, std::atomic<td::uint64> &id) {
|
static void do_run_list_test(ListRootT &root, std::atomic<td::uint64> &id) {
|
||||||
td::vector<NodeT> nodes;
|
td::vector<NodeT> nodes;
|
||||||
|
|
||||||
td::Random::Xorshift128plus rnd(123);
|
td::Random::Xorshift128plus rnd(123);
|
||||||
@ -122,22 +107,22 @@ void do_run_list_test(ListRootT &root, std::atomic<td::uint64> &id) {
|
|||||||
auto validate = [&] {
|
auto validate = [&] {
|
||||||
std::multiset<td::uint64> in_list, not_in_list;
|
std::multiset<td::uint64> in_list, not_in_list;
|
||||||
for (auto &node : nodes) {
|
for (auto &node : nodes) {
|
||||||
if (get_data(node).in_list) {
|
if (get_data(node).in_list.get()) {
|
||||||
in_list.insert(get_data(node).value);
|
in_list.insert(get_data(node).value.get());
|
||||||
} else {
|
} else {
|
||||||
not_in_list.insert(get_data(node).value);
|
not_in_list.insert(get_data(node).value.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto guard = lock(root);
|
auto guard = lock(root);
|
||||||
for (auto *begin = root.begin(), *end = root.end(); begin != end; begin = begin->get_next()) {
|
for (auto *begin = root.begin(), *end = root.end(); begin != end; begin = begin->get_next()) {
|
||||||
auto &data = get_data(*static_cast<NodeT *>(begin));
|
auto &data = get_data(*static_cast<NodeT *>(begin));
|
||||||
CHECK(data.in_list);
|
CHECK(data.in_list.get());
|
||||||
CHECK(data.value != 0);
|
CHECK(data.value.get() != 0);
|
||||||
auto it = in_list.find(data.value);
|
auto it = in_list.find(data.value.get());
|
||||||
if (it != in_list.end()) {
|
if (it != in_list.end()) {
|
||||||
in_list.erase(it);
|
in_list.erase(it);
|
||||||
} else {
|
} else {
|
||||||
ASSERT_EQ(0u, not_in_list.count(data.value));
|
ASSERT_EQ(0u, not_in_list.count(data.value.get()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ASSERT_EQ(0u, in_list.size());
|
ASSERT_EQ(0u, in_list.size());
|
||||||
|
Loading…
Reference in New Issue
Block a user