Skip to content

Commit 44b223a

Browse files
committed
Tests for not-so-well-behaved functions
1 parent 3f7d80b commit 44b223a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

tests/testthat/test-step.K.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ test_that("Kostyrka's method returns reasonable values with tricky functions", {
3232
expect_equal(s$value, -140, tolerance = 1e-10)
3333
})
3434

35+
test_that("Mathur's algorithm returns expected non-zero exit codes", {
36+
# Very noisy right branch
37+
expect_identical(suppressWarnings(step.K(function(x) x^2, x = 1e-8)$exitcode), 1L)
38+
# No right branch at all
39+
expect_identical(suppressWarnings(step.K(function(x) x^2, x = 0)$exitcode), 2L)
40+
})
41+
3542
test_that("Parallelisation in Kostyrka's algorithm works", {
3643
expect_identical(step.K(sin, 1, cores = 1), step.K(sin, 1, cores = 2))
3744
clus <- parallel::makePSOCKcluster(2)

tests/testthat/test-step.M.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ test_that("Mathur's algorithm returns reasonable values even with bad slopes", {
1414

1515
f <- function(x) ifelse(x %in% 1:2, x^2, NA)
1616
expect_warning(m <- step.M(f, 1, cores = 1), "<3 finite function values")
17+
expect_identical(m$exitcode, 3L)
1718
})
1819

1920
test_that("Mathur's algorithm may return unreasonable values", {
2021
f <- function(x) x^3 + 1/x # Produces wildly wrong results due to the shape of the error plot
2122
expect_gt(plot(step.M(f, 1, cores = 1))$par, 0.01)
2223
})
2324

25+
test_that("Mathur's algorithm returns expected non-zero exit codes", {
26+
# Noisy right branch
27+
expect_identical(suppressWarnings(step.M(function(x) x^2, x = 1e-8)$exitcode), 2L)
28+
# No branches at all
29+
expect_identical(suppressWarnings(step.M(function(x) x^2, x = 0)$exitcode), 2L)
30+
})
31+
2432
test_that("Parallelisation in Mathur's algorithm works", {
2533
expect_identical(step.M(sin, 1, cores = 1), step.M(sin, 1, cores = 2))
2634
clus <- parallel::makePSOCKcluster(2)
2735
expect_identical(step.M(sin, 1, cores = 1), step.M(sin, 1, cl = clus))
2836
parallel::stopCluster(clus)
29-
30-
# Testing a slow function
31-
# f <- function(x) {Sys.sleep(0.1); sin(x)}
32-
# system.time(step.M(f, 1))
33-
# system.time(step.M(f, 1, cores = 12))
3437
})

0 commit comments

Comments
 (0)