print(x <- (1:5)*pi)
print(y <- bin2raw(x))
print(z <- raw2bin(y,"double"))
x = (10*runif(10)>5) # logical
for (i in c(NA, 1, 2, 4)) {
y = bin2raw(x, size=i)
z = raw2bin(y,typeof(x), size=i)
stopifnot(x==z)
}
print("Checked bin2raw and raw2bin conversion for logical type")
x = as.integer(1:10) # integer
for (i in c(NA, 1, 2, 4)) {
y = bin2raw(x, size=i)
z = raw2bin(y,typeof(x), size=i)
stopifnot(x==z)
}
print("Checked bin2raw and raw2bin conversion for integer type")
x = (1:10)*pi # double
for (i in c(NA, 4, 8)) {
y = bin2raw(x, size=i)
z = raw2bin(y,typeof(x), size=i)
stopifnot(mean(abs(x-z))<1e-5)
}
print("Checked bin2raw and raw2bin conversion for double type")
x = log(as.complex(-(1:10)*pi)) # complex
y = bin2raw(x)
z = raw2bin(y,typeof(x))
stopifnot(x==z)
print("Checked bin2raw and raw2bin conversion for complex type")
x = "Chance favors the prepared mind" # character
y = bin2raw(x)
z = raw2bin(y,typeof(x))
stopifnot(x==z)
print("Checked bin2raw and raw2bin conversion for character type")
x=(1:10000000)*pi
system.time(raw2bin(bin2raw(x),typeof(x)))
system.time(readBin(writeBin(x, raw()), typeof(x), length(x)))
Run the code above in your browser using DataLab