summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/unpack/x_spec.rb
blob: 5adcb720d1c2c311963b29bfd9e40e2c0bb1bf4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- encoding: binary -*-
require_relative '../../../spec_helper'
require_relative '../fixtures/classes'
require_relative 'shared/basic'

describe "String#unpack with format 'X'" do
  it_behaves_like :string_unpack_basic, 'X'
  it_behaves_like :string_unpack_no_platform, 'X'

  it "moves the read index back by the number of bytes specified by count" do
    "\x01\x02\x03\x04".unpack("C3X2C").should == [1, 2, 3, 2]
  end

  it "does not change the read index when passed a count of zero" do
    "\x01\x02\x03\x04".unpack("C3X0C").should == [1, 2, 3, 4]
  end

  it "implicitly has a count of one when count is not specified" do
    "\x01\x02\x03\x04".unpack("C3XC").should == [1, 2, 3, 3]
  end

  it "moves the read index back by the remaining bytes when passed the '*' modifier" do
    "abcd".unpack("C3X*C").should == [97, 98, 99, 99]
  end

  it "raises an ArgumentError when passed the '*' modifier if the remaining bytes exceed the bytes from the index to the start of the String" do
    lambda { "abcd".unpack("CX*C") }.should raise_error(ArgumentError)
  end

  it "raises an ArgumentError if the count exceeds the bytes from current index to the start of the String" do
    lambda { "\x01\x02\x03\x04".unpack("C3X4C") }.should raise_error(ArgumentError)
  end
end

describe "String#unpack with format 'x'" do
  it_behaves_like :string_unpack_basic, 'x'
  it_behaves_like :string_unpack_no_platform, 'x'

  it "moves the read index forward by the number of bytes specified by count" do
    "\x01\x02\x03\x04".unpack("Cx2C").should == [1, 4]
  end

  it "implicitly has a count of one when count is not specified" do
    "\x01\x02\x03\x04".unpack("CxC").should == [1, 3]
  end

  it "does not change the read index when passed a count of zero" do
    "\x01\x02\x03\x04".unpack("Cx0C").should == [1, 2]
  end

  it "moves the read index to the end of the string when passed the '*' modifier" do
    "\x01\x02\x03\x04".unpack("Cx*C").should == [1, nil]
  end

  it "positions the read index one beyond the last readable byte in the String" do
    "\x01\x02\x03\x04".unpack("C2x2C").should == [1, 2, nil]
  end

  it "raises an ArgumentError if the count exceeds the size of the String" do
    lambda { "\x01\x02\x03\x04".unpack("C2x3C") }.should raise_error(ArgumentError)
  end
end