blob: 9457cd883bbf7be64680af796b623d4be19e2c40 (
plain)
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
|
#include <assert.h>
#include <iostream>
#include <stdlib.h>
#include "priority_queue.hpp"
using namespace std;
int main() {
// PQ is an open ended data structure -> alloc an arbitrary mem amount
PriorityQueue<unsigned int, unsigned int> *pq = new(malloc(1024 * 1024)) PriorityQueue<unsigned int, unsigned int>();
// macro helpers
#define I(v) { uint s=pq->length; pq->insert(v,10*v); if (s+1 != pq->length) return -1; }
#define T(v) { if (pq->minVal() != v || pq->minPayload() != 10*v) return -1; pq->removeMin(); }
#define DP { for (uint i=0; i<pq->length; i++) { cout << pq->heap[i].val << ", "; } cout << "-" << endl; }
// add and remove some values
I(3);
I(2);
I(1);
T(1);
T(2);
T(3);
// add many random values
srand(42);
for (int i=0; i<100000; i++) {
// cout << ".";
pq->insert(rand(), 0);
}
unsigned int prev = 0;
for (int i=0; i<100000; i++) {
if (pq->minVal() < prev) return -1;
prev = pq->minVal();
pq->removeMin();
}
// got until here -> success
return 0;
}
|