diff --git a/tests/reg-encodings.R b/tests/reg-encodings.R index de66237f184..9d35a9ddec5 100644 --- a/tests/reg-encodings.R +++ b/tests/reg-encodings.R @@ -336,3 +336,22 @@ source(tf, encoding = c("UTF-8", "latin1")) ## in R 4.2.{0,1} gave Warning (that would now be an error): ## 'length(x) = 2 > 1' in coercion to 'logical(1)' if (UTF8) stopifnot(identical(Encoding(x), "UTF-8")) + +## Check that UTF-16 with BOM can be read from a connection. This tests a +## work-around in R for a bug in libiconv-86 on macOS (at least since +## libiconv-107). +words <- c(0xfeff, 0x30+c(1:9,0,1:9,0), 0x0a) # bom + 12345678901234567890 + newline +hi <- as.raw(words %/% 0x100) +low <- as.raw(words %% 0x100) +be <- c(rbind(hi, low)) +befile <- tempfile("be_", fileext=".txt") +writeBin(be, befile) +becon <- file(befile, encoding = "UTF-16", open="r") +stopifnot(identical(readLines(becon), "12345678901234567890")) +close(becon) +le <- c(rbind(low, hi)) +lefile <- tempfile("le_", fileext=".txt") +writeBin(le, lefile) +lecon <- file(lefile, encoding = "UTF-16", open="r") +stopifnot(identical(readLines(lecon), "12345678901234567890")) +close(lecon)