2019-10-26 17:20:36 +02:00
|
|
|
import encoding.base64
|
|
|
|
|
2021-02-21 16:05:03 +01:00
|
|
|
fn test_long_encoding() {
|
2019-10-26 17:20:36 +02:00
|
|
|
repeats := 1000
|
|
|
|
input_size := 3000
|
2019-12-07 13:13:23 +01:00
|
|
|
|
2022-04-15 14:35:35 +02:00
|
|
|
s_original := []u8{len: input_size, init: `a`}
|
2019-10-26 17:20:36 +02:00
|
|
|
s_encoded := base64.encode(s_original)
|
2021-08-13 17:37:34 +02:00
|
|
|
s_encoded_bytes := s_encoded.bytes()
|
2019-10-26 17:20:36 +02:00
|
|
|
s_decoded := base64.decode(s_encoded)
|
2019-12-07 13:13:23 +01:00
|
|
|
|
2019-10-26 17:20:36 +02:00
|
|
|
assert s_encoded.len > s_original.len
|
|
|
|
assert s_original == s_decoded
|
|
|
|
|
2021-02-21 16:05:03 +01:00
|
|
|
ebuffer := unsafe { malloc(s_encoded.len) }
|
2021-08-06 23:09:55 +02:00
|
|
|
dbuffer := unsafe { malloc(s_decoded.len) }
|
2021-08-13 17:37:34 +02:00
|
|
|
defer {
|
|
|
|
unsafe { free(ebuffer) }
|
|
|
|
unsafe { free(dbuffer) }
|
|
|
|
}
|
2021-08-06 23:09:55 +02:00
|
|
|
//
|
|
|
|
encoded_size := base64.encode_in_buffer(s_original, ebuffer)
|
2022-04-15 14:35:35 +02:00
|
|
|
mut encoded_in_buf := []u8{len: encoded_size}
|
2021-08-06 23:09:55 +02:00
|
|
|
unsafe { C.memcpy(encoded_in_buf.data, ebuffer, encoded_size) }
|
|
|
|
assert input_size * 4 / 3 == encoded_size
|
|
|
|
assert encoded_in_buf[0] == `Y`
|
|
|
|
assert encoded_in_buf[1] == `W`
|
|
|
|
assert encoded_in_buf[2] == `F`
|
|
|
|
assert encoded_in_buf[3] == `h`
|
|
|
|
|
|
|
|
assert encoded_in_buf[encoded_size - 4] == `Y`
|
|
|
|
assert encoded_in_buf[encoded_size - 3] == `W`
|
|
|
|
assert encoded_in_buf[encoded_size - 2] == `F`
|
|
|
|
assert encoded_in_buf[encoded_size - 1] == `h`
|
2021-08-13 17:37:34 +02:00
|
|
|
|
|
|
|
assert encoded_in_buf == s_encoded_bytes
|
2021-08-06 23:09:55 +02:00
|
|
|
|
|
|
|
decoded_size := base64.decode_in_buffer(s_encoded, dbuffer)
|
|
|
|
assert decoded_size == input_size
|
2022-04-15 14:35:35 +02:00
|
|
|
mut decoded_in_buf := []u8{len: decoded_size}
|
2021-08-06 23:09:55 +02:00
|
|
|
unsafe { C.memcpy(decoded_in_buf.data, dbuffer, decoded_size) }
|
|
|
|
assert decoded_in_buf == s_original
|
|
|
|
|
|
|
|
mut s := 0
|
2021-02-21 16:05:03 +01:00
|
|
|
for _ in 0 .. repeats {
|
2019-12-07 13:13:23 +01:00
|
|
|
resultsize := base64.encode_in_buffer(s_original, ebuffer)
|
2019-10-26 17:20:36 +02:00
|
|
|
s += resultsize
|
|
|
|
assert resultsize == s_encoded.len
|
|
|
|
}
|
|
|
|
|
2021-02-21 16:05:03 +01:00
|
|
|
for _ in 0 .. repeats {
|
2019-12-07 13:13:23 +01:00
|
|
|
resultsize := base64.decode_in_buffer(s_encoded, dbuffer)
|
2019-10-26 17:20:36 +02:00
|
|
|
s += resultsize
|
|
|
|
assert resultsize == s_decoded.len
|
|
|
|
}
|
|
|
|
|
2021-02-21 16:05:03 +01:00
|
|
|
println('Final s: $s')
|
2019-10-26 17:20:36 +02:00
|
|
|
// assert s == 39147008
|
|
|
|
}
|