Class: ADSP::Test::Stream::Raw::Decompressor

Inherits:
Abstract
  • Object
show all
Defined in:
lib/adsp/test/stream/raw/decompressor.rb

Overview

ADSP::Test::Stream::Raw::Decompressor class.

Constant Summary collapse

Target =
Mock::Stream::Raw::Decompressor
Option =
Test::Option
String =
Mock::String
TEXTS =
Common::TEXTS
LARGE_TEXTS =
Common::LARGE_TEXTS
PORTION_LENGTHS =
Common::PORTION_LENGTHS
LARGE_PORTION_LENGTHS =
Common::LARGE_PORTION_LENGTHS
BUFFER_LENGTH_NAMES =
%i[destination_buffer_length].freeze
BUFFER_LENGTH_MAPPING =
{ :destination_buffer_length => :destination_buffer_length }.freeze

Constants inherited from Abstract

Abstract::NOOP_PROC

Instance Method Summary collapse

Methods inherited from Abstract

#test_invalid_close, #test_invalid_flush

Instance Method Details

#get_compatible_decompressor_options(compressor_options, &block) ⇒ Object



161
162
163
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 161

def get_compatible_decompressor_options(compressor_options, &block)
  option.get_compatible_decompressor_options compressor_options, BUFFER_LENGTH_MAPPING, &block
end

#get_invalid_decompressor_options(&block) ⇒ Object




153
154
155
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 153

def get_invalid_decompressor_options(&block)
  option.get_invalid_decompressor_options BUFFER_LENGTH_NAMES, &block
end

#optionObject



165
166
167
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 165

def option
  self.class::Option
end

#parallel_compressor_options(&block) ⇒ Object



157
158
159
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 157

def parallel_compressor_options(&block)
  Common.parallel_options option.get_compressor_options_generator(BUFFER_LENGTH_NAMES), &block
end

#stringObject



169
170
171
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 169

def string
  self.class::String
end

#test_invalid_initializeObject



32
33
34
35
36
37
38
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 32

def test_invalid_initialize
  get_invalid_decompressor_options do |invalid_options|
    assert_raises ValidateError do
      target.new invalid_options
    end
  end
end

#test_invalid_readObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 40

def test_invalid_read
  decompressor = target.new

  Validation::INVALID_STRINGS.each do |invalid_string|
    assert_raises ValidateError do
      decompressor.read invalid_string, &NOOP_PROC
    end
  end

  assert_raises ValidateError do
    decompressor.read ""
  end

  decompressor.close(&NOOP_PROC)

  assert_raises UsedAfterCloseError do
    decompressor.read "", &NOOP_PROC
  end
end

#test_large_textsObject



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
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 108

def test_large_texts
  options_generator = OCG.new(
    :text           => LARGE_TEXTS,
    :portion_length => LARGE_PORTION_LENGTHS
  )

  Common.parallel_options options_generator do |options|
    text           = options[:text]
    portion_length = options[:portion_length]

    compressed_text = string.compress text

    decompressed_buffer = ::StringIO.new
    decompressed_buffer.set_encoding ::Encoding::BINARY

    writer       = proc { |portion| decompressed_buffer << portion }
    decompressor = target.new

    begin
      source                 = "".b
      compressed_text_offset = 0

      loop do
        portion = compressed_text.byteslice compressed_text_offset, portion_length
        break if portion.nil?

        compressed_text_offset += portion_length
        source << portion

        bytes_read = decompressor.read source, &writer
        source     = source.byteslice bytes_read, source.bytesize - bytes_read
      end
    ensure
      decompressor.close(&writer)
    end

    decompressed_text = decompressed_buffer.string
    decompressed_text.force_encoding text.encoding

    assert_equal text, decompressed_text
  end
end

#test_textsObject



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
# File 'lib/adsp/test/stream/raw/decompressor.rb', line 60

def test_texts
  parallel_compressor_options do |compressor_options|
    TEXTS.each do |text|
      compressed_text = string.compress text, compressor_options

      PORTION_LENGTHS.each do |portion_length|
        get_compatible_decompressor_options compressor_options do |decompressor_options|
          decompressed_buffer = ::StringIO.new
          decompressed_buffer.set_encoding ::Encoding::BINARY

          writer       = proc { |portion| decompressed_buffer << portion }
          decompressor = target.new decompressor_options

          begin
            source                 = "".b
            compressed_text_offset = 0
            index                  = 0

            loop do
              portion = compressed_text.byteslice compressed_text_offset, portion_length
              break if portion.nil?

              compressed_text_offset += portion_length
              source << portion

              bytes_read = decompressor.read source, &writer
              source     = source.byteslice bytes_read, source.bytesize - bytes_read

              decompressor.flush(&writer) if index.even?
              index += 1
            end

          ensure
            refute_predicate decompressor, :closed?
            decompressor.close(&writer)
            assert_predicate decompressor, :closed?
          end

          decompressed_text = decompressed_buffer.string
          decompressed_text.force_encoding text.encoding

          assert_equal text, decompressed_text
        end
      end
    end
  end
end