mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Port tests\encoding\hex
This commit is contained in:
@@ -2,8 +2,8 @@ package encoding_hex
|
|||||||
|
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
encode :: proc(src: []byte, allocator := context.allocator) -> []byte #no_bounds_check {
|
encode :: proc(src: []byte, allocator := context.allocator, loc := #caller_location) -> []byte #no_bounds_check {
|
||||||
dst := make([]byte, len(src) * 2, allocator)
|
dst := make([]byte, len(src) * 2, allocator, loc)
|
||||||
for i, j := 0, 0; i < len(src); i += 1 {
|
for i, j := 0, 0; i < len(src); i += 1 {
|
||||||
v := src[i]
|
v := src[i]
|
||||||
dst[j] = HEXTABLE[v>>4]
|
dst[j] = HEXTABLE[v>>4]
|
||||||
@@ -15,12 +15,12 @@ encode :: proc(src: []byte, allocator := context.allocator) -> []byte #no_bounds
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
decode :: proc(src: []byte, allocator := context.allocator) -> (dst: []byte, ok: bool) #no_bounds_check {
|
decode :: proc(src: []byte, allocator := context.allocator, loc := #caller_location) -> (dst: []byte, ok: bool) #no_bounds_check {
|
||||||
if len(src) % 2 == 1 {
|
if len(src) % 2 == 1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dst = make([]byte, len(src) / 2, allocator)
|
dst = make([]byte, len(src) / 2, allocator, loc)
|
||||||
for i, j := 0, 1; j < len(src); j += 2 {
|
for i, j := 0, 1; j < len(src); j += 2 {
|
||||||
p := src[j-1]
|
p := src[j-1]
|
||||||
q := src[j]
|
q := src[j]
|
||||||
@@ -69,5 +69,4 @@ hex_digit :: proc(char: byte) -> (u8, bool) {
|
|||||||
case 'A' ..= 'F': return char - 'A' + 10, true
|
case 'A' ..= 'F': return char - 'A' + 10, true
|
||||||
case: return 0, false
|
case: return 0, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
+1
-1
@@ -47,7 +47,7 @@ crypto_test:
|
|||||||
encoding_test:
|
encoding_test:
|
||||||
$(ODIN) test encoding/base64 $(COMMON) -out:test_base64
|
$(ODIN) test encoding/base64 $(COMMON) -out:test_base64
|
||||||
$(ODIN) test encoding/cbor $(COMMON) -out:test_cbor
|
$(ODIN) test encoding/cbor $(COMMON) -out:test_cbor
|
||||||
$(ODIN) run encoding/hex $(COMMON) -out:test_hex
|
$(ODIN) test encoding/hex $(COMMON) -out:test_hex
|
||||||
$(ODIN) run encoding/hxa $(COMMON) $(COLLECTION) -out:test_hxa
|
$(ODIN) run encoding/hxa $(COMMON) $(COLLECTION) -out:test_hxa
|
||||||
$(ODIN) run encoding/json $(COMMON) -out:test_json
|
$(ODIN) run encoding/json $(COMMON) -out:test_json
|
||||||
$(ODIN) run encoding/varint $(COMMON) -out:test_varint
|
$(ODIN) run encoding/varint $(COMMON) -out:test_varint
|
||||||
|
|||||||
@@ -2,42 +2,6 @@ package test_core_hex
|
|||||||
|
|
||||||
import "core:encoding/hex"
|
import "core:encoding/hex"
|
||||||
import "core:testing"
|
import "core:testing"
|
||||||
import "core:fmt"
|
|
||||||
import "core:os"
|
|
||||||
|
|
||||||
TEST_count := 0
|
|
||||||
TEST_fail := 0
|
|
||||||
|
|
||||||
when ODIN_TEST {
|
|
||||||
expect :: testing.expect
|
|
||||||
log :: testing.log
|
|
||||||
} else {
|
|
||||||
expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
|
|
||||||
TEST_count += 1
|
|
||||||
if !condition {
|
|
||||||
TEST_fail += 1
|
|
||||||
fmt.printf("[%v] %v\n", loc, message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
|
|
||||||
fmt.printf("[%v] ", loc)
|
|
||||||
fmt.printf("log: %v\n", v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
main :: proc() {
|
|
||||||
t := testing.T{}
|
|
||||||
|
|
||||||
hex_encode(&t)
|
|
||||||
hex_decode(&t)
|
|
||||||
hex_decode_sequence(&t)
|
|
||||||
|
|
||||||
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
|
||||||
if TEST_fail > 0 {
|
|
||||||
os.exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CASES :: [][2]string{
|
CASES :: [][2]string{
|
||||||
{"11", "3131"},
|
{"11", "3131"},
|
||||||
@@ -49,10 +13,14 @@ CASES :: [][2]string{
|
|||||||
hex_encode :: proc(t: ^testing.T) {
|
hex_encode :: proc(t: ^testing.T) {
|
||||||
for test in CASES {
|
for test in CASES {
|
||||||
encoded := string(hex.encode(transmute([]byte)test[0]))
|
encoded := string(hex.encode(transmute([]byte)test[0]))
|
||||||
expect(
|
defer delete(encoded)
|
||||||
|
testing.expectf(
|
||||||
t,
|
t,
|
||||||
encoded == test[1],
|
encoded == test[1],
|
||||||
fmt.tprintf("encode: %q -> %q (should be: %q)", test[0], encoded, test[1]),
|
"encode: %q -> %q (should be: %q)",
|
||||||
|
test[0],
|
||||||
|
encoded,
|
||||||
|
test[1],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,11 +29,20 @@ hex_encode :: proc(t: ^testing.T) {
|
|||||||
hex_decode :: proc(t: ^testing.T) {
|
hex_decode :: proc(t: ^testing.T) {
|
||||||
for test in CASES {
|
for test in CASES {
|
||||||
decoded, ok := hex.decode(transmute([]byte)test[1])
|
decoded, ok := hex.decode(transmute([]byte)test[1])
|
||||||
expect(t, ok, fmt.tprintf("decode: %q not ok", test[1]))
|
defer delete(decoded)
|
||||||
expect(
|
testing.expectf(
|
||||||
|
t,
|
||||||
|
ok,
|
||||||
|
"decode: %q not ok",
|
||||||
|
test[1],
|
||||||
|
)
|
||||||
|
testing.expectf(
|
||||||
t,
|
t,
|
||||||
string(decoded) == test[0],
|
string(decoded) == test[0],
|
||||||
fmt.tprintf("decode: %q -> %q (should be: %q)", test[1], string(decoded), test[0]),
|
"decode: %q -> %q (should be: %q)",
|
||||||
|
test[1],
|
||||||
|
string(decoded),
|
||||||
|
test[0],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,20 +50,37 @@ hex_decode :: proc(t: ^testing.T) {
|
|||||||
@(test)
|
@(test)
|
||||||
hex_decode_sequence :: proc(t: ^testing.T) {
|
hex_decode_sequence :: proc(t: ^testing.T) {
|
||||||
b, ok := hex.decode_sequence("0x23")
|
b, ok := hex.decode_sequence("0x23")
|
||||||
expect(t, ok, "decode_sequence: 0x23 not ok")
|
testing.expect(t, ok, "decode_sequence: 0x23 not ok")
|
||||||
expect(t, b == '#', fmt.tprintf("decode_sequence: 0x23 -> %c (should be: %c)", b, '#'))
|
testing.expectf(
|
||||||
|
t,
|
||||||
|
b == '#',
|
||||||
|
"decode_sequence: 0x23 -> %c (should be: %c)",
|
||||||
|
b,
|
||||||
|
'#',
|
||||||
|
)
|
||||||
|
|
||||||
b, ok = hex.decode_sequence("0X3F")
|
b, ok = hex.decode_sequence("0X3F")
|
||||||
expect(t, ok, "decode_sequence: 0X3F not ok")
|
testing.expect(t, ok, "decode_sequence: 0X3F not ok")
|
||||||
expect(t, b == '?', fmt.tprintf("decode_sequence: 0X3F -> %c (should be: %c)", b, '?'))
|
testing.expectf(
|
||||||
|
t,
|
||||||
|
b == '?',
|
||||||
|
"decode_sequence: 0X3F -> %c (should be: %c)",
|
||||||
|
b,
|
||||||
|
'?',
|
||||||
|
)
|
||||||
|
|
||||||
b, ok = hex.decode_sequence("2a")
|
b, ok = hex.decode_sequence("2a")
|
||||||
expect(t, ok, "decode_sequence: 2a not ok")
|
testing.expect(t, ok, "decode_sequence: 2a not ok")
|
||||||
expect(t, b == '*', fmt.tprintf("decode_sequence: 2a -> %c (should be: %c)", b, '*'))
|
testing.expectf(t,
|
||||||
|
b == '*',
|
||||||
|
"decode_sequence: 2a -> %c (should be: %c)",
|
||||||
|
b,
|
||||||
|
'*',
|
||||||
|
)
|
||||||
|
|
||||||
_, ok = hex.decode_sequence("1")
|
_, ok = hex.decode_sequence("1")
|
||||||
expect(t, !ok, "decode_sequence: 1 should be too short")
|
testing.expect(t, !ok, "decode_sequence: 1 should be too short")
|
||||||
|
|
||||||
_, ok = hex.decode_sequence("123")
|
_, ok = hex.decode_sequence("123")
|
||||||
expect(t, !ok, "decode_sequence: 123 should be too long")
|
testing.expect(t, !ok, "decode_sequence: 123 should be too long")
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user