base64.odin 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package test_encoding_base64
  2. import "base:intrinsics"
  3. import "core:encoding/base64"
  4. import "core:fmt"
  5. import "core:os"
  6. import "core:reflect"
  7. import "core:testing"
  8. TEST_count := 0
  9. TEST_fail := 0
  10. when ODIN_TEST {
  11. expect_value :: testing.expect_value
  12. } else {
  13. expect_value :: proc(t: ^testing.T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
  14. TEST_count += 1
  15. ok := value == expected || reflect.is_nil(value) && reflect.is_nil(expected)
  16. if !ok {
  17. TEST_fail += 1
  18. fmt.printf("[%v] expected %v, got %v\n", loc, expected, value)
  19. }
  20. return ok
  21. }
  22. }
  23. main :: proc() {
  24. t := testing.T{}
  25. test_encoding(&t)
  26. test_decoding(&t)
  27. fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
  28. if TEST_fail > 0 {
  29. os.exit(1)
  30. }
  31. }
  32. @(test)
  33. test_encoding :: proc(t: ^testing.T) {
  34. expect_value(t, base64.encode(transmute([]byte)string("")), "")
  35. expect_value(t, base64.encode(transmute([]byte)string("f")), "Zg==")
  36. expect_value(t, base64.encode(transmute([]byte)string("fo")), "Zm8=")
  37. expect_value(t, base64.encode(transmute([]byte)string("foo")), "Zm9v")
  38. expect_value(t, base64.encode(transmute([]byte)string("foob")), "Zm9vYg==")
  39. expect_value(t, base64.encode(transmute([]byte)string("fooba")), "Zm9vYmE=")
  40. expect_value(t, base64.encode(transmute([]byte)string("foobar")), "Zm9vYmFy")
  41. }
  42. @(test)
  43. test_decoding :: proc(t: ^testing.T) {
  44. expect_value(t, string(base64.decode("")), "")
  45. expect_value(t, string(base64.decode("Zg==")), "f")
  46. expect_value(t, string(base64.decode("Zm8=")), "fo")
  47. expect_value(t, string(base64.decode("Zm9v")), "foo")
  48. expect_value(t, string(base64.decode("Zm9vYg==")), "foob")
  49. expect_value(t, string(base64.decode("Zm9vYmE=")), "fooba")
  50. expect_value(t, string(base64.decode("Zm9vYmFy")), "foobar")
  51. }