Skip to content

Commit 4afc511

Browse files
authored
Merge pull request #186 from keaven/to-integer-tests
Add independent tests for `toInteger()`
2 parents e9cec5a + f11e99e commit 4afc511

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
test_that("toInteger() handles gsDesign objects", {
2+
# Create a gsDesign object
3+
x <- gsDesign(k = 3, test.type = 2, alpha = 0.025, beta = 0.1, delta = 0.5)
4+
5+
# Test toInteger() with a non-survival design
6+
result <- toInteger(x, ratio = 1)
7+
8+
# Check if the output retains the class gsDesign
9+
expect_s3_class(result, "gsDesign")
10+
11+
# Ensure the final count is rounded up to the nearest multiple of (ratio + 1)
12+
expect_true(result$n.I[x$k] %% (1 + 1) == 0)
13+
})
14+
15+
test_that("toInteger() handles gsDesign object integer conversion correctly", {
16+
# Create a gsDesign object with n.fix
17+
x <- gsDesign(k = 3, test.type = 4, alpha = 0.025, beta = 0.1, n.fix = 300)
18+
19+
# Test toInteger() with ratio = 3
20+
result <- toInteger(x, ratio = 3)
21+
22+
# Check that all n.I values are integers
23+
expect_true(all(result$n.I == floor(result$n.I)))
24+
25+
# Check that final n.I is a multiple of ratio + 1
26+
expect_equal(result$n.I[x$k] %% (3 + 1), 0)
27+
})
28+
29+
test_that("toInteger() handles gsSurv object integer conversion correctly", {
30+
# Create a gsSurv object
31+
x <- gsSurv(
32+
k = 3,
33+
test.type = 4,
34+
alpha = 0.025,
35+
beta = 0.1,
36+
timing = c(0.45, 0.7),
37+
sfu = sfHSD,
38+
sfupar = -4,
39+
sfl = sfLDOF,
40+
sflpar = 0,
41+
lambdaC = 0.001,
42+
hr = 0.3,
43+
hr0 = 0.7,
44+
eta = 5e-04,
45+
gamma = 10,
46+
R = 16,
47+
T = 24,
48+
minfup = 8,
49+
ratio = 2
50+
)
51+
52+
# Test with a different ratio
53+
result <- toInteger(x, ratio = 2)
54+
55+
# Test if the final sample size is a multiple of ratio + 1
56+
expect_true(result$n.I[x$k] %% (2 + 1) == 0)
57+
58+
# Ensure final count is rounded up correctly when roundUpFinal is TRUE
59+
expect_equal(result$n.I[x$k], ceiling(x$n.I[x$k]))
60+
})
61+
62+
test_that("toInteger() handles edge case where no rounding is needed", {
63+
x <- gsDesign(k = 3, test.type = 1, alpha = 0.05, beta = 0.2, n.fix = 150)
64+
65+
# Call toInteger() with a ratio of 0 (no adjustment needed)
66+
result <- toInteger(x, ratio = 0)
67+
68+
# Check if all values are integers
69+
expect_true(all(result$n.I == floor(result$n.I)))
70+
})
71+
72+
test_that("toInteger() raises an error when n.I contains negative values", {
73+
# Create a gsDesign object with arbitrary settings
74+
x_test <- gsDesign(k = 3, test.type = 2, alpha = 0.025, beta = 0.1, sfu = sfHSD, sfupar = -4)
75+
76+
# Set n.I with a negative value
77+
x_test$n.I <- c(100, 200, -250.5) # Negative value to trigger the error
78+
79+
# Check that toInteger raises an error
80+
expect_error(
81+
toInteger(x_test, ratio = 3, roundUpFinal = TRUE),
82+
"maxn.IPlan not on interval \\[0, Inf\\]"
83+
)
84+
})
85+
86+
test_that("toInteger() prints a message for invalid ratio values", {
87+
# Create a valid gsDesign object with n.fix
88+
x_test <- gsDesign(k = 3, test.type = 1, alpha = 0.025, beta = 0.1, n.fix = 300)
89+
90+
# Test for negative ratio
91+
expect_message(
92+
toInteger(x_test, ratio = -1),
93+
"rounding done to nearest integer since ratio was not specified as postive integer"
94+
)
95+
96+
# Test for non-integer ratio (numeric)
97+
expect_message(
98+
toInteger(x_test, ratio = 2.5),
99+
"rounding done to nearest integer since ratio was not specified as postive integer"
100+
)
101+
102+
# Test for non-numeric ratio
103+
expect_message(
104+
toInteger(x_test, ratio = "two"),
105+
"rounding done to nearest integer since ratio was not specified as postive integer"
106+
)
107+
108+
# Test for NULL ratio
109+
expect_message(
110+
toInteger(x_test, ratio = NULL),
111+
"rounding done to nearest integer since ratio was not specified as postive integer"
112+
)
113+
})
114+
115+
test_that("toInteger() throws an error when input is not a gsDesign object", {
116+
invalid_object <- data.frame(a = 1, b = 2) # Not a gsDesign object
117+
expect_error(toInteger(invalid_object), "must have class gsDesign as input")
118+
})

0 commit comments

Comments
 (0)