Modul:Set/testiranje
Dokumentaciju za ovaj modul možete napraviti na stranici Modul:Set/testiranje/dok
-- Unit tests for [[Module:Set]]. Click talk page to run tests.
local set = require('Module:Set') -- the module to be tested
local tt = require('Module:TableTools') -- contains some necessary functions
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()
------------------------------------------------------------------------
-- Helper functions
------------------------------------------------------------------------
suite.isNan = tt.isNan
function suite.arraysHaveSameValues(t1, t2)
-- Checks whether two arrays contain the same values, not necessarily in the same order.
-- Returns true if so, and false if not. Values that occur more than once will cause
-- unexpected behaviour.
if #t1 ~= #t2 then
return false
end
for i1, v1 in ipairs(t1) do
local isMatch = false
for i2, v2 in ipairs(t2) do
if v1 == v2 or suite.isNan(v1) and suite.isNan(v2) then
isMatch = true
end
end
if not isMatch then
return false
end
end
-- The above fails if there is more than one NaN, so count the total number of NaNs in each table.
local t1Nans = 0
for i1, v1 in ipairs(t1) do
if suite.isNan(v1) then
t1Nans = t1Nans + 1
end
end
local t2Nans = 0
for i2, v2 in ipairs(t2) do
if suite.isNan(v2) then
t2Nans = t2Nans + 1
end
end
if t1Nans ~= t2Nans then
return false
end
return true
end
function suite:assertErrorEquals(expected, func, ...)
local success, msg = pcall(func, ...)
self:assertEquals(expected, msg)
end
function suite:assertTypeErrorEquals(argId, name, expectedType, actualType, func, ...)
local expected = "bad argument #%d to '%s' (%s expected, got %s)"
expected = expected:format(argId, name, expectedType, actualType)
self:assertErrorEquals(expected, func, ...)
end
------------------------------------------------------------------------
-- Make test sets
------------------------------------------------------------------------
function suite.getKeyValueTables()
local t1 = {eggs = 'eggs', beans = 'beans', sausage = 'sausage'}
local t2 = {eggs = 'eggs', beans = 'baked beans', sausage = 'sausage', spam = 'spam'}
local t3 = {lobster = 'lobster thermidor', eggs = 'scrambled eggs', beans = 'beans', sausage = 'sausage'}
local union = {eggs = {'eggs', 'scrambled eggs'}, beans = {'beans', 'baked beans'}, sausage = 'sausage', spam = 'spam', lobster = 'lobster thermidor'}
local intersection = {sausage = 'sausage'}
local complement123 = {lobster = 'lobster thermidor', eggs = 'scrambled eggs'}
local complement321 = {}
return t1, t2, t3, union, intersection, complement123, complement321
end
function suite.getValueNumsTables()
local nums1 = {1, 3, 4, 5, foo = 7}
local nums2 = {2, bar = 3, 5, 6}
local nums3 = {2, 3, 5, 8}
local numsUnion = {1, 2, 3, 4, 5, 6, 7, 8}
local numsIntersection = {3, 5}
local numsComplement = {8}
return nums1, nums2, nums3, numsUnion, numsIntersection, numsComplement
end
function suite.getValueStringsTables()
local strings1 = {'foo', 'bar', 'baz'}
local strings2 = {'eggs', 'sausage', 'spam', 'bar', 'baz'}
local stringsUnion = {'foo', 'eggs', 'sausage', 'spam', 'bar', 'baz'}
local stringsIntersection = {'bar', 'baz'}
local stringsComplement = {'eggs', 'sausage', 'spam'}
return strings1, strings2, stringsUnion, stringsIntersection, stringsComplement
end
function suite.getValueMixedTables()
local mixed1 = {1, 3, 4, 5, foo = 7, 'foo', 'bar', 'baz'}
local mixed2 = {2, bar = 3, 5, 6, 'eggs', 'sausage', 'spam', 'bar', 'baz'}
local mixedUnion = {1, 2, 3, 4, 5, 6, 7, 'foo', 'bar', 'baz', 'eggs', 'sausage', 'spam'}
local mixedIntersection = {3, 5, 'bar', 'baz'}
local mixedComplement = {2, 6, 'eggs', 'sausage', 'spam'}
return mixed1, mixed2, mixedUnion, mixedIntersection, mixedComplement
end
function suite.getValueFalseTables()
local false1 = {1, 3, false, 4}
local false2 = {2, 3, false, 5, 6}
local falseUnion = {1, 2, 3, 4, 5, 6, false}
local falseIntersection = {3, false}
local falseComplement = {2, 5, 6}
return false1, false2, falseUnion, falseIntersection, falseComplement
end
function suite.getValueNansTables()
local nans1 = {1, 3, 0/0, 2}
local nans2 = {5, 2, 3, 0/0, 6}
local nansUnion = {1, 2, 3, 5, 6, 0/0, 0/0} -- We have two 0/0s here as NaNs are never equal to each other.
local nansIntersection = {2, 3} -- 0/0 is not included as a NaN is never equal to another NaN.
local nansComplement = {5, 0/0, 6} -- The 0/0 in nans2 is included as it is a NaN, and therefore not equal to the NaN in nans1.
return nans1, nans2, nansUnion, nansIntersection, nansComplement
end
------------------------------------------------------------------------
-- Test union
------------------------------------------------------------------------
function suite:testUnion()
local t1, t2, t3, union, intersection = suite.getKeyValueTables()
self:assertDeepEquals(union, set.union(t1, t2, t3))
self:assertErrorEquals("too few arguments to 'union' (minimum is 2, received 0)", set.union)
self:assertErrorEquals("too few arguments to 'union' (minimum is 2, received 1)", set.union, 3)
self:assertTypeErrorEquals(1, 'union', 'table', 'number', set.union, 4, 5)
self:assertTypeErrorEquals(2, 'union', 'table', 'string', set.union, {}, 'foo')
self:assertTypeErrorEquals(1, 'union', 'table', 'nil', set.union, nil, nil)
end
------------------------------------------------------------------------
-- Test intersection
------------------------------------------------------------------------
function suite:testIntersection()
local t1, t2, t3, union, intersection = suite.getKeyValueTables()
self:assertDeepEquals(intersection, tt.intersection(t1, t2, t3))
self:assertErrorEquals("too few arguments to 'intersection' (minimum is 2, received 0)", set.intersection)
self:assertErrorEquals("too few arguments to 'intersection' (minimum is 2, received 1)", set.intersection, 3)
self:assertTypeErrorEquals(1, 'intersection', 'table', 'number', set.intersection, 4, 5)
self:assertTypeErrorEquals(2, 'intersection', 'table', 'string', set.intersection, {}, 'foo')
self:assertTypeErrorEquals(1, 'intersection', 'table', 'nil', set.intersection, nil, nil)
end
------------------------------------------------------------------------
-- Test complement
------------------------------------------------------------------------
function suite:testComplement()
local t1, t2, t3, union, intersection, complement123, complement321 = suite.getKeyValueTables()
self:assertDeepEquals(complement123, set.complement(t1, t2, t3))
self:assertDeepEquals(complement321, set.complement(t3, t2, t1))
self:assertErrorEquals("too few arguments to 'complement' (minimum is 2, received 0)", set.complement)
self:assertErrorEquals("too few arguments to 'complement' (minimum is 2, received 1)", set.complement, 3)
self:assertTypeErrorEquals(2, 'complement', 'table', 'string', set.complement, {}, 'foo')
self:assertTypeErrorEquals(3, 'complement', 'table', 'nil', set.complement, {}, {}, nil, {})
end
------------------------------------------------------------------------
-- Test valueUnion
------------------------------------------------------------------------
function suite:testValueUnion()
local nums1, nums2, nums3, numsUnion, numsIntersection = suite.getValueNumsTables()
local numsUnionResult = set.valueUnion(nums1, nums2, nums3)
self:assertTrue(suite.arraysHaveSameValues(numsUnion, numsUnionResult))
local strings1, strings2, stringsUnion, stringsIntersection = suite.getValueStringsTables()
local stringsUnionResult = set.valueUnion(strings1, strings2)
self:assertTrue(suite.arraysHaveSameValues(stringsUnion, stringsUnionResult))
local mixed1, mixed2, mixedUnion, mixedIntersection = suite.getValueMixedTables()
local mixedUnionResult = set.valueUnion(mixed1, mixed2)
self:assertTrue(suite.arraysHaveSameValues(mixedUnion, mixedUnionResult))
local false1, false2, falseUnion, falseIntersection, falseComplement = suite.getValueFalseTables()
local falseUnionResult = set.valueUnion(false1, false2)
self:assertTrue(suite.arraysHaveSameValues(falseUnion, falseUnionResult))
local nans1, nans2, nansUnion, nansIntersection = suite.getValueNansTables()
local nansUnionResult = set.valueUnion(nans1, nans2)
self:assertTrue(suite.arraysHaveSameValues(nansUnion, nansUnionResult))
self:assertErrorEquals("too few arguments to 'valueUnion' (minimum is 2, received 0)", set.valueUnion)
self:assertErrorEquals("too few arguments to 'valueUnion' (minimum is 2, received 1)", set.valueUnion, 3)
self:assertTypeErrorEquals(1, 'valueUnion', 'table', 'number', set.valueUnion, 4, 4)
self:assertTypeErrorEquals(2, 'valueUnion', 'table', 'string', set.valueUnion, {}, 'foo')
self:assertTypeErrorEquals(1, 'valueUnion', 'table', 'nil', set.valueUnion, nil, nil)
end
------------------------------------------------------------------------
-- Test valueIntersection
------------------------------------------------------------------------
function suite:testValueIntersection()
local nums1, nums2, nums3, numsUnion, numsIntersection = suite.getValueNumsTables()
local numsIntersectionResult = set.valueIntersection(nums1, nums2, nums3)
self:assertTrue(suite.arraysHaveSameValues(numsIntersection, numsIntersectionResult))
local strings1, strings2, stringsUnion, stringsIntersection = suite.getValueStringsTables()
local stringsIntersectionResult = set.valueIntersection(strings1, strings2)
self:assertTrue(suite.arraysHaveSameValues(stringsIntersection, stringsIntersectionResult))
local mixed1, mixed2, mixedUnion, mixedIntersection = suite.getValueMixedTables()
local mixedIntersectionResult = set.valueIntersection(mixed1, mixed2)
self:assertTrue(suite.arraysHaveSameValues(mixedIntersection, mixedIntersectionResult))
local false1, false2, falseUnion, falseIntersection, falseComplement = suite.getValueFalseTables()
local falseIntersectionResult = set.valueIntersection(false1, false2)
self:assertTrue(suite.arraysHaveSameValues(falseIntersection, falseIntersectionResult))
local nans1, nans2, nansUnion, nansIntersection = suite.getValueNansTables()
local nansIntersectionResult = set.valueIntersection(nans1, nans2)
self:assertTrue(suite.arraysHaveSameValues(nansIntersection, nansIntersectionResult))
self:assertErrorEquals("too few arguments to 'valueIntersection' (minimum is 2, received 0)", set.valueIntersection)
self:assertErrorEquals("too few arguments to 'valueIntersection' (minimum is 2, received 1)", set.valueIntersection, 3)
self:assertTypeErrorEquals(1, 'valueIntersection', 'table', 'number', set.valueIntersection, 4, 5)
self:assertTypeErrorEquals(2, 'valueIntersection', 'table', 'string', set.valueIntersection, {}, 'foo')
self:assertTypeErrorEquals(1, 'valueIntersection', 'table', 'nil', set.valueIntersection, nil, nil)
end
------------------------------------------------------------------------
-- Test valueComplement
------------------------------------------------------------------------
function suite:testValueComplement()
local nums1, nums2, nums3, numsUnion, numsIntersection, numsComplement = suite.getValueNumsTables()
local numsComplementResult = set.valueComplement(nums1, nums2, nums3)
self:assertTrue(suite.arraysHaveSameValues(numsComplement, numsComplementResult))
local strings1, strings2, stringsUnion, stringsIntersection, stringsComplement = suite.getValueStringsTables()
local stringsComplementResult = set.valueComplement(strings1, strings2)
self:assertTrue(suite.arraysHaveSameValues(stringsComplement, stringsComplementResult))
local mixed1, mixed2, mixedUnion, mixedIntersection, mixedComplement = suite.getValueMixedTables()
local mixedComplementResult = set.valueComplement(mixed1, mixed2)
self:assertTrue(suite.arraysHaveSameValues(mixedComplement, mixedComplementResult))
local false1, false2, falseUnion, falseIntersection, falseComplement = suite.getValueFalseTables()
local falseComplementResult = set.valueComplement(false1, false2)
self:assertTrue(suite.arraysHaveSameValues(falseComplement, falseComplementResult))
local nans1, nans2, nansUnion, nansIntersection, nansComplement = suite.getValueNansTables()
local nansComplementResult = set.valueComplement(nans1, nans2)
self:assertTrue(suite.arraysHaveSameValues(nansComplement, nansComplementResult))
self:assertErrorEquals("too few arguments to 'valueComplement' (minimum is 2, received 0)", set.valueComplement)
self:assertErrorEquals("too few arguments to 'valueComplement' (minimum is 2, received 1)", set.valueComplement, 3)
self:assertTypeErrorEquals(1, 'valueComplement', 'table', 'number', set.valueComplement, 4, 5)
self:assertTypeErrorEquals(2, 'valueComplement', 'table', 'string', set.valueComplement, {}, 'foo')
self:assertTypeErrorEquals(1, 'valueComplement', 'table', 'nil', set.valueComplement, nil, nil)
end
return suite