Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/integer64.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,17 @@ SEXP as_integer64_character(SEXP x_, SEXP ret_){
char * endpointer;
for(i=0; i<n; i++){
str = CHAR(STRING_ELT(x_, i)); endpointer = (char *)str; // thanks to Murray Stokely 28.1.2012
ret[i] = strtoll(str, &endpointer, 10);
int base = 10; // default
int sign = 1; // default
if (str[0]=='-'){
sign = -1;
str++;
}
if (str[0]=='0' && (str[1] == 'x' || str[1] == 'X')){
base = 16;
str += 2;
}
ret[i] = strtoll(str, &endpointer, base) * sign;
if (*endpointer)
ret[i] = NA_INTEGER64;
}
Expand Down
22 changes: 20 additions & 2 deletions tests/testthat/test-integer64.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ test_that("integer64 coercion to/from other types work", {

# to integer64
expect_identical(as.integer64(TRUE), as.integer64(1L))
expect_identical(as.integer64(as.character(1:10)), as.integer64(1:10))
expect_identical(as.integer64(as.double(1:10)), as.integer64(1:10))
expect_identical(as.integer64(as.character(-10:10)), as.integer64(-10:10))
expect_identical(as.integer64(as.double(-10:10)), as.integer64(-10:10))
expect_identical(as.integer64(NULL), as.integer64())
x = as.integer64(1:10)
expect_identical(as.integer64(x), x)
Expand All @@ -29,6 +29,24 @@ test_that("integer64 coercion to/from other types work", {
expect_identical(as.integer64(NA_character_), NA_integer64_)
})

test_that("hex string --> integer64 coercion works (as it does for integer)", {
expect_identical(as.integer64(c("0x1", "0xF")), as.integer64(c(1L, 15L)))
# Test the first value too large to store in a 32-bit signed integer.
expect_identical(as.integer64("0x100000000"), as.integer64(2L^32L))
# Test some negative values.
expect_identical(as.integer64(c("-0x0", "-0x1", "-0xF")), as.integer64(c(0L, -1L, -15L)))
# Test mixed positive and negatives
expect_identical(as.integer64(c("-0xA", "0xF")), as.integer64(c(-10L, 15L)))
# Test the smallest and largest values different from the extreme values of the representation.
expect_identical(as.integer64(c("-0x7FFFFFFFFFFFFFFE", "0x7FFFFFFFFFFFFFFE")), lim.integer64() + c(1L, -1L))
# Test smallest and largest representable values.
expect_identical(as.integer64(c("-0x7FFFFFFFFFFFFFFF", "0x7FFFFFFFFFFFFFFF")), lim.integer64())
# not representable in 64 bits --> lim.integer64(), same as non-hex string input
expect_identical(as.integer64(c("-0x8000000000000000", "0x8000000000000000")), lim.integer64())
# case insensitive
expect_identical(as.integer64(c("0xa", "0Xa", "0xA", "0xa")), as.integer64(rep(10L, 4L)))
})

test_that("S3 class basics work", {
x = as.integer64(1:10)
expect_s3_class(x, "integer64")
Expand Down
Loading