summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authortreeform <starplant@gmail.com>2019-10-17 00:50:00 -0700
committerAndreas Rumpf <rumpf_a@web.de>2019-10-17 09:50:00 +0200
commit5ba932e43c9c971555d8fdc9e25e2edcdcdd70b4 (patch)
tree6dd27e515d5af0408efa64f84d6a460f4d98850b /tests
parent37dfb7ecc6b482578bdd8778379f506032b961c9 (diff)
downloadNim-5ba932e43c9c971555d8fdc9e25e2edcdcdd70b4.tar.gz
About 50% faster base64 implemention. (#12436)
Diffstat (limited to 'tests')
-rw-r--r--tests/stdlib/tbase64.nim44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/stdlib/tbase64.nim b/tests/stdlib/tbase64.nim
new file mode 100644
index 000000000..9db6e8802
--- /dev/null
+++ b/tests/stdlib/tbase64.nim
@@ -0,0 +1,44 @@
+discard """
+  output: "OK"
+"""
+import base64
+
+proc main() =
+  doAssert encode("Hello World") == "SGVsbG8gV29ybGQ="
+  doAssert encode("leasure.") == "bGVhc3VyZS4="
+  doAssert encode("easure.") == "ZWFzdXJlLg=="
+  doAssert encode("asure.") == "YXN1cmUu"
+  doAssert encode("sure.") == "c3VyZS4="
+  doAssert encode([1,2,3]) == "AQID"
+  doAssert encode(['h','e','y']) == "aGV5"
+
+  doAssert encode("") == ""
+  doAssert decode("") == ""
+
+  const testInputExpandsTo76 = "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
+  const testInputExpands = "++++++++++++++++++++++++++++++"
+  const longText = """Man is distinguished, not only by his reason, but by this
+    singular passion from other animals, which is a lust of the mind,
+    that by a perseverance of delight in the continued and indefatigable
+    generation of knowledge, exceeds the short vehemence of any carnal
+    pleasure."""
+  const tests = ["", "abc", "xyz", "man", "leasure.", "sure.", "easure.",
+                 "asure.", longText, testInputExpandsTo76, testInputExpands]
+
+  doAssert encodeMIME("foobarbaz", lineLen=4) == "Zm9v\r\nYmFy\r\nYmF6"
+  doAssert decode("Zm9v\r\nYmFy\r\nYmF6") == "foobarbaz"
+
+  for t in items(tests):
+    doAssert decode(encode(t)) == t
+    doAssert decode(encodeMIME(t, lineLen=40)) == t
+    doAssert decode(encodeMIME(t, lineLen=76)) == t
+
+  const invalid = "SGVsbG\x008gV29ybGQ="
+  try:
+    doAssert decode(invalid) == "will throw error"
+  except ValueError:
+    discard
+
+  echo "OK"
+
+main()