# Stress-testing function calls

#### Jack O. Wasey

#### 2018-06-11

This is a set of simple utilities for various data manipulation and testing tasks. The goal is to use core R tools well, without bringing in many dependencies. Main areas of interest are semi-automated data frame manipulation, such as converting factors in multiple binary indicator columns. There are testing functions which provide ‘testthat’ expectations to permute arguments to function calls. There are functions and data to test extreme numbers, dates, and bad input of various kinds which should allow testing failure and corner cases, which can be used for fuzzing your functions. The test suite has many examples of usage.

# Stress and Fuzz testing

`jwutil` has some minimal features to allow fuzz and stress testing functions. This is a complicated topic, but at a minimum, the following functions allow you to to give enexpected input to a function, with the expectation it should still work. The packge does more, but does different things.

## Extreme integers

Maximum magnitude positive and negative doubles and integers

    extreme_numbers

    ## [1]   2.147484e+09  -2.147484e+09   1.000000e+00  -1.000000e+00
    ## [5]  2.225074e-308  1.797693e+308 -2.225074e-308 -1.797693e+308

There are several types of zero in R (excluding string representations):

    zeroes

    ## [[1]]
    ## [1] 0
    ##
    ## [[2]]
    ## [1] 0
    ##
    ## [[3]]
    ## [1] 0+0i

    sapply(zeroes, class)

    ## [1] "integer" "numeric" "complex"

## Permuting

If the order of arguments in a function should not matter, the result should be the same each time. `expect_that_comabine_all_args` tests this.

    expect_that_combine_all_args(sum(1, 2), testthat::equals(3))

is equivalent to

    expect_that(sum(1, 2), testthat::equals(3))
    expect_that(sum(2, 1), testthat::equals(3))

This becomes more interesting with named arguments, where the order also should not matter. Beware of testing features of R, rather than your code: R has a complicated but standard way of matching arguments, described in Writing R Extensions. If your function does complicated argument manipulation, and maybe uses `...`, this function could verify that named argument order doesn’t matter.

    res <- gsub(pattern = "the", replacement = "le", x = "Pierre the chat")
    expect_that_combine_all_args(
      gsub(pattern = "the", replacement = "le", x = "Pierre the chat"),
      testthat::equals(res)
      )

For functions where the first argument is a vector, and the order of elements in the vector should not matter, `expect_that_combine_first_arg` will permute all combinations of the first argument, and expect the same result. A large number of elements will take a very long time, factorial with the number of items.

    expect_that_combine_first_arg(sum(c(-1, 0, 1)), testthat::equals(0))

## Other

`lsf` lists all the functions in a package, which may be useful to start an attack on all functions with a range of absurd data, looking perhaps for crashes in C/C++ code rather than R errors.

    lsf("jwutil")[1:10]

    ##  [1] "%ine%"            "%nin%"            "add_time_to_date"
    ##  [4] "affixFields"      "allIsInteger"     "allIsNumeric"
    ##  [7] "areIntegers"      "areNumeric"       "asIntegerNoWarn"
    ## [10] "asNumericNoWarn"