Files
DataStruct/main.cpp

260 lines
7.7 KiB
C++

#include <iostream>
#include <RedBlackTree.h>
#include <HeapSort.h>
#include <random>
#include <chrono>
#include <iomanip>
#include <gtest/gtest.h>
#include <mutex/shared_mutex.h>
class RedBlackTree : public ling::RedBlackTree<unsigned long long> {
protected:
[[nodiscard]] ling::Relation equal(const unsigned long long &val1, const unsigned long long &val2) const override {
if (val1 == val2)
return ling::Relation::EQUAL;
if (val1 < val2)
return ling::Relation::SMALL;
return ling::Relation::BIG;
}
};
#define DATA_SIZE 100000
//开始一个统计点
#define StartStating(name) auto name = std::chrono::high_resolution_clock::now()
//结束统计
#define EndStating(name, text) do {auto __ling_end_time = std::chrono::high_resolution_clock::now(); \
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(__ling_end_time - name); \
std::cout << text << ": " << std::fixed << std::setprecision(6) <<((double)duration.count() / 1000000) << " 秒(" << duration.count() <<" 微秒)" << std::endl;\
}while(false)
class mutex_test {
public:
int test() {
return 1;
}
int test_const() const {
return 2;
}
};
TEST(, 1) {
auto mutex = ling::shared_mutex<mutex_test>(mutex_test());
auto ptr = std::make_shared<mutex_test>();
{
auto lock = mutex.lock();
lock->test();
lock->test_const();
}
{
auto shared = mutex.lock_shared();
auto shared2 = mutex.lock_shared();
shared->test_const();
shared2->test_const();
}
{
auto lock = mutex.try_lock();
ASSERT_TRUE(lock);
auto lock_2 = mutex.try_lock();
ASSERT_FALSE(lock_2);
auto lock_3 = mutex.try_lock_shared();
ASSERT_FALSE(lock_3);
}
{
auto lock = mutex.try_lock_shared();
ASSERT_TRUE(lock);
auto lock_2 = mutex.try_lock();
ASSERT_FALSE(lock_2);
auto lock_3 = mutex.try_lock_shared();
ASSERT_TRUE(lock_3);
}
}
TEST(, ) {
std::random_device rd;
std::mt19937 gen(rd());
std::vector<int> list;
list.reserve(DATA_SIZE);
std::uniform_int_distribution<> dis(1, DATA_SIZE * 10);
for (int i = 0; i < DATA_SIZE; i++) {
list.push_back(dis(gen));
}
StartStating(start);
ling::HeapSortDefault sort(list);
sort.init();
sort.sort();
EndStating(start, "排序耗时");
for (int i = 1; i < list.size(); i++) {
ASSERT_TRUE(list[i] >= list[i - 1]);
}
}
TEST(, 2) {
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<unsigned long long> dist(0x70000000, 0x7ffffffffff);
RedBlackTree tree;
for (int i = 0; i < 3000000; i++) {
tree.insert(dist(gen));
}
ASSERT_TRUE(tree.getSize() == 3000000);
}
TEST(, ) {
RedBlackTree tree;
std::vector<unsigned long long> vec;
/*for (int i = 0; i < 3000; i++) {
vec.push_back(dist(gen));
}*/
vec.push_back(0x2e27b3d26bc); //1 317
vec.push_back(0x6ff71223ecc); //2 769
vec.push_back(0x2d69a2c3138); //3 312
vec.push_back(0x2de3d825c0); //4 197
vec.push_back(0x1ecd65c2c5); //5 132
vec.push_back(0x77561e1ec5e); //6 820
vec.push_back(0xeb31b2ddab); //7 101
vec.push_back(0x98b4038275); //8 655
vec.push_back(0x16243d02489); //9 152
for (unsigned long i: vec) {
tree.insert(i);
}
ASSERT_TRUE(tree.getSize() == vec.size());
}
TEST(, ) {
RedBlackTree tree;
tree.insert(5);
tree.insert(10);
tree.insert(15);
tree.insert(20);
tree.insert(25);
tree.insert(30);
tree.insert(33);
tree.insert(35);
tree.insert(40);
tree.insert(45);
tree.insert(50);
auto node = tree.iterativeSearch(1);
ASSERT_TRUE(node == nullptr);
node = tree.iterativeSearch(4);
ASSERT_TRUE(node == nullptr);
node = tree.iterativeSearch(5);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 5);
node = tree.iterativeSearch(6);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 5);
node = tree.iterativeSearch(7);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 5);
node = tree.iterativeSearch(8);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 5);
node = tree.iterativeSearch(9);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 5);
node = tree.iterativeSearch(10);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 10);
node = tree.iterativeSearch(11);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 10);
node = tree.iterativeSearch(12);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 10);
node = tree.iterativeSearch(13);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 10);
node = tree.iterativeSearch(14);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 10);
node = tree.iterativeSearch(15);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 15);
node = tree.iterativeSearch(16);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 15);
node = tree.iterativeSearch(17);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 15);
node = tree.iterativeSearch(18);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 15);
node = tree.iterativeSearch(19);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 15);
node = tree.iterativeSearch(20);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 20);
node = tree.iterativeSearch(21);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 20);
node = tree.iterativeSearch(22);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 20);
node = tree.iterativeSearch(23);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 20);
node = tree.iterativeSearch(24);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 20);
node = tree.iterativeSearch(25);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 25);
node = tree.iterativeSearch(26);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 25);
node = tree.iterativeSearch(27);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 25);
node = tree.iterativeSearch(28);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 25);
node = tree.iterativeSearch(29);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 25);
node = tree.iterativeSearch(30);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 30);
node = tree.iterativeSearch(31);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 30);
node = tree.iterativeSearch(32);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 30);
node = tree.iterativeSearch(33);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 33);
node = tree.iterativeSearch(34);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 33);
node = tree.iterativeSearch(35);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 35);
node = tree.iterativeSearch(44);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 40);
node = tree.iterativeSearch(45);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 45);
node = tree.iterativeSearch(46);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 45);
node = tree.iterativeSearch(49);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 45);
node = tree.iterativeSearch(50);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 50);
node = tree.iterativeSearch(1000);
ASSERT_FALSE(node == nullptr);
ASSERT_EQ(node->value, 50);
}