1
2context("Pretty milliseconds")
3
4test_that("pretty_ms works", {
5
6  expect_equal(pretty_ms(0), '0ms')
7  expect_equal(pretty_ms(0.1), '1ms')
8  expect_equal(pretty_ms(1), '1ms')
9  expect_equal(pretty_ms(1000 + 400), '1.4s')
10  expect_equal(pretty_ms(1000 * 2 + 400), '2.4s')
11  expect_equal(pretty_ms(1000 * 55), '55s')
12  expect_equal(pretty_ms(1000 * 67), '1m 7s')
13  expect_equal(pretty_ms(1000 * 60 * 5), '5m')
14  expect_equal(pretty_ms(1000 * 60 * 67), '1h 7m')
15  expect_equal(pretty_ms(1000 * 60 * 60 * 12), '12h')
16  expect_equal(pretty_ms(1000 * 60 * 60 * 40), '1d 16h')
17  expect_equal(pretty_ms(1000 * 60 * 60 * 999), '41d 15h')
18
19})
20
21test_that("compact pretty_ms works", {
22
23  expect_equal(pretty_ms(1000 + 4, compact = TRUE), '~1s')
24  expect_equal(pretty_ms(1000 * 60 * 60 * 999, compact =  TRUE), '~41d')
25
26})
27
28test_that("pretty_ms handles vectors", {
29
30  v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
31         1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
32         1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999)
33  v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
34          "5m", "1h 7m", "12h", "1d 16h", "41d 15h")
35
36  expect_equal(pretty_ms(v), v2)
37})
38
39context("Pretty seconds")
40
41test_that("pretty_sec works", {
42
43  expect_equal(pretty_sec(0 / 1000), '0ms')
44  expect_equal(pretty_sec(0.1 / 1000), '1ms')
45  expect_equal(pretty_sec(1 / 1000), '1ms')
46  expect_equal(pretty_sec((1000 + 400) / 1000), '1.4s')
47  expect_equal(pretty_sec((1000 * 2 + 400) / 1000), '2.4s')
48  expect_equal(pretty_sec(1000 * 55 / 1000), '55s')
49  expect_equal(pretty_sec(1000 * 67 / 1000), '1m 7s')
50  expect_equal(pretty_sec(1000 * 60 * 5 / 1000), '5m')
51  expect_equal(pretty_sec(1000 * 60 * 67 / 1000), '1h 7m')
52  expect_equal(pretty_sec(1000 * 60 * 60 * 12 / 1000), '12h')
53  expect_equal(pretty_sec(1000 * 60 * 60 * 40 / 1000), '1d 16h')
54  expect_equal(pretty_sec(1000 * 60 * 60 * 999 / 1000), '41d 15h')
55
56})
57
58test_that("compact pretty_sec works", {
59
60  expect_equal(pretty_sec((1000 + 4) / 1000, compact = TRUE), '~1s')
61  expect_equal(pretty_sec(1000 * 60 * 60 * 999 / 1000, compact =  TRUE), '~41d')
62
63})
64
65test_that("pretty_sec handles vectors", {
66
67  v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
68         1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
69         1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999) / 1000
70  v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
71          "5m", "1h 7m", "12h", "1d 16h", "41d 15h")
72
73  expect_equal(pretty_sec(v), v2)
74})
75
76context("Pretty dt")
77
78test_that("pretty_dt works", {
79
80  expect_equal(pretty_dt(as.difftime(units = "secs", 0 / 1000)), '0ms')
81  expect_equal(pretty_dt(as.difftime(units = "secs", 0.1 / 1000)), '1ms')
82  expect_equal(pretty_dt(as.difftime(units = "secs", 1 / 1000)), '1ms')
83  expect_equal(pretty_dt(as.difftime(units = "secs", (1000 + 400) / 1000)), '1.4s')
84  expect_equal(pretty_dt(as.difftime(units = "secs", (1000 * 2 + 400) / 1000)), '2.4s')
85  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 55 / 1000)), '55s')
86  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 67 / 1000)), '1m 7s')
87  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 5 / 1000)), '5m')
88  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 67 / 1000)), '1h 7m')
89  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 12 / 1000)), '12h')
90  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 40 / 1000)), '1d 16h')
91  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 999 / 1000)), '41d 15h')
92
93})
94
95test_that("compact pretty_dt works", {
96
97  expect_equal(pretty_dt(as.difftime(units = "secs", (1000 + 4) / 1000), compact = TRUE), '~1s')
98  expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 999 / 1000), compact =  TRUE), '~41d')
99
100})
101
102test_that("pretty_dt handles vectors", {
103
104  v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
105         1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
106         1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999) / 1000
107  v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
108          "5m", "1h 7m", "12h", "1d 16h", "41d 15h")
109
110  expect_equal(pretty_dt(as.difftime(units = "secs", v)), v2)
111})
112
113
114test_that("pretty_dt works with NAs", {
115
116  stime <- Sys.time()
117  v  <- .difftime(c(difftime(NA, NA), difftime(stime + 1, stime)), "secs")
118  v2 <- c(NA_character_, "~1s")
119  v3 <- c(NA_character_, "1s")
120
121  expect_equal(pretty_dt(v, compact = TRUE), v2)
122  expect_equal(pretty_dt(v, compact = FALSE), v3)
123
124})
125