Debian Codegraph @

tests/testthat/test-stats.R

raw
1 test_that("inline_linegraph returns expected response for a ts vector", {
2 input <- inline_linegraph(freeny$y)
3 expect_identical(input, "⣀⣀⠤⠤⠒⠒⠉⠉")
4 })
5
6 test_that("inline_linegraph returns expected response for a long ts vector.", {
7 input <- inline_linegraph(AirPassengers)
8 expect_identical(input, "⣀⣀⣀⠔⠤⠊⠑⠊")
9 })
10
11 test_that("inline_linegraph returns expected response for an NA ts vector.", {
12 input <- inline_linegraph(ts(c(NA, NA, NA, NA, NA, NA)))
13 expect_identical(input, "⠀")
14 })
15
16 test_that("ts_start returns expected response for a ts vector", {
17 input <- ts_start(freeny$y)
18 expect_identical(input, 1962)
19 })
20
21 test_that("ts_start returns expected response for a ts vector", {
22 input <- ts_end(freeny$y)
23 expect_identical(input, 1971)
24 })
25
26 test_that("n_missing is calculated correctly.", {
27 data <- c("a", "b", "c", NA, " ")
28 input <- n_missing(data)
29 expect_identical(input, 1L)
30 })
31
32 test_that("n_complete is calculated correctly.", {
33 data <- c("a", "b", "c", NA)
34 input <- n_complete(data)
35 expect_identical(input, 3L)
36 })
37
38 test_that("complete_rate is calculated correctly.", {
39 data <- c("a", "b", "c", NA, " ")
40 input <- complete_rate(data)
41 expect_equal(input, .8, tolerance = .001)
42 })
43
44 test_that("n_whitespace is calculated correctly.", {
45 data <- c("a", "b", "c", NA, " ", " a", " ", " a")
46 input <- n_whitespace(data)
47 expect_identical(input, 2L)
48 })
49
50 test_that("inline histogram is calculated correctly.", {
51 input <- inline_hist(iris$Sepal.Length)
52 expect_identical(input, "▂▇▅▇▆▅▂▂")
53 })
54
55 test_that("inline histogram is calculated correctly when x is all zeros.", {
56 input <- inline_hist(numeric(10))
57 expect_identical(input, "▁▁▁▇▁▁▁▁")
58 })
59
60 test_that("inline histogram returns an empty string when x is length 0.", {
61 input <- inline_hist(numeric(0))
62 expect_identical(input, " ")
63 })
64
65 test_that("inline hist is calculated correctly when x is all zeores or NAs", {
66 input <- inline_hist(as.numeric(c(NA, NA, NA, 0, 0)))
67 expect_identical(input, "▁▁▁▇▁▁▁▁")
68 })
69
70 test_that("inline histogram is calculated correctly when x is all 1s.", {
71 input <- inline_hist(c(1, 1, 1, 1, 1, 1))
72 expect_identical(input, "▁▁▁▇▁▁▁▁")
73 })
74
75 test_that("inline histogram returns empty string when x is all NAs.", {
76 input <- inline_hist(as.numeric(rep(NA, 10)))
77 expect_identical(input, " ")
78 })
79
80 test_that("inline histogram is returns empty string when x is all NaN.", {
81 input <- inline_hist(rep(NaN, 10))
82 expect_identical(input, " ")
83 })
84
85 test_that("inline histogram is correct when x is evenly distributed.", {
86 input <- inline_hist(c(1, 2, 3, 4, 5, 6, 7, 8))
87 expect_identical(input, "▇▇▇▇▇▇▇▇")
88 })
89
90 test_that("inline histogram is calculated correctly with NaN.", {
91 input <- inline_hist(c(1, 2, 3, 3, 6, 6, 6, 8, NaN))
92 expect_identical(input, "▂▂▅▁▁▇▁▂")
93 })
94
95 test_that("inline histogram is calculated correctly with NA.", {
96 input <- inline_hist(c(1, 2, 3, 3, 6, 6, 6, 8, NA))
97 expect_identical(input, "▂▂▅▁▁▇▁▂")
98 })
99
100 test_that("inline histogram is calculated correctly with Inf.", {
101 expect_warning(input <-
102 inline_hist(c(1, 2, 3, 3, 6, 6, 6, 8, Inf, -Inf)))
103 expect_identical(input, "▂▂▅▁▁▇▁▂")
104 })
105
106 test_that("n_empty is calculated correctly.", {
107 data <- c("a", "ab", "abc", "")
108 correct <- as.integer(1)
109 input <- n_empty(data)
110 expect_identical(input, correct)
111 })
112
113 test_that("min_char is calculated correctly, including empty strings.", {
114 data <- c("a", "ab", "abc", "")
115 correct <- as.integer(0)
116 input <- min_char(data)
117 expect_identical(input, correct)
118 })
119
120 test_that("min_char with a multibyte character does not throw an error.", {
121 data <- c("a", "ab", "abc", "Coleophora asteris M\x9fhl.")
122 expect_error(min_char(data), NA)
123 })
124
125 test_that("min_char returns NA when there are only NA values.", {
126 data <- as.character(c(NA, NA, NA, NA))
127 input <- min_char(data)
128 expect_equal(input, NA)
129 })
130
131 test_that("max_char is calculated correctly, including empty strings.", {
132 data <- c("a", "ab", "abc", "")
133 correct <- as.integer(3)
134 input <- max_char(data)
135 expect_identical(input, correct)
136 })
137
138 test_that("max_char with a multibyte character does not throw an error.", {
139 data <- c("a", "ab", "abc", "Coleophora asteris M\x9fhl.")
140 # correct <- as.integer(3)
141 # input <- max_char(data)
142 # expect_identical(input, correct)
143 expect_error(max_char(data, NA))
144 })
145
146 test_that("max_char returns NA when there are only NA values.", {
147 data <- as.character(c(NA, NA, NA, NA))
148 input <- max_char(data)
149 expect_equal(input, NA)
150 })
151
152 test_that("n_unique is calculated correctly.", {
153 correct <- 4L
154 data <- c("a", "ab", "abc", "")
155 input <- n_unique(data)
156 expect_identical(input, correct)
157 })
158
159 test_that("n_unique handles NA as expected.", {
160 correct <- 4L
161 data <- c("a", "ab", "abc", "", NA)
162 input <- n_unique(data)
163 expect_identical(input, correct)
164 })
165
166 test_that("list_lengths_min is calculated correctly.", {
167 dat <- list(
168 list("a", "b", "c"),
169 list("d", "b", "d"),
170 list("e", "f", "g"),
171 d = list("h"),
172 e = list("i", "j", "k", "l"),
173 f = NA
174 )
175 expect_identical(list_lengths_min(dat), 1L)
176 })
177
178 test_that("list_lengths_max is calculated correctly.", {
179 dat <- list(
180 list("a", "b", "c"),
181 list("d", "b", "d"),
182 list("e", "f", "g"),
183 d = list("h"),
184 e = list("i", "j", "k", "l"),
185 f = NA
186 )
187 expect_identical(list_lengths_max(dat), 4L)
188 })
189
190 test_that("list_lengths_median is calculated correctly.", {
191 dat <- list(
192 list("a", "b", "c"),
193 list("d", "b", "d"),
194 list("e", "f", "g"),
195 d = list("h"),
196 e = list("i", "j", "k", "l"),
197 f = NA
198 )
199 expect_identical(list_lengths_median(dat), 3L)
200 })
201
202 test_that("list_min_length is calculated correctly.", {
203 dat <- I(list(5, 5:6, 5:10, NA))
204 expect_identical(list_min_length(dat), 1L)
205 })
206
207 test_that("list_max_length is calculated correctly.", {
208 dat <- I(list(5, 5:6, 5:10, NA))
209 expect_identical(list_max_length(dat), 6L)
210 })
211
212 test_that("sorted count is calculated correctly.", {
213 dat <- c("A", "A", "A", "B", "C", "C")
214 expect_equal(sorted_count(dat)[1:3], c("A" = 3, "C" = 2, "B" = 1))
215 expect_equal(names(sorted_count(dat)), c("A", "C", "B"))
216 })
217
218 test_that("top counts is calculated correctly", {
219 dat <- c("A", "A", "A", "B", "C", "C")
220 expect_equal(top_counts(dat), "A: 3, C: 2, B: 1")
221 })
222
223 test_that("sorted count is calculated correctly with a NA.", {
224 # NA should be sorted as if it is a regular value
225 dat <- c("A", "A", "A", "A", "B", NA, NA, "C", "C", "C")
226 expect_equal(unname(sorted_count(dat)), c(4, 3, 1))
227 expect_equal(names(sorted_count(dat)), c("A", "C", "B"))
228 })
229
230 test_that("top counts is calculated correctly with a NA", {
231 dat <- c("A", "A", "A", "B", "C", "C")
232 expect_equal(top_counts(dat), "A: 3, C: 2, B: 1")
233 })
234
235 test_that("sorted count is calculated correctly with \"\".", {
236 # \"\" should be converted to \"empty \" and a warning issued.
237 dat <- c("A", "A", "A", "A", "B", "", "", "C", "C", "C")
238 dat <- as.factor(dat)
239 expect_warning(expected <- sorted_count(dat))
240 expect_equal(unname(expected), c(4, 3, 2, 1))
241 expect_equal(names(expected), c("A", "C", "empty", "B"))
242 })