Class: ADSP::Test::Stream::WriterHelpers

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/adsp/test/stream/writer_helpers.rb

Overview

ADSP::Test::Stream::WriterHelpers class.

Constant Summary collapse

Target =
Mock::Stream::Writer
Option =
Test::Option
String =
Mock::String
ARCHIVE_PATH =
Common::ARCHIVE_PATH
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

Instance Method Summary collapse

Instance Method Details

#get_compatible_decompressor_options(compressor_options, &block) ⇒ Object



258
259
260
# File 'lib/adsp/test/stream/writer_helpers.rb', line 258

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

#parallel_compressor_options(&block) ⇒ Object



254
255
256
# File 'lib/adsp/test/stream/writer_helpers.rb', line 254

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

#test_invalid_openObject



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/adsp/test/stream/writer_helpers.rb', line 181

def test_invalid_open
  Validation::INVALID_STRINGS.each do |invalid_string|
    assert_raises ValidateError do
      target.open(invalid_string) {} # no-op
    end
  end

  # Proc is required.
  assert_raises ValidateError do
    target.open ARCHIVE_PATH
  end
end

#test_invalid_putcObject



106
107
108
109
110
111
112
113
114
# File 'lib/adsp/test/stream/writer_helpers.rb', line 106

def test_invalid_putc
  instance = target.new ::StringIO.new

  Validation::INVALID_CHARS.each do |invalid_char|
    assert_raises ValidateError do
      instance.putc invalid_char
    end
  end
end

#test_openObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/adsp/test/stream/writer_helpers.rb', line 194

def test_open
  parallel_compressor_options do |compressor_options, worker_index|
    archive_path = Common.get_path ARCHIVE_PATH, worker_index

    TEXTS.each do |text|
      target.open(archive_path, compressor_options) { |instance| instance.write text }

      compressed_text = ::File.read archive_path, :mode => "rb"

      get_compatible_decompressor_options compressor_options do |decompressor_options|
        check_text text, compressed_text, decompressor_options
      end
    end
  end
end

#test_open_with_large_textsObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/adsp/test/stream/writer_helpers.rb', line 210

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

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

    archive_path = Common.get_path ARCHIVE_PATH, worker_index

    sources = get_sources text, portion_length

    target.open archive_path do |instance|
      sources.each { |source| instance.write source }
    end

    compressed_text = ::File.read archive_path, :mode => "rb"

    check_text text, compressed_text, {}
  end
end

#test_printObject



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

def test_print
  parallel_compressor_options do |compressor_options, worker_index|
    archive_path = Common.get_path ARCHIVE_PATH, worker_index

    TEXTS.reject(&:empty?).each do |text|
      PORTION_LENGTHS.each do |portion_length|
        sources          = get_sources text, portion_length
        field_separator  = " ".encode text.encoding
        record_separator = "\n".encode text.encoding

        target_text = "".encode text.encoding
        sources.each { |source| target_text << (source + field_separator) }
        target_text << record_separator

        target.open archive_path, compressor_options do |instance|
          keyword_args = { :field_separator => field_separator, :record_separator => record_separator }
          instance.print(*sources, **keyword_args)
        end

        compressed_text = ::File.read archive_path, :mode => "rb"

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          check_text target_text, compressed_text, decompressor_options
        end
      end
    end
  end
end

#test_printfObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/adsp/test/stream/writer_helpers.rb', line 84

def test_printf
  parallel_compressor_options do |compressor_options, worker_index|
    archive_path = Common.get_path ARCHIVE_PATH, worker_index

    TEXTS.each do |text|
      PORTION_LENGTHS.each do |portion_length|
        sources = get_sources text, portion_length

        target.open archive_path, compressor_options do |instance|
          sources.each { |source| instance.printf "%s", source }
        end

        compressed_text = ::File.read archive_path, :mode => "rb"

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          check_text text, compressed_text, decompressor_options
        end
      end
    end
  end
end

#test_putcObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/adsp/test/stream/writer_helpers.rb', line 116

def test_putc
  parallel_compressor_options do |compressor_options, worker_index|
    archive_path = Common.get_path ARCHIVE_PATH, worker_index

    TEXTS.each do |text|
      target.open archive_path, compressor_options do |instance|
        # Putc should process numbers and strings.
        text.chars.each.with_index do |char, index|
          if index.even?
            instance.putc char.ord, :encoding => text.encoding
          else
            instance.putc char
          end
        end
      end

      compressed_text = ::File.read archive_path, :mode => "rb"

      get_compatible_decompressor_options compressor_options do |decompressor_options|
        check_text text, compressed_text, decompressor_options
      end
    end
  end
end

#test_putsObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/adsp/test/stream/writer_helpers.rb', line 141

def test_puts
  parallel_compressor_options do |compressor_options, worker_index|
    archive_path = Common.get_path ARCHIVE_PATH, worker_index

    TEXTS.each do |text|
      PORTION_LENGTHS.each do |portion_length|
        newline = "\n".encode text.encoding

        sources = get_sources text, portion_length
        sources = sources.map do |source|
          source.delete_suffix! newline while source.end_with? newline
          source
        end

        target_text = "".encode text.encoding
        sources.each { |source| target_text << (source + newline) }

        target.open archive_path, compressor_options do |instance|
          # Puts should ignore additional newlines and process arrays.
          args = sources.map.with_index do |source, index|
            if index.even?
              source + newline
            else
              [source]
            end
          end

          instance.puts(*args)
        end

        compressed_text = ::File.read archive_path, :mode => "rb"

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          check_text target_text, compressed_text, decompressor_options
        end
      end
    end
  end
end

#test_writeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/adsp/test/stream/writer_helpers.rb', line 33

def test_write
  parallel_compressor_options do |compressor_options, worker_index|
    archive_path = Common.get_path ARCHIVE_PATH, worker_index

    TEXTS.each do |text|
      PORTION_LENGTHS.each do |portion_length|
        sources = get_sources text, portion_length

        target.open archive_path, compressor_options do |instance|
          sources.each { |current_source| instance << current_source }
        end

        compressed_text = ::File.read archive_path, :mode => "rb"

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          check_text text, compressed_text, decompressor_options
        end
      end
    end
  end
end