1test_that("auto units always rounds down", { 2 expect_equal(label_bytes()(1000^(1:3)), c("1 kB", "1 MB", "1 GB")) 3}) 4 5test_that("auto units handles 0 and other special values", { 6 expect_equal(label_bytes()(NA), NA_character_) 7 expect_equal(label_bytes()(0), "0 B") 8 expect_equal(label_bytes()(-1), "-1 B") 9 expect_equal(label_bytes()(Inf), "Inf") 10}) 11 12test_that("can use either binary or si units", { 13 expect_equal(label_bytes("kB")(1000), "1 kB") 14 expect_equal(label_bytes("kiB")(1024), "1 kiB") 15}) 16 17test_that("errors if unknown unit", { 18 expect_error(label_bytes("unit")(0), "valid unit") 19}) 20 21# deprecated interface ---------------------------------------------------- 22 23test_that("Byte formatter can take a symbol designator", { 24 expect_equal( 25 number_bytes(c(50, 400, 502, NA), symbol = "B"), 26 c("50 B", "400 B", "502 B", NA) 27 ) 28 expect_equal( 29 number_bytes(3:5 * 1024^2, symbol = "MiB"), 30 c("3 MiB", "4 MiB", "5 MiB") 31 ) 32 expect_equal( 33 number_bytes(1000^(1:3), symbol = "kB", units = "si"), 34 c("1 kB", "1 000 kB", "1 000 000 kB") 35 ) 36 37 # informative warning for incorrect spelling 38 expect_warning(number_bytes(c(50, 400, 502, NA), symbol = "k"), "must be") 39 40 # respects unit designation 41 expect_equal(number_bytes(1024, accuracy = .01), c("1.00 KiB")) 42 expect_equal(number_bytes(1024, units = "si", accuracy = .01), c("1.02 kB")) 43 expect_equal(number_bytes(1000, units = "si", accuracy = .01), c("1.00 kB")) 44 45 # takes parameters from number() 46 expect_equal( 47 number_bytes(c(3e6, 4e6, 5e6), accuracy = .001), 48 c("2.861 MiB", "3.815 MiB", "4.768 MiB") 49 ) 50 expect_equal( 51 number_bytes(c(3e6, 4e6, 5e6), units = "si", accuracy = .1), 52 c("3.0 MB", "4.0 MB", "5.0 MB") 53 ) 54 55 # unit system is enforced 56 expect_warning(number_bytes(1024^(1:2), "kB", units = "binary"), "KiB") 57 expect_warning(number_bytes(1024^(1:2), "KiB", units = "si"), "kB") 58}) 59 60test_that("Byte formatter handles zero values", { 61 expect_equal(number_bytes(0), "0 B") 62}) 63 64test_that("Byte formatter handles large values", { 65 expect_equal(number_bytes(1024^11), "1 073 741 824 YiB") 66 expect_equal(number_bytes(1000^9, units = "si"), "1 000 YB") 67}) 68 69test_that("Byte formatter handles negative values", { 70 expect_equal(number_bytes(-1024^2), "-1 MiB") 71}) 72 73test_that('Byte formatter symbol = "auto" can show variable multiples', { 74 expect_equal(number_bytes(1024^(1:3)), c("1 KiB", "1 MiB", "1 GiB")) 75}) 76 77test_that("Byte formatter throws informative error for wrong length symbol", { 78 expect_error(number_bytes(symbol = character()), "not length 0") 79 expect_error(number_bytes(symbol = c("kB", "MB")), "not length 2") 80}) 81 82test_that("preserves names", { 83 expect_named(number_bytes(c(a = 1)), "a") 84}) 85