| 1 |
describe("as_fs_bytes", { |
| 2 |
it("accepts numeric input unchanged", { |
| 3 |
expect_equal(unclass(as_fs_bytes(123L)), 123L) |
| 4 |
expect_equal(unclass(as_fs_bytes(123)), 123) |
| 5 |
}) |
| 6 |
it("accepts fs_byte input unchanged", { |
| 7 |
x <- as_fs_bytes(123) |
| 8 |
expect_equal(as_fs_bytes(x), x) |
| 9 |
}) |
| 10 |
it("coerces character input", { |
| 11 |
expect_equal(unclass(as_fs_bytes("1")), 1) |
| 12 |
expect_equal(unclass(as_fs_bytes("1K")), 1024) |
| 13 |
expect_equal(unclass(as_fs_bytes("1M")), 1024 * 1024) |
| 14 |
expect_equal(unclass(as_fs_bytes("10M")), 10 * 1024 * 1024) |
| 15 |
expect_equal(unclass(as_fs_bytes("1G")), 1024 * 1024 * 1024) |
| 16 |
}) |
| 17 |
}) |
| 18 |
|
| 19 |
describe("format.fs_bytes", { |
| 20 |
it("formats bytes under 1024 as whole numbers", { |
| 21 |
expect_equal(format(fs_bytes(0)), "0") |
| 22 |
expect_equal(format(fs_bytes(1)), "1") |
| 23 |
expect_equal(format(fs_bytes(1023)), "1023") |
| 24 |
}) |
| 25 |
it("formats bytes 1024 and up as abbreviated numbers", { |
| 26 |
expect_equal(format(fs_bytes(1024)), "1K") |
| 27 |
expect_equal(format(fs_bytes(1025)), "1K") |
| 28 |
expect_equal(format(fs_bytes(2^16)), "64K") |
| 29 |
expect_equal(format(fs_bytes(2^24)), "16M") |
| 30 |
expect_equal(format(fs_bytes(2^24 + 555555)), "16.5M") |
| 31 |
expect_equal(format(fs_bytes(2^32)), "4G") |
| 32 |
expect_equal(format(fs_bytes(2^48)), "256T") |
| 33 |
expect_equal(format(fs_bytes(2^64)), "16E") |
| 34 |
}) |
| 35 |
it("works with vectors", { |
| 36 |
v <- c(NA, 1, 2^13, 2^20, NaN, 2^15) |
| 37 |
expect_equal( |
| 38 |
format(fs_bytes(v), trim = TRUE), |
| 39 |
c("NA", "1", "8K", "1M", "NaN", "32K") |
| 40 |
) |
| 41 |
|
| 42 |
expect_equal(format(fs_bytes(numeric())), character()) |
| 43 |
}) |
| 44 |
it("handles NA and NaN", { |
| 45 |
expect_equal(format(fs_bytes(NaN)), "NaN") |
| 46 |
if (R.version$arch == "riscv64") { |
| 47 |
skip("NA formatting broken on riscv64") |
| 48 |
} |
| 49 |
expect_equal(format(fs_bytes(NA)), "NA") |
| 50 |
}) |
| 51 |
}) |
| 52 |
|
| 53 |
describe("sum.fs_bytes", { |
| 54 |
it("sums its input and returns a fs_byte", { |
| 55 |
expect_equal(sum(fs_bytes(0)), new_fs_bytes(0)) |
| 56 |
expect_equal(sum(fs_bytes(c(1, 2))), new_fs_bytes(3)) |
| 57 |
expect_equal(sum(fs_bytes(c(1, NA))), new_fs_bytes(NA_real_)) |
| 58 |
}) |
| 59 |
}) |
| 60 |
|
| 61 |
describe("min.fs_bytes", { |
| 62 |
it("finds minimum input and returns a fs_byte", { |
| 63 |
expect_equal(min(fs_bytes(0)), new_fs_bytes(0)) |
| 64 |
expect_equal(min(fs_bytes(c(1, 2))), new_fs_bytes(1)) |
| 65 |
expect_equal(min(fs_bytes(c(1, NA))), new_fs_bytes(NA_real_)) |
| 66 |
}) |
| 67 |
}) |
| 68 |
|
| 69 |
describe("max.fs_bytes", { |
| 70 |
it("finds maximum input and returns a fs_byte", { |
| 71 |
expect_equal(max(fs_bytes(0)), new_fs_bytes(0)) |
| 72 |
expect_equal(max(fs_bytes(c(1, 2))), new_fs_bytes(2)) |
| 73 |
expect_equal(max(fs_bytes(c(1, NA))), new_fs_bytes(NA_real_)) |
| 74 |
}) |
| 75 |
}) |
| 76 |
|
| 77 |
describe("[.fs_bytes", { |
| 78 |
it("retains the fs_bytes class", { |
| 79 |
x <- fs_bytes(c(100, 200, 300)) |
| 80 |
expect_equal(x[], x) |
| 81 |
expect_equal(x[1], new_fs_bytes(100)) |
| 82 |
expect_equal(x[1:2], new_fs_bytes(c(100, 200))) |
| 83 |
}) |
| 84 |
}) |
| 85 |
|
| 86 |
describe("[[.fs_bytes", { |
| 87 |
it("retains the fs_bytes class", { |
| 88 |
x <- fs_bytes(c(100, 200, 300)) |
| 89 |
expect_equal(x[[1]], new_fs_bytes(100)) |
| 90 |
}) |
| 91 |
}) |
| 92 |
|
| 93 |
describe("Ops.fs_bytes", { |
| 94 |
it("errors for unary operators", { |
| 95 |
x <- fs_bytes(c(100, 200, 300)) |
| 96 |
expect_snapshot(error = TRUE, { |
| 97 |
!x |
| 98 |
+x |
| 99 |
-x |
| 100 |
}) |
| 101 |
}) |
| 102 |
|
| 103 |
it("works with boolean comparison operators", { |
| 104 |
x <- fs_bytes(c(100, 200, 300)) |
| 105 |
|
| 106 |
expect_equal(x == 100, c(TRUE, FALSE, FALSE)) |
| 107 |
expect_equal(x != 100, c(FALSE, TRUE, TRUE)) |
| 108 |
expect_equal(x > 100, c(FALSE, TRUE, TRUE)) |
| 109 |
expect_equal(x >= 100, c(TRUE, TRUE, TRUE)) |
| 110 |
expect_equal(x < 200, c(TRUE, FALSE, FALSE)) |
| 111 |
expect_equal(x <= 200, c(TRUE, TRUE, FALSE)) |
| 112 |
}) |
| 113 |
|
| 114 |
it("works with arithmetic operators", { |
| 115 |
x <- fs_bytes(c(100, 200, 300)) |
| 116 |
|
| 117 |
expect_equal(x + 100, fs_bytes(c(200, 300, 400))) |
| 118 |
expect_equal(x - 100, fs_bytes(c(0, 100, 200))) |
| 119 |
expect_equal(x * 100, fs_bytes(c(10000, 20000, 30000))) |
| 120 |
expect_equal(x / 2, fs_bytes(c(50, 100, 150))) |
| 121 |
expect_equal(x^2, fs_bytes(c(10000, 40000, 90000))) |
| 122 |
}) |
| 123 |
|
| 124 |
it("errors for other binary operators", { |
| 125 |
x <- fs_bytes(c(100, 200, 300)) |
| 126 |
expect_snapshot(error = TRUE, { |
| 127 |
x %% 2 |
| 128 |
x %/% 2 |
| 129 |
x & TRUE |
| 130 |
x | TRUE |
| 131 |
}) |
| 132 |
}) |
| 133 |
}) |
| 134 |
|
| 135 |
test_that("common type of double and fs_bytes is fs_bytes", { |
| 136 |
expect_identical( |
| 137 |
vctrs::vec_ptype2(double(), fs_bytes()), |
| 138 |
fs_bytes()[0] |
| 139 |
) |
| 140 |
expect_identical( |
| 141 |
vctrs::vec_ptype2(fs_bytes(), double()), |
| 142 |
fs_bytes()[0] |
| 143 |
) |
| 144 |
}) |
| 145 |
|
| 146 |
test_that("fs_bytes and double are coercible", { |
| 147 |
expect_identical( |
| 148 |
vctrs::vec_cast(1, fs_bytes()), |
| 149 |
fs_bytes(1) |
| 150 |
) |
| 151 |
expect_identical( |
| 152 |
vctrs::vec_cast(fs_bytes(1), double()), |
| 153 |
1 |
| 154 |
) |
| 155 |
expect_identical( |
| 156 |
vctrs::vec_cast(fs_bytes(1), fs_bytes()), |
| 157 |
fs_bytes(1) |
| 158 |
) |
| 159 |
}) |
| 160 |
|
| 161 |
test_that("can concatenate fs_bytes", { |
| 162 |
expect_identical( |
| 163 |
vctrs::vec_c(fs_bytes(1), fs_bytes(2)), |
| 164 |
as_fs_bytes(c(1, 2)) |
| 165 |
) |
| 166 |
}) |
| 167 |
|
| 168 |
test_that("common type of integer and fs_bytes is fs_bytes", { |
| 169 |
expect_identical( |
| 170 |
vctrs::vec_ptype2(integer(), fs_bytes()), |
| 171 |
fs_bytes()[0] |
| 172 |
) |
| 173 |
expect_identical( |
| 174 |
vctrs::vec_ptype2(fs_bytes(), integer()), |
| 175 |
fs_bytes()[0] |
| 176 |
) |
| 177 |
}) |
| 178 |
|
| 179 |
test_that("fs_bytes and integer are coercible", { |
| 180 |
expect_identical( |
| 181 |
vctrs::vec_cast(1L, fs_bytes()), |
| 182 |
fs_bytes(1L) |
| 183 |
) |
| 184 |
expect_identical( |
| 185 |
vctrs::vec_cast(fs_bytes(1L), integer()), |
| 186 |
1L |
| 187 |
) |
| 188 |
}) |