This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KleinRefrigerator's profile picture

idoneus Testing System

Started by KleinRefrigerator, 16 November 2012 - 07:11 AM
KleinRefrigerator #1
Posted 16 November 2012 - 08:11 AM
About
idoneus is a testing system: given a set of test cases that include inputs to the function and expected outputs, it verifies that the function produces the proper results. When I was considering how to implement my AES test vectors for ccc, I realized my initial plan was bound to turn into a massive, hard-coded monstrosity, so I came up with a way to standardize and simplify testing.

idoneus is intended primarily to work with inputs and outputs of immutable types (strings, numbers, booleans), but it can deal with table inputs to some extent. It will let you shoot yourself in the foot, though, so be careful. Some potential problems are mentioned in the documentation of its functions.

Download
http://pastebin.com/uXgtzwNs

pastebin get uXgtzwNs idoneus

Functions

-- builds a test structure given a function, fixed arguments, and variable argument positions
function buildTS(f, fA, vA)
-- adds a test case to a test structure given inputs and expected output
function addCase(ts, inputs, output)
-- runs a test and returns results in two tables, the first containing true/false for whether the test worked and the second containing the outputs of the tests that failed
function runTest(testStruct)
-- returns a string containing a simple test report given the outputs of runTest
function testReport(results, badOuts)


Sample Usage
Spoiler
os.loadAPI("idoneus")

-- let's test this function
function test(a, b, c)
	if (a == 1) then
		return b + c
	elseif (a == 2) then
		return b * c
	elseif (a == 3) then
		return b ^ c
	else
		return "WAT"
	end
end

-- these are the main test structure parameters
-- the function to test, no fixed arguments, and the three var args go in the first three places
testStr = idoneus.buildTS(test, {}, {1, 2, 3})

-- test these cases
idoneus.addCase(testStr, {1, 5, 6}, 11)
idoneus.addCase(testStr, {1, 89, 103}, 190)
idoneus.addCase(testStr, {2, 8, 7}, 56)
idoneus.addCase(testStr, {2, 13, 3}, 12)
idoneus.addCase(testStr, {3, 3, 3}, 27)
idoneus.addCase(testStr, {3, 16, 2}, 308)
idoneus.addCase(testStr, {4, 1, 1}, "WAT")
idoneus.addCase(testStr, {4, 2, 2}, "pancakes!")

-- run the test
res, bad = idoneus.runTest(testStr)

-- this function probably does not do what I need it to since it fails half the time :c
print(idoneus.testReport(res, bad))

-- output:
1   true
2   false   192
3   true
4   false   39
5   true
6   false   256
7   true
8   false   WAT

Change Log
rev1: added the testing system, test structure constructors, and a basic test report generator

possible upcoming features: better handling of table inputs and
Espen #2
Posted 16 November 2012 - 08:35 AM
Nice work. Very useful utility and also very well documented, especially the function-comments within the program itself. I like it.
Kudos! :P/>/>
Leo Verto #3
Posted 16 November 2012 - 10:19 AM
Nice program, really useful for coding an OS when you don't want to run the code in your mind.