File: test_PriorityQueue.cc

package info (click to toggle)
trafficserver 6.2.0-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 45,456 kB
  • sloc: cpp: 271,894; ansic: 80,740; sh: 6,032; makefile: 3,364; python: 2,135; perl: 2,040; java: 277; lex: 128; sql: 94; yacc: 68; sed: 8
file content (288 lines) | stat: -rw-r--r-- 7,304 bytes parent folder | download
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/** @file

    Unit tests for PriorityQueue

    @section license License

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include <iostream>
#include <string.h>

#include <ts/TestBox.h>

#include "PriorityQueue.h"

using namespace std;

class N
{
public:
  N(uint32_t w, string c) : weight(w), content(c) {}
  bool
  operator<(const N &n) const
  {
    return weight < n.weight;
  }

  uint32_t weight;
  string content;
};

typedef PriorityQueueEntry<N *> Entry;
typedef PriorityQueue<N *> PQ;

// For debug
void
dump(PQ *pq)
{
  Vec<Entry *> v = pq->dump();

  for (uint32_t i = 0; i < v.length(); i++) {
    cout << v[i]->index << "," << v[i]->node->weight << "," << v[i]->node->content << endl;
  }
  cout << "--------" << endl;
}

// Push, top, and pop a entry
REGRESSION_TEST(PriorityQueue_1)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus)
{
  TestBox box(t, pstatus);
  box = REGRESSION_TEST_PASSED;

  PQ *pq         = new PQ();
  N *a           = new N(6, "A");
  Entry *entry_a = new Entry(a);

  pq->push(entry_a);
  box.check(pq->top() == entry_a, "top should be entry_a");

  pq->pop();
  box.check(pq->top() == NULL, "top should be NULL");
}

// Increase weight
REGRESSION_TEST(PriorityQueue_2)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus)
{
  TestBox box(t, pstatus);
  box = REGRESSION_TEST_PASSED;

  PQ *pq = new PQ();

  N *a = new N(10, "A");
  N *b = new N(20, "B");
  N *c = new N(30, "C");

  Entry *entry_a = new Entry(a);
  Entry *entry_b = new Entry(b);
  Entry *entry_c = new Entry(c);

  pq->push(entry_a);
  pq->push(entry_b);
  pq->push(entry_c);

  box.check(pq->top() == entry_a, "top should be entry_a");

  a->weight = 40;
  pq->update(entry_a);

  box.check(pq->top() == entry_b, "top should be entry_b");

  b->weight = 50;
  pq->update(entry_b, true);

  box.check(pq->top() == entry_c, "top should be entry_c");
}

// Decrease weight
REGRESSION_TEST(PriorityQueue_3)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus)
{
  TestBox box(t, pstatus);
  box = REGRESSION_TEST_PASSED;

  PQ *pq = new PQ();

  N *a = new N(10, "A");
  N *b = new N(20, "B");
  N *c = new N(30, "C");

  Entry *entry_a = new Entry(a);
  Entry *entry_b = new Entry(b);
  Entry *entry_c = new Entry(c);

  pq->push(entry_a);
  pq->push(entry_b);
  pq->push(entry_c);

  box.check(pq->top() == entry_a, "top should be entry_a");

  b->weight = 5;
  pq->update(entry_b);

  box.check(pq->top() == entry_b, "top should be entry_b");

  c->weight = 3;
  pq->update(entry_c, false);

  box.check(pq->top() == entry_c, "top should be entry_c");
}

// Push, top, and pop 9 entries
REGRESSION_TEST(PriorityQueue_4)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus)
{
  TestBox box(t, pstatus);
  box = REGRESSION_TEST_PASSED;

  PQ *pq = new PQ();

  N *a = new N(6, "A");
  N *b = new N(1, "B");
  N *c = new N(9, "C");
  N *d = new N(8, "D");
  N *e = new N(4, "E");
  N *f = new N(3, "F");
  N *g = new N(2, "G");
  N *h = new N(7, "H");
  N *i = new N(5, "I");

  Entry *entry_a = new Entry(a);
  Entry *entry_b = new Entry(b);
  Entry *entry_c = new Entry(c);
  Entry *entry_d = new Entry(d);
  Entry *entry_e = new Entry(e);
  Entry *entry_f = new Entry(f);
  Entry *entry_g = new Entry(g);
  Entry *entry_h = new Entry(h);
  Entry *entry_i = new Entry(i);

  pq->push(entry_a);
  pq->push(entry_b);
  pq->push(entry_c);
  pq->push(entry_d);
  pq->push(entry_e);
  pq->push(entry_f);
  pq->push(entry_g);
  pq->push(entry_h);
  pq->push(entry_i);

  box.check(pq->top() == entry_b, "top should be entry_b"); // 1
  pq->pop();
  box.check(pq->top() == entry_g, "top should be entry_g"); // 2
  pq->pop();
  box.check(pq->top() == entry_f, "top should be entry_f"); // 3
  pq->pop();
  box.check(pq->top() == entry_e, "top should be entry_e"); // 4
  pq->pop();
  box.check(pq->top() == entry_i, "top should be entry_i"); // 5
  pq->pop();
  box.check(pq->top() == entry_a, "top should be entry_a"); // 6
  pq->pop();
  box.check(pq->top() == entry_h, "top should be entry_h"); // 7
  pq->pop();
  box.check(pq->top() == entry_d, "top should be entry_d"); // 8
  pq->pop();
  box.check(pq->top() == entry_c, "top should be entry_c"); // 9
  pq->pop();

  box.check(pq->top() == NULL, "top should be NULL");
}

// // Push, top, pop, and update 9 entries
REGRESSION_TEST(PriorityQueue_5)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus)
{
  TestBox box(t, pstatus);
  box = REGRESSION_TEST_PASSED;

  PQ *pq = new PQ();

  N *a = new N(6, "A");
  N *b = new N(1, "B");
  N *c = new N(9, "C");
  N *d = new N(8, "D");
  N *e = new N(4, "E");
  N *f = new N(3, "F");
  N *g = new N(2, "G");
  N *h = new N(7, "H");
  N *i = new N(5, "I");

  Entry *entry_a = new Entry(a);
  Entry *entry_b = new Entry(b);
  Entry *entry_c = new Entry(c);
  Entry *entry_d = new Entry(d);
  Entry *entry_e = new Entry(e);
  Entry *entry_f = new Entry(f);
  Entry *entry_g = new Entry(g);
  Entry *entry_h = new Entry(h);
  Entry *entry_i = new Entry(i);

  pq->push(entry_a);
  pq->push(entry_b);
  pq->push(entry_c);
  pq->push(entry_d);
  pq->push(entry_e);
  pq->push(entry_f);
  pq->push(entry_g);
  pq->push(entry_h);
  pq->push(entry_i);

  // Pop head and push it back again
  box.check(pq->top() == entry_b, "top should be entry_b"); // 1
  pq->pop();
  b->weight += 100;
  pq->push(entry_b);
  // Update weight
  a->weight += 100;
  pq->update(entry_a);
  c->weight += 100;
  pq->update(entry_d);
  e->weight += 100;
  pq->update(entry_e);
  g->weight += 100;
  pq->update(entry_g);

  // Check
  box.check(pq->top() == entry_f, "top should be entry_f"); // 3
  pq->pop();
  box.check(pq->top() == entry_i, "top should be entry_i"); // 5
  pq->pop();
  box.check(pq->top() == entry_h, "top should be entry_h"); // 7
  pq->pop();
  box.check(pq->top() == entry_d, "top should be entry_d"); // 8
  pq->pop();
  box.check(pq->top() == entry_b, "top should be entry_b"); // 101
  pq->pop();
  box.check(pq->top() == entry_g, "top should be entry_g"); // 102
  pq->pop();
  box.check(pq->top() == entry_e, "top should be entry_e"); // 104
  pq->pop();
  box.check(pq->top() == entry_a, "top should be entry_a"); // 106
  pq->pop();
  box.check(pq->top() == entry_c, "top should be entry_c"); // 109
  pq->pop();

  box.check(pq->top() == NULL, "top should be NULL");
}

int
main(int /* argc ATS_UNUSED */, const char ** /* argv ATS_UNUSED */)
{
  const char *name = "PriorityQueue";
  RegressionTest::run(name);

  return RegressionTest::final_status == REGRESSION_TEST_PASSED ? 0 : 1;
}