summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorAkira Yumiyama <[email protected]>2012-12-31 11:31:51 +0900
committerAkira Yumiyama <[email protected]>2012-12-31 11:38:22 +0900
commite205905d022b661a1dd279f8a8e6ecac19d1e979 (patch)
treefebe6d497ce90575a3dfe90119fbfe5d082db308 /test
downloadmruby-e205905d022b661a1dd279f8a8e6ecac19d1e979.tar.gz
mruby-e205905d022b661a1dd279f8a8e6ecac19d1e979.zip
mruby-pack
Diffstat (limited to 'test')
-rw-r--r--test/pack.rb85
1 files changed, 85 insertions, 0 deletions
diff --git a/test/pack.rb b/test/pack.rb
new file mode 100644
index 000000000..7339bbdee
--- /dev/null
+++ b/test/pack.rb
@@ -0,0 +1,85 @@
+# pack & unpack 'm' (base64)
+assert('[""].pack("m")') do
+ ary = ""
+ str = ""
+ [ary].pack("m") == str and
+ str.unpack("m") == [ary]
+end
+
+assert('["\0"].pack("m")') do
+ ary = "\0"
+ str = "AA==\n"
+ [ary].pack("m") == str and
+ str.unpack("m") == [ary]
+end
+
+assert('["\0\0"].pack("m")') do
+ ary = "\0\0"
+ str = "AAA=\n"
+ [ary].pack("m") == str and
+ str.unpack("m") == [ary]
+end
+
+assert('["\0\0\0"].pack("m")') do
+ ary = "\0\0\0"
+ str = "AAAA\n"
+ [ary].pack("m") == str and
+ str.unpack("m") == [ary]
+end
+
+assert('["abc..xyzABC..XYZ"].pack("m")') do
+ ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m") == "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n"
+end
+
+assert('"YWJ...".unpack("m") should "abc..xyzABC..XYZ"') do
+ str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m") == [str] and
+ "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m") == [str]
+end
+
+# pack & unpack 'H'
+assert('["3031"].pack("H*")') do
+ ary = "3031"
+ str = "01"
+ [ary].pack("H*") == str and
+ str.unpack("H*") == [ary]
+end
+
+assert('["10"].pack("H*")') do
+ ary = "10"
+ str = "\020"
+ [ary].pack("H*") == str and
+ str.unpack("H*") == [ary]
+end
+
+assert('[0,1,127,128,255].pack("C*")') do
+ ary = [ 0, 1, 127, 128, 255 ]
+ str = "\x00\x01\x7F\x80\xFF"
+ ary.pack("C*") == str and str.unpack("C*") == ary
+end
+
+# pack "a"
+assert('["abc"].pack("a")') do
+ ["abc"].pack("a") == "a" and
+ ["abc"].pack("a*") == "abc" and
+ ["abc"].pack("a4") == "abc\0"
+end
+
+# upack "a"
+assert('["abc"].pack("a")') do
+ "abc\0".unpack("a4") == ["abc\0"] and
+ "abc ".unpack("a4") == ["abc "]
+end
+
+# pack "A"
+assert('["abc"].pack("A")') do
+ ["abc"].pack("A") == "a" and
+ ["abc"].pack("A*") == "abc" and
+ ["abc"].pack("A4") == "abc "
+end
+
+# upack "A"
+assert('["abc"].pack("A")') do
+ "abc\0".unpack("A4") == ["abc"] and
+ "abc ".unpack("A4") == ["abc"]
+end