blob: 1a6f884cffb03c273ed8933e074a4e2e9201c657 (
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
41
42
43
44
45
46
47
48
49
50
51
|
(;module:
lux
(lux [io]
(control [monad #+ do Monad])
(data (coll ["&" queue])
[number])
["r" math/random])
lux/test)
(context: "Queues"
(<| (times +100)
(do @
[size (:: @ map (n/% +100) r;nat)
sample (r;queue size r;nat)
non-member (|> r;nat
(r;filter (. not (&;member? number;Eq<Nat> sample))))]
($_ seq
(test "I can query the size of a queue (and empty queues have size 0)."
(if (n/= +0 size)
(&;empty? sample)
(n/= size (&;size sample))))
(test "Enqueueing and dequeing affects the size of queues."
(and (n/= (n/inc size) (&;size (&;push non-member sample)))
(or (&;empty? sample)
(n/= (n/dec size) (&;size (&;pop sample))))
(n/= size (&;size (&;pop (&;push non-member sample))))))
(test "Transforming to/from list can't change the queue."
(let [(^open "&/") (&;Eq<Queue> number;Eq<Nat>)]
(|> sample
&;to-list &;from-list
(&/= sample))))
(test "I can always peek at a non-empty queue."
(case (&;peek sample)
#;None (&;empty? sample)
(#;Some _) true))
(test "I can query whether an element belongs to a queue."
(and (not (&;member? number;Eq<Nat> sample non-member))
(&;member? number;Eq<Nat> (&;push non-member sample)
non-member)
(case (&;peek sample)
#;None
(&;empty? sample)
(#;Some first)
(and (&;member? number;Eq<Nat> sample first)
(not (&;member? number;Eq<Nat> (&;pop sample) first))))))
))))
|