| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('rand_unit() returns values in [0, 1)', { |
| 4 |
x = rand_unit(100, seed = 42) |
| 5 |
(length(x) %==% 100L) |
| 6 |
(x >= 0) |
| 7 |
(x < 1) |
| 8 |
}) |
| 9 |
|
| 10 |
assert('rand_unit() produces reproducible results with the same seed', { |
| 11 |
x1 = rand_unit(10, seed = 0) |
| 12 |
x2 = rand_unit(10, seed = 0) |
| 13 |
(x1 %==% x2) |
| 14 |
}) |
| 15 |
|
| 16 |
assert('rand_unit() returns 0 values for n = 0', { |
| 17 |
(length(rand_unit(0, seed = 1)) %==% 0L) |
| 18 |
}) |
| 19 |
|
| 20 |
assert('rand_n() returns integers in [1, M]', { |
| 21 |
x = rand_n(100, 6) |
| 22 |
(length(x) %==% 100L) |
| 23 |
(x >= 1) |
| 24 |
(x <= 6) |
| 25 |
}) |
| 26 |
|
| 27 |
assert('md5() computes checksums', { |
| 28 |
(is.character(md5(1:10))) |
| 29 |
(nchar(md5(1:10)) %==% 32L) |
| 30 |
# x2 and x3 should be identical since they are the same integer sequence |
| 31 |
x2 = 1:10; x3 = seq(1, 10) |
| 32 |
(md5(x2) %==% md5(x3)) |
| 33 |
# named arguments produce named results |
| 34 |
res = md5(a = 1, b = 'hello') |
| 35 |
(names(res) %==% c('a', 'b')) |
| 36 |
}) |
| 37 |
|
| 38 |
assert('find_locals() identifies locally created variables', { |
| 39 |
(find_locals('y = x + 1') %==% 'y') |
| 40 |
("y" %in% find_locals("assign('y', x + 1)")) |
| 41 |
}) |
| 42 |
|
| 43 |
assert('cache_exec() returns correct result', { |
| 44 |
r = cache_exec({2 + 2}, path = ':memory:', id = 'xfun-test-result') |
| 45 |
(r %==% 4) |
| 46 |
}) |
| 47 |
|
| 48 |
assert('cache_exec() uses cache on second call', { |
| 49 |
id = 'xfun-test-cache-hit' |
| 50 |
r1 = cache_exec({Sys.time()}, path = ':memory:', id = id) |
| 51 |
Sys.sleep(0.01) # ensure time would differ if re-evaluated |
| 52 |
r2 = cache_exec({Sys.time()}, path = ':memory:', id = id) |
| 53 |
# r2 should come from cache, so they should be identical |
| 54 |
(r1 %==% r2) |
| 55 |
}) |