local t = require("Modul:UnitTests")
local mut = require("Modul:queue")
function t:test01_enqueue()
local q = mut()
self:equals('mut.enqueue(q)', mut.enqueue(q, "one"), nil)
end
function t:test02_empty_queue()
-- {}
local q = mut()
self:equals('mut.isEmpty(q)', mut.isEmpty(q), true)
self:equals('mut.length(q)', mut.length(q), 0)
self:equals('mut.peekFirst(q)', mut.peekFirst(q), nil)
self:equals('mut.peekSecond(q)', mut.peekSecond(q), nil)
self:equals('mut.peekLast(q)', mut.peekLast(q), nil)
self:equals('mut.peek(q, 1)', mut.peek(q, 1), nil)
self:equals('mut.peek(q, 0)', mut.peek(q, 0), nil)
self:equals('mut.peek(q, -1)', mut.peek(q, -1), nil)
self:equals('mut.dequeue(q)', mut.dequeue(q), nil)
end
function t:test03_one_element_added()
-- {"one"}
local q = mut()
mut.enqueue(q, "one")
self:equals('mut.isEmpty(q)', mut.isEmpty(q), false)
self:equals('mut.length(q)', mut.length(q), 1)
self:equals('mut.peekFirst(q)', mut.peekFirst(q), "one")
self:equals('mut.peekSecond(q)', mut.peekSecond(q), nil)
self:equals('mut.peekLast(q)', mut.peekLast(q), "one")
self:equals('mut.peek(q, 1)', mut.peek(q, 1), "one")
self:equals('mut.peek(q, -1)', mut.peek(q, -1), "one")
self:equals('mut.dequeue(q)', mut.dequeue(q), "one")
end
function t:test04_two_elements_added()
-- {"two", "one"}
local q = mut()
mut.enqueue(q, "one")
mut.enqueue(q, "two")
self:equals('mut.isEmpty(q)', mut.isEmpty(q), false)
self:equals('mut.length(q)', mut.length(q), 2)
self:equals('mut.peekFirst(q)', mut.peekFirst(q), "one")
self:equals('mut.peekSecond(q)', mut.peekSecond(q), "two")
self:equals('mut.peekLast(q)', mut.peekLast(q), "two")
self:equals('mut.peek(q, 1)', mut.peek(q, 1), "one")
self:equals('mut.peek(q, 2)', mut.peek(q, 2), "two")
self:equals('mut.peek(q, -1)', mut.peek(q, -1), "two")
self:equals('mut.peek(q, -2)', mut.peek(q, -2), "one")
end
function t:test05_four_elements_added()
-- {"four", "three", "two", "one"}
local q = mut()
mut.enqueue(q, "one")
mut.enqueue(q, "two")
mut.enqueue(q, "three")
mut.enqueue(q, "four")
self:equals('mut.length(q)', mut.isEmpty(q), false)
self:equals('mut.length(q)', mut.length(q), 4)
self:equals('mut.peekFirst(q)', mut.peekFirst(q), "one")
self:equals('mut.peekSecond(q)', mut.peekSecond(q), "two")
self:equals('mut.peekLast(q)', mut.peekLast(q), "four")
self:equals('mut.peek(q, 1)', mut.peek(q, 1), "one")
self:equals('mut.peek(q, 4)', mut.peek(q, 4), "four")
self:equals('mut.peek(q, 5)', mut.peek(q, 5), nil)
self:equals('mut.peek(q, 0)', mut.peek(q, 0), nil)
self:equals('mut.peek(q, -1)', mut.peek(q, -1), "four")
self:equals('mut.peek(q, -4)', mut.peek(q, -4), "one")
self:equals('mut.peek(q, -5)', mut.peek(q, -5), nil)
end
function t:test06_multiple()
local q = mut()
-- {}
self:equals('mut.dequeue(q)', mut.dequeue(q), nil)
self:equals('mut.length(q)', mut.isEmpty(q), true)
self:equals('mut.length(q)', mut.length(q), 0)
-- {}
self:equals('mut.enqueue(q, "one")', mut.enqueue(q, "one"), nil)
self:equals('mut.enqueue(q, "two")', mut.enqueue(q, "two"), nil)
-- {"two", "one"}
self:equals('mut.length(q)', mut.length(q), 2)
self:equals('mut.dequeue(q)', mut.dequeue(q), "one")
-- {"two"}
self:equals('mut.length(q)', mut.length(q), 1)
self:equals('mut.enqueue(q, "three")', mut.enqueue(q, "three"), nil)
self:equals('mut.enqueue(q, "four")', mut.enqueue(q, "four"), nil)
-- {"four", "three", "two"}
self:equals('mut.length(q)', mut.length(q), 3)
self:equals('mut.dequeue(q)', mut.dequeue(q), "two")
-- {"four", "three"}
self:equals('mut.peekFirst(q)', mut.peekFirst(q), "three")
self:equals('mut.peekSecond(q)', mut.peekSecond(q), "four")
self:equals('mut.peekLast(q)', mut.peekLast(q), "four")
self:equals('mut.length(q)', mut.length(q), 2)
self:equals('mut.dequeue(q)', mut.dequeue(q), "three")
self:equals('mut.dequeue(q)', mut.dequeue(q), "four")
-- {}
self:equals('mut.length(q)', mut.isEmpty(q), true)
self:equals('mut.dequeue(q)', mut.dequeue(q), nil)
-- {}
self:equals('mut.length(q)', mut.length(q), 0)
end
return t