tests/testthat/test-math.R

# Tests for Math group generics (round, floor, ceiling) on time objects.
#
# These operations apply to the inner numeric value of the time object,
# preserving the time class and its attributes.

# ---------------------------------------------------------------------------
# round / floor / ceiling on durations
# ---------------------------------------------------------------------------

test_that("round() on a duration rounds the inner value", {
  expect_equal(as.numeric(round(days(1.3))),  1)
  expect_equal(as.numeric(round(days(1.5))),  2)
  expect_equal(as.numeric(round(days(1.7))),  2)
  expect_equal(as.numeric(round(days(-1.3))), -1)
})

test_that("round() on a duration preserves the time class", {
  result <- round(days(1.3))
  expect_true(is_mixtime(result))
})

test_that("floor() on a duration floors the inner value", {
  expect_equal(as.numeric(floor(days(1.3))),  1)
  expect_equal(as.numeric(floor(days(1.9))),  1)
  expect_equal(as.numeric(floor(days(-1.3))), -2)
})

test_that("floor() on a duration preserves the time class", {
  result <- floor(days(1.3))
  expect_true(is_mixtime(result))
})

test_that("ceiling() on a duration ceils the inner value", {
  expect_equal(as.numeric(ceiling(days(1.1))),  2)
  expect_equal(as.numeric(ceiling(days(1.0))),  1)
  expect_equal(as.numeric(ceiling(days(-1.3))), -1)
})

test_that("ceiling() on a duration preserves the time class", {
  result <- ceiling(days(1.3))
  expect_true(is_mixtime(result))
})

# ---------------------------------------------------------------------------
# round / floor / ceiling on linear time
# ---------------------------------------------------------------------------

test_that("round() on a linear time rounds the inner value", {
  expect_equal(as.numeric(round(yearmonth(1.3))),  1)
  expect_equal(as.numeric(round(yearmonth(1.5))),  2)
  expect_equal(as.numeric(round(yearmonth(1.7))),  2)
})

test_that("round() on a linear time preserves the time class", {
  result <- round(yearmonth(1.3))
  expect_true(is_mixtime(result))
})

test_that("floor() on a linear time floors the inner value", {
  expect_equal(as.numeric(floor(yearmonth(1.3))), 1)
  expect_equal(as.numeric(floor(yearmonth(1.9))), 1)
})

test_that("ceiling() on a linear time ceils the inner value", {
  expect_equal(as.numeric(ceiling(yearmonth(1.1))), 2)
  expect_equal(as.numeric(ceiling(yearmonth(1.0))), 1)
})

# ---------------------------------------------------------------------------
# round() with digits argument
# ---------------------------------------------------------------------------

test_that("round() respects the digits argument for durations", {
  expect_equal(as.numeric(round(days(1.567), digits = 1)), 1.6)
  expect_equal(as.numeric(round(days(1.567), digits = 2)), 1.57)
})

# ---------------------------------------------------------------------------
# Summary group generics (min, max, range)
# ---------------------------------------------------------------------------

test_that("min(), max() and range() preserve the time class", {
  x <- yearmonth(c("2014 Feb", "2013 Jan", "2013 Jun"))
  expect_true(is_mixtime(max(x)))
  expect_equal(format(max(x)), "2014 Feb")
  expect_equal(format(min(x)), "2013 Jan")
  expect_equal(format(range(x)), c("2013 Jan", "2014 Feb"))
  expect_equal(format(max(x, na.rm = TRUE)), "2014 Feb")
})

test_that("max() result supports further arithmetic", {
  x <- yearmonth(c("2013 Jan", "2014 Feb"))
  expect_equal(format(max(x) - 11L), "2013 Mar")
})

Try the mixtime package in your browser

Any scripts or data that you put into this service are public.

mixtime documentation built on Nov. 18, 2026, 5:08 p.m.