Skip to content

Commit

Permalink
first dummy maths tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anitaapplegarth committed Feb 27, 2025
1 parent dc7935e commit 56aaa75
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Suggests: tinytest
Imports:
ggplot2,
here,
testthat,
tidyr,
reticulate (>= 1.14)
Depends: R (>= 3.3.0)
Expand Down
142 changes: 142 additions & 0 deletions R/anita_maths.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#' Add two numbers together
#'
#' @param x First number
#' @param y Second number
#' @return The sum of x and y
#' @examples
#' add(1, 2)
#' add(-1, 1)
add <- function(x, y) {
return(x + y)
}

#' Subtract the second number from the first
#'
#' @param x First number
#' @param y Second number
#' @return The result of x - y
#' @examples
#' subtract(5, 2)
#' subtract(-5, -3)
subtract <- function(x, y) {
return(x - y)
}

#' Multiply two numbers
#'
#' @param x First number
#' @param y Second number
#' @return The product of x and y
#' @examples
#' multiply(3, 4)
#' multiply(-2, 5)
multiply <- function(x, y) {
return(x * y)
}

#' Divide the first number by the second
#'
#' @param x Numerator
#' @param y Denominator
#' @return The result of x / y
#' @examples
#' divide(10, 2)
#' divide(0, 5)
divide <- function(x, y) {
if (y == 0) {
stop("Cannot divide by zero")
}
return(x / y)
}

#' Calculate the arithmetic mean of a vector
#'
#' @param x A numeric vector
#' @return The arithmetic mean
#' @examples
#' mean_calc(c(1, 2, 3, 4))
#' mean_calc(c(10, 20))
mean_calc <- function(x) {
return(sum(x) / length(x))
}

#' Calculate the standard deviation of a vector
#'
#' @param x A numeric vector
#' @return The standard deviation
#' @examples
#' std_dev(c(1, 2, 3, 4))
#' std_dev(c(5, 5, 5))
std_dev <- function(x) {
mean_x <- mean_calc(x)
variance <- sum((x - mean_x)^2) / length(x)
return(sqrt(variance))
}

#' Check if a value is a valid probability (between 0 and 1)
#'
#' @param p Value to check
#' @return TRUE if valid probability, FALSE otherwise
#' @examples
#' is_probability(0.5)
#' is_probability(1.5)
is_probability <- function(p) {
return(p >= 0 && p <= 1)
}

#' Calculate risk based on probability and risk factor
#'
#' @param prob Probability value
#' @param factor Risk factor
#' @return Calculated risk
#' @examples
#' calculate_risk(0.3, 0.5)
#' calculate_risk(0.1, 0.2)
calculate_risk <- function(prob, factor) {
if (!is_probability(prob) || !is_probability(factor)) {
warning("Inputs should be valid probabilities")
}
return(prob * factor)
}

#' Calculate something with validation
#'
#' @param input Input value
#' @return Calculated result
#' @examples
#' calculate_something(5)
#' calculate_something(10)
calculate_something <- function(input) {
if (is.null(input)) {
stop("Input cannot be NULL")
}
return(input * 2)
}

#' Validate input value
#'
#' @param input Input value to validate
#' @return Validated input
#' @examples
#' validate_input(5)
#' validate_input(-1)
validate_input <- function(input) {
if (input < 0) {
warning("Negative input detected")
}
return(abs(input))
}

#' Perform a safe calculation with error handling
#'
#' @param input Input vector
#' @return Calculation result or NA for empty inputs
#' @examples
#' safe_calculation(c(1, 2, 3))
#' safe_calculation(c())
safe_calculation <- function(input) {
if (length(input) == 0) {
return(NA)
}
return(sum(input))
}
Binary file modified simulation_outputs/simulation_flow_SIR_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file removed tests/test.R
Empty file.
4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(rEpiabm)

test_check("rEpiabm") # Run all tests in the testthat directory
51 changes: 51 additions & 0 deletions tests/testthat/test-anita_maths.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Tests for functions in anita_maths.r
library(testthat)
library(rEpiabm)

test_that("Addition functions work correctly", {
expect_equal(add(1, 2), 3)
expect_equal(add(1.5, 2.5), 4.0)
})

test_that("Subtraction functions work correctly", {
# Assuming there's a subtract() function
expect_equal(subtract(5, 2), 3)
expect_equal(subtract(-5, -3), -2)
})

test_that("Multiplication functions work correctly", {
# Assuming there's a multiply() function
expect_equal(multiply(3, 4), 12)
expect_equal(multiply(0, 100), 0)
})

test_that("Division functions work correctly", {
# Assuming there's a divide() function
expect_equal(divide(10, 2), 5)
expect_equal(divide(0, 5), 0)

# Test that division by zero raises an error
expect_error(divide(5, 0), "Cannot divide by zero")
})

test_that("Statistical functions work correctly", {
# Assuming there might be functions like mean_calc() or std_dev()
# For example:
expect_equal(mean_calc(c(1, 2, 3, 4)), 2.5)
expect_equal(std_dev(c(1, 2, 3, 4)), 1.12, tolerance = 0.01)
})

# If there are simulation-related functions in anita_maths.r
test_that("Simulation calculations are accurate", {
# Example tests:
expect_true(is_probability(0.5))
expect_false(is_probability(1.5))
expect_equal(calculate_risk(0.3, 0.5), 0.15)
})

# Edge cases and error handling
test_that("Functions handle edge cases correctly", {
expect_error(calculate_something(NULL), "Input cannot be NULL")
expect_warning(validate_input(-1), "Negative input detected")
expect_equal(safe_calculation(c()), NA, "Empty input should return NA")
})

0 comments on commit 56aaa75

Please sign in to comment.