1context("stat_density") # and stat_ydensity
2
3test_that("compute_density succeeds when variance is zero", {
4  dens <- compute_density(rep(0, 10), NULL, from = 0.5, to = 0.5)
5  expect_equal(dens$n, rep(10, 512))
6})
7
8test_that("stat_density works in both directions", {
9  p <- ggplot(mpg, aes(hwy)) + stat_density()
10  x <- layer_data(p)
11  expect_false(x$flipped_aes[1])
12
13  p <- ggplot(mpg, aes(y = hwy)) + stat_density()
14  y <- layer_data(p)
15  expect_true(y$flipped_aes[1])
16
17  x$flipped_aes <- NULL
18  y$flipped_aes <- NULL
19  expect_identical(x, flip_data(y, TRUE)[,names(x)])
20})
21
22test_that("compute_density returns useful df and throws warning when <2 values", {
23  expect_warning(dens <- compute_density(1, NULL, from = 0, to = 0))
24
25  expect_equal(nrow(dens), 1)
26  expect_equal(names(dens), c("x", "density", "scaled", "ndensity", "count", "n"))
27  expect_type(dens$x, "double")
28})
29