Jump to content

Module:Math/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 13:21, 6 December 2013 (new test cases for the random function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Unit tests for [[Module:Math]]. Click talk page to run tests.

local mm = require('Module:Math/sandbox') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

local function invokeMath(funcName, argString)
	return '{{#invoke:math/sandbox|' .. funcName .. '|' .. (argString or '') .. '}}'
end

local function testTable(obj, funcName, mathFunc, tests)
	for i, t in ipairs(tests) do
		obj[funcName](obj, t[1], invokeMath(mathFunc, t[2]))
	end
end

local function resultEqualsMany(obj, mathFunc, tests)
	return testTable(obj, 'assertResultEquals', mathFunc, tests)
end
 
function suite:test_random()
	local rand0 = tonumber(invokeMath('random'))
	local rand2 = tonumber(invokeMath('random', '2'))
	local rand10 = tonumber(invokeMath('random', '10'))
	local rand10_20 = tonumber(invokeMath('random', '10', '20'))
	suite:assertTrue(rand0 >= 0 and rand0 < 1)
	suite:assertTrue(rand2 == 1 or rand2 == 2)
	suite:assertTrue(rand10 >= 1 and rand10 <= 10 and math.floor(rand10) == rand10)
	suite:assertTrue(rand10_20 >= 10 and rand10_20 <= 20 and math.floor(rand10_20) == rand10_20)
end
 
function suite:test_max()
	local tests = {
		{'', ''},
		{'9', '5|6|9'},
		{'-5', '-5|-6|-9'},
	}
	resultEqualsMany(self, 'max', tests)
end
 
function suite:test_average()
	local tests = {
		{'6', '5|6|7'},
		{'-7', '-7'},
		{'10000000002', '10000000001|10000000002|10000000003'},
	}
	resultEqualsMany(self, 'average', tests)
end
 
function suite:test_min()
	local tests = {
		{'', ''},
		{'1', '1|2|3'},
		{'-3', '-1|-2|-3'},
    }
	resultEqualsMany(self, 'min', tests)
end
 
function suite:test_order()
	local tests = {
		{'0', '2'},
		{'1', '20'},
		{'2', '200'},
		{'0', 'x = 5'},
		{'<strong class="error">Formatting error: order of magnitude input appears non-numeric</strong>', 'string'},
	}
	resultEqualsMany(self, 'order', tests)
end
 
function suite:test_precison()
	local tests = {
		{'4', '1.9856'},
		{'1', '1.1'},
		{'10', '1.9999999999'},
		{'4', 'x = 1.9888'},
		{'<strong class="error">Formatting error: precision input appears non-numeric</strong>', 'letra'},
	}
	resultEqualsMany(self, 'precision', tests)
end
 
function suite:test_round()
	local tests = {
		{'2', '1.99999'},
		{'2', '1.99999|0'},
		{'1.9', '1.94|1'},
		{'20', '15|-1'},
		{'3', 'value = 2.99999|precision = 2'},
	}
	resultEqualsMany(self, 'round', tests)
end
 
function suite:test_precison_format()
	local tests = {
		{'10.00', '10|2'}
	}
	resultEqualsMany(self, 'precision_format', tests)
end

return suite