-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueue_test.c
127 lines (94 loc) · 1.87 KB
/
queue_test.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "queue_test.h"
#include "test_util.h"
#include <assert.h>
Graph *graph;
Vertex *vertices;
Queue *q;
#define EQUAL(x, y, z, w) ((x) == (z) && (y) == (w)) || ((x) == (w) && (y) == (z))
Vertex *q_pop_min()
{
return Queue__pop_min(q, &the_graph);
}
int pop_min()
{
return q_pop_min() - &the_graph.vertices[0];
}
void insert(Vertex_Num v, Distance d)
{
Queue__insert(q, &vertices[v], d, graph);
}
void init()
{
Queue__init(q);
}
void assert_finished()
{
assert(q_pop_min() == NULL);
}
void test_insert_pop_min()
{
init();
insert(0, 0);
insert(1, 0);
int v1 = pop_min();
int v2 = pop_min();
assert(EQUAL(v1, v2, 0, 1));
assert_finished();
}
void test_inserting_two_vertices_with_zero_distance()
{
init();
insert(0, 0);
assert(pop_min() == 0);
assert_finished();
}
void test_inserting_vertex_with_3_dist_then_2_dist()
{
init();
insert(17, 3);
insert(20, 2);
assert(pop_min() == 20);
assert(pop_min() == 17);
assert_finished();
}
void test_insert_max_distance()
{
init();
// This is done for faster TESTING ONLY, obviously not part of the API;
// Do not use this technique yourself unless you know what you're doing
q->min_distance_candidate = DISTANCE__MAX - 10;
q->max_distance_ever_seen = DISTANCE__MAX - 20;
insert(34, DISTANCE__MAX);
assert(pop_min() == 34);
assert(graph->vertices[34].distance == DISTANCE__MAX);
assert_finished();
}
bool_t setup_done = FALSE;
void setup()
{
if (setup_done) {
return;
}
graph = &the_graph;
vertices = &graph->vertices[0];
q = the_queue;
load_graph("example.graph", &parse_simple_space_delimited_line, graph);
setup_done = TRUE;
}
void run_build_tests()
{
setup();
test_insert_pop_min();
test_insert_pop_min();
test_inserting_vertex_with_3_dist_then_2_dist();
}
void run_extended_tests()
{
setup();
test_insert_max_distance();
}
void run_all_tests()
{
run_build_tests();
run_extended_tests();
}