Debian Codegraph @

tests/test-cran/test-base64.R

raw
1 library(testit)
2
3 assert('base64_encode() encodes the string correctly', {
4 ref = c(
5 "AQ==", "AQI=", "AQID", "AQIDBA==", "AQIDBAU=", "AQIDBAUG",
6 "AQIDBAUGBw==", "AQIDBAUGBwg=", "AQIDBAUGBwgJ", "AQIDBAUGBwgJCg=="
7 )
8 (sapply(1:10, function(i) base64_encode(as.raw(1:i))) == ref)
9
10 ref = c(
11 "/w==", "//4=", "//79", "//79/A==", "//79/Ps=", "//79/Pv6",
12 "//79/Pv6+Q==", "//79/Pv6+fg=", "//79/Pv6+fj3", "//79/Pv6+fj39g=="
13 )
14 (sapply(255:246, function(i) base64_encode(as.raw(255:i))) == ref)
15 })
16
17 assert('base64_decode() decodes the string correctly', {
18 (sapply(c(1:10, 255:246), function(i) {
19 input = as.raw(1:i)
20 output = base64_encode(input)
21 input2 = base64_decode(output)
22 input %==% input2
23 }))
24 })
25
26 assert('base64_decode() will not make R crash if the input is not valid', {
27 (has_error(base64_decode("lz..??")))
28 })
29
30 assert('base64_encode_r() returns the same result as base64_encode()', {
31 f = R_logo()
32 (base64_encode_r(f) %==% base64_encode(f))
33 })
34
35 assert('base64_decode() does not accept a non-string input', {
36 (has_error(base64_decode(x = 42)))
37 })
38
39 assert('base64_decode() does not accept both string and file input', {
40 f = tempfile()
41 (has_error(base64_decode(x = 'Kg==', from = f)))
42 })
43
44 assert('base64_decode() returns the same result when the same string is used as an input directly or from a file connection', {
45 f = tempfile()
46 writeLines(text = "Kg==", con = f, sep = "")
47 (base64_decode(from = f) %==% base64_decode(x = 'Kg=='))
48 })
49
50 assert('base64_uri() returns proper data type', {
51 f = R_logo()
52 (!grepl('[.]svg$', f) || strsplit(base64_uri(f), split = ';')[[1]][1] %==% 'data:image/svg+xml')
53 })
54
55 assert('base64_encode_r() handles all remainder cases (n mod 3)', {
56 # n mod 3 == 1 (e.g., 1 byte): 1 data byte -> 2 base64 chars + "=="
57 (base64_encode_r(as.raw(0xff)) %==% base64_encode(as.raw(0xff)))
58 # n mod 3 == 2 (e.g., 2 bytes): 2 data bytes -> 3 base64 chars + "="
59 (base64_encode_r(as.raw(1:2)) %==% base64_encode(as.raw(1:2)))
60 # n mod 3 == 0 (e.g., 3 bytes): no padding
61 (base64_encode_r(as.raw(1:3)) %==% base64_encode(as.raw(1:3)))
62 })
63
64 assert('mime_type() returns correct types', {
65 if (loadable('mime')) {
66 f = tempfile(fileext = '.png'); file.create(f)
67 (mime_type(f) %==% 'image/png')
68 unlink(f)
69 }
70 })
71
72 assert('mime_type() without mime package uses system command', {
73 f = tempfile(fileext = '.css'); writeLines('body {}', f)
74 m = mime_type(f, use_mime = FALSE)
75 (m %==% 'text/css')
76 unlink(f)
77 })