bitstream 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ class FieldInfo < Hash
2
+
3
+ def initialize(value, length)
4
+ @table = {}
5
+ @table[:value] = value
6
+ @table[:length] = length
7
+ end
8
+
9
+ def [](key)
10
+ @table[key]
11
+ end
12
+
13
+ end
@@ -0,0 +1,24 @@
1
+ # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
+ # Copyright:: Copyright (c) 2012 Natsuki Kawai
3
+ # License:: 2-clause BSDL or Ruby's
4
+
5
+ module BitStream
6
+
7
+ module Utils
8
+
9
+ def self.class2symbol(type)
10
+ name = type.name.split("::").last
11
+ name = self.camel2snake(name).intern
12
+ end
13
+
14
+ def self.camel2snake(camel)
15
+ snake = camel.dup
16
+ snake[0] = snake[0].downcase
17
+ snake.gsub(/[A-Z]/) do |s|
18
+ "_" + s.downcase
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-14 00:00:00.000000000 Z
12
+ date: 2014-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: random-accessible
@@ -46,24 +46,10 @@ files:
46
46
  - lib/types/integer.rb
47
47
  - lib/types/cstring.rb
48
48
  - lib/types/string.rb
49
- - test/test-dynarray.rb
50
- - test/test-overload.rb
51
- - test/test-array.rb
52
- - test/test-suite.rb
53
- - test/test-nesting.rb
54
- - test/test-lazy-string.rb
55
- - test/test-primitives.rb
56
- - test/test-simple-properties.rb
57
- - test/test-condition.rb
58
- - test/types/test-character.rb
59
- - test/types/test-cstring.rb
60
- - test/types/test-string.rb
61
- - test/types/test-integer.rb
62
- - sample/flac.rb
63
- - sample/flac-front.rb
64
- - sample/gzip.rb
65
- - sample/gzip-viewer.rb
49
+ - lib/bitstream/field-info.rb
50
+ - lib/bitstream/utils.rb
66
51
  - README.en
52
+ - test/test-suite.rb
67
53
  homepage: https://github.com/natsuki14/bitstream
68
54
  licenses:
69
55
  - Ruby's
data/sample/flac-front.rb DELETED
@@ -1,8 +0,0 @@
1
- require './flac'
2
-
3
- if ARGV.size != 1
4
- STDERR.puts "Usage: #{__FILE__} flac_file"
5
- exit 1
6
- end
7
-
8
- flac = FLAC.create(ARGV[0])
data/sample/flac.rb DELETED
@@ -1,37 +0,0 @@
1
- require 'bitstream'
2
-
3
- class FLAC
4
-
5
- include BitStream
6
-
7
- class MetadataBlock
8
-
9
- include BitStream
10
-
11
- #add_type MetadataBlockData
12
-
13
- fields do
14
- unsigned_int :last_metadata_block, 1
15
- unsigned_int :block_type, 7
16
- unsigned_int :body_length, 24
17
- string :data, body_length
18
- end
19
-
20
- def length
21
- 32 + body_length
22
- end
23
-
24
- end
25
-
26
- add_type MetadataBlock
27
-
28
- fields do
29
- string :magic, 4#, "The FLAC stream marker"
30
- dyn_array :metadata_blocks, :metadata_block
31
- unless metadata_blocks.last.last_metadata_block
32
- dyn_array :metadata_blocks, :metadata_block
33
- #, "The basic property of the stream."
34
- end
35
- end
36
-
37
- end
@@ -1,12 +0,0 @@
1
- require_relative 'gzip'
2
-
3
- gzip = nil
4
- File.open(ARGV[0], "rb") do |file|
5
- gzip = Gzip.create(file.read)
6
- end
7
-
8
- if gzip.respond_to? :original_file_name
9
- puts "original_file_name:#{gzip.original_file_name}"
10
- else
11
- puts "The gzip does not contain its original file name."
12
- end
data/sample/gzip.rb DELETED
@@ -1,38 +0,0 @@
1
- require 'bitstream'
2
-
3
- class Gzip
4
-
5
- include BitStream
6
- byte_order :little_endian
7
-
8
- FHCRC = 1 << 1
9
- FEXTRA = 1 << 2
10
- FNAME = 1 << 3
11
- FCOMMENT = 1 << 4
12
-
13
- fields do
14
- unsigned :id1, 8
15
- unsigned :id2, 8
16
- unsigned :cm, 8
17
- unsigned :flg, 8
18
- unsigned :mtime, 32
19
- unsigned :xfl, 8
20
- unsigned :os, 8
21
- if (flg & FEXTRA) != 0
22
- unsigned :xlen, 16
23
- string :extra_field, xlen
24
- end
25
- if (flg & FNAME) != 0
26
- # cstring means a NULL-terminated string.
27
- cstring :original_file_name
28
- end
29
- if (flg & FCOMMENT) != 0
30
- # cstring means a NULL-terminated string.
31
- cstring :file_comment
32
- end
33
- if (flg & FHCRC) != 0
34
- unsigned :crc16, 16
35
- end
36
- end
37
-
38
- end
data/test/test-array.rb DELETED
@@ -1,75 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright (c) 2011, 2012 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'bitstream'
8
-
9
- class ArraySample
10
-
11
- include BitStream
12
-
13
- fields do
14
- array :a1, 5, :unsigned_int, 16
15
- end
16
-
17
- end
18
-
19
- class InfiniteArraySample
20
-
21
- include BitStream
22
-
23
- fields do
24
- array :a1, nil, :uint16
25
- end
26
-
27
- end
28
-
29
- class VarlenArraySample
30
-
31
- include BitStream
32
-
33
- fields do
34
- array :a1, nil, :cstring
35
- end
36
-
37
- end
38
-
39
- class TestArray < Test::Unit::TestCase
40
-
41
- def setup
42
- @spec = ArraySample.create "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
43
- @spec_inf = InfiniteArraySample.create "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
44
- @spec_var = VarlenArraySample.create "foo\0quux\0baz\0"
45
- end
46
-
47
- def test_a1
48
- assert_equal(5, @spec.a1.size)
49
- assert_equal(0x0102, @spec.a1[0])
50
- assert_equal(0x0304, @spec.a1[1])
51
- assert_equal(0x0506, @spec.a1[2])
52
- assert_equal(0x0708, @spec.a1[3])
53
- assert_equal(0x090a, @spec.a1[4])
54
- assert_equal(nil, @spec.a1[5])
55
- end
56
-
57
- def test_infinite
58
- assert_equal(5, @spec_inf.a1.size)
59
- assert_equal(0x0102, @spec_inf.a1[0])
60
- assert_equal(0x0304, @spec_inf.a1[1])
61
- assert_equal(0x0506, @spec_inf.a1[2])
62
- assert_equal(0x0708, @spec_inf.a1[3])
63
- assert_equal(0x090a, @spec_inf.a1[4])
64
- assert_equal(nil, @spec_inf.a1[5])
65
- end
66
-
67
- def test_varlen
68
- assert_equal(3, @spec_var.a1.size)
69
- assert_equal("foo", @spec_var.a1[0])
70
- assert_equal("quux", @spec_var.a1[1])
71
- assert_equal("baz", @spec_var.a1[2])
72
- assert_equal(nil, @spec_var.a1[3])
73
- end
74
-
75
- end
@@ -1,50 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'bitstream'
8
-
9
- class ConditionSample
10
-
11
- include BitStream
12
-
13
- fields {
14
- unsigned_int :u1, 32
15
- if u1 == 0
16
- unsigned_int :u2, 32
17
- end
18
- }
19
-
20
- end
21
-
22
- class TestCondition < Test::Unit::TestCase
23
-
24
- def setup
25
- end
26
-
27
- def test_condition_true
28
- spec = ConditionSample.create "\x00\x00\x00\x00\x00\x00\x00\x01"
29
- assert_equal(0, spec.u1)
30
- assert_equal(1, spec.u2)
31
- end
32
-
33
- def test_condition_false
34
- spec = ConditionSample.create "\x00\x00\x00\x01\x00\x00\x00\x01"
35
- assert_equal(1, spec.u1)
36
- assert_raise(NoMethodError) do
37
- spec.u2
38
- end
39
- end
40
-
41
- def test_condition_false_with_dummy
42
- ConditionSample.create "\x00\x00\x00\x00\x00\x00\x00\x01" # dummy
43
- spec = ConditionSample.create "\x00\x00\x00\x01\x00\x00\x00\x01"
44
- assert_equal(1, spec.u1)
45
- assert_raise(NoMethodError) do
46
- spec.u2
47
- end
48
- end
49
-
50
- end
@@ -1,34 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'bitstream'
8
-
9
- class DynArraySample
10
-
11
- include BitStream
12
-
13
- fields {
14
- dyn_array :a1, :char
15
- while a1.last != "\0"
16
- dyn_array :a1, :char
17
- end
18
- }
19
-
20
- end
21
-
22
-
23
- class TestDynArray < Test::Unit::TestCase
24
-
25
- def setup
26
- @spec = DynArraySample.create "foobar\0"
27
- end
28
-
29
- def test_a1
30
- assert_equal("foobar\0", @spec.a1.join)
31
- end
32
-
33
- end
34
-
@@ -1,36 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright (c) 2012 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
- require 'test/unit'
6
- require 'lazy-string'
7
-
8
- class TestLazyString < Test::Unit::TestCase
9
-
10
- def setup
11
- @sample = LazyString.new
12
- @sample << "foo"
13
- @sample << "bar"
14
- @sample << "quux"
15
- @sample << "baz"
16
- end
17
-
18
- def test_head
19
- assert_equal("fo", @sample[0, 2].to_str)
20
- assert_equal("foo", @sample[0, 3].to_str)
21
- assert_equal("foob", @sample[0, 4].to_str)
22
- end
23
-
24
- def test_im
25
- assert_equal("a", @sample[4, 1].to_str)
26
- assert_equal("bar", @sample[3, 3].to_str)
27
- assert_equal("obarq", @sample[2, 5].to_str)
28
- end
29
-
30
- def test_last
31
- assert_equal("az", @sample[11, 2].to_str)
32
- assert_equal("baz", @sample[10, 3].to_str)
33
- assert_equal("xbaz", @sample[9, 4].to_str)
34
- end
35
-
36
- end
data/test/test-nesting.rb DELETED
@@ -1,64 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'bitstream'
8
-
9
- class Nested
10
-
11
- include BitStream
12
-
13
- fields do
14
- unsigned_int "u1", 8
15
- unsigned_int :u2, 8
16
- end
17
-
18
- end
19
-
20
- class Nesting
21
-
22
- include BitStream
23
-
24
- add_type Nested, :nested
25
-
26
- fields do
27
- unsigned_int "u1", 8
28
- nested "n"
29
- unsigned_int :u2, 8
30
- end
31
-
32
- end
33
-
34
- class TestNesting < Test::Unit::TestCase
35
-
36
- def setup
37
- @spec = Nesting.create "\x01\x02\x03\x04"
38
- end
39
-
40
- def test_nesting_u1
41
- assert_equal(0x01, @spec.u1)
42
- end
43
-
44
- def test_nesting_u2
45
- assert_equal(0x04, @spec.u2)
46
- end
47
-
48
- def test_nesting
49
- assert_equal(32, @spec.length)
50
- end
51
-
52
- def test_nested_u1
53
- assert_equal(0x02, @spec.n.u1)
54
- end
55
-
56
- def test_nested_u2
57
- assert_equal(0x03, @spec.n.u2)
58
- end
59
-
60
- def test_nested
61
- assert_equal(16, @spec.n.length)
62
- end
63
-
64
- end
@@ -1,69 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright (c) 2012 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
- require 'test/unit'
6
- require 'bitstream'
7
-
8
- class OverloadSample1
9
-
10
- include BitStream
11
-
12
- fields do
13
- unsigned :a, 32
14
- unsigned :a, 32
15
- end
16
-
17
- end
18
-
19
- class OverloadSample2
20
-
21
- include BitStream
22
-
23
- fields do
24
- unsigned :a, 8
25
- if a == 1
26
- cstring :a
27
- else
28
- string :a, 1
29
- end
30
- end
31
-
32
- end
33
-
34
- class OverloadSampleArray
35
-
36
- include BitStream
37
-
38
- fields do
39
- array :a, 3, :unsigned, 8
40
- unsigned :a, 16
41
- unsigned :b, 16
42
- array :b, 2, :unsigned, 8
43
- end
44
-
45
- end
46
-
47
- class TestOverload < Test::Unit::TestCase
48
-
49
- def setup
50
- @spec1 = OverloadSample1.create "\x00\x00\x00\x01\x00\x00\x00\x02"
51
- @spec2 = OverloadSample2.create "\x01foo\0"
52
- @spec_array = OverloadSampleArray.create "abc\x01\x01\x02\x02\x03\x03"
53
- end
54
-
55
- def test_a1
56
- assert_equal(2, @spec1.a)
57
- end
58
-
59
- def test_a2
60
- assert_equal("foo", @spec2.a)
61
- end
62
-
63
- def test_array
64
- assert_equal(0x0101, @spec_array.a)
65
- assert_equal([3, 3], @spec_array.b)
66
- end
67
-
68
- end
69
-
@@ -1,92 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'bitstream'
8
-
9
- class SimpleIntBe
10
-
11
- include BitStream
12
-
13
- byte_order :big_endian
14
-
15
- fields {
16
- unsigned "u1", 32
17
- uint32 :u2
18
- cstring "cs1"
19
- unsigned "u3", 1
20
- uint7 "u4"
21
- string :s1, 3
22
- }
23
-
24
- end
25
-
26
- class SimpleIntLe
27
-
28
- include BitStream
29
-
30
- byte_order :little_endian
31
-
32
- fields {
33
- unsigned_int "u1", 32
34
- unsigned_int :u2, 32
35
- cstring "cs1"
36
- unsigned_int "u3", 1
37
- unsigned_int "u4", 7
38
- string :s1, 3
39
- }
40
-
41
- end
42
-
43
- class TestSimpleInt < Test::Unit::TestCase
44
-
45
- def setup
46
- @spec = SimpleIntBe.create "\x10\x20\x30\x40\x50\x60\x70\x80foobar\00\xfebazdummy"
47
-
48
- # dummy
49
- dummy = SimpleIntBe.create "\x10\x20\x30\x40\x50\x60\x70\x80foobar\00\xfebazdummy"
50
-
51
- @spec_le = SimpleIntLe.create "\x10\x20\x30\x40\x50\x60\x70\x80foobar\00\xfebazdummy"
52
- end
53
-
54
- def test_u1
55
- assert_equal(0x10203040.to_s(16), @spec.u1.to_s(16))
56
- end
57
- def test_u1le
58
- assert_equal(0x40302010.to_s(16), @spec_le.u1.to_s(16))
59
- end
60
-
61
- def test_u2
62
- assert_equal(0x50607080.to_s(16), @spec.u2.to_s(16))
63
- end
64
- def test_u2le
65
- assert_equal(0x80706050.to_s(16), @spec_le.u2.to_s(16))
66
- end
67
-
68
- def test_u3
69
- assert_equal(1, @spec.u3)
70
- end
71
- def test_u3le
72
- assert_equal(1, @spec_le.u3)
73
- end
74
-
75
- def test_u4
76
- assert_equal(0x7e, @spec.u4)
77
- end
78
- def test_u4le
79
- assert_equal(0x7e, @spec_le.u4)
80
- end
81
-
82
- def test_cs1
83
- assert_equal("foobar", @spec.cs1)
84
- assert_equal("foobar", @spec_le.cs1)
85
- end
86
-
87
- def test_s1
88
- assert_equal("baz", @spec.s1)
89
- assert_equal("baz", @spec_le.s1)
90
- end
91
-
92
- end
@@ -1,34 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'bitstream'
8
-
9
- class HavingProps
10
-
11
- include BitStream
12
-
13
- fields do
14
- unsigned_int :u1, props[:lengths][0]
15
- unsigned_int :u2, props[:lengths][1]
16
- end
17
-
18
- end
19
-
20
- class TestSimpleProperties < Test::Unit::TestCase
21
-
22
- def setup
23
- @spec = HavingProps.create "\x01\x02\x03\x04", lengths: [24, 8]
24
- end
25
-
26
- def test_u1
27
- assert_equal(0x010203, @spec.u1)
28
- end
29
-
30
- def test_u2
31
- assert_equal(0x04, @spec.u2)
32
- end
33
-
34
- end
@@ -1,19 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'types/character'
8
-
9
- class TestUint < Test::Unit::TestCase
10
-
11
- def test_char_nooffset_read
12
- type = BitStream::Char.instance({})
13
- info = type.read("abcd", 8)
14
- assert_equal("b", info[:value])
15
- assert_equal(8, info[:length])
16
- end
17
-
18
- end
19
-
@@ -1,18 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'types/cstring'
8
-
9
- class TestCstring < Test::Unit::TestCase
10
-
11
- def test_aligned_read
12
- type = BitStream::Cstring.instance({})
13
- info = type.read("foobar\0baz", 16)
14
- assert_equal("obar", info[:value])
15
- assert_equal(8 * "obar\0".size, info[:length])
16
- end
17
-
18
- end
@@ -1,98 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'types/integer'
8
-
9
- class TestUint < Test::Unit::TestCase
10
-
11
- BE_PROP = { :byte_order => :big_endian }
12
- LE_PROP = { :byte_order => :little_endian }
13
-
14
- def test_uint32be_nooffset_read
15
- type = BitStream::Unsigned.instance(BE_PROP,32)
16
- info = type.read("\x01\x02\x03\x04", 0)
17
- assert_equal(0x01020304, info[:value])
18
- assert_equal(32, info[:length])
19
- end
20
-
21
- def test_uint32le_nooffset_read
22
- type = BitStream::Unsigned.instance(LE_PROP,32)
23
- info = type.read("\x01\x02\x03\x04", 0)
24
- assert_equal(0x04030201, info[:value])
25
- assert_equal(32, info[:length])
26
- end
27
-
28
- def test_sint32be_nooffset_read
29
- type = BitStream::Signed.instance(BE_PROP,32)
30
- info = type.read("\xfe\xfd\xfc\xfb", 0)
31
- assert_equal(-0x01020304 - 1, info[:value])
32
- assert_equal(32, info[:length])
33
- end
34
-
35
- def test_sint32le_nooffset_read
36
- type = BitStream::Signed.instance(LE_PROP,32)
37
- info = type.read("\xfe\xfd\xfc\xfb", 0)
38
- assert_equal(-0x04030201 - 1, info[:value])
39
- assert_equal(32, info[:length])
40
- end
41
-
42
- def test_uint32_nooffset_write
43
- type = BitStream::Unsigned.instance(BE_PROP, 32)
44
- val = "\xff\x00"
45
- type.write(val, 8, 0x01020304)
46
- assert_equal("\xff\x01\x02\x03\x04", val)
47
- end
48
-
49
- def test_uint32be_offset4_read
50
- type = BitStream::Unsigned.instance(BE_PROP, 32)
51
- info = type.read("\xf1\x02\x03\x04\x05", 4)
52
- assert_equal(0x10203040.to_s(16), info[:value].to_s(16))
53
- assert_equal(32, info[:length])
54
- end
55
-
56
- def test_uint32be_offset1_read
57
- type = BitStream::Unsigned.instance(BE_PROP, 32)
58
- info = type.read("\x12\x23\x34\x45\x56", 1)
59
- assert_equal(0x2446688a.to_s(16), info[:value].to_s(16))
60
- assert_equal(32, info[:length])
61
- end
62
-
63
- def test_uint32le_offset4_read
64
- type = BitStream::Unsigned.instance(LE_PROP, 32)
65
- info = type.read("\xf1\x02\x03\x04\x05", 4)
66
- assert_equal(0x5040302f.to_s(16), info[:value].to_s(16))
67
- assert_equal(32, info[:length])
68
- end
69
-
70
- def test_uint32le_offset1_read
71
- type = BitStream::Unsigned.instance(LE_PROP, 32)
72
- info = type.read("\x80\x23\x34\x45\x56", 1)
73
- assert_equal(0xac8a6847.to_s(16), info[:value].to_s(16))
74
- assert_equal(32, info[:length])
75
- end
76
-
77
- def test_uint1be_read
78
- type = BitStream::Unsigned.instance(BE_PROP, 1)
79
- info = type.read("\x40", 1)
80
- assert_equal(1, info[:value])
81
- assert_equal(1, info[:length])
82
-
83
- info = type.read("\xfd", 6)
84
- assert_equal(0, info[:value])
85
- assert_equal(1, info[:length])
86
- end
87
-
88
- def test_uint1le_read
89
- type = BitStream::Unsigned.instance(LE_PROP, 1)
90
- info = type.read("\x40", 1)
91
- assert_equal(1, info[:value])
92
- assert_equal(1, info[:length])
93
-
94
- info = type.read("\xfd", 6)
95
- assert_equal(0, info[:value])
96
- assert_equal(1, info[:length])
97
- end
98
- end
@@ -1,41 +0,0 @@
1
- # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
- # License:: 2-clause BSDL or Ruby's
4
-
5
-
6
- require 'test/unit'
7
- require 'types/string'
8
-
9
- class TestString < Test::Unit::TestCase
10
-
11
- def test_aligned_read
12
- type = BitStream::String.instance({}, 3)
13
- info = type.read("foobarbaz", 16)
14
- assert_equal("oba", info[:value])
15
- assert_equal(3 * 8, info[:length])
16
- end
17
-
18
- def test_aligned_write
19
- type = BitStream::String.instance({}, 3)
20
- val = "foobarbaz"
21
- ret = type.write(val, 24, "qux")
22
- assert_equal("fooquxbaz", val)
23
- end
24
-
25
- def test_unaligned_read
26
- type = BitStream::String.instance({}, 2)
27
- info = type.read([0x12345678].pack('N'), 1)
28
- assert_equal("\x24\x68", info[:value])
29
- assert_equal(2 * 8, info[:length])
30
- end
31
-
32
- def test_unaligned_write
33
- type = BitStream::String.instance({}, 2)
34
- val = "\x12\x34"
35
- ret = type.write(val, 7, "\xcd\xef")
36
-
37
- assert_equal("\x13\x9b", val[0..1])
38
- assert(val[2].ord & 0xfe == 0xde)
39
- end
40
-
41
- end