File: index_spec.rb

package info (click to toggle)
ruby-ttfunk 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,472 kB
  • sloc: ruby: 7,954; makefile: 7
file content (161 lines) | stat: -rw-r--r-- 3,868 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

require 'spec_helper'
require 'ttfunk/table/cff/index'

RSpec.describe TTFunk::Table::Cff::Index do
  test_cases = {
    [
      # count
      0x00, 0x03,
      # offset len
      0x01,
      # offsets
      0x01, 0x02, 0x03, 0x04,
      # data
      0xA,
      0xB,
      0xC,
    ] => [[10], [11], [12]],

    [
      # count
      0x00, 0x03,
      # offset len
      0x01,
      # offsets
      0x01, 0x03, 0x07, 0x08,
      # data
      0x11, 0x22,
      0x33, 0x44, 0x55, 0x66,
      0x77,
    ] => [[17, 34], [51, 68, 85, 102], [119]],

    [0x00, 0x00] => [],
  }

  describe 'decoding' do
    test_cases.each_with_index do |(bytes, decoded_values), idx|
      context "with example #{idx}" do
        subject(:index) do
          io = StringIO.new(bytes.pack('C*'))
          described_class.new(TestFile.new(io), 0, bytes.size)
        end

        it 'parses correctly' do
          expect(index.map(&:bytes)).to eq(decoded_values)
        end

        it 'encodes correctly' do
          expect(index.encode.bytes).to eq(bytes)
        end

        it 'calculates the length correctly' do
          expect(index.length).to eq(bytes.size)
        end
      end
    end
  end

  describe 'encoding' do
    it 'properly encodes items (change)' do
      inc_index_class =
        Class.new(described_class) do
          private

          def encode_items(*)
            # Increase each byte by 1
            items.map { |i| [i.unpack1('C') + 1].pack('C') }
          end
        end

      data = [
        # count
        0x00, 0x03,
        # offset len
        0x01,
        # offsets
        0x01, 0x02, 0x03, 0x04,
        # data
        0x01, 0x02, 0x03,
      ].pack('C*')

      index = inc_index_class.new(TestFile.new(StringIO.new(data)), 0, data.length)

      expect(index.encode.string).to eq("\00\03\01\01\02\03\04\02\03\04")
    end

    it 'properly encodes items (filter)' do
      dup_index_class =
        Class.new(described_class) do
          private

          def encode_items(*)
            # duplicate each item
            items.flat_map { |i| [i, i] }
          end
        end

      data = [
        # count
        0x00, 0x03,
        # offset len
        0x01,
        # offsets
        0x01, 0x02, 0x03, 0x04,
        # data
        0x01, 0x02, 0x03,
      ].pack('C*')

      index = dup_index_class.new(TestFile.new(StringIO.new(data)), 0, data.length)

      expect(index.encode.string).to eq("\00\06\01\01\02\03\04\05\06\07\01\01\02\02\03\03")
    end

    [
      { item_size: 1, data_size: 6, offset_size: 1 },
      { item_size: 0xff, data_size: 262, offset_size: 2 },
      { item_size: 0xffff, data_size: 65_544, offset_size: 3 },
      { item_size: 0xffffff, data_size: 16_777_226, offset_size: 4 },
    ].each do |params|
      it "properly encodes offset size #{params[:offset_size]}" do
        gen_index_class =
          Class.new(described_class) do
            attr_accessor :item_size

            private

            def encode_items(*)
              ["\00" * item_size]
            end
          end

        gen_index = gen_index_class.new(TestFile.new(StringIO.new("\00\00")), 0, 2)
        gen_index.item_size = params[:item_size]

        data = gen_index.encode.string

        expect(data.length).to eq params[:data_size]

        index = described_class.new(TestFile.new(StringIO.new(data)), 0, data.length)

        expect(index.items_count).to eq 1
      end
    end

    it 'raises on more items than is possible to encode' do
      gen_index_class =
        Class.new(described_class) do
          private

          def encode_items(*)
            ["\00"] * 0x10000
          end
        end

      gen_index = gen_index_class.new(TestFile.new(StringIO.new("\00\00")), 0, 2)

      expect { gen_index.encode }.to raise_error(/too many items/i)
    end
  end
end