aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/encoding/hex/hex_test.v
blob: 62501e9ded321f76377036f2eade788e2f762719 (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
module hex

fn test_decode() ? {
	assert decode('') ? == []
	assert decode('0') ? == [byte(0x0)]
	assert decode('f') ? == [byte(0xf)]
	assert decode('0f') ? == [byte(0x0f)]
	assert decode('ff') ? == [byte(0xff)]
	assert decode('123') ? == [byte(0x1), 0x23]
	assert decode('1234') ? == [byte(0x12), 0x34]
	assert decode('12345') ? == [byte(0x1), 0x23, 0x45]
	assert decode('0123456789abcdef') ? == [byte(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
	assert decode('123456789ABCDEF') ? == [byte(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
}

fn test_decode_fails() ? {
	if x := decode('foo') {
		return error('expected decode to fail, got $x')
	}
	if x := decode('g') {
		return error('expected decode to fail, got $x')
	}
	if x := decode('000000000g') {
		return error('expected decode to fail, got $x')
	}
	if x := decode('_') {
		return error('expected decode to fail, got $x')
	}
	if x := decode('!') {
		return error('expected decode to fail, got $x')
	}
}

fn test_encode() ? {
	assert encode(decode('') ?) == ''
	assert encode(decode('0') ?) == '00'
	assert encode(decode('f') ?) == '0f'
	assert encode(decode('0f') ?) == '0f'
	assert encode(decode('ff') ?) == 'ff'
	assert encode(decode('123') ?) == '0123'
	assert encode(decode('1234') ?) == '1234'
	assert encode(decode('12345') ?) == '012345'
	assert encode(decode('abcdef') ?) == 'abcdef'
	assert encode(decode('ABCDEF') ?) == 'abcdef'
}

fn test_decode_0x() ? {
	assert decode('0x') ? == []
	assert decode('0x0') ? == [byte(0x0)]
	assert decode('0X1234') ? == [byte(0x12), 0x34]
	assert decode('0x12345') ? == [byte(0x1), 0x23, 0x45]
	assert decode('0x0123456789abcdef') ? == [byte(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
	assert decode('0X123456789ABCDEF') ? == [byte(0x01), 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]
}