aboutsummaryrefslogtreecommitdiff
path: root/tests/core/encoding/hex/test_core_hex.odin
blob: 6a00c970597a75e810cfc2baa80bac449eff8ec5 (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
package test_core_hex

import "core:encoding/hex"
import "core:testing"

CASES :: [][2]string{
	{"11", "3131"},
	{"g", "67"},
	{"Hello", "48656c6c6f"},
}

@(test)
hex_encode :: proc(t: ^testing.T) {
	for test in CASES {
		encoded := string(hex.encode(transmute([]byte)test[0]))
		defer delete(encoded)
		testing.expectf(
			t,
			encoded == test[1],
			"encode: %q -> %q (should be: %q)",
			test[0],
			encoded,
			test[1],
		)
	}
}

@(test)
hex_decode :: proc(t: ^testing.T) {
	for test in CASES {
		decoded, ok := hex.decode(transmute([]byte)test[1])
		defer delete(decoded)
		testing.expectf(
			t,
			ok,
			"decode: %q not ok",
			test[1],
		)
		testing.expectf(
			t,
			string(decoded) == test[0],
			"decode: %q -> %q (should be: %q)",
			test[1],
			string(decoded),
			test[0],
		)
	}
}

@(test)
hex_decode_sequence :: proc(t: ^testing.T) {
	b, ok := hex.decode_sequence("0x23")
	testing.expect(t, ok, "decode_sequence: 0x23 not ok")
	testing.expectf(
		t,
		b == '#',
		"decode_sequence: 0x23 -> %c (should be: %c)",
		b,
		'#',
	)

	b, ok = hex.decode_sequence("0X3F")
	testing.expect(t, ok, "decode_sequence: 0X3F not ok")
	testing.expectf(
		t,
		b == '?',
		"decode_sequence: 0X3F -> %c (should be: %c)",
		b,
		'?',
	)

	b, ok = hex.decode_sequence("2a")
	testing.expect(t, ok, "decode_sequence: 2a not ok")
	testing.expectf(t,
		b == '*',
		"decode_sequence: 2a -> %c (should be: %c)",
		b,
		'*',
	)

	_, ok = hex.decode_sequence("1")
	testing.expect(t, !ok, "decode_sequence: 1 should be too short")

	_, ok = hex.decode_sequence("123")
	testing.expect(t, !ok, "decode_sequence: 123 should be too long")
}