| 1 | module base58 |
| 2 | |
| 3 | fn test_encode_int() { |
| 4 | a := 0x24 // should be 'd' in base58 |
| 5 | assert encode_int(a)! == 'd' |
| 6 | |
| 7 | test_encode_int_walpha() |
| 8 | } |
| 9 | |
| 10 | fn test_encode_int_walpha() { |
| 11 | // random alphabet |
| 12 | abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or { |
| 13 | panic(@MOD + '.' + @FN + ': this should never happen') |
| 14 | } |
| 15 | a := 0x24 // should be '_' in base58 with our custom alphabet |
| 16 | assert encode_int_walpha(a, abc)! == '_' |
| 17 | } |
| 18 | |
| 19 | fn test_decode_int() { |
| 20 | a := 'd' |
| 21 | assert decode_int(a)! == 0x24 |
| 22 | |
| 23 | test_decode_int_walpha() |
| 24 | } |
| 25 | |
| 26 | fn test_decode_int_walpha() { |
| 27 | abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or { |
| 28 | panic(@MOD + '.' + @FN + ': this should never happen') |
| 29 | } |
| 30 | a := '_' |
| 31 | assert decode_int_walpha(a, abc)! == 0x24 |
| 32 | } |
| 33 | |
| 34 | fn test_encode_string() { |
| 35 | // should be 'TtaR6twpTGu8VpY' in base58 and '0P7yfPSL0pQh2L5' with our custom alphabet |
| 36 | a := 'lorem ipsum' |
| 37 | assert encode(a) == 'TtaR6twpTGu8VpY' |
| 38 | |
| 39 | aa := '0JHRktAz1yEtdGl6RnO' |
| 40 | assert encode(aa) == '9qkYDxBXPdc6nW825HkNx7oFMY' |
| 41 | |
| 42 | abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or { |
| 43 | panic(@MOD + '.' + @FN + ': this should never happen') |
| 44 | } |
| 45 | assert encode_walpha(a, abc) == '0P7yfPSL0pQh2L5' |
| 46 | } |
| 47 | |
| 48 | fn test_decode_string() { |
| 49 | a := 'TtaR6twpTGu8VpY' |
| 50 | assert decode(a)! == 'lorem ipsum' |
| 51 | |
| 52 | abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or { |
| 53 | panic(@MOD + '.' + @FN + ': this should never happen') |
| 54 | } |
| 55 | b := '0P7yfPSL0pQh2L5' |
| 56 | assert decode_walpha(b, abc)! == 'lorem ipsum' |
| 57 | } |
| 58 | |
| 59 | fn test_fails() ! { |
| 60 | a := -238 |
| 61 | b := 0 |
| 62 | if z := encode_int(a) { |
| 63 | return error(@MOD + '.' + @FN + ': expected encode_int to fail, got ${z}') |
| 64 | } |
| 65 | if z := encode_int(b) { |
| 66 | return error(@MOD + '.' + @FN + ': expected encode_int to fail, got ${z}') |
| 67 | } |
| 68 | |
| 69 | c := '!' |
| 70 | if z := decode_int(c) { |
| 71 | return error(@MOD + '.' + @FN + ': expected decode_int to fail, got ${z}') |
| 72 | } |
| 73 | if z := decode(c) { |
| 74 | return error(@MOD + '.' + @FN + ': expected decode to fail, got ${z}') |
| 75 | } |
| 76 | |
| 77 | // repeating character |
| 78 | if abc := new_alphabet('aaaaafghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') { |
| 79 | return error(@MOD + '.' + @FN + ': expected new_alphabet to fail, got ${abc}') |
| 80 | } |
| 81 | // more than 58 characters long |
| 82 | if abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUVWXYZ') { |
| 83 | return error(@MOD + '.' + @FN + ': expected new_alphabet to fail, got ${abc}') |
| 84 | } |
| 85 | } |
| 86 | |