File: pack_spec.rb

package info (click to toggle)
ruby-amq-protocol 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 572 kB
  • sloc: ruby: 5,975; python: 248; makefile: 4
file content (58 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download
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
# encoding: binary

RSpec.describe AMQ::Pack do
  context "16-bit big-endian packing / unpacking" do
    let(:examples_16bit) {
      {
        0x068D => "\x06\x8D",  # 1677
        0x0000 => "\x00\x00",  # 0
        0x7FFF => "\x7F\xFF"   # 32767 (max positive signed 16-bit)
      }
    }

    it "packs signed integers into a big-endian string" do
      examples_16bit.each do |key, value|
        expect(described_class.pack_int16_big_endian(key)).to eq(value)
      end
    end

    it "unpacks signed integers from a string to a number" do
      examples_16bit.each do |key, value|
        expect(described_class.unpack_int16_big_endian(value)[0]).to eq(key)
      end
    end
  end

  context "64-bit big-endian packing / unpacking" do
    let(:examples) {
      {
        0x0000000000000000 => "\x00\x00\x00\x00\x00\x00\x00\x00",
        0x000000000000000A => "\x00\x00\x00\x00\x00\x00\x00\x0A",
        0x00000000000000A0 => "\x00\x00\x00\x00\x00\x00\x00\xA0",
        0x000000000000B0A0 => "\x00\x00\x00\x00\x00\x00\xB0\xA0",
        0x00000000000CB0AD => "\x00\x00\x00\x00\x00\x0C\xB0\xAD",
        0x8BADF00DDEFEC8ED => "\x8B\xAD\xF0\x0D\xDE\xFE\xC8\xED",
        0x0D15EA5EFEE1DEAD => "\x0D\x15\xEA\x5E\xFE\xE1\xDE\xAD",
        0xDEADBEEFDEADBABE => "\xDE\xAD\xBE\xEF\xDE\xAD\xBA\xBE"
      }
    }

    it "packs integers into big-endian string" do
      examples.each do |key, value|
        expect(described_class.pack_uint64_big_endian(key)).to eq(value)
      end
    end

    it "unpacks string representation into integer" do
      examples.each do |key, value|
        expect(described_class.unpack_uint64_big_endian(value)[0]).to eq(key)
      end
    end
  end

  describe "AMQ::Hacks alias" do
    it "is an alias for AMQ::Pack (backwards compatibility)" do
      expect(AMQ::Hacks).to eq(AMQ::Pack)
    end
  end
end