Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
mllg committed Mar 9, 2018
1 parent 78213ed commit 79f29a5
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion R/base32.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ base32_decode = function(x, use.padding = FALSE) {
if (length(i))
x[i] = paste0(x[i], strrep("=", 7L - ((nchar(x[i]) - 1L) %% 8L)))
}
enc2native(.Call(b32d, x))
.Call(b32d, x)
}
2 changes: 1 addition & 1 deletion R/base64.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ base64_urlencode = function(x) {
#' @useDynLib base64url b64d
#' @export
base64_urldecode = function(x) {
enc2native(.Call(b64d, x))
.Call(b64d, x)
}
8 changes: 4 additions & 4 deletions src/base32.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ SEXP b32e(SEXP strings) {
if (STRING_ELT(strings, i) == NA_STRING) {
SET_STRING_ELT(result, i, NA_STRING);
} else {
const char *plain = CHAR(STRING_ELT(strings, i));
const char *plain = translateCharUTF8(STRING_ELT(strings, i));
R_len_t n_in = strlen(plain);
R_len_t n_out = 1 + ((n_in + 4) / 5) * 8;

Expand All @@ -274,7 +274,7 @@ SEXP b32e(SEXP strings) {
}

base32_encode(plain, n_in, encoded);
SET_STRING_ELT(result, i, mkChar(encoded));
SET_STRING_ELT(result, i, mkCharCE(encoded, CE_ANY));
free(encoded);
}
}
Expand All @@ -294,7 +294,7 @@ SEXP b32d(SEXP strings) {
if (STRING_ELT(strings, i) == NA_STRING) {
SET_STRING_ELT(result, i, NA_STRING);
} else {
const char *encoded = CHAR(STRING_ELT(strings, i));
const char *encoded = translateCharUTF8(STRING_ELT(strings, i));
R_len_t n_in = strlen(encoded);
if (n_in == 0) {
SET_STRING_ELT(result, i, mkChar(""));
Expand All @@ -312,7 +312,7 @@ SEXP b32d(SEXP strings) {
error("Error decoding string from base32");
}

SET_STRING_ELT(result, i, mkChar(plain));
SET_STRING_ELT(result, i, mkCharCE(plain, CE_UTF8));
free(plain);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ SEXP b64e(SEXP strings) {
if (STRING_ELT(strings, i) == NA_STRING) {
SET_STRING_ELT(result, i, NA_STRING);
} else {
const char *plain = CHAR(STRING_ELT(strings, i));
const char *plain = translateCharUTF8(STRING_ELT(strings, i));
char *encoded = malloc(sizeof(char) * Base64encode_len(strlen(plain)));
Base64encode(encoded, plain, strlen(plain));
SET_STRING_ELT(result, i, mkChar(encoded));
SET_STRING_ELT(result, i, mkCharCE(encoded, CE_ANY));
free(encoded);
}
}
Expand All @@ -169,10 +169,10 @@ SEXP b64d(SEXP strings) {
if (STRING_ELT(strings, i) == NA_STRING) {
SET_STRING_ELT(result, i, NA_STRING);
} else {
const char *encoded = CHAR(STRING_ELT(strings, i));
const char *encoded = translateCharUTF8(STRING_ELT(strings, i));
char *plain = malloc(sizeof(char) * Base64decode_len(encoded));
Base64decode(plain, encoded);
SET_STRING_ELT(result, i, mkChar(plain));
SET_STRING_ELT(result, i, mkCharCE(plain, CE_UTF8));
free(plain);
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
library(testthat)
library(checkmate)

rand = function(n, min = 1L, max = 32L) {
chars = c(letters, LETTERS, c("=", "+", "-", "_", "/", "&", "ö", "=", "?", ":", ".", "`", "%", "\n"))
rand = function(n, min = 1L, max = 32L, only.ascii = FALSE) {
chars = c(letters, LETTERS, c("=", "+", "-", "_", "/", "&", "=", "?", ":", ".", "%"))
if (!isTRUE(only.ascii))
chars = c(chars, "ö", "`", "\n")
replicate(n, paste0(sample(chars, sample(min:max, 1L), replace = TRUE), collapse = ""))
}
29 changes: 10 additions & 19 deletions tests/testthat/test_base32.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
context("base32")

# test_that("encode and decode on random strings", {
# for (i in 1:10) {
# plain = rand(1000)
# enc = base32_encode(plain, use.padding = TRUE)
# expect_character(enc, any.missing = FALSE, len = length(plain), pattern = "^[A-Z234567=]+$")
# unenc = base32_decode(enc, use.padding = TRUE)
# expect_character(unenc, any.missing = FALSE, len = length(plain), min.chars = 1L)
# expect_equal(plain, unenc)
# }

# for (i in 1:10) {
# plain = rand(1000)
# enc = base32_encode(plain, use.padding = FALSE)
# expect_character(enc, any.missing = FALSE, len = length(plain), pattern = "^[A-Z234567]+$")
# unenc = base32_decode(enc, use.padding = FALSE)
# expect_character(unenc, any.missing = FALSE, len = length(plain), min.chars = 1L)
# expect_equal(plain, unenc)
# }
# })
test_that("encode and decode on random strings", {
for (i in 1:10) {
plain = rand(1000)
enc = base32_encode(plain, use.padding = TRUE)
expect_character(enc, any.missing = FALSE, len = length(plain), pattern = "^[A-Z234567=]+$")
unenc = base32_decode(enc, use.padding = TRUE)
expect_character(unenc, any.missing = FALSE, len = length(plain), min.chars = 1L)
expect_equal(plain, unenc)
}
})

test_that("unexpected input", {
expect_identical(base32_encode("", use.padding = TRUE), "")
Expand Down
31 changes: 17 additions & 14 deletions tests/testthat/test_base64.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,32 @@ base64dec_rfc = function(x) {
vapply(x, function(x) rawToChar(base64enc::base64decode(x)), NA_character_, USE.NAMES = FALSE)
}

# test_that("encode and decode on random strings", {
# for (i in 1:10) {
# plain = rand(1000)
# enc = base64_urlencode(plain)
# expect_character(enc, any.missing = FALSE, len = length(plain), pattern = "^[[:alnum:]_-]+$")
# unenc = base64_urldecode(enc)
# expect_character(unenc, any.missing = FALSE, len = length(plain), min.chars = 1L)
# expect_equal(plain, unenc)
# }
# })
test_that("encode and decode on random strings", {
for (i in 1:10) {
plain = rand(1000)
enc = base64_urlencode(plain)
expect_character(enc, any.missing = FALSE, len = length(plain), pattern = "^[[:alnum:]_-]+$")
unenc = base64_urldecode(enc)
expect_character(unenc, any.missing = FALSE, len = length(plain), min.chars = 1L)
expect_equal(plain, unenc)
}
})

test_that("comparison with base64enc", {
for (i in 1:10) {
plain = rand(1000)
plain = rand(1000, only.ascii = TRUE)
enc_rfc = base64enc_rfc(plain)
enc_url = base64_urlencode(plain)

# do we get plain back?
expect_equal(plain, base64_urldecode(enc_url), label = "url <-> url works")
expect_equal(plain, base64dec_rfc(enc_rfc), label = "rfc <-> rfc works")

# do we get the same encoding as rfc?
expect_equal(enc_url, convert_to_url(enc_rfc))
expect_equal(enc_rfc, convert_to_rfc(enc_url))

expect_equal(plain, base64_urldecode(enc_url))
expect_equal(plain, base64dec_rfc(enc_rfc))

# do we get the same decodings?
expect_equal(plain, base64_urldecode(convert_to_url(enc_rfc)))
expect_equal(plain, base64dec_rfc(convert_to_rfc(enc_url)))
}
Expand Down
2 changes: 1 addition & 1 deletion vignettes/Benchmarks.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ vignette: >
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r,include=FALSE}
```{r,include=FALSE,cache=FALSE}
do.eval = requireNamespace("microbenchmark", quietly = TRUE)
```

Expand Down

0 comments on commit 79f29a5

Please sign in to comment.