forked from recolic/2017aut-quest2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_test.cc
103 lines (91 loc) · 3.11 KB
/
list_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* By recolic, Nov 10.
*/
#include <chrono>
#include <iostream>
#include <random>
#include <functional>
#include "test_utils.hpp"
using rlib::println;
std::default_random_engine rand_eng(810);
std::uniform_real_distribution<double> distribution(0, 100);
double m_rand() {return distribution(rand_eng);}
template <class operation_t, typename... args_t>
void timed_func(const std::string &info, std::function<operation_t> f, args_t... args)
{
println(info, "launched.");
auto begin = std::chrono::high_resolution_clock::now();
f(args ...);
auto end = std::chrono::high_resolution_clock::now();
println(info, "used", std::chrono::duration<double>(end - begin).count(), "s");
}
template <class operation_t, typename... args_t>
void repeat(size_t count, std::function<operation_t> f, args_t... args)
{
for(size_t cter = 0; cter < count; ++cter)
f(args ...);
}
int main()
{
using data_t = double;
Lab::list<data_t> lsa;
std::list<data_t> lsb;
using op_arg1_t = Lab::list<data_t> &;
using op_arg2_t = std::list<data_t> &;
#define op_args_t op_arg1_t, op_arg2_t
using operation_t = void(op_args_t);
auto co_push_back = [](auto &bufa, auto &bufb){
auto val = m_rand();
bufa.push_back(val);
bufb.push_back(val);
};
auto co_push_front = [](auto &bufa, auto &bufb){
auto val = m_rand();
bufa.push_front(val);
bufb.push_front(val);
};
auto co_pop_front = [](auto &bufa, auto &bufb){
bufa.pop_front();
bufb.pop_front();
};
auto co_pop_back = [](auto &bufa, auto &bufb){
bufa.pop_back();
bufb.pop_back();
};
auto co_erase = [](auto &bufa, auto &bufb){
bufa.erase(++bufa.begin());
bufb.erase(++bufb.begin());
};
auto co_clear = [](auto &bufa, auto &bufb){
bufa.clear();
bufb.clear();
};
using namespace std::placeholders;
#define TEST(count, operation, desc) LIST_ASSERT_EQUIVALENCE(lsa, lsb, std::function<operation_t>( \
std::bind(timed_func<operation_t, op_args_t>, desc, \
std::function<operation_t>(std::bind(repeat<operation_t, op_args_t>, count, operation, _1, _2)), \
_1, _2)))
TEST(1000, co_push_back, "push1");
TEST(1000000, co_push_back, "push2");
TEST(9999, co_pop_back, "pop1");
TEST(5432, co_push_back, "push3");
TEST(123, co_pop_back, "pop2");
TEST(1, co_erase, "erase1");
TEST(66, co_push_back, "push4");
TEST(543, co_erase, "erase2");
TEST(2, co_clear, "clear1");
TEST(3456, co_push_back, "push5");
println("s/back/front/g and retest...");
TEST(1000, co_push_front, "push1");
TEST(1000000, co_push_front, "push2");
TEST(9999, co_pop_front, "pop1");
TEST(5432, co_push_front, "push3");
TEST(123, co_pop_front, "pop2");
TEST(1, co_erase, "erase1");
TEST(66, co_push_front, "push4");
TEST(543, co_erase, "erase2");
TEST(2, co_clear, "clear1");
TEST(3456, co_push_front, "push5");
println("All tests done.");
return 0;
}