[ create a new paste ] login | about

Link: http://codepad.org/wGi28FgC    [ raw code | output | fork ]

saltheads - Ruby, pasted on Jun 9:
# coding: ascii-8bit

require 'test/unit'

class TestPackUnpack < Test::Unit::TestCase
  def test_pack_unpack
    
    # hex数値 to hex文字列
    assert_equal(0x12345678.to_s(16),"12345678")
    assert_equal(0x1234_5678.to_s(16),"12345678")
    assert_equal(0x12_34_56_78.to_s(16),"12345678")
    
    # hex文字列 to hex数値
    assert_equal("12345678".to_i(16), 0x1234_5678)
    assert_equal("ab cd".split, ["ab","cd"])
    assert_equal("12 34".split.collect{|c| c.to_i(16)}, [0x12,0x34])
    
    # hex文字列 to バイナリーデータ
    # Array#pack('C*')は、配列に入っているものを8ビット符号なし整数と見てpackする
    assert_equal("61 62 63 64".split.collect{|c| c.to_i(16)}.pack('C*'), "abcd")
    assert_equal("61 62 63 64".split.collect{|c| c.to_i(16)}.pack('C*'), "\x61\x62\x63\x64")
    assert_equal("90 a0 e0 f0".split.collect{|c| c.to_i(16)}.pack('C*'), "\x90\xa0\xe0\xf0")
    # バイナリーデータ to hex文字列
    # String#unpack('C*')は、文字列を8ビット符号なし整数の配列にunpackする
    assert_equal("\x90\xa0\xe0\xf0".unpack('C*').collect{|d| sprintf("%02x",d)}.join(' '), "90 a0 e0 f0")

    hex_line = "00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0"
    num_array = [0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240]
    bin_data = "\x00\x10\x20\x30\x40\x50\x60\x70\x80\x90\xa0\xb0\xc0\xd0\xe0\xf0"
    assert_equal(hex_line.split.collect{|c| c.to_i(16)}.pack('C*'),bin_data)
    # hex文字列 to バイナリーデータ
    assert_equal(hex_line.split.collect{|c| c.to_i(16)}, num_array)
    assert_equal(hex_line.split.collect{|c| c.to_i(16)}.pack('C*'), bin_data)
    # バイナリーデータ to hex文字列
    assert_equal(bin_data.unpack('C*'), num_array)
    assert_equal(bin_data.unpack('C*').collect{|d| sprintf("%02x",d)}.join(' '), hex_line)
    
    # endian reverse unsigned 16bit
    assert_equal("\001\002".unpack("n"), [0x0102])
    assert_equal("\001\002".unpack("n").pack("v"),"\002\001")
    assert_equal("\001\002".unpack("v"), [0x0201])
    assert_equal("\001\002".unpack("v").pack("n"),"\002\001")
    
    # endian reverse unsigned 32bit
    assert_equal("\001\002\003\004".unpack("N"), [0x01020304])
    assert_equal("\001\002\003\004".unpack("N").pack("V"),"\004\003\002\001")
    assert_equal("\001\002\003\004".unpack("n2"), [0x0102, 0x0304])
    assert_equal("\001\002\003\004".unpack("n2").reverse, [0x0304, 0x0102])
    assert_equal("\001\002\003\004".unpack("n2").reverse.pack("v2"),"\004\003\002\001")
    
    # endian reverse unsigned 64bit
    assert_equal("\001\002\003\004\005\006\a\b".unpack("N2").reverse.pack("V2"),"\b\a\006\005\004\003\002\001")
    assert_equal("\001\002\003\004","\x01\x02\x03\x04")
    
    # big endian unsigned 16bit to integer
    assert_equal("\001\002".unpack("n"),[258])
    assert_equal("\001\002".unpack("n")[0],258)
    assert_equal("\001\002".unpack("n1")[0], 0x0102)
    
    # big endian unsigned 32bit to integer
    assert_equal("\001\002\003\004".unpack("N1")[0], 0x01020304)
    
    # big endian unsigned 64bit to integer
    b = "\001\002\003\004\005\006\a\b"
    assert_equal(0x1_0000_0000 * (b.unpack("N2")[0]) + b.unpack("N2")[1], 0x0102030405060708)
    
    # big endian unsigned 32bit to 4 unsigned 8bit
    adrs = 0x11223344
    assert_equal([adrs].pack("N*"), "\x11\x22\x33\x44")
    assert_equal([adrs].pack("N*").unpack("C*"), [0x11,0x22,0x33,0x44])
    adrs = 0xFFEEDDCC
    assert_equal([adrs].pack("N*").unpack("C*"), [0xFF,0xEE,0xDD,0xCC])
    
    # 0x01020304を"1.2.3.4"にする
    adrs = 0x01020304
    assert_equal([adrs].pack("N*"), "\x01\x02\x03\x04")
    assert_equal([adrs].pack("N*").unpack("C*"), [1, 2, 3, 4])
    assert_equal([adrs].pack("N*").unpack("C*").join("."), "1.2.3.4")
    
    # big endian unsigned 32bit to IP Address
    # "\x01\x02\x03\x04"を"1.2.3.4"にする
    assert_equal("\x01\x02\x03\x04".unpack("C4"), [1, 2, 3, 4])
    assert_equal("\x01\x02\x03\x04".unpack("C4").map{|d| d.to_s}, ["1", "2", "3", "4"])
    assert_equal("\x01\x02\x03\x04".unpack("C4").map{|d| d.to_s}.join("."), "1.2.3.4")
    assert_equal("\x01\x02\x03\x04".unpack("C4").join("."), "1.2.3.4")
    assert_equal("\xC0\xC1\xC2\xC3".unpack("C4").join("."), "192.193.194.195")
    
    # "1.2.3.4"を"\x01\x02\x03\x04" にする
    assert_equal("1.2.3.4".split("."), ["1", "2", "3", "4"])
    assert_equal("1.2.3.4".split(".").map{|s| s.to_i}, [1, 2, 3, 4])
    assert_equal("1.2.3.4".split(".").map{|s| s.to_i}.pack("C4"), "\x01\x02\x03\x04")
    assert_equal("192.193.194.195".split(".").map{|s| s.to_i}.pack("C4"), "\xC0\xC1\xC2\xC3")
  end
end


Output:
1
2
3
4
5
6
Loaded suite t
Started
.
Finished in 0.004887 seconds.

1 tests, 46 assertions, 0 failures, 0 errors


Create a new paste based on this one


Comments: