Skip to content

Commit

Permalink
tests: for multiple duplicates in x (similar to issue #72)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgibb committed Oct 21, 2020
1 parent 9a67767 commit c8710fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/closest.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ SEXP C_closest_dup_closest(SEXP x, SEXP table, SEXP tolerance, SEXP nomatch) {
diffnxttbl =
itbl + 1 < ntable ? fabs(px[ix] - ptable[itbl + 1]) : R_PosInf;

printf("ix %i, ixlastused %i, itbl %i, itbllastused %i\n> diff %.2f, diffnxtx %.2f, diffnxttbl %.2f\n", ix, ixlastused, itbl, itbllastused, diff, diffnxtx, diffnxttbl);

if (diff <= ptolerance[ix]) {
/* valid match, add + 1 to convert between R/C index */
pout[ix] = itbl + 1;
Expand All @@ -109,7 +111,7 @@ SEXP C_closest_dup_closest(SEXP x, SEXP table, SEXP tolerance, SEXP nomatch) {
} else
pout[ix] = inomatch;

if (diffnxtx < diff || diffnxttbl < diff) {
if (diffnxtx <= diff || diffnxttbl <= diff) {
/* increment the index with the smaller distance */
if (diffnxtx < diffnxttbl)
++ix;
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ test_that("closest, duplicates", {
expect_equal(closest(1:2, rep(1:2, each = 3),
tolerance = 0, duplicates = "closest"),
c(1, 4))
# multiple duplicates in x, similar to #72 above
expect_equal(closest(rep(1:2, each = 2), 1:2,
tolerance = 0, duplicates = "closest"),
c(1, NA, 2, NA))
expect_equal(closest(rep(1:2, each = 3), 1:2,
tolerance = 0, duplicates = "closest"),
c(1, NA, NA, 2, NA, NA))
})

test_that("common", {
Expand Down

0 comments on commit c8710fe

Please sign in to comment.