ludic/packages/ludic.base/tests/queue_test.ludic

165 lines
4.7 KiB
Text

# queue_test.ludic - Queue<T>: first in first out, drain empties, undrained queues are named.
# A generic call is not resolved inside a `test` body yet, so each case is a function.
import "ludic.base"
program QueueTest {
numbers float
property Caught { species: int = 0, weight: float = 0.0 }
function caught(sp: int, w: float) -> Caught {
let c = new Caught
c.species = sp
c.weight = w
return c
}
function fifo_case() -> void {
let q: Queue<int> = queue_new("ints")
q_push(q, 3)
q_push(q, 1)
q_push(q, 2)
expect_eq(q_len(q), 3)
let xs = q_drain(q)
expect_eq(len(xs), 3)
expect_eq(xs[0], 3)
expect_eq(xs[1], 1)
expect_eq(xs[2], 2)
expect_eq(q_len(q), 0)
expect_eq(len(q_drain(q)), 0)
}
function record_case() -> void {
let q: Queue<Caught> = queue_new("caught")
q_push(q, caught(2, 1.5))
q_push(q, caught(4, 0.25))
let cs = q_drain(q)
expect_eq(cs[1].species, 4)
expect(cs[0].weight == 1.5)
}
# waiting and last handed out are held; what an earlier drain handed out is free again
function unheld_case() -> void {
let q: Queue<Caught> = queue_new("caught")
let a = caught(1, 1.0)
let b = caught(2, 2.0)
let c = caught(3, 3.0)
let pool = new []Caught
push(pool, a)
push(pool, b)
push(pool, c)
let free = new []Caught
q_push(q, a)
expect_eq(q_unheld(q, pool, free), 2)
q_drain(q)
q_push(q, b)
expect_eq(q_unheld(q, pool, free), 1)
expect(free[0] == c)
q_drain(q)
expect_eq(q_unheld(q, pool, free), 2)
q_drain(q)
expect_eq(q_unheld(q, pool, free), 2) # an empty drain swaps nothing: b stays held, the safe side
q_push(q, a)
q_drain(q)
expect_eq(q_unheld(q, pool, free), 2) # b is free at last, a is handed out
}
function clear_case() -> void {
let q: Queue<string> = queue_new("words")
q_push(q, "a")
q_clear(q)
expect_eq(q_len(q), 0)
}
# nothing but the queue says what T is: a drain and a clear still know it
function infer_case() -> void {
let q: Queue<Caught> = queue_new("told")
q_push(q, caught(1, 2.0))
expect_eq(q_drain(q)[0].species, 1)
q_push(q, caught(3, 1.0))
q_clear(q)
expect_eq(q_len(q), 0)
}
function undrained_case() -> void {
let a: Queue<int> = queue_new("left_behind")
let b: Queue<int> = queue_new("read")
q_push(a, 1)
q_push(b, 2)
q_drain(b)
let tags = [q_tag(a), q_tag(b)]
let names = new []string
expect_eq(core_undrained(tags, names), 1)
expect(names[0] == "left_behind")
q_drain(a)
expect_eq(core_undrained(tags, names), 0)
}
# an empty drain allocates nothing and hands back the same empty list; a drained list is the
# caller's, untouched by what is pushed or cleared after it
function empty_case() -> void {
let q: Queue<int> = queue_new("quiet")
let a = q_drain(q)
let b = q_drain(q)
expect_eq(len(a), 0)
expect(a == b)
q_push(q, 5)
expect_eq(len(a), 0)
let got = q_drain(q)
q_push(q, 6)
q_clear(q)
expect_eq(len(got), 1)
expect_eq(got[0], 5)
expect_eq(q_len(q), 0)
expect_eq(len(q_drain(q)), 0)
}
test "a queue drains in the order it was pushed" () { fifo_case() }
# two lists, reused: a drained list is the caller's until the next drain of that queue
function reuse_case() -> void {
let q: Queue<int> = queue_new("reused")
q_push(q, 1)
let a = q_drain(q)
q_push(q, 2)
expect_eq(a[0], 1)
let b = q_drain(q)
expect_eq(b[0], 2)
q_push(q, 3)
let c = q_drain(q)
expect(c == a)
expect_eq(len(c), 1)
expect_eq(c[0], 3)
}
test "a drain reuses the queue's two lists" () { reuse_case() }
test "an empty drain allocates nothing, and a drained list is the caller's" () { empty_case() }
test "a queue holds records" () { record_case() }
test "clear drops what is waiting" () { clear_case() }
test "a pool's records are free once no drain holds them" () { unheld_case() }
function rec_of(q: Queue<Caught>) -> Caught {
var c = q_rec(q)
if c == null {
c = new Caught
q_ring_add(q, c)
}
return c
}
function ring_case() -> void {
let q: Queue<Caught> = queue_new("caught")
let a = rec_of(q)
q_push(q, a)
let b = rec_of(q)
expect(a != b)
q_push(q, b)
q_drain(q)
expect_eq(len(q.ring), 2)
let c = rec_of(q)
expect(c != a and c != b)
q_push(q, c)
q_drain(q)
let d = rec_of(q)
expect(d == a or d == b)
expect_eq(len(q.ring), 3)
}
test "a fact's record comes round again once no list holds it" () { ring_case() }
test "a queue's element type is told by the queue alone" () { infer_case() }
test "the undrained queues are named, and a drain clears the report" () { undrained_case() }
}