1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/encoding/base64/base64_memory_test.v
Sebastian Schicho 9995f6cca1
encoding.base64: speed up encoding and decoding (#11055)
* add new function headers

* new encoding function

* rename to decode_micro and add helper functions

* implement new decoding function

* add test for buffer to buffer decoding

* - add notice to GO code
- v fmt base64.v

* implement new decoding function

* fix base64_memory_test.v by commenting a few lines

* vfmt base64.v

* add some more asserts to base64_memory_test.v

* remove unused decoding function

* add bounds check, when detecting the padding

* use union for storing the decoded data

Co-authored-by: Delyan Angelov <delian66@gmail.com>
2021-08-07 00:09:55 +03:00

54 lines
1.5 KiB
V

import encoding.base64
fn test_long_encoding() {
repeats := 1000
input_size := 3000
s_original := []byte{len: input_size, init: `a`}
s_encoded := base64.encode(s_original)
s_decoded := base64.decode(s_encoded)
assert s_encoded.len > s_original.len
assert s_original == s_decoded
ebuffer := unsafe { malloc(s_encoded.len) }
dbuffer := unsafe { malloc(s_decoded.len) }
//
encoded_size := base64.encode_in_buffer(s_original, ebuffer)
mut encoded_in_buf := []byte{len: encoded_size}
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`
assert encoded_in_buf == s_encoded.bytes()
decoded_size := base64.decode_in_buffer(s_encoded, dbuffer)
assert decoded_size == input_size
mut decoded_in_buf := []byte{len: decoded_size}
unsafe { C.memcpy(decoded_in_buf.data, dbuffer, decoded_size) }
assert decoded_in_buf == s_original
mut s := 0
for _ in 0 .. repeats {
resultsize := base64.encode_in_buffer(s_original, ebuffer)
s += resultsize
assert resultsize == s_encoded.len
}
for _ in 0 .. repeats {
resultsize := base64.decode_in_buffer(s_encoded, dbuffer)
s += resultsize
assert resultsize == s_decoded.len
}
println('Final s: $s')
// assert s == 39147008
}