
vec_as_location() checks for mix of negative and missing locations
==================================================================

> vec_as_location(-c(1L, NA), 30)
Error: Must subset elements with a valid subscript vector.
x Negative locations can't have missing values.
i Subscript has a missing value at location 2.

> vec_as_location(-c(1L, rep(NA, 10)), 30)
Error: Must subset elements with a valid subscript vector.
x Negative locations can't have missing values.
i Subscript has 10 missing values at locations 2, 3, 4, 5, 6, etc.


vec_as_location() checks for mix of negative and positive locations
===================================================================

> vec_as_location(c(-1L, 1L), 30)
Error: Must subset elements with a valid subscript vector.
x Negative and positive locations can't be mixed.
i Subscript has a positive value at location 2.

> vec_as_location(c(-1L, rep(1L, 10)), 30)
Error: Must subset elements with a valid subscript vector.
x Negative and positive locations can't be mixed.
i Subscript has 10 positive values at locations 2, 3, 4, 5, 6, etc.


num_as_location() optionally forbids negative indices
=====================================================

> num_as_location(dbl(1, -1), 2L, negative = "error")
Error: Must subset elements with a valid subscript vector.
x Subscript can't contain negative locations.


num_as_location() optionally forbids zero indices
=================================================

> num_as_location(0L, 1L, zero = "error")
Error: Must subset elements with a valid subscript vector.
x Subscript can't contain `0` values.
i It has a `0` value at location 1.

> num_as_location(c(0, 0, 0, 0, 0, 0), 1, zero = "error")
Error: Must subset elements with a valid subscript vector.
x Subscript can't contain `0` values.
i It has 6 `0` values at locations 1, 2, 3, 4, 5, etc.


logical subscripts must match size of indexed vector
====================================================

> vec_as_location(c(TRUE, FALSE), 3)
Error: Must subset elements with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x Input has size 3 but subscript has size 2.


character subscripts require named vectors
==========================================

> vec_as_location(letters[1], 3)
Error: Can't use character names to index an unnamed vector.


vec_as_location() requires integer, character, or logical inputs
================================================================

> vec_as_location(mtcars, 10L)
Error: Must subset elements with a valid subscript vector.
x Subscript has the wrong type `data.frame<
  mpg : double
  cyl : double
  disp: double
  hp  : double
  drat: double
  wt  : double
  qsec: double
  vs  : double
  am  : double
  gear: double
  carb: double
>`.
i It must be logical, numeric, or character.

> vec_as_location(env(), 10L)
Error: Must subset elements with a valid subscript vector.
x Subscript has the wrong type `environment`.
i It must be logical, numeric, or character.

> vec_as_location(foobar(), 10L)
Error: Must subset elements with a valid subscript vector.
x Subscript has the wrong type `vctrs_foobar`.
i It must be logical, numeric, or character.

> vec_as_location(2.5, 3L)
Error: Must subset elements with a valid subscript vector.
x Can't convert from <double> to <integer> due to loss of precision.

> vec_as_location(list(), 10L)
Error: Must subset elements with a valid subscript vector.
x Subscript has the wrong type `list`.
i It must be logical, numeric, or character.

> vec_as_location(function() NULL, 10L)
Error: Must subset elements with a valid subscript vector.
x Subscript has the wrong type `function`.
i It must be logical, numeric, or character.

> vec_as_location(Sys.Date(), 3L)
Error: Must subset elements with a valid subscript vector.
x Subscript has the wrong type `date`.
i It must be logical, numeric, or character.

> # Idem with custom `arg`
> vec_as_location(env(), 10L, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Subscript `foo` has the wrong type `environment`.
i It must be logical, numeric, or character.

> vec_as_location(foobar(), 10L, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Subscript `foo` has the wrong type `vctrs_foobar`.
i It must be logical, numeric, or character.

> vec_as_location(2.5, 3L, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Can't convert from `foo` <double> to <integer> due to loss of precision.


vec_as_location2() requires integer or character inputs
=======================================================

> vec_as_location2(TRUE, 10L)
Error: Must extract element with a single valid subscript.
x Subscript has the wrong type `logical`.
i It must be numeric or character.

> vec_as_location2(mtcars, 10L)
Error: Must extract element with a single valid subscript.
x Subscript has the wrong type `data.frame<
  mpg : double
  cyl : double
  disp: double
  hp  : double
  drat: double
  wt  : double
  qsec: double
  vs  : double
  am  : double
  gear: double
  carb: double
>`.
i It must be numeric or character.

> vec_as_location2(env(), 10L)
Error: Must extract element with a single valid subscript.
x Subscript has the wrong type `environment`.
i It must be numeric or character.

> vec_as_location2(foobar(), 10L)
Error: Must extract element with a single valid subscript.
x Subscript has the wrong type `vctrs_foobar`.
i It must be numeric or character.

> vec_as_location2(2.5, 3L)
Error: Must extract element with a single valid subscript.
x Can't convert from <double> to <integer> due to loss of precision.

> vec_as_location2(Inf, 10L)
Error: Must extract element with a single valid subscript.
x Can't convert from <double> to <integer> due to loss of precision.

> vec_as_location2(-Inf, 10L)
Error: Must extract element with a single valid subscript.
x Can't convert from <double> to <integer> due to loss of precision.

> # Idem with custom `arg`
> vec_as_location2(foobar(), 10L, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` has the wrong type `vctrs_foobar`.
i It must be numeric or character.

> vec_as_location2(2.5, 3L, arg = "foo")
Error: Must extract element with a single valid subscript.
x Can't convert from `foo` <double> to <integer> due to loss of precision.

> with_tibble_rows(vec_as_location2(TRUE))
Error: Must remove row with a single valid subscript.
x Subscript `foo(bar)` has the wrong type `logical`.
i It must be numeric or character.


vec_as_location2() requires length 1 inputs
===========================================

> vec_as_location2(1:2, 2L)
Error: Must extract element with a single valid subscript.
x Subscript has size 2 but must be size 1.

> vec_as_location2(mtcars, 10L)
Error: Must extract element with a single valid subscript.
x Subscript has the wrong type `data.frame<
  mpg : double
  cyl : double
  disp: double
  hp  : double
  drat: double
  wt  : double
  qsec: double
  vs  : double
  am  : double
  gear: double
  carb: double
>`.
i It must be numeric or character.

> # Idem with custom `arg`
> vec_as_location2(1:2, 2L, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` has size 2 but must be size 1.

> vec_as_location2(mtcars, 10L, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` has the wrong type `data.frame<
  mpg : double
  cyl : double
  disp: double
  hp  : double
  drat: double
  wt  : double
  qsec: double
  vs  : double
  am  : double
  gear: double
  carb: double
>`.
i It must be numeric or character.

> vec_as_location2(1:2, 2L, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` has size 2 but must be size 1.


vec_as_location2() requires positive integers
=============================================

> vec_as_location2(0, 2L)
Error: Must extract element with a single valid subscript.
x Subscript has value 0 but must be a positive location.

> vec_as_location2(-1, 2L)
Error: Must extract element with a single valid subscript.
x Subscript has value -1 but must be a positive location.

> # Idem with custom `arg`
> vec_as_location2(0, 2L, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` has value 0 but must be a positive location.

> # vec_as_location2() fails with NA
> vec_as_location2(na_int, 2L)
Error: Must extract element with a single valid subscript.
x Subscript can't be `NA`.

> vec_as_location2(na_chr, 1L, names = "foo")
Error: Must extract element with a single valid subscript.
x Subscript can't be `NA`.

> # Idem with custom `arg`
> vec_as_location2(na_int, 2L, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` can't be `NA`.


vec_as_location() and variants check for OOB elements
=====================================================

> # Numeric subscripts
> vec_as_location(10L, 2L)
Error: Can't subset elements that don't exist.
x Location 10 doesn't exist.
i There are only 2 elements.

> vec_as_location(-10L, 2L)
Error: Can't negate elements that don't exist.
x Location 10 doesn't exist.
i There are only 2 elements.

> vec_as_location2(10L, 2L)
Error: Can't subset elements that don't exist.
x Location 10 doesn't exist.
i There are only 2 elements.

> # Character subscripts
> vec_as_location("foo", 1L, names = "bar")
Error: Can't subset elements that don't exist.
x Element `foo` doesn't exist.

> vec_as_location2("foo", 1L, names = "bar")
Error: Can't subset elements that don't exist.
x Element `foo` doesn't exist.


can optionally extend beyond the end
====================================

> num_as_location(c(1, 3), 1, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i Input has size 1.
x Subscript contains non-consecutive location 3.

> num_as_location(c(1:5, 7), 3, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i Input has size 3.
x Subscript contains non-consecutive locations 4 and 7.

> num_as_location(c(1:5, 7, 1), 3, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i Input has size 3.
x Subscript contains non-consecutive locations 4 and 7.

> num_as_location(c(1:5, 7, 1, 10), 3, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i Input has size 3.
x Subscript contains non-consecutive locations 4, 7, and 10.


missing values are supported in error formatters
================================================

> num_as_location(c(1, NA, 2, 3), 1)
Error: Can't subset elements that don't exist.
x Locations 2 and 3 don't exist.
i There are only 1 element.

> num_as_location(c(1, NA, 3), 1, oob = "extend")
Error: Can't subset elements beyond the end with non-consecutive locations.
i Input has size 1.
x Subscript contains non-consecutive location 3.


can customise subscript type errors
===================================

> # With custom `arg`
> num_as_location(-1, 2, negative = "error", arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Subscript `foo` can't contain negative locations.

> num_as_location2(-1, 2, negative = "error", arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` has value -1 but must be a positive location.

> vec_as_location2(0, 2, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` has value 0 but must be a positive location.

> vec_as_location2(na_dbl, 2, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` can't be `NA`.

> vec_as_location2(c(1, 2), 2, arg = "foo")
Error: Must extract element with a single valid subscript.
x Subscript `foo` has size 2 but must be size 1.

> vec_as_location(c(TRUE, FALSE), 3, arg = "foo")
Error: Must subset elements with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x Input has size 3 but subscript `foo` has size 2.

> vec_as_location(c(-1, NA), 3, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Negative locations can't have missing values.
i Subscript `foo` has a missing value at location 2.

> vec_as_location(c(-1, 1), 3, arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Negative and positive locations can't be mixed.
i Subscript `foo` has a positive value at location 2.

> num_as_location(c(1, 4), 2, oob = "extend", arg = "foo")
Error: Can't subset elements beyond the end with non-consecutive locations.
i Input has size 2.
x Subscript `foo` contains non-consecutive location 4.

> num_as_location(0, 1, zero = "error", arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Subscript `foo` can't contain `0` values.
i It has a `0` value at location 1.

> # With tibble columns
> with_tibble_cols(num_as_location(-1, 2, negative = "error"))
Error: Must rename columns with a valid subscript vector.
x Subscript `foo(bar)` can't contain negative locations.

> with_tibble_cols(num_as_location2(-1, 2, negative = "error"))
Error: Must rename column with a single valid subscript.
x Subscript `foo(bar)` has value -1 but must be a positive location.

> with_tibble_cols(vec_as_location2(0, 2))
Error: Must rename column with a single valid subscript.
x Subscript `foo(bar)` has value 0 but must be a positive location.

> with_tibble_cols(vec_as_location2(na_dbl, 2))
Error: Must rename column with a single valid subscript.
x Subscript `foo(bar)` can't be `NA`.

> with_tibble_cols(vec_as_location2(c(1, 2), 2))
Error: Must rename column with a single valid subscript.
x Subscript `foo(bar)` has size 2 but must be size 1.

> with_tibble_cols(vec_as_location(c(TRUE, FALSE), 3))
Error: Must rename columns with a valid subscript vector.
i Logical subscripts must match the size of the indexed input.
x Input has size 3 but subscript `foo(bar)` has size 2.

> with_tibble_cols(vec_as_location(c(-1, NA), 3))
Error: Must rename columns with a valid subscript vector.
x Negative locations can't have missing values.
i Subscript `foo(bar)` has a missing value at location 2.

> with_tibble_cols(vec_as_location(c(-1, 1), 3))
Error: Must rename columns with a valid subscript vector.
x Negative and positive locations can't be mixed.
i Subscript `foo(bar)` has a positive value at location 2.

> with_tibble_cols(num_as_location(c(1, 4), 2, oob = "extend"))
Error: Can't rename columns beyond the end with non-consecutive locations.
i Input has size 2.
x Subscript `foo(bar)` contains non-consecutive location 4.

> with_tibble_cols(num_as_location(0, 1, zero = "error"))
Error: Must rename columns with a valid subscript vector.
x Subscript `foo(bar)` can't contain `0` values.
i It has a `0` value at location 1.


can customise OOB errors
========================

> vec_slice(set_names(letters), "foo")
Error: Can't subset elements that don't exist.
x Element `foo` doesn't exist.

> # With custom `arg`
> vec_as_location(30, length(letters), arg = "foo")
Error: Can't subset elements that don't exist.
x Location 30 doesn't exist.
i There are only 26 elements.

> vec_as_location("foo", NULL, letters, arg = "foo")
Error: Can't subset elements that don't exist.
x Element `foo` doesn't exist.

> # With tibble columns
> with_tibble_cols(vec_slice(set_names(letters), "foo"))
Error: Can't rename columns that don't exist.
x Column `foo` doesn't exist.

> with_tibble_cols(vec_slice(set_names(letters), 30))
Error: Can't rename columns that don't exist.
x Location 30 doesn't exist.
i There are only 26 columns.

> with_tibble_cols(vec_slice(set_names(letters), -30))
Error: Can't rename columns that don't exist.
x Location 30 doesn't exist.
i There are only 26 columns.

> # With tibble rows
> with_tibble_rows(vec_slice(set_names(letters), c("foo", "bar")))
Error: Can't remove rows that don't exist.
x Rows `foo` and `bar` don't exist.

> with_tibble_rows(vec_slice(set_names(letters), 1:30))
Error: Can't remove rows that don't exist.
x Locations 27, 28, 29, and 30 don't exist.
i There are only 26 rows.

> with_tibble_rows(vec_slice(set_names(letters), -(1:30)))
Error: Can't remove rows that don't exist.
x Locations 27, 28, 29, and 30 don't exist.
i There are only 26 rows.


can disallow missing values
===========================

> vec_as_location(c(1, NA), 2, missing = "error")
Error: Must subset elements with a valid subscript vector.
x Subscript can't contain missing values.
x It has a missing value at location 2.

> vec_as_location(c(1, NA, 2, NA), 2, missing = "error", arg = "foo")
Error: Must subset elements with a valid subscript vector.
x Subscript can't contain missing values.
x It has missing values at locations 2 and 4.

> with_tibble_cols(vec_as_location(c(1, NA, 2, NA), 2, missing = "error"))
Error: Must rename columns with a valid subscript vector.
x Subscript `foo(bar)` can't contain missing values.
x It has missing values at locations 2 and 4.


vec_as_location() checks dimensionality
=======================================

> vec_as_location(matrix(TRUE, nrow = 1), 3L)
Error: Must subset elements with a valid subscript vector.
x Subscript must be a simple vector, not a matrix.

> vec_as_location(array(TRUE, dim = c(1, 1, 1)), 3L)
Error: Must subset elements with a valid subscript vector.
x Subscript must be a simple vector, not an array.

> with_tibble_rows(vec_as_location(matrix(TRUE, nrow = 1), 3L))
Error: Must remove rows with a valid subscript vector.
x Subscript `foo(bar)` must be a simple vector, not a matrix.

