test_function: Test if Function Reproduces Stored Results

Description Usage Arguments Value Examples

Description

Call the function functionName with the arguments contained in testdata and compare the results with the results in testdata for identity.

Usage

1
2
test_function(functionName, testdata = loadArgs(functionName,
  file.path(tempdir(), "test")), dbg = TRUE)

Arguments

functionName

Name of the function to test. It must be callable, i.e. either defined in the global environment or on the search path.

testdata

List of lists containing function arguments (in elemenet args) and results (in element result), just as returned by loadArgs. If no testdata are given, it is tried to load test data by calling loadArgs on functionName.

dbg

if TRUE (default) debug messages are shown

Value

TRUE If the function functionName is able to reproduce the same results as given in the result elements in testdata for all the argument combinations given in the args elements in testdata.

Examples

 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
# Define a function using saveArgs() to save arguments and result
squareSum <- function(a, b) {
  result <- a * a + b * b
  saveArgs("squareSum", args = list(a = a, b = b), result = result)
  result
}

# Set global variable TESTMODE to "activate" saveArgs() in squareSum()
TESTMODE <- TRUE

# Call the function with different arguments
squareSum(1, 2)
squareSum(2, 3)
squareSum(-1, -2)

# The arguments and function results were saved here:
dir(file.path(tempdir(), "test"))

# Write a new (wrong) version of the function
squareSum.new <- function(a, b) {
  a * a - b * b
}

# Check if it returns the same results
test_function("squareSum.new", loadArgs("squareSum"))

# If no test data are given, loadArgs is called on the function to test,
# i.e. testing squareSum on the test data created by the same function will
# return TRUE if the function did not change in the meanwhile.
test_function("squareSum")

KWB-R/kwb.test documentation built on Sept. 12, 2019, 3:41 a.m.