Skip to content

Commit

Permalink
Remove remaining is.na(useNames) etc. from unit tests [#246]
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Dec 29, 2024
1 parent 8a0396d commit 3895ee7
Show file tree
Hide file tree
Showing 47 changed files with 164 additions and 164 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: matrixStats
Version: 1.4.1-9007
Version: 1.4.1-9008
Depends:
R (>= 3.4.0)
Suggests:
Expand Down
4 changes: 2 additions & 2 deletions tests/diff2.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
library("matrixStats")

diff2_R <- function(..., useNames = NA){
diff2_R <- function(..., useNames = TRUE){
res <- diff(...)
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

Expand Down
2 changes: 1 addition & 1 deletion tests/diff2_subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ library("matrixStats")

diff2_R <- function(..., useNames=NA){
res <- diff(...)
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
20 changes: 10 additions & 10 deletions tests/rowAllAnys.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library("matrixStats")

rowAlls_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {
rowAlls_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = TRUE) {
if (is.na(value)) {
res <- apply(is.na(x), MARGIN = 1L, FUN = all, na.rm = na.rm)
} else {
Expand All @@ -15,11 +15,11 @@ rowAlls_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {

res <- apply(y, MARGIN = 1L, FUN = all, na.rm = na.rm)
}
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

rowAnys_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {
rowAnys_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = TRUE) {
if (is.na(value)) {
res <- apply(is.na(x), MARGIN = 1L, FUN = any, na.rm = na.rm)
} else {
Expand All @@ -34,13 +34,13 @@ rowAnys_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {

res <- apply(y, MARGIN = 1L, FUN = any, na.rm = na.rm)
}
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

rowAnyMissings_R <- function(x, ..., useNames = NA) {
rowAnyMissings_R <- function(x, ..., useNames = TRUE) {
res <- apply(x, MARGIN = 1L, FUN = anyMissing)
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

Expand Down Expand Up @@ -126,7 +126,7 @@ for (setDimnames in c(TRUE, FALSE)) {
r2 <- colAlls(t(x), value = value, na.rm = na.rm, useNames = useNames)
stopifnot(identical(r1, r0))
stopifnot(identical(r2, r1))
if (!is.na(useNames) && !useNames && !setDimnames) {
if (!useNames && !setDimnames) {
for (rr in seq_len(nrow(x))) {
c <- allValue(x[rr, ], value = value, na.rm = na.rm)
stopifnot(identical(c, r1[rr]))
Expand All @@ -140,7 +140,7 @@ for (setDimnames in c(TRUE, FALSE)) {
r2 <- colAnys(t(x), value = value, na.rm = na.rm, useNames = useNames)
stopifnot(identical(r1, r0))
stopifnot(identical(r2, r1))
if (!is.na(useNames) && !useNames && !setDimnames) {
if (!useNames && !setDimnames) {
for (rr in seq_len(nrow(x))) {
c <- anyValue(x[rr, ], value = value, na.rm = na.rm)
stopifnot(identical(c, r1[rr]))
Expand Down Expand Up @@ -262,7 +262,7 @@ for (setDimnames in c(TRUE, FALSE)) {
r2 <- colAlls(t(x), value = value, na.rm = na.rm, useNames = useNames)
stopifnot(identical(r1, r0))
stopifnot(identical(r2, r1))
if (!is.na(useNames) && !useNames && !setDimnames) {
if (!useNames && !setDimnames) {
for (rr in seq_len(nrow(x))) {
c0 <- all_R(x[rr, ], value, na.rm = na.rm)
c <- allValue(x[rr, ], value = value, na.rm = na.rm)
Expand All @@ -276,7 +276,7 @@ for (setDimnames in c(TRUE, FALSE)) {
r2 <- colAnys(t(x), value = value, na.rm = na.rm, useNames = useNames)
stopifnot(identical(r1, r0))
stopifnot(identical(r2, r1))
if (!is.na(useNames) && !useNames && !setDimnames) {
if (!useNames && !setDimnames) {
for (rr in seq_len(nrow(x))) {
c0 <- any_R(x[rr, ], value, na.rm = na.rm)
c <- anyValue(x[rr, ], value = value, na.rm = na.rm)
Expand Down
12 changes: 6 additions & 6 deletions tests/rowAllAnys_subset.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library("matrixStats")

rowAlls_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {
rowAlls_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = TRUE) {
if (is.na(value)) {
res <- apply(is.na(x), MARGIN = 1L, FUN = all, na.rm = na.rm)
} else {
Expand All @@ -15,11 +15,11 @@ rowAlls_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {

res <- apply(y, MARGIN = 1L, FUN = all, na.rm = na.rm)
}
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

rowAnys_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {
rowAnys_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = TRUE) {
if (is.na(value)) {
res <- apply(is.na(x), MARGIN = 1L, FUN = any, na.rm = na.rm)
} else {
Expand All @@ -34,13 +34,13 @@ rowAnys_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {

res <- apply(y, MARGIN = 1L, FUN = any, na.rm = na.rm)
}
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

rowAnyMissings_R <- function(x, ..., useNames = NA) {
rowAnyMissings_R <- function(x, ..., useNames = TRUE) {
res <- apply(x, MARGIN = 1L, FUN = anyMissing)
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

Expand Down
12 changes: 6 additions & 6 deletions tests/rowCollapse.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ x <- matrix(1:27, ncol = 3)
# To check names attribute
dimnames <- list(letters[1:9], LETTERS[1:3])

rowCollapse_R <- function(x, idxs, ..., useNames = NA) {
rowCollapse_R <- function(x, idxs, ..., useNames = TRUE) {
res <- x[, idxs]
# Preserve names attribute?
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

Expand Down Expand Up @@ -43,10 +43,10 @@ for (setDimnames in c(TRUE, FALSE)) {
}


rowCollapse_R <- function(x, idxs, ..., useNames = NA) {
rowCollapse_R <- function(x, idxs, ..., useNames = TRUE) {
res <- c(x[1:5, 1], x[6:9, 3])
# Preserve names attribute?
if (is.na(useNames) || !useNames) names(res) <- NULL
if (!useNames) names(res) <- NULL
res
}

Expand All @@ -66,11 +66,11 @@ for (setDimnames in c(TRUE, FALSE)) {
}


rowCollapse_R <- function(x, idxs, ..., useNames = NA) {
rowCollapse_R <- function(x, idxs, ..., useNames = TRUE) {
res <- c(x[1, 1], x[2, 2], x[3, 3], x[4, 1], x[5, 2],
x[6, 3], x[7, 1], x[8, 2], x[9, 3])
# Preserve names attribute?
if (isTRUE(useNames)) {
if (useNames) {
names <- rownames(x)
if (!is.null(names)) names(res) <- names
}
Expand Down
4 changes: 2 additions & 2 deletions tests/rowCollapse_subset.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
library("matrixStats")

rowCollapse_R <- function(x, idxs, ..., useNames = NA) {
rowCollapse_R <- function(x, idxs, ..., useNames = TRUE) {
ans <- c()
storage.mode(ans) <- storage.mode(x)
for (ii in seq_len(length(idxs))) {
ans[ii] <- x[ii, idxs[ii]]
}

# Preserve names attribute
if (isTRUE(useNames)) {
if (useNames) {
names <- rownames(x)
if (!is.null(names)) names(ans) <- names
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rowCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rowCounts_R <- function(x, value = TRUE, na.rm = FALSE, ...) {
# Preserve names attribute
names <- names(counts)
counts <- as.integer(counts)
if (isTRUE(useNames) && !is.null(names)) names(counts) <- names
if (useNames && !is.null(names)) names(counts) <- names
counts
}

Expand Down
4 changes: 2 additions & 2 deletions tests/rowCounts_subset.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library("matrixStats")

rowCounts_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {
rowCounts_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = TRUE) {
if (is.na(value)) {
counts <- apply(x, MARGIN = 1L, FUN = function(x)
sum(is.na(x))
Expand All @@ -13,7 +13,7 @@ rowCounts_R <- function(x, value = TRUE, na.rm = FALSE, ..., useNames = NA) {
# Preserve names attribute
names <- names(counts)
counts <- as.integer(counts)
if (isTRUE(useNames) && !is.null(names)) names(counts) <- names
if (useNames && !is.null(names)) names(counts) <- names
counts
} # rowCounts_R()

Expand Down
8 changes: 4 additions & 4 deletions tests/rowCumMinMaxs.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
library("matrixStats")

rowCummins_R <- function(x, ..., useNames = NA) {
rowCummins_R <- function(x, ..., useNames = TRUE) {
suppressWarnings({
y <- t(apply(x, MARGIN = 1L, FUN = cummin))
})

# Preserve dimnames attribute?
dim(y) <- dim(x)
dimnames(y) <- if (isTRUE(useNames)) dimnames(x) else NULL
dimnames(y) <- if (useNames) dimnames(x) else NULL
y
}

rowCummaxs_R <- function(x, ..., useNames = NA) {
rowCummaxs_R <- function(x, ..., useNames = TRUE) {
mode <- storage.mode(x)
# Change mode because a bug is detected on cummax for integer in R-3.2.0
storage.mode(x) <- "numeric"
Expand All @@ -21,7 +21,7 @@ rowCummaxs_R <- function(x, ..., useNames = NA) {

# Preserve dimnames attribute?
dim(y) <- dim(x)
dimnames(y) <- if (isTRUE(useNames)) dimnames(x) else NULL
dimnames(y) <- if (useNames) dimnames(x) else NULL

y
}
Expand Down
8 changes: 4 additions & 4 deletions tests/rowCumMinMaxs_subset.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
library("matrixStats")

rowCummins_R <- function(x, ..., useNames = NA) {
rowCummins_R <- function(x, ..., useNames = TRUE) {
suppressWarnings({
y <- t(apply(x, MARGIN = 1L, FUN = cummin))
})

# Preserve dimnames attribute?
dim(y) <- dim(x)
dimnames(y) <- if (isTRUE(useNames)) dimnames(x) else NULL
dimnames(y) <- if (useNames) dimnames(x) else NULL

y
}

rowCummaxs_R <- function(x, ..., useNames = NA) {
rowCummaxs_R <- function(x, ..., useNames = TRUE) {
mode <- storage.mode(x)
# Change mode because a bug is detected on cummax for integer in R-3.2.0
storage.mode(x) <- "numeric"
Expand All @@ -22,7 +22,7 @@ rowCummaxs_R <- function(x, ..., useNames = NA) {

# Preserve dimnames attribute?
dim(y) <- dim(x)
dimnames(y) <- if (isTRUE(useNames)) dimnames(x) else NULL
dimnames(y) <- if (useNames) dimnames(x) else NULL

storage.mode(y) <- mode
y
Expand Down
4 changes: 2 additions & 2 deletions tests/rowCumprods.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
library("matrixStats")

rowCumprods_R <- function(x, ..., useNames = NA) {
rowCumprods_R <- function(x, ..., useNames = TRUE) {
suppressWarnings({
y <- t(apply(x, MARGIN = 1L, FUN = cumprod))
})

# Preserve dimnames attribute?
dim(y) <- dim(x)
dimnames(y) <- if (isTRUE(useNames)) dimnames(x) else NULL
dimnames(y) <- if (useNames) dimnames(x) else NULL

y
}
Expand Down
4 changes: 2 additions & 2 deletions tests/rowCumprods_subset.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
library("matrixStats")

rowCumprods_R <- function(x, ..., useNames = NA) {
rowCumprods_R <- function(x, ..., useNames = TRUE) {
suppressWarnings({
y <- t(apply(x, MARGIN = 1L, FUN = cumprod))
})

# Preserve dimnames attribute?
dim(y) <- dim(x)
dimnames(y) <- if (isTRUE(useNames)) dimnames(x) else NULL
dimnames(y) <- if (useNames) dimnames(x) else NULL

y
}
Expand Down
4 changes: 2 additions & 2 deletions tests/rowCumsums.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
library("matrixStats")

rowCumsums_R <- function(x, ..., useNames = NA) {
rowCumsums_R <- function(x, ..., useNames = TRUE) {
suppressWarnings({
y <- t(apply(x, MARGIN = 1L, FUN = cumsum))
})

# Preserve dimnames attribute?
dim(y) <- dim(x)
dimnames(y) <- if (isTRUE(useNames)) dimnames(x) else NULL
dimnames(y) <- if (useNames) dimnames(x) else NULL

y
}
Expand Down
4 changes: 2 additions & 2 deletions tests/rowCumsums_subset.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
library("matrixStats")

rowCumsums_R <- function(x, ..., useNames = NA) {
rowCumsums_R <- function(x, ..., useNames = TRUE) {
suppressWarnings({
y <- t(apply(x, MARGIN = 1L, FUN = cumsum))
})

# Preserve dimnames attribute?
dim(y) <- dim(x)
dimnames(y) <- if (isTRUE(useNames)) dimnames(x) else NULL
dimnames(y) <- if (useNames) dimnames(x) else NULL

y
}
Expand Down
6 changes: 3 additions & 3 deletions tests/rowDiffs.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
library("matrixStats")

rowDiffs_R <- function(x, lag = 1L, differences = 1L, ..., useNames = NA) {
rowDiffs_R <- function(x, lag = 1L, differences = 1L, ..., useNames = TRUE) {
ncol2 <- ncol(x) - lag * differences
if (ncol2 <= 0) {
y <- matrix(x[integer(0L)], nrow = nrow(x), ncol = 0L)
# Preserve names attribute
if (isTRUE(useNames) && !is.null(rownames(x))) rownames(y) <- rownames(x)
if (useNames && !is.null(rownames(x))) rownames(y) <- rownames(x)
return(y)
}
suppressWarnings({
Expand All @@ -15,7 +15,7 @@ rowDiffs_R <- function(x, lag = 1L, differences = 1L, ..., useNames = NA) {

# Preserve dimnames attribute
dim(y) <- c(nrow(x), ncol2)
if (isTRUE(useNames) && !is.null(dimnames(x))) {
if (useNames && !is.null(dimnames(x))) {
colnames <- colnames(x)
if (!is.null(colnames)) {
len <- length(colnames)
Expand Down
6 changes: 3 additions & 3 deletions tests/rowDiffs_subset.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
library("matrixStats")

rowDiffs_R <- function(x, lag = 1L, differences = 1L, ..., useNames = NA) {
rowDiffs_R <- function(x, lag = 1L, differences = 1L, ..., useNames = TRUE) {
ncol2 <- ncol(x) - lag * differences
if (ncol2 <= 0) {
y <- matrix(x[integer(0L)], nrow = nrow(x), ncol = 0L)
# Preserve names attribute
if (isTRUE(useNames) && !is.null(rownames(x))) rownames(y) <- rownames(x)
if (useNames && !is.null(rownames(x))) rownames(y) <- rownames(x)
return(y)
}
suppressWarnings({
Expand All @@ -15,7 +15,7 @@ rowDiffs_R <- function(x, lag = 1L, differences = 1L, ..., useNames = NA) {

# Preserve dimnames attribute
dim(y) <- c(nrow(x), ncol2)
if (isTRUE(useNames) && !is.null(dimnames(x))) {
if (useNames && !is.null(dimnames(x))) {
colnames <- colnames(x)
if (!is.null(colnames)) {
len <- length(colnames)
Expand Down
Loading

0 comments on commit 3895ee7

Please sign in to comment.