Order resource data from instructions by resource class. This simplifies
[jitcs.git] / tests / test_adt_heap.cpp
blobcd3f4241ca853a069777e94f9ee2a5285d755453
1 #include "jitcs_int_adt_heap.h"
2 #include "unittest.h"
4 #include <stdio.h>
6 using namespace jitcs;
8 struct HeapTest {
9 HeapTest() = default;
10 HeapTest(const HeapTest&) = default;
11 HeapTest(size_t id_, size_t v) : id(id_), value(v), heappos(0) {};
13 size_t id;
14 size_t heappos;
15 size_t value;
17 void dump(TopHeap<HeapTest>& th) {
18 printf("TopHeap:");
19 for (size_t i = 0; i < th.data.size(); ++i) {
20 printf(" %d:%d(%d,%d)",
21 i, th.data[i].id, th.data[i].value, th.data[i].heappos);
23 printf("\n");
26 bool operator ==(const HeapTest& h1, const HeapTest& h2) {
27 return h1.id == h2.id;
29 bool operator !=(const HeapTest& h1, const HeapTest& h2) {
30 return h1.id != h2.id;
33 static void test(UnitTest& t) {
34 TopHeap<HeapTest> h1;
35 t.check("Heap/empty", h1.isEmpty());
36 for (size_t i = 0; i < 10; ++i) {
37 h1.push(HeapTest(i * 2 + 0, i * 2 + 0));
38 t.check("Heap/push", h1.check());
40 for (size_t i = 0; i < 10; ++i) {
41 h1.push(HeapTest(i * 2 + 1, i * 2 + 1));
42 t.check("Heap/push", h1.check());
44 t.check("Heap/top", h1.peekTopValue() == 19 && h1.peekTop().id == 19);
45 h1.drop(h1.data[3]);
46 t.check("Heap/drop", h1.check()
47 && h1.peekTopValue() == 19 && h1.peekTop().id == 19);
48 h1.dropTop();
49 t.check("Heap/dropTop", h1.check()
50 && h1.peekTopValue() == 18 && h1.peekTop().id == 18);
51 while (!h1.isEmpty()) {
52 h1.dropTop();
53 t.check("Heap/drop", h1.check());
57 static UnitTestRun _1("ADT/Heap", test);