-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(horner): New tests for polynomial evaluation methods
- Loading branch information
Showing
5 changed files
with
155 additions
and
27 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,32 @@ | ||
library("testthat") | ||
context("betterpoly") | ||
|
||
test_that("betterpoly evaluates known polynomials correctly", { | ||
expect_equal(betterpoly(1, c(1, 2, 3)), 6) # 1 + 2*1 + 3*1^2 = 6 | ||
expect_equal(betterpoly(0, c(5, 0, 0)), 5) # Testing zero | ||
expect_equal(betterpoly(-1, c(1, -1, 1)), 3) # Testing negative x | ||
expect_equal(betterpoly(1, c(0.5, 0.25)), 0.75) # Testing fractional coefficients | ||
}) | ||
|
||
test_that("betterpoly handles edge cases", { | ||
expect_error(betterpoly(1, c())) # Empty coefficients | ||
expect_equal(betterpoly(1, c(5)), 5) # Single coefficient | ||
}) | ||
|
||
test_that("betterpoly handles random coefficients and values", { | ||
for (i in 1:5) { | ||
coefs <- runif(10, -10, 10) | ||
x_val <- runif(1, -10, 10) | ||
expect_equal(betterpoly(x_val, coefs), sum(coefs * x_val^(0:(length(coefs) - 1)))) | ||
} | ||
}) | ||
|
||
test_that("betterpoly handles large coefficients and values", { | ||
large_coefs <- rep(10^6, 10) | ||
expect_equal(betterpoly(1, large_coefs), sum(large_coefs)) | ||
}) | ||
|
||
test_that("betterpoly rejects non-numeric input", { | ||
expect_error(betterpoly("a", c(1, 2, 3))) | ||
expect_error(betterpoly(1, c("a", "b", "c"))) | ||
}) |
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 |
---|---|---|
@@ -1,11 +1,32 @@ | ||
library("testthat") | ||
context("horner") | ||
|
||
## If you're curious, it's from the Addams Family movie | ||
beta <- c(2, 10, 11) | ||
expect_equal(horner(5, beta), 327) | ||
expect_equal(horner(87, beta), 84131) | ||
expect_equal(horner(-10, beta), 1002) | ||
expect_equal(horner(0, beta), 2) | ||
test_that("horner evaluates known polynomials correctly", { | ||
expect_equal(horner(1, c(1, 2, 3)), 6) # 1 + 2*1 + 3*1^2 = 6 | ||
expect_equal(horner(0, c(5, 0, 0)), 5) # Testing zero | ||
expect_equal(horner(-1, c(1, -1, 1)), 3) # Testing negative x | ||
expect_equal(horner(1, c(0.5, 0.25)), 0.75) # Testing fractional coefficients | ||
}) | ||
|
||
beta <- c(-1, 0, 1) | ||
expect_equal(horner(c(1, 2, 3), beta), c(0, 3, 8)) | ||
test_that("horner handles edge cases", { | ||
expect_error(horner(1, c())) # Empty coefficients | ||
expect_equal(horner(1, c(5)), 5) # Single coefficient | ||
}) | ||
|
||
test_that("horner handles random coefficients and values", { | ||
for (i in 1:5) { | ||
coefs <- runif(10, -10, 10) | ||
x_val <- runif(1, -10, 10) | ||
expect_equal(horner(x_val, coefs), sum(coefs * x_val^(0:(length(coefs) - 1)))) | ||
} | ||
}) | ||
|
||
test_that("horner handles large coefficients and values", { | ||
large_coefs <- rep(10^6, 10) | ||
expect_equal(horner(1, large_coefs), sum(large_coefs)) | ||
}) | ||
|
||
test_that("horner rejects non-numeric input", { | ||
expect_error(horner("a", c(1, 2, 3))) | ||
expect_error(horner(1, c("a", "b", "c"))) | ||
}) |
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,32 @@ | ||
library("testthat") | ||
context("rhorner") | ||
|
||
test_that("rhorner evaluates known polynomials correctly", { | ||
expect_equal(rhorner(1, c(1, 2, 3)), 6) # 1 + 2*1 + 3*1^2 = 6 | ||
expect_equal(rhorner(0, c(5, 0, 0)), 5) # Testing zero | ||
expect_equal(rhorner(-1, c(1, -1, 1)), 3) # Testing negative x | ||
expect_equal(rhorner(1, c(0.5, 0.25)), 0.75) # Testing fractional coefficients | ||
}) | ||
|
||
test_that("rhorner handles edge cases", { | ||
expect_error(rhorner(1, c())) # Empty coefficients | ||
expect_equal(rhorner(1, c(5)), 5) # Single coefficient | ||
}) | ||
|
||
test_that("rhorner handles random coefficients and values", { | ||
for (i in 1:5) { | ||
coefs <- runif(10, -10, 10) | ||
x_val <- runif(1, -10, 10) | ||
expect_equal(rhorner(x_val, coefs), sum(coefs * x_val^(0:(length(coefs) - 1)))) | ||
} | ||
}) | ||
|
||
test_that("rhorner handles large coefficients and values", { | ||
large_coefs <- rep(10^6, 10) | ||
expect_equal(rhorner(1, large_coefs), sum(large_coefs)) | ||
}) | ||
|
||
test_that("rhorner rejects non-numeric input", { | ||
expect_error(rhorner("a", c(1, 2, 3))) | ||
expect_error(rhorner(1, c("a", "b", "c"))) | ||
}) |
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,32 @@ | ||
library("testthat") | ||
context("rhorner") | ||
|
||
test_that("rhorner evaluates known polynomials correctly", { | ||
expect_equal(rhorner(1, c(1, 2, 3)), 6) # 1 + 2*1 + 3*1^2 = 6 | ||
expect_equal(rhorner(0, c(5, 0, 0)), 5) # Testing zero | ||
expect_equal(rhorner(-1, c(1, -1, 1)), 3) # Testing negative x | ||
expect_equal(rhorner(1, c(0.5, 0.25)), 0.75) # Testing fractional coefficients | ||
}) | ||
|
||
test_that("rhorner handles edge cases", { | ||
expect_error(rhorner(1, c())) # Empty coefficients | ||
expect_equal(rhorner(1, c(5)), 5) # Single coefficient | ||
}) | ||
|
||
test_that("rhorner handles random coefficients and values", { | ||
for (i in 1:5) { | ||
coefs <- runif(10, -10, 10) | ||
x_val <- runif(1, -10, 10) | ||
expect_equal(rhorner(x_val, coefs), sum(coefs * x_val^(0:(length(coefs) - 1)))) | ||
} | ||
}) | ||
|
||
test_that("rhorner handles large coefficients and values", { | ||
large_coefs <- rep(10^6, 10) | ||
expect_equal(rhorner(1, large_coefs), sum(large_coefs)) | ||
}) | ||
|
||
test_that("rhorner rejects non-numeric input", { | ||
expect_error(rhorner("a", c(1, 2, 3))) | ||
expect_error(rhorner(1, c("a", "b", "c"))) | ||
}) |