Skip to content

Commit e68489e

Browse files
Fixed signed comparison in IndexHash; unit tests for unique() (closes #574)
1 parent 769ab8c commit e68489e

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2016-11-03 Nathan Russell <[email protected]>
2+
3+
* inst/include/Rcpp/hash/IndexHash.h: Add casts to eliminate
4+
signed / unsigned comparison warning
5+
* inst/include/Rcpp/hash/SelfHash.h: Idem
6+
* inst/unitTests/cpp/sugar.cpp: Added unit tests for sugar function
7+
unique()
8+
* inst/unitTests/runit.sugar.R: Idem
9+
110
2016-10-30 Dirk Eddelbuettel <[email protected]>
211

312
* src/api.cpp: New capabilities field for new date(time) vectors

inst/include/Rcpp/hash/IndexHash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace Rcpp{
170170
unsigned int addr = get_addr(val) ;
171171
while (data[addr] && not_equal( src[data[addr] - 1], val)) {
172172
addr++;
173-
if (addr == m) {
173+
if (addr == static_cast<unsigned int>(m)) {
174174
addr = 0;
175175
}
176176
}
@@ -191,7 +191,7 @@ namespace Rcpp{
191191
if (src[data[addr] - 1] == value)
192192
return data[addr];
193193
addr++;
194-
if (addr == m) addr = 0;
194+
if (addr == static_cast<unsigned int>(m)) addr = 0;
195195
}
196196
return NA_INTEGER;
197197
}

inst/include/Rcpp/hash/SelfHash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace sugar{
6565
unsigned int addr = get_addr(val) ;
6666
while (data[addr] && src[data[addr] - 1] != val) {
6767
addr++;
68-
if (addr == m) addr = 0;
68+
if (addr == static_cast<unsigned int>(m)) addr = 0;
6969
}
7070
if (!data[addr]) {
7171
data[addr] = i ;
@@ -81,7 +81,7 @@ namespace sugar{
8181
if (src[data[addr] - 1] == value)
8282
return data[addr];
8383
addr++;
84-
if (addr == m) addr = 0;
84+
if (addr == static_cast<unsigned int>(m)) addr = 0;
8585
}
8686
return NA_INTEGER;
8787
}

inst/unitTests/cpp/sugar.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,21 @@ IntegerVector runit_self_match( CharacterVector x){
595595
return self_match( x ) ;
596596
}
597597

598+
// [[Rcpp::export]]
599+
Rcpp::IntegerVector runit_unique_int(Rcpp::IntegerVector x) {
600+
return Rcpp::unique(x);
601+
}
602+
603+
// [[Rcpp::export]]
604+
Rcpp::NumericVector runit_unique_dbl(Rcpp::NumericVector x) {
605+
return Rcpp::unique(x);
606+
}
607+
608+
// [[Rcpp::export]]
609+
Rcpp::CharacterVector runit_unique_ch(Rcpp::CharacterVector x) {
610+
return Rcpp::unique(x);
611+
}
612+
598613
// [[Rcpp::export]]
599614
IntegerVector runit_table( CharacterVector x){
600615
return table( x ) ;

inst/unitTests/runit.sugar.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,50 @@ if (.runThisTest) {
638638
x <- sample( letters, 1000, replace = TRUE )
639639
checkEquals( runit_self_match(x), match(x,unique(x)) )
640640
}
641+
642+
test.unique <- function() {
643+
x <- sample(LETTERS[1:5], 10, TRUE)
644+
checkEquals(
645+
sort(unique(x)),
646+
sort(runit_unique_ch(x)),
647+
"unique / character / without NA"
648+
)
649+
650+
x <- c(x, NA, NA)
651+
checkEquals(
652+
sort(unique(x), na.last = TRUE),
653+
sort(runit_unique_ch(x), na.last = TRUE),
654+
"unique / character / with NA"
655+
)
656+
657+
x <- sample(1:5, 10, TRUE)
658+
checkEquals(
659+
sort(unique(x)),
660+
sort(runit_unique_int(x)),
661+
"unique / integer / without NA"
662+
)
663+
664+
x <- c(x, NA, NA)
665+
checkEquals(
666+
sort(unique(x), na.last = TRUE),
667+
sort(runit_unique_int(x), na.last = TRUE),
668+
"unique / integer / with NA"
669+
)
670+
671+
x <- sample(1:5 + 0.5, 10, TRUE)
672+
checkEquals(
673+
sort(unique(x)),
674+
sort(runit_unique_dbl(x)),
675+
"unique / numeric / without NA"
676+
)
677+
678+
x <- c(x, NA, NA)
679+
checkEquals(
680+
sort(unique(x), na.last = TRUE),
681+
sort(runit_unique_dbl(x), na.last = TRUE),
682+
"unique / numeric / with NA"
683+
)
684+
}
641685

642686
test.table <- function(){
643687
x <- sample( letters, 1000, replace = TRUE )

0 commit comments

Comments
 (0)