Files
c3c/test/unit/stdlib/collections/priorityqueue.c3
Pierre Curto 35bffdadc2 improve the sort and collections libs (#853)
* lib/std/sort: unify binarysearch and binarysearch_with; add comments to quicksort

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/collections: mark List.{len, is_empty, get} with @inline

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/collections: add PriorityQueueMax; add tests for PriorityQueue and PriorityQueueMax

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

---------

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2023-07-15 19:08:54 +02:00

59 lines
1.2 KiB
C

module priorityqueue_test @test;
import std::collections;
import std::collections::priorityqueue;
def Queue = PriorityQueue(<int>);
fn void! priorityqueue()
{
Queue q;
assert(q.is_empty());
q.push(1);
q.push(2);
assert(q.len() == 2);
int x;
x = q.pop()!;
assert(x == 1, "got %d; want %d", x, 1);
x = q.pop()!;
assert(x == 2, "got %d; want %d", x, 2);
q.push(3);
q.push(2);
q.push(1);
x = q.pop()!;
assert(x == 1, "got %d; want %d", x, 1);
x = q.pop()!;
assert(x == 2, "got %d; want %d", x, 2);
x = q.pop()!;
assert(x == 3, "got %d; want %d", x, 3);
}
def QueueMax = PriorityQueueMax(<int>);
fn void! priorityqueue_max()
{
QueueMax q;
assert(q.is_empty());
q.push(1);
q.push(2);
assert(q.len() == 2);
int x;
x = q.pop()!;
assert(x == 2, "got %d; want %d", x, 2);
x = q.pop()!;
assert(x == 1, "got %d; want %d", x, 1);
q.push(3);
q.push(2);
q.push(1);
x = q.pop()!;
assert(x == 3, "got %d; want %d", x, 3);
x = q.pop()!;
assert(x == 2, "got %d; want %d", x, 2);
x = q.pop()!;
assert(x == 1, "got %d; want %d", x, 1);
}