1# vec_identify_runs ------------------------------------------------------------
2
3test_that("vec_identify_runs() works with size zero input", {
4  expect <- structure(integer(), n = 0L)
5
6  expect_identical(vec_identify_runs(integer()), expect)
7  expect_identical(vec_identify_runs(data.frame()), expect)
8})
9
10test_that("works with atomic input of various types", {
11  expect <- structure(c(1L, 1L, 2L, 2L, 3L), n = 3L)
12
13  expect_identical(vec_identify_runs(c(TRUE, TRUE, FALSE, FALSE, TRUE)), expect)
14  expect_identical(vec_identify_runs(c(1L, 1L, 2L, 2L, 3L)), expect)
15  expect_identical(vec_identify_runs(c(1, 1, 2, 2, 3)), expect)
16  expect_identical(vec_identify_runs(complex(real = c(1, 1, 2, 2, 2), imaginary = c(1, 1, 2, 2, 3))), expect)
17  expect_identical(vec_identify_runs(c("a", "a", "b", "b", "c")), expect)
18  expect_identical(vec_identify_runs(as.raw(c(1, 1, 2, 2, 3))), expect)
19  expect_identical(vec_identify_runs(list(1, 1, 2, 2, 3)), expect)
20})
21
22test_that("NA values are identical", {
23  expect <- structure(c(1L, 1L), n = 1L)
24
25  expect_identical(vec_identify_runs(c(NA, NA)), expect)
26  expect_identical(vec_identify_runs(c(NA_integer_, NA_integer_)), expect)
27  expect_identical(vec_identify_runs(c(NA_real_, NA_real_)), expect)
28  expect_identical(vec_identify_runs(c(NA_complex_, NA_complex_)), expect)
29  expect_identical(vec_identify_runs(c(NA_character_, NA_character_)), expect)
30  # No NA type for raw
31  expect_identical(vec_identify_runs(list(NULL, NULL)), expect)
32})
33
34test_that("NA and NaN are different", {
35  expect <- structure(c(1L, 2L), n = 2L)
36  expect_identical(vec_identify_runs(c(NA_real_, NaN)), expect)
37})
38
39test_that("normalizes character encodings", {
40  encs <- encodings()
41  x <- c(encs$utf8, encs$unknown, encs$latin1)
42  expect_identical(vec_identify_runs(x), structure(rep(1L, 3), n = 1L))
43})
44
45test_that("errors on scalars", {
46  expect_error(vec_identify_runs(foobar()), class = "vctrs_error_scalar_type")
47})
48
49test_that("works with data frames rowwise", {
50  df <- data_frame(x = c(1, 1, 1, 2), y = c(1, 1, 2, 3))
51  expect <- structure(c(1L, 1L, 2L, 3L), n = 3L)
52  expect_identical(vec_identify_runs(df), expect)
53
54  df <- data_frame(x = c(1, 1, 1), y = c(2, 2, 2), z = c("b", "a", "a"))
55  expect <- structure(c(1L, 2L, 2L), n = 2L)
56  expect_identical(vec_identify_runs(df), expect)
57})
58
59test_that("works with data frames with rows but no columns", {
60  expect <- structure(rep(1L, 5), n = 1L)
61  expect_identical(vec_identify_runs(new_data_frame(n = 5L)), expect)
62})
63
64test_that("works with data frame columns", {
65  col <- data_frame(a = c(1, 1, 2, 2), b = c(1, 2, 3, 3))
66  df <- data_frame(x = rep(1, 4), y = col)
67  expect <- structure(c(1L, 2L, 3L, 3L), n = 3L)
68  expect_identical(vec_identify_runs(df), expect)
69})
70
71test_that("works with columns of various types", {
72  # Use two columns to keep the data frame from being squashed to a vector
73  add_col <- function(col) {
74    x <- rep(1L, 5)
75    data_frame(x = x, y = col)
76  }
77
78  expect <- structure(c(1L, 1L, 2L, 2L, 3L), n = 3L)
79
80  expect_identical(vec_identify_runs(add_col(c(TRUE, TRUE, FALSE, FALSE, TRUE))), expect)
81  expect_identical(vec_identify_runs(add_col(c(1L, 1L, 2L, 2L, 3L))), expect)
82  expect_identical(vec_identify_runs(add_col(c(1, 1, 2, 2, 3))), expect)
83  expect_identical(vec_identify_runs(add_col(complex(real = c(1, 1, 2, 2, 2), imaginary = c(1, 1, 2, 2, 3)))), expect)
84  expect_identical(vec_identify_runs(add_col(c("a", "a", "b", "b", "c"))), expect)
85  expect_identical(vec_identify_runs(add_col(as.raw(c(1, 1, 2, 2, 3)))), expect)
86  expect_identical(vec_identify_runs(add_col(list(1, 1, 2, 2, 3))), expect)
87})
88
89# vec_locate_runs --------------------------------------------------------------
90
91test_that("can locate run starts", {
92  expect_identical(
93    vec_locate_runs(c(1, 3, 3, 1, 5, 5, 6)),
94    c(1L, 2L, 4L, 5L, 7L)
95  )
96})
97
98test_that("can locate run ends", {
99  expect_identical(
100    vec_locate_runs(c(1, 3, 3, 1, 5, 5, 6), start = FALSE),
101    c(1L, 3L, 4L, 6L, 7L)
102  )
103})
104
105test_that("vec_locate_runs() works with size zero input", {
106  expect_identical(vec_locate_runs(integer(), start = TRUE), integer())
107  expect_identical(vec_locate_runs(integer(), start = FALSE), integer())
108})
109
110test_that("vec_locate_runs() validates `start`", {
111  expect_error(vec_locate_runs(1, start = "x"), "single `TRUE` or `FALSE`")
112  expect_error(vec_locate_runs(1, start = NA), "single `TRUE` or `FALSE`")
113  expect_error(vec_locate_runs(1, start = c(TRUE, TRUE)), "single `TRUE` or `FALSE`")
114})
115
116# vec_detect_runs --------------------------------------------------------------
117
118test_that("can detect run starts", {
119  expect_identical(
120    vec_detect_runs(c(1, 3, 3, 1, 5, 5, 6)),
121    c(TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE)
122  )
123})
124
125test_that("can detect run ends", {
126  expect_identical(
127    vec_detect_runs(c(1, 3, 3, 1, 5, 5, 6), start = FALSE),
128    c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE)
129  )
130})
131
132test_that("vec_detect_runs() works with size zero input", {
133  expect_identical(vec_detect_runs(integer(), start = TRUE), logical())
134  expect_identical(vec_detect_runs(integer(), start = FALSE), logical())
135})
136
137test_that("vec_detect_runs() validates `start`", {
138  expect_error(vec_detect_runs(1, start = "x"), "single `TRUE` or `FALSE`")
139  expect_error(vec_detect_runs(1, start = NA), "single `TRUE` or `FALSE`")
140  expect_error(vec_detect_runs(1, start = c(TRUE, TRUE)), "single `TRUE` or `FALSE`")
141})
142