encoding/base64: add decode_into, add tests

This commit is contained in:
Laytan Laats
2023-12-16 21:40:41 +01:00
parent 5533a327eb
commit b6c47e7963
4 changed files with 158 additions and 46 deletions
+3
View File
@@ -51,11 +51,14 @@ noise_test:
$(ODIN) run math/noise $(COMMON) -out:test_noise
encoding_test:
<<<<<<< HEAD
$(ODIN) run encoding/hxa $(COMMON) $(COLLECTION) -out:test_hxa
$(ODIN) run encoding/json $(COMMON) -out:test_json
$(ODIN) run encoding/varint $(COMMON) -out:test_varint
$(ODIN) run encoding/xml $(COMMON) -out:test_xml
$(ODIN) run encoding/cbor $(COMMON) -out:test_cbor
$(ODIN) run encoding/hex $(COMMON) -out:test_hex
$(ODIN) run encoding/base64 $(COMMON) -out:test_base64
math_test:
$(ODIN) run math $(COMMON) $(COLLECTION) -out:test_core_math
+2
View File
@@ -41,6 +41,8 @@ rem %PATH_TO_ODIN% run encoding/hxa %COMMON% %COLLECTION% -out:test_hxa.exe |
%PATH_TO_ODIN% run encoding/varint %COMMON% -out:test_varint.exe || exit /b
%PATH_TO_ODIN% run encoding/xml %COMMON% -out:test_xml.exe || exit /b
%PATH_TO_ODIN% test encoding/cbor %COMMON% -out:test_cbor.exe || exit /b
%PATH_TO_ODIN% run encoding/hex %COMMON% -out:test_hex.exe || exit /b
%PATH_TO_ODIN% run encoding/base64 %COMMON% -out:test_base64.exe || exit /b
echo ---
echo Running core:math/noise tests
+60
View File
@@ -0,0 +1,60 @@
package test_encoding_base64
import "core:encoding/base64"
import "core:fmt"
import "core:intrinsics"
import "core:os"
import "core:reflect"
import "core:testing"
TEST_count := 0
TEST_fail := 0
when ODIN_TEST {
expect_value :: testing.expect_value
} else {
expect_value :: proc(t: ^testing.T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
TEST_count += 1
ok := value == expected || reflect.is_nil(value) && reflect.is_nil(expected)
if !ok {
TEST_fail += 1
fmt.printf("[%v] expected %v, got %v\n", loc, expected, value)
}
return ok
}
}
main :: proc() {
t := testing.T{}
test_encoding(&t)
test_decoding(&t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
if TEST_fail > 0 {
os.exit(1)
}
}
@(test)
test_encoding :: proc(t: ^testing.T) {
expect_value(t, base64.encode(transmute([]byte)string("")), "")
expect_value(t, base64.encode(transmute([]byte)string("f")), "Zg==")
expect_value(t, base64.encode(transmute([]byte)string("fo")), "Zm8=")
expect_value(t, base64.encode(transmute([]byte)string("foo")), "Zm9v")
expect_value(t, base64.encode(transmute([]byte)string("foob")), "Zm9vYg==")
expect_value(t, base64.encode(transmute([]byte)string("fooba")), "Zm9vYmE=")
expect_value(t, base64.encode(transmute([]byte)string("foobar")), "Zm9vYmFy")
}
@(test)
test_decoding :: proc(t: ^testing.T) {
expect_value(t, string(base64.decode("")), "")
expect_value(t, string(base64.decode("Zg==")), "f")
expect_value(t, string(base64.decode("Zm8=")), "fo")
expect_value(t, string(base64.decode("Zm9v")), "foo")
expect_value(t, string(base64.decode("Zm9vYg==")), "foob")
expect_value(t, string(base64.decode("Zm9vYmE=")), "fooba")
expect_value(t, string(base64.decode("Zm9vYmFy")), "foobar")
}