aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/encoding/base58/base58_test.v
blob: 5cbd37b5bdc6cec56701c9bb35123747eb5939e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module base58

fn main() {
	test_encode_int() or {}
	test_decode_int() or {}
	test_encode_string()
	test_fails() or {}
}

fn test_encode_int() ? {
	a := 0x24 // should be 'd' in base58
	assert encode_int(a) ? == 'd'

	test_encode_int_walpha() ?
}

fn test_encode_int_walpha() ? {
	// random alphabet
	abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or {
		panic(@MOD + '.' + @FN + ': this should never happen')
	}
	a := 0x24 // should be '_' in base58 with our custom alphabet
	assert encode_int_walpha(a, abc) ? == '_'
}

fn test_decode_int() ? {
	a := 'd'
	assert decode_int(a) ? == 0x24

	test_decode_int_walpha() ?
}

fn test_decode_int_walpha() ? {
	abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or {
		panic(@MOD + '.' + @FN + ': this should never happen')
	}
	a := '_'
	assert decode_int_walpha(a, abc) ? == 0x24
}

fn test_encode_string() {
	// should be 'TtaR6twpTGu8VpY' in base58 and '0P7yfPSL0pQh2L5' with our custom alphabet
	a := 'lorem ipsum'
	assert encode(a) == 'TtaR6twpTGu8VpY'

	abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or {
		panic(@MOD + '.' + @FN + ': this should never happen')
	}
	assert encode_walpha(a, abc) == '0P7yfPSL0pQh2L5'
}

fn test_decode_string() ? {
	a := 'TtaR6twpTGu8VpY'
	assert decode(a) ? == 'lorem ipsum'

	abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') or {
		panic(@MOD + '.' + @FN + ': this should never happen')
	}
	b := '0P7yfPSL0pQh2L5'
	assert decode_walpha(b, abc) ? == 'lorem ipsum'
}

fn test_fails() ? {
	a := -238
	b := 0
	if z := encode_int(a) {
		return error(@MOD + '.' + @FN + ': expected encode_int to fail, got $z')
	}
	if z := encode_int(b) {
		return error(@MOD + '.' + @FN + ': expected encode_int to fail, got $z')
	}

	c := '!'
	if z := decode_int(c) {
		return error(@MOD + '.' + @FN + ': expected decode_int to fail, got $z')
	}
	if z := decode(c) {
		return error(@MOD + '.' + @FN + ': expected decode to fail, got $z')
	}

	// repeating character
	if abc := new_alphabet('aaaaafghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUV') {
		return error(@MOD + '.' + @FN + ': expected new_alphabet to fail, got $abc')
	}
	// more than 58 characters long
	if abc := new_alphabet('abcdefghij\$lmnopqrstuvwxyz0123456789_ABCDEFGHIJLMNOPQRSTUVWXYZ') {
		return error(@MOD + '.' + @FN + ': expected new_alphabet to fail, got $abc')
	}
}