aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLaytan Laats <laytanlaats@hotmail.com>2023-12-16 21:40:41 +0100
committerLaytan Laats <laytanlaats@hotmail.com>2024-03-04 17:26:17 +0100
commitb6c47e796390924faabd236204bc620ea35c1d13 (patch)
treec8d7912237ec13e81ec4bcb04418d2dfe0979584 /tests
parent5533a327eb0f526cbebbe71124620fcbb0bc0649 (diff)
encoding/base64: add decode_into, add tests
Diffstat (limited to 'tests')
-rw-r--r--tests/core/Makefile3
-rw-r--r--tests/core/build.bat2
-rw-r--r--tests/core/encoding/base64/base64.odin60
3 files changed, 65 insertions, 0 deletions
diff --git a/tests/core/Makefile b/tests/core/Makefile
index 1fca7bf97..3fa38cd34 100644
--- a/tests/core/Makefile
+++ b/tests/core/Makefile
@@ -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
diff --git a/tests/core/build.bat b/tests/core/build.bat
index 5bf8e1ead..b9fc4e828 100644
--- a/tests/core/build.bat
+++ b/tests/core/build.bat
@@ -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
diff --git a/tests/core/encoding/base64/base64.odin b/tests/core/encoding/base64/base64.odin
new file mode 100644
index 000000000..41dbba683
--- /dev/null
+++ b/tests/core/encoding/base64/base64.odin
@@ -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")
+}