Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
pqmh_tests.h File Reference
#include <stdint.h>
#include "acutest.h"
#include "base.h"
#include "serene/rt/impl/pqmh.h"
Include dependency graph for pqmh_tests.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PQMH_TESTS(X)
#define PQ_INT(v)

Functions

static bool pqmh_int_less (srn_context_t *ctx, const void *a, const void *b)
static void test_pqmh_empty ()
static void test_pqmh_ordering ()
static void test_pqmh_peek ()
static void test_pqmh_grow ()

Variables

static const pqmh_control_t pqmh_int_ctl = {.less = pqmh_int_less}

Macro Definition Documentation

◆ PQ_INT

#define PQ_INT ( v)
Value:
((void *)(intptr_t)(v))

Definition at line 40 of file pqmh_tests.h.

◆ PQMH_TESTS

#define PQMH_TESTS ( X)
Value:
X("pqmh::empty", test_pqmh_empty), X("pqmh::ordering", test_pqmh_ordering), \
X("pqmh::peek", test_pqmh_peek), X("pqmh::grow", test_pqmh_grow)
static void test_pqmh_empty()
Definition pqmh_tests.h:43
static void test_pqmh_peek()
Definition pqmh_tests.h:86
static void test_pqmh_grow()
Definition pqmh_tests.h:110
static void test_pqmh_ordering()
Definition pqmh_tests.h:61

Definition at line 27 of file pqmh_tests.h.

27#define PQMH_TESTS(X) \
28 X("pqmh::empty", test_pqmh_empty), X("pqmh::ordering", test_pqmh_ordering), \
29 X("pqmh::peek", test_pqmh_peek), X("pqmh::grow", test_pqmh_grow)

Function Documentation

◆ pqmh_int_less()

bool pqmh_int_less ( srn_context_t * ctx,
const void * a,
const void * b )
static

Definition at line 33 of file pqmh_tests.h.

33 {
34 UNUSED(ctx);
35 return (intptr_t)a < (intptr_t)b;
36}
#define UNUSED(x)
Definition utils.h:45

◆ test_pqmh_empty()

void test_pqmh_empty ( )
static

Definition at line 43 of file pqmh_tests.h.

43 {
44 MAKE_ENGINE(mm, engine);
45 MAKE_CONTEXT(engine, ctx);
46
47 pqmh_t pq = pqmh_make(ctx, &pqmh_int_ctl, 0);
48 TEST_CHECK(pqmh_len(&pq) == 0);
49 TEST_CHECK(pqmh_peek(&pq) == nullptr);
50
51 void *out = PQ_INT(123);
52 TEST_CHECK(!pqmh_pop(ctx, &pq, &out));
53 TEST_CHECK(out == PQ_INT(123)); // untouched on empty
54
55 pqmh_free(ctx, &pq);
56 RELEASE_CONTEXT(ctx);
57 SHUTDOWN_ENGINE(mm, engine);
58}
#define TEST_CHECK(cond)
Definition acutest.h:95
#define RELEASE_CONTEXT(x)
Definition base.h:46
#define SHUTDOWN_ENGINE(mm, engine)
Definition base.h:38
#define MAKE_ENGINE(mm, engine)
Definition base.h:32
#define MAKE_CONTEXT(engine, x)
Definition base.h:42
void pqmh_free(srn_context_t *ctx, pqmh_t *pq)
Release the backing array and reset the queue to empty.
Definition pqmh.c:142
pqmh_t pqmh_make(srn_context_t *ctx, const pqmh_control_t *ctl, size_t initial_cap)
Create an empty queue ordered by ctl, with room for initial_cap elements (a small default is used whe...
Definition pqmh.c:81
void * pqmh_peek(const pqmh_t *pq)
The highest priority element without removing it, or NULL when the queue is empty.
Definition pqmh.c:132
bool pqmh_pop(srn_context_t *ctx, pqmh_t *pq, void **out)
Remove the highest priority element into *out and return true, or return false (leaving *out untouche...
Definition pqmh.c:111
size_t pqmh_len(const pqmh_t *pq)
The number of elements currently in the queue.
Definition pqmh.c:137
static const pqmh_control_t pqmh_int_ctl
Definition pqmh_tests.h:38
#define PQ_INT(v)
Definition pqmh_tests.h:40
A priority queue.
Definition pqmh.h:66
Here is the call graph for this function:

◆ test_pqmh_grow()

void test_pqmh_grow ( )
static

Definition at line 110 of file pqmh_tests.h.

110 {
111 MAKE_ENGINE(mm, engine);
112 MAKE_CONTEXT(engine, ctx);
113
114 pqmh_t pq = pqmh_make(ctx, &pqmh_int_ctl, 2); // forces several doublings
115 enum { N = 1000 };
116 for (int i = N - 1; i >= 0; i--) { // descending in, each push sifts to root
117 pqmh_push(ctx, &pq, PQ_INT(i));
118 }
119 TEST_CHECK(pqmh_len(&pq) == (size_t)N);
120
121 for (int i = 0; i < N; i++) {
122 void *out = nullptr;
123 TEST_CHECK(pqmh_pop(ctx, &pq, &out));
124 TEST_CHECK((intptr_t)out == i);
125 }
126 TEST_CHECK(pqmh_len(&pq) == 0);
127
128 pqmh_free(ctx, &pq);
129 RELEASE_CONTEXT(ctx);
130 SHUTDOWN_ENGINE(mm, engine);
131}
void pqmh_push(srn_context_t *ctx, pqmh_t *pq, void *elem)
Insert elem, growing the backing array (doubling) if it is full.
Definition pqmh.c:92
Here is the call graph for this function:

◆ test_pqmh_ordering()

void test_pqmh_ordering ( )
static

Definition at line 61 of file pqmh_tests.h.

61 {
62 MAKE_ENGINE(mm, engine);
63 MAKE_CONTEXT(engine, ctx);
64
65 pqmh_t pq = pqmh_make(ctx, &pqmh_int_ctl, 4);
66 int in[] = {5, 1, 4, 2, 8, 3, 7, 6, 0, 9};
67 enum { N = (int)(sizeof(in) / sizeof(in[0])) };
68 for (int i = 0; i < N; i++) {
69 pqmh_push(ctx, &pq, PQ_INT(in[i]));
70 }
71 TEST_CHECK(pqmh_len(&pq) == (size_t)N);
72
73 for (int i = 0; i < N; i++) {
74 void *out = nullptr;
75 TEST_CHECK(pqmh_pop(ctx, &pq, &out));
76 TEST_CHECK((intptr_t)out == i);
77 }
78 TEST_CHECK(pqmh_len(&pq) == 0);
79
80 pqmh_free(ctx, &pq);
81 RELEASE_CONTEXT(ctx);
82 SHUTDOWN_ENGINE(mm, engine);
83}
Here is the call graph for this function:

◆ test_pqmh_peek()

void test_pqmh_peek ( )
static

Definition at line 86 of file pqmh_tests.h.

86 {
87 MAKE_ENGINE(mm, engine);
88 MAKE_CONTEXT(engine, ctx);
89
90 pqmh_t pq = pqmh_make(ctx, &pqmh_int_ctl, 0);
91 pqmh_push(ctx, &pq, PQ_INT(42));
92 pqmh_push(ctx, &pq, PQ_INT(7));
93 pqmh_push(ctx, &pq, PQ_INT(99));
94
95 TEST_CHECK((intptr_t)pqmh_peek(&pq) == 7);
96 TEST_CHECK(pqmh_len(&pq) == 3); // peek does not remove
97
98 void *out = nullptr;
99 TEST_CHECK(pqmh_pop(ctx, &pq, &out));
100 TEST_CHECK((intptr_t)out == 7);
101 TEST_CHECK((intptr_t)pqmh_peek(&pq) == 42);
102
103 pqmh_free(ctx, &pq);
104 RELEASE_CONTEXT(ctx);
105 SHUTDOWN_ENGINE(mm, engine);
106}
Here is the call graph for this function:

Variable Documentation

◆ pqmh_int_ctl

const pqmh_control_t pqmh_int_ctl = {.less = pqmh_int_less}
static

Definition at line 38 of file pqmh_tests.h.

38{.less = pqmh_int_less};
static bool pqmh_int_less(srn_context_t *ctx, const void *a, const void *b)
Definition pqmh_tests.h:33