-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_queue.cc
90 lines (69 loc) · 2.01 KB
/
test_queue.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
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <unistd.h>
#include <lace/singleton.h>
#include <lace/random.h>
#include "queue.h"
struct node {
int value;
node(int v) : value(v) { }
lite::queue_link<node> random_link, forward_link, reverse_link;
bool
bound() const {
return false
|| forward_link.bound()
|| reverse_link.bound()
;;
}
void kill() { if (!bound()) delete this; }
typedef lite::queue<node, &node::random_link> random_queue_t;
typedef lite::queue<node, &node::forward_link> forward_queue_t;
typedef lite::queue<node, &node::reverse_link> reverse_queue_t;
typedef forward_queue_t::sorter<typeof(value), &node::value> forward_sorter_t;
typedef reverse_queue_t::reverse_sorter<typeof(value), &node::value> reverse_sorter_t;
};
int
main(int, char*[]) {
lace::random & rng = lace::singleton<lace::random>::instance();
node::random_queue_t random;
node::forward_queue_t forward;
node::reverse_queue_t reverse;
static const unsigned n = 16;
for (unsigned i = 0 ; i < n ; ++i) {
node* x = new node(rng.l() % n);
random.enqueue(x);
forward.enqueue(x);
}
node::forward_sorter_t::sort(forward);
for (node * n = forward.iterator() ; n ; n = forward.next(n))
reverse.enqueue(n);
reverse.reverse();
assert(node::reverse_sorter_t::sorted(reverse));
assert(forward.peek()->value == reverse.last()->value);
assert(forward.last()->value == reverse.peek()->value);
std::cout << "random" << '\t';
while (!random.empty()) {
node* x = random.dequeue();
std::cout << ' ' << x->value;
x->kill();
}
std::cout << std::endl;
std::cout << "forward" << '\t';
while (!forward.empty()) {
node* x = forward.dequeue();
std::cout << ' ' << x->value;
x->kill();
}
std::cout << std::endl;
std::cout << "reverse" << '\t';
while (!reverse.empty()) {
node* x = reverse.dequeue();
std::cout << ' ' << x->value;
x->kill();
}
std::cout << std::endl;
reverse.reverse();
return EXIT_SUCCESS;
}
//