0
  6   坙栞m_J	򳅯2燵��阞芽慆D]Y筟~27%憼?�儁悋吐�  w    P <- structure(
  list(difftime = structure(0, units = "secs", class = "difftime"),
       frequency = 0,
       start = structure(.POSIXct(1, "UTC"), tclass = c("POSIXct", "POSIXt")),
       end = structure(.POSIXct(1, "UTC"), tclass = c("POSIXct", "POSIXt")),
       units = "secs",
       scale = "seconds",
       label = "second"),
  class = "periodicity")

test_date <- as.Date("2022-10-15")

info_msg <- "test.periodicity_on_one_observation_warns"
x <- xts(1, .POSIXct(1, "UTC"))
suppressWarnings(p <- periodicity(x))
expect_identical(p, P, info = info_msg)
expect_warning(p <- periodicity(x), info = info_msg)

info_msg <- "test.periodicity_on_zero_observations_warns"
x <- xts(, .POSIXct(numeric(0), "UTC"))
suppressWarnings(p <- periodicity(x))
P$start <- NA
P$end <- NA
expect_identical(p, P, info = info_msg)
expect_warning(p <- periodicity(x))

check_periodicity_result <-
function(p, units, scale, freq, msg)
{
  info_msg <- paste0(msg, " - units: ", p$units, ", expected: ", units)
  expect_equivalent(p$units, units, info = info_msg)

  info_msg <- paste0(msg, " - scale: ", p$scale, ", expected: ", scale)
  expect_equivalent(p$scale, scale, info = info_msg)

  info_msg <- paste0(msg, " - frequency: ", p$frequency, ", expected: ", freq)
  expect_equivalent(p$frequency, freq, info = info_msg)

  info_msg <- paste0(msg, " - difftime: ", p$difftime, ", expected: ", freq)
  expect_equivalent(as.numeric(p$difftime), freq, info = info_msg)

  invisible(NULL)
}

###############################################################################
info_msg <- "test.periodicity_on_sub_second_data"
set.seed(Sys.Date())
for (i in 1:100) {
  n <- sample(1000, 1) / 1000
  #if (n >= eps) n <- 1
  p <- periodicity(.xts(seq_len(100), n * seq_len(100)))
  check_periodicity_result(p, "secs", "seconds", n, info_msg)
}

# test periodicity between 0.95 and 1, which should round up to 1
#set.seed(Sys.Date())
#for (n in seq(0.9505, 0.99, 0.005)) {
#  p <- periodicity(.xts(seq_len(100), n * seq_len(100)))
#  check_periodicity_result(p, "secs", "seconds", n, info_msg)
#}

###############################################################################
info_msg <- "test.periodicity_on_second_data"
i <- seq_len(100)
for (n in 1:59) {
  p <- periodicity(.xts(i, i))
  check_periodicity_result(p, "secs", "seconds", 1, info_msg)
}

###############################################################################
info_msg <- "test.periodicity_on_minute_data"
i <- seq_len(100) * 60

for (n in 1:59) {
  p <- periodicity(.xts(i, n * i))
  check_periodicity_result(p, "mins", "minute", n, info_msg)
}

###############################################################################
info_msg <- "test.periodicity_on_hourly_data"
i <- seq_len(100) * 3600

for (n in 1:23) {
  p <- periodicity(.xts(i, n * i))
  # NOTE: frequency is in seconds for hourly data (see #54)
  check_periodicity_result(p, "hours", "hourly", n * 3600, info_msg)
}

###############################################################################
info_msg <- "test.periodicity_on_daily_data"
i <- seq_len(100) * 86400

# NOTE: frequency is in seconds for daily data (see #54)
n <- 1
p <- periodicity(.xts(i, n * i))
check_periodicity_result(p, "days", "daily", n * 86400, info_msg)

n <- 2
p <- periodicity(.xts(i, n * i))
check_periodicity_result(p, "days", "weekly", n * 86400, info_msg)

n <- 3
p <- periodicity(.xts(i, n * i))
check_periodicity_result(p, "days", "weekly", n * 86400, info_msg)

###############################################################################
info_msg <- "test.periodicity_on_weekly_data"
i <- 7 * seq_len(100) * 86400

# NOTE: frequency is in seconds for weekly data (see #54)
n <- 1
p <- periodicity(.xts(i, n * i))
check_periodicity_result(p, "days", "weekly", n * 86400 * 7, info_msg)

n <- 2
p <- periodicity(.xts(i, n * i))
check_periodicity_result(p, "days", "monthly", n * 86400 * 7, info_msg)

n <- 3
p <- periodicity(.xts(i, n * i))
check_periodicity_result(p, "days", "monthly", n * 86400 * 7, info_msg)