Skip to content

Commit 9e300a2

Browse files
committed
fix NaN + more tests
1 parent 295bd26 commit 9e300a2

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

src/stack.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ template<int RTYPE> Vector<RTYPE> cpp_stack_impl(List array_list, int along, Vec
107107
// Rprintf("result[%i] = a[%i][%i]\n", *it[0] + dim_offset, ai, aidx);
108108
int ri = *it[0] + dim_offset;
109109
auto newval = a[aidx];
110-
if (!ovr) {
111-
if (! (result[ri] == fill[0] || result[ri] == newval))
110+
// same-value comparisons below: catch double NaN equality
111+
if (ovr || (result[ri] == fill[0]) || (!(result[ri] == result[ri])))
112+
result[ri] = newval;
113+
else
114+
if (!((newval == fill[0]) || (result[ri] == newval) || (!(newval == newval))))
112115
stop("Different values on same position and allow_overwrite=FALSE");
113-
}
114-
result[ri] = newval;
115116

116117
it[0]++;
117118
for (int d=0; d<maxdim; d++) { // check if we're jumping dimensions

tests/testthat/test-stack.r

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ context("stack")
22

33
A = matrix(1:4, nrow=2, ncol=2, dimnames=list(c('a','b'),c('x','y')))
44
B = matrix(5:6, nrow=2, ncol=1, dimnames=list(c('b','a'),'z'))
5-
6-
C = stack(list(A, B), along=2)
7-
# x y z
8-
# a 1 3 6 # B is stacked correctly according to its names
9-
# b 2 4 5
5+
C = structure(c(1L, 2L, 3L, 4L, 6L, 5L), .Dim = 2:3,
6+
.Dimnames = list(c("a", "b"), c("x", "y", "z")))
107

118
test_that("match names, not extent", {
12-
Cref = structure(c(1L, 2L, 3L, 4L, 6L, 5L), .Dim = 2:3,
13-
.Dimnames = list(c("a", "b"), c("x", "y", "z")))
9+
Cref = stack(list(A, B), along=2)
1410
expect_equal(C, Cref)
1511
})
1612

@@ -76,6 +72,23 @@ test_that("1-row/col matrix stacking", {
7672
expect_equal(stack(list(B=t(B)), along=2), t(B))
7773
})
7874

75+
test_that("allow overwrite", {
76+
ov = A
77+
ov[1,1] = 10
78+
expect_error(stack(ov, A, along=2))
79+
expect_error(stack(A, ov, along=2))
80+
expect_equal(stack(ov, A, along=2, allow_overwrite=TRUE), A)
81+
expect_equal(stack(A, ov, along=2, allow_overwrite=TRUE), ov)
82+
expect_equal(stack(A, A, along=2), A)
83+
84+
ov[] = NA
85+
expect_equal(stack(A, ov, along=2), A)
86+
expect_equal(stack(ov, A, along=2), A)
87+
ov[] = 0
88+
expect_equal(stack(A, ov, along=2, fill=0), A)
89+
expect_equal(stack(ov, A, along=2, fill=0), A)
90+
})
91+
7992
test_that("keep_empty arg when stacking zero-length vectors", {
8093
a = setNames(1:3, letters[1:3])
8194
b = numeric()
@@ -91,15 +104,6 @@ test_that("keep_empty arg when stacking zero-length vectors", {
91104
expect_equal(re2, t(re3))
92105
})
93106

94-
test_that("allow overwrite", {
95-
ov = A
96-
ov[1,1] = 10
97-
expect_error(stack(ov, A, along=2))
98-
expect_error(stack(A, ov, along=2))
99-
expect_equal(stack(ov, A, along=2, allow_overwrite=TRUE), A)
100-
expect_equal(stack(A, ov, along=2, allow_overwrite=TRUE), ov)
101-
})
102-
103107
test_that("performance", {
104108
skip_on_cran()
105109

0 commit comments

Comments
 (0)