v / vlib / encoding / base58 / base58_usage_test.v
42 lines · 39 sloc · 1.37 KB · c5e8ad19dfd6b57396b7781d423195305ce8c6b4
Raw
1import encoding.base58
2import encoding.hex
3
4fn test_encode() {
5 for input, expected in {
6 '': ''
7 '6263': '2PMCRQ'
8 'hello world': 'StV1DL6CwTryKyV'
9 '\x00\x00hello world': '11StV1DL6CwTryKyV'
10 } {
11 output := base58.encode(input)
12 println('> input: `${input}` | ${input.hex()} | => output: `${output}`')
13 assert output == expected
14 }
15}
16
17fn test_encode_int() {
18 for input, expected in {
19 0x6263: '8VG'
20 0x61: '2g'
21 0x626262: 'a3gV'
22 0x636363: 'aPEr'
23 } {
24 output := base58.encode_int(input)!
25 println('> input: 0x${input:x} | => output: `${output}`')
26 assert output == expected
27 }
28}
29
30fn test_decode() {
31 for output, expected in {
32 'USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z': 'The quick brown fox jumps over the lazy dog.'
33 '11StV1DL6CwTryKyV': '\x00\x00hello world'
34 '2NEpo7TZRRrLZSi2U': 'Hello World!'
35 '14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3': hex.decode('0027b5891b01da2db74cde1689a97a2acbe23d5fb1c0205bf6')!.bytestr()
36 '3vQB7B6MrGQZaxCuFg4oh': hex.decode('68656c6c6f20776f726c64bc62d4b8')!.bytestr()
37 } {
38 input := base58.decode(output)!
39 println('> output: `${output}` | decoded input: `${input}` | bytes: ${input.hex()}')
40 assert input.hex() == expected.hex()
41 }
42}
43