Class: ADSP::Test::Stream::Raw::Compressor

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

Overview

ADSP::Test::Stream::Raw::Compressor class.

Constant Summary collapse

Target =
Mock::Stream::Raw::Compressor
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/compressor.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_compressor_options(&block) ⇒ Object




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

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

#optionObject



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

def option
  self.class::Option
end

#parallel_compressor_options(&block) ⇒ Object



157
158
159
# File 'lib/adsp/test/stream/raw/compressor.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/compressor.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/compressor.rb', line 32

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

#test_invalid_writeObject



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/compressor.rb', line 40

def test_invalid_write
  compressor = target.new

  Validation::INVALID_STRINGS.each do |invalid_string|
    assert_raises ValidateError do
      compressor.write invalid_string, &NOOP_PROC
    end
  end

  assert_raises ValidateError do
    compressor.write ""
  end

  compressor.close(&NOOP_PROC)

  assert_raises UsedAfterCloseError do
    compressor.write "", &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/compressor.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_buffer = ::StringIO.new
    compressed_buffer.set_encoding ::Encoding::BINARY

    writer     = proc { |portion| compressed_buffer << portion }
    compressor = target.new

    begin
      source      = "".b
      text_offset = 0

      loop do
        portion = text.byteslice text_offset, portion_length
        break if portion.nil?

        text_offset += portion_length
        source << portion

        bytes_written = compressor.write source, &writer
        source        = source.byteslice bytes_written, source.bytesize - bytes_written
      end
    ensure
      compressor.close(&writer)
    end

    compressed_text = compressed_buffer.string

    decompressed_text = string.decompress compressed_text
    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/compressor.rb', line 60

def test_texts
  parallel_compressor_options do |compressor_options|
    TEXTS.each do |text|
      PORTION_LENGTHS.each do |portion_length|
        compressed_buffer = ::StringIO.new
        compressed_buffer.set_encoding ::Encoding::BINARY

        writer     = proc { |portion| compressed_buffer << portion }
        compressor = target.new compressor_options

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

          loop do
            portion = text.byteslice text_offset, portion_length
            break if portion.nil?

            text_offset += portion_length
            source << portion

            bytes_written = compressor.write source, &writer
            source        = source.byteslice bytes_written, source.bytesize - bytes_written

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

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

        compressed_text = compressed_buffer.string

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          decompressed_text = string.decompress compressed_text, decompressor_options
          decompressed_text.force_encoding text.encoding

          assert_equal text, decompressed_text
        end
      end
    end
  end
end