fork(8) download
  1. # coding: ascii-8bit
  2.  
  3. require 'test/unit'
  4.  
  5. class TestPackUnpack < Test::Unit::TestCase
  6. def test_pack_unpack
  7.  
  8. # hex数値 to hex文字列
  9. assert_equal(0x12345678.to_s(16),"12345678")
  10. assert_equal(0x1234_5678.to_s(16),"12345678")
  11. assert_equal(0x12_34_56_78.to_s(16),"12345678")
  12.  
  13. # hex文字列 to hex数値
  14. assert_equal("12345678".to_i(16), 0x1234_5678)
  15. assert_equal("ab cd".split, ["ab","cd"])
  16. assert_equal("12 34".split.collect{|c| c.to_i(16)}, [0x12,0x34])
  17.  
  18. # hex文字列 to バイナリーデータ
  19. # Array#pack('C*')は、配列に入っているものを8ビット符号なし整数と見てpackする
  20. assert_equal("61 62 63 64".split.collect{|c| c.to_i(16)}.pack('C*'), "abcd")
  21. assert_equal("61 62 63 64".split.collect{|c| c.to_i(16)}.pack('C*'), "\x61\x62\x63\x64")
  22. assert_equal("90 a0 e0 f0".split.collect{|c| c.to_i(16)}.pack('C*'), "\x90\xa0\xe0\xf0")
  23. # バイナリーデータ to hex文字列
  24. # String#unpack('C*')は、文字列を8ビット符号なし整数の配列にunpackする
  25. assert_equal("\x90\xa0\xe0\xf0".unpack('C*').collect{|d| sprintf("%02x",d)}.join(' '), "90 a0 e0 f0")
  26.  
  27. hex_line = "00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0"
  28. num_array = [0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240]
  29. bin_data = "\x00\x10\x20\x30\x40\x50\x60\x70\x80\x90\xa0\xb0\xc0\xd0\xe0\xf0"
  30. assert_equal(hex_line.split.collect{|c| c.to_i(16)}.pack('C*'),bin_data)
  31. # hex文字列 to バイナリーデータ
  32. assert_equal(hex_line.split.collect{|c| c.to_i(16)}, num_array)
  33. assert_equal(hex_line.split.collect{|c| c.to_i(16)}.pack('C*'), bin_data)
  34. # バイナリーデータ to hex文字列
  35. assert_equal(bin_data.unpack('C*'), num_array)
  36. assert_equal(bin_data.unpack('C*').collect{|d| sprintf("%02x",d)}.join(' '), hex_line)
  37.  
  38. # endian reverse unsigned 16bit
  39. assert_equal("\001\002".unpack("n"), [0x0102])
  40. assert_equal("\001\002".unpack("n").pack("v"),"\002\001")
  41. assert_equal("\001\002".unpack("v"), [0x0201])
  42. assert_equal("\001\002".unpack("v").pack("n"),"\002\001")
  43.  
  44. # endian reverse unsigned 32bit
  45. assert_equal("\001\002\003\004".unpack("N"), [0x01020304])
  46. assert_equal("\001\002\003\004".unpack("N").pack("V"),"\004\003\002\001")
  47. assert_equal("\001\002\003\004".unpack("n2"), [0x0102, 0x0304])
  48. assert_equal("\001\002\003\004".unpack("n2").reverse, [0x0304, 0x0102])
  49. assert_equal("\001\002\003\004".unpack("n2").reverse.pack("v2"),"\004\003\002\001")
  50.  
  51. # endian reverse unsigned 64bit
  52. assert_equal("\001\002\003\004\005\006\a\b".unpack("N2").reverse.pack("V2"),"\b\a\006\005\004\003\002\001")
  53. assert_equal("\001\002\003\004","\x01\x02\x03\x04")
  54.  
  55. # big endian unsigned 16bit to integer
  56. assert_equal("\001\002".unpack("n"),[258])
  57. assert_equal("\001\002".unpack("n")[0],258)
  58. assert_equal("\001\002".unpack("n1")[0], 0x0102)
  59.  
  60. # big endian unsigned 32bit to integer
  61. assert_equal("\001\002\003\004".unpack("N1")[0], 0x01020304)
  62.  
  63. # big endian unsigned 64bit to integer
  64. b = "\001\002\003\004\005\006\a\b"
  65. assert_equal(0x1_0000_0000 * (b.unpack("N2")[0]) + b.unpack("N2")[1], 0x0102030405060708)
  66.  
  67. # big endian unsigned 32bit to 4 unsigned 8bit
  68. adrs = 0x11223344
  69. assert_equal([adrs].pack("N*"), "\x11\x22\x33\x44")
  70. assert_equal([adrs].pack("N*").unpack("C*"), [0x11,0x22,0x33,0x44])
  71. adrs = 0xFFEEDDCC
  72. assert_equal([adrs].pack("N*").unpack("C*"), [0xFF,0xEE,0xDD,0xCC])
  73.  
  74. # 0x01020304を"1.2.3.4"にする
  75. adrs = 0x01020304
  76. assert_equal([adrs].pack("N*"), "\x01\x02\x03\x04")
  77. assert_equal([adrs].pack("N*").unpack("C*"), [1, 2, 3, 4])
  78. assert_equal([adrs].pack("N*").unpack("C*").join("."), "1.2.3.4")
  79.  
  80. # big endian unsigned 32bit to IP Address
  81. # "\x01\x02\x03\x04"を"1.2.3.4"にする
  82. assert_equal("\x01\x02\x03\x04".unpack("C4"), [1, 2, 3, 4])
  83. assert_equal("\x01\x02\x03\x04".unpack("C4").map{|d| d.to_s}, ["1", "2", "3", "4"])
  84. assert_equal("\x01\x02\x03\x04".unpack("C4").map{|d| d.to_s}.join("."), "1.2.3.4")
  85. assert_equal("\x01\x02\x03\x04".unpack("C4").join("."), "1.2.3.4")
  86. assert_equal("\xC0\xC1\xC2\xC3".unpack("C4").join("."), "192.193.194.195")
  87.  
  88. # "1.2.3.4"を"\x01\x02\x03\x04" にする
  89. assert_equal("1.2.3.4".split("."), ["1", "2", "3", "4"])
  90. assert_equal("1.2.3.4".split(".").map{|s| s.to_i}, [1, 2, 3, 4])
  91. assert_equal("1.2.3.4".split(".").map{|s| s.to_i}.pack("C4"), "\x01\x02\x03\x04")
  92. assert_equal("192.193.194.195".split(".").map{|s| s.to_i}.pack("C4"), "\xC0\xC1\xC2\xC3")
  93. end
  94. end
Success #stdin #stdout 0.03s 5672KB
stdin
Standard input is empty
stdout
Loaded suite prog
Started
.
Finished in 0.010528 seconds.

1 tests, 46 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 18911