-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc7935e
commit 56aaa75
Showing
6 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) |