@@ -15,6 +15,14 @@ test_that("match & %in% basics work", {
1515 expect_identical(y %in% x , c(TRUE , TRUE , TRUE , FALSE ))
1616 expect_identical(x %in% 3 : 6 , c(FALSE , TRUE , TRUE , TRUE ))
1717 expect_identical(x %in% c(3.0 , 4.0 , 5.0 , 6.0 ), c(FALSE , TRUE , TRUE , TRUE ))
18+
19+ expect_identical(match(integer64(), as.integer64(1L )), integer())
20+ expect_identical(match(as.integer64(1L ), integer64()), NA_integer_ )
21+ expect_identical(match(as.integer64(1L ), integer64(), nomatch = 0L ), 0L )
22+
23+ x_nm <- as.integer64(c(1L , 3L ))
24+ table_nm <- as.integer64(2 : 4 )
25+ expect_identical(match(x_nm , table_nm , nomatch = - 1L ), c(- 1L , 2L ))
1826})
1927
2028test_that(" Different method= for match() and %in% work" , {
@@ -43,6 +51,99 @@ test_that("Different method= for match() and %in% work", {
4351 expect_identical(x %in% table , rep(TRUE , 10L ))
4452})
4553
54+ test_that(" match.integer64: automatic method selection without cache" , {
55+ # hashrev path: short x, long table
56+ nx <- 10L
57+ ny <- 2L ^ 16L
58+ x <- as.integer64(1 : nx )
59+ table <- as.integer64(1 : ny )
60+ # As of this writing, this should invoke hashrev
61+ expect_identical(match(x , table ), 1 : nx )
62+
63+ # hashpos path: long x, short table
64+ x_long_match <- as.integer64(1 : ny )
65+ table_short_match <- as.integer64(1 : nx )
66+ # As of this writing, this should use hashpos
67+ expect_identical(match(x_long_match [1 : nx ], table_short_match ), 1 : nx )
68+ expect_identical(match(x_long_match [(nx + 1L ): (nx + 10L )], table_short_match ), rep(NA_integer_ , 10L ))
69+ })
70+
71+ test_that(" %in%.integer64: automatic method selection without cache" , {
72+ # hashrin path: short x, long table
73+ nx <- 10L
74+ ny <- 2L ^ 16L
75+ x <- as.integer64(1 : nx )
76+ table <- as.integer64(1 : ny )
77+ # As of this writing, this should use hashrin
78+ expect_identical(x %in% table , rep(TRUE , nx ))
79+
80+ # hashfin path: long x, short table
81+ x_long_in <- as.integer64(1 : ny )
82+ table_short_in <- as.integer64(1 : nx )
83+ # As of this writing, this should use hashfin.
84+ expect_identical(x_long_in [1 : nx ] %in% table_short_in , rep(TRUE , nx ))
85+ expect_identical(x_long_in [(nx + 1L ): (nx + 10L )] %in% table_short_in , rep(FALSE , 10L ))
86+ })
87+
88+ test_that(" match.integer64: cache-based method selection" , {
89+ x <- as.integer64(c(1L , 3L , 5L ))
90+ table <- as.integer64(c(2L , 4L , 3L , 1L ))
91+
92+ # hashcache
93+ hashcache(table )
94+ expect_identical(match(x , table ), c(4L , 3L , NA_integer_ ))
95+ remcache(table )
96+
97+ # sortordercache
98+ sortordercache(table )
99+ expect_identical(match(x , table ), c(4L , 3L , NA_integer_ ))
100+ remcache(table )
101+
102+ # ordercache
103+ ordercache(table )
104+ expect_identical(match(x , table ), c(4L , 3L , NA_integer_ ))
105+ remcache(table )
106+ })
107+
108+ test_that(" %in%.integer64: cache-based method selection" , {
109+ x <- as.integer64(c(1L , 3L , 5L ))
110+ table <- as.integer64(c(2L , 4L , 3L , 1L ))
111+ expected_in <- c(TRUE , TRUE , FALSE )
112+
113+ # hashcache
114+ hashcache(table )
115+ expect_identical(x %in% table , expected_in )
116+ remcache(table )
117+
118+ # sortcache
119+ sortcache(table )
120+ expect_identical(x %in% table , expected_in )
121+ remcache(table )
122+
123+ # ordercache
124+ ordercache(table )
125+ expect_identical(x %in% table , expected_in )
126+ remcache(table )
127+ })
128+
129+ test_that(" match.integer64: nunique argument" , {
130+ x <- as.integer64(c(1L , 3L , 5L ))
131+ table <- as.integer64(c(2L , 4L , 3L , 1L , 3L ))
132+ expect_identical(match(x , table , nunique = 4L ), c(4L , 3L , NA_integer_ ))
133+ })
134+
135+ test_that(" %in%.integer64: nunique argument" , {
136+ x <- as.integer64(c(1L , 3L , 5L ))
137+ table <- as.integer64(c(2L , 4L , 3L , 1L , 3L ))
138+ expect_identical(x %in% table , c(TRUE , TRUE , FALSE ))
139+ })
140+
141+ test_that(" %in%.integer64: with NA values" , {
142+ x <- as.integer64(c(1L , NA , 3L ))
143+ table <- as.integer64(c(NA , 2L , 1L ))
144+ expect_identical(x %in% table , c(TRUE , TRUE , FALSE ))
145+ })
146+
46147# TODO(#59): Don't call table.integer64() directly.
47148test_that(" duplicated, unique, table methods work" , {
48149 x = as.integer64(1 : 3 )
@@ -211,3 +312,22 @@ test_that("prank() works as intended", {
211312 expect_identical(prank(x ), (x - 1.0 )/ 99.0 )
212313 expect_identical(prank(x [1L ]), NA_integer64_ )
213314})
315+
316+ test_that(" match.integer64 with method='orderpos' fails due to bug" , {
317+ x <- as.integer64(1 : 5 )
318+ table <- as.integer64(3 : 7 )
319+ expect_error(match(x , table , method = " orderpos" ), " object 's' not found" , fixed = TRUE )
320+ })
321+
322+ test_that(" match.integer64 with partial cache triggers fallback" , {
323+ x <- as.integer64(1 : 5 )
324+ table <- as.integer64(3 : 7 )
325+
326+ sortcache(table ) # creates 'sort' and 'nunique' in cache
327+
328+ # This should fallback to hashpos/hashrev logic.
329+ # x is small, table is small.
330+ expect_identical(match(x , table ), c(NA_integer_ , NA_integer_ , 1L , 2L , 3L ))
331+
332+ remcache(table )
333+ })
0 commit comments