adsp 1.0.1 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/{test → lib/adsp/test}/common.rb +2 -1
  4. data/{test → lib/adsp/test}/coverage_helper.rb +0 -0
  5. data/lib/adsp/test/file.rb +120 -0
  6. data/{test → lib/adsp/test}/minitest.rb +1 -0
  7. data/{test → lib/adsp/test}/mock/common.rb +1 -0
  8. data/{test → lib/adsp/test}/mock/file.rb +1 -0
  9. data/{test → lib/adsp/test}/mock/stream/raw/compressor.rb +1 -0
  10. data/{test → lib/adsp/test}/mock/stream/raw/decompressor.rb +1 -0
  11. data/{test → lib/adsp/test}/mock/stream/raw/native_compressor.rb +1 -0
  12. data/{test → lib/adsp/test}/mock/stream/raw/native_decompressor.rb +1 -0
  13. data/{test → lib/adsp/test}/mock/stream/reader.rb +1 -0
  14. data/{test → lib/adsp/test}/mock/stream/writer.rb +1 -0
  15. data/{test → lib/adsp/test}/mock/string.rb +1 -0
  16. data/{test → lib/adsp/test}/option.rb +1 -0
  17. data/{test → lib/adsp/test}/stream/abstract.rb +1 -0
  18. data/lib/adsp/test/stream/minitar.rb +50 -0
  19. data/{test → lib/adsp/test}/stream/raw/abstract.rb +1 -0
  20. data/lib/adsp/test/stream/raw/compressor.rb +165 -0
  21. data/lib/adsp/test/stream/raw/decompressor.rb +165 -0
  22. data/lib/adsp/test/stream/reader.rb +642 -0
  23. data/lib/adsp/test/stream/reader_helpers.rb +421 -0
  24. data/lib/adsp/test/stream/writer.rb +609 -0
  25. data/lib/adsp/test/stream/writer_helpers.rb +267 -0
  26. data/lib/adsp/test/string.rb +95 -0
  27. data/{test → lib/adsp/test}/validation.rb +7 -0
  28. data/lib/adsp/test/version.rb +18 -0
  29. data/lib/adsp/version.rb +1 -1
  30. data/test/file.test.rb +3 -116
  31. data/test/stream/minitar.test.rb +3 -46
  32. data/test/stream/raw/compressor.test.rb +3 -162
  33. data/test/stream/raw/decompressor.test.rb +3 -162
  34. data/test/stream/reader.test.rb +3 -639
  35. data/test/stream/reader_helpers.test.rb +3 -417
  36. data/test/stream/writer.test.rb +3 -606
  37. data/test/stream/writer_helpers.test.rb +3 -263
  38. data/test/string.test.rb +3 -91
  39. data/test/version.test.rb +3 -14
  40. metadata +28 -19
@@ -0,0 +1,421 @@
1
+ # Abstract data stream processor.
2
+ # Copyright (c) 2021 AUTHORS, MIT License.
3
+
4
+ require "English"
5
+ require "stringio"
6
+
7
+ require_relative "../common"
8
+ require_relative "../minitest"
9
+ require_relative "../option"
10
+ require_relative "../validation"
11
+ require_relative "../mock/stream/reader"
12
+ require_relative "../mock/string"
13
+
14
+ module ADSP
15
+ module Test
16
+ # ADSP::Test::Stream module.
17
+ module Stream
18
+ # ADSP::Test::Stream::ReaderHelpers class.
19
+ class ReaderHelpers < Minitest::Test
20
+ Target = Mock::Stream::Reader
21
+ String = Mock::String
22
+
23
+ ARCHIVE_PATH = Common::ARCHIVE_PATH
24
+ ENCODINGS = Common::ENCODINGS
25
+ TRANSCODE_OPTIONS = Common::TRANSCODE_OPTIONS
26
+ TEXTS = Common::TEXTS
27
+ LARGE_TEXTS = Common::LARGE_TEXTS
28
+
29
+ BUFFER_LENGTH_NAMES = %i[source_buffer_length destination_buffer_length].freeze
30
+ BUFFER_LENGTH_MAPPING = {
31
+ :source_buffer_length => :destination_buffer_length,
32
+ :destination_buffer_length => :source_buffer_length
33
+ }
34
+ .freeze
35
+
36
+ LIMITS = [nil, 1].freeze
37
+
38
+ def test_invalid_ungetbyte
39
+ instance = target.new ::StringIO.new
40
+
41
+ Validation::INVALID_STRINGS.each do |invalid_string|
42
+ assert_raises ValidateError do
43
+ instance.ungetbyte invalid_string
44
+ end
45
+ end
46
+ end
47
+
48
+ def test_byte
49
+ parallel_compressor_options do |compressor_options, worker_index|
50
+ archive_path = Common.get_path ARCHIVE_PATH, worker_index
51
+
52
+ TEXTS.each do |text|
53
+ write_archive archive_path, text, compressor_options
54
+
55
+ get_compatible_decompressor_options compressor_options do |decompressor_options|
56
+ Target.open archive_path, decompressor_options do |instance|
57
+ # getbyte
58
+
59
+ byte = instance.getbyte
60
+ instance.ungetbyte byte unless byte.nil?
61
+
62
+ # readbyte
63
+
64
+ begin
65
+ byte = instance.readbyte
66
+ instance.ungetc byte
67
+ rescue ::EOFError
68
+ # ok
69
+ end
70
+
71
+ # each_byte
72
+
73
+ decompressed_text = "".b
74
+ instance.each_byte { |current_byte| decompressed_text << current_byte }
75
+
76
+ decompressed_text.force_encoding text.encoding
77
+ assert_equal text, decompressed_text
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ # -- char --
85
+
86
+ def test_invalid_ungetc
87
+ instance = target.new ::StringIO.new
88
+
89
+ Validation::INVALID_STRINGS.each do |invalid_string|
90
+ assert_raises ValidateError do
91
+ instance.ungetc invalid_string
92
+ end
93
+ end
94
+ end
95
+
96
+ def test_char
97
+ parallel_compressor_options do |compressor_options, worker_index|
98
+ archive_path = Common.get_path ARCHIVE_PATH, worker_index
99
+
100
+ TEXTS.each do |text|
101
+ write_archive archive_path, text, compressor_options
102
+
103
+ get_compatible_decompressor_options compressor_options do |decompressor_options|
104
+ Target.open archive_path, decompressor_options do |instance|
105
+ # getc
106
+
107
+ char = instance.getc
108
+ instance.ungetc char unless char.nil?
109
+
110
+ # readchar
111
+
112
+ begin
113
+ char = instance.readchar
114
+ instance.ungetc char
115
+ rescue ::EOFError
116
+ # ok
117
+ end
118
+
119
+ # each_char
120
+
121
+ decompressed_text = "".b
122
+ instance.each_char { |current_char| decompressed_text << current_char }
123
+
124
+ decompressed_text.force_encoding text.encoding
125
+ assert_equal text, decompressed_text
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ def test_char_encoding
133
+ parallel_compressor_options do |compressor_options, worker_index|
134
+ archive_path = Common.get_path ARCHIVE_PATH, worker_index
135
+
136
+ TEXTS.each do |text|
137
+ write_archive archive_path, text, compressor_options
138
+
139
+ external_encoding = text.encoding
140
+
141
+ (ENCODINGS - [external_encoding]).each do |internal_encoding|
142
+ target_text = text.encode internal_encoding, **TRANSCODE_OPTIONS
143
+
144
+ get_compatible_decompressor_options compressor_options do |decompressor_options|
145
+ Target.open archive_path, decompressor_options do |instance|
146
+ instance.set_encoding external_encoding, internal_encoding, TRANSCODE_OPTIONS
147
+
148
+ # getc
149
+
150
+ char = instance.getc
151
+
152
+ unless char.nil?
153
+ assert_equal internal_encoding, char.encoding
154
+ instance.ungetc char
155
+ end
156
+
157
+ # readchar
158
+
159
+ begin
160
+ char = instance.readchar
161
+ assert_equal internal_encoding, char.encoding
162
+
163
+ instance.ungetc char
164
+ rescue ::EOFError
165
+ # ok
166
+ end
167
+
168
+ # each_char
169
+
170
+ decompressed_text = ::String.new :encoding => internal_encoding
171
+
172
+ instance.each_char do |current_char|
173
+ assert_equal internal_encoding, current_char.encoding
174
+ decompressed_text << current_char
175
+ end
176
+
177
+ assert_equal target_text, decompressed_text
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
184
+
185
+ # -- lines --
186
+
187
+ def test_invalid_gets
188
+ instance = target.new ::StringIO.new
189
+
190
+ (Validation::INVALID_STRINGS - [nil, 1, 1.1]).each do |invalid_string|
191
+ assert_raises ValidateError do
192
+ instance.gets invalid_string
193
+ end
194
+ end
195
+
196
+ (Validation::INVALID_POSITIVE_INTEGERS - [nil]).each do |invalid_integer|
197
+ assert_raises ValidateError do
198
+ instance.gets nil, invalid_integer
199
+ end
200
+ end
201
+ end
202
+
203
+ def test_invalid_ungetline
204
+ instance = target.new ::StringIO.new
205
+
206
+ Validation::INVALID_STRINGS.each do |invalid_string|
207
+ assert_raises ValidateError do
208
+ instance.ungetline invalid_string
209
+ end
210
+ end
211
+ end
212
+
213
+ def test_lines
214
+ parallel_compressor_options do |compressor_options, worker_index|
215
+ archive_path = Common.get_path ARCHIVE_PATH, worker_index
216
+
217
+ TEXTS.each do |text|
218
+ write_archive archive_path, text, compressor_options
219
+
220
+ separator =
221
+ if text.empty?
222
+ nil
223
+ else
224
+ text[0]
225
+ end
226
+
227
+ get_compatible_decompressor_options compressor_options do |decompressor_options|
228
+ Target.open archive_path, decompressor_options do |instance|
229
+ # lineno
230
+
231
+ assert_equal 0, instance.lineno
232
+
233
+ instance.lineno = 1
234
+ assert_equal 1, instance.lineno
235
+
236
+ instance.lineno = 0
237
+ assert_equal 0, instance.lineno
238
+
239
+ # gets
240
+
241
+ LIMITS.each do |limit|
242
+ line = instance.gets limit
243
+ next if line.nil?
244
+
245
+ assert_equal 1, instance.lineno
246
+
247
+ instance.ungetline line
248
+ assert_equal 0, instance.lineno
249
+
250
+ # Same test with separator.
251
+
252
+ line = instance.gets separator, limit
253
+ next if line.nil?
254
+
255
+ assert_equal 1, instance.lineno
256
+
257
+ instance.ungetline line
258
+ assert_equal 0, instance.lineno
259
+ end
260
+
261
+ # readline
262
+
263
+ begin
264
+ line = instance.readline
265
+ assert_equal 1, instance.lineno
266
+
267
+ instance.ungetline line
268
+ assert_equal 0, instance.lineno
269
+ rescue ::EOFError
270
+ # ok
271
+ end
272
+
273
+ # readlines
274
+
275
+ lines = instance.readlines
276
+ lines.each { |current_line| instance.ungetline current_line }
277
+
278
+ decompressed_text = lines.join
279
+ decompressed_text.force_encoding text.encoding
280
+
281
+ assert_equal text, decompressed_text
282
+
283
+ # each_line
284
+
285
+ decompressed_text = "".b
286
+ instance.each_line { |current_line| decompressed_text << current_line }
287
+
288
+ decompressed_text.force_encoding text.encoding
289
+ assert_equal text, decompressed_text
290
+ end
291
+ end
292
+ end
293
+ end
294
+ end
295
+
296
+ def test_lines_encoding
297
+ parallel_compressor_options do |compressor_options, worker_index|
298
+ archive_path = Common.get_path ARCHIVE_PATH, worker_index
299
+
300
+ TEXTS.each do |text|
301
+ write_archive archive_path, text, compressor_options
302
+
303
+ external_encoding = text.encoding
304
+
305
+ (ENCODINGS - [external_encoding]).each do |internal_encoding|
306
+ target_text = text.encode internal_encoding, **TRANSCODE_OPTIONS
307
+
308
+ separator =
309
+ if text.empty?
310
+ nil
311
+ else
312
+ text[0]
313
+ end
314
+
315
+ get_compatible_decompressor_options compressor_options do |decompressor_options|
316
+ Target.open archive_path, decompressor_options do |instance|
317
+ instance.set_encoding external_encoding, internal_encoding, TRANSCODE_OPTIONS
318
+
319
+ # gets
320
+
321
+ line = instance.gets separator
322
+
323
+ unless line.nil?
324
+ assert_equal internal_encoding, line.encoding
325
+ instance.ungetline line
326
+ end
327
+
328
+ # readline
329
+
330
+ begin
331
+ line = instance.readline
332
+ assert_equal internal_encoding, line.encoding
333
+
334
+ instance.ungetline line
335
+ rescue ::EOFError
336
+ # ok
337
+ end
338
+
339
+ # each_line
340
+
341
+ decompressed_text = ::String.new :encoding => internal_encoding
342
+
343
+ instance.each_line do |current_line|
344
+ assert_equal internal_encoding, current_line.encoding
345
+ decompressed_text << current_line
346
+ end
347
+
348
+ assert_equal target_text, decompressed_text
349
+ end
350
+ end
351
+ end
352
+ end
353
+ end
354
+ end
355
+
356
+ # -- etc --
357
+
358
+ def test_invalid_open
359
+ Validation::INVALID_STRINGS.each do |invalid_string|
360
+ assert_raises ValidateError do
361
+ Target.open(invalid_string) {} # no-op
362
+ end
363
+ end
364
+
365
+ # Proc is required.
366
+ assert_raises ValidateError do
367
+ Target.open ARCHIVE_PATH
368
+ end
369
+ end
370
+
371
+ def test_open
372
+ parallel_compressor_options do |compressor_options, worker_index|
373
+ archive_path = Common.get_path ARCHIVE_PATH, worker_index
374
+
375
+ TEXTS.each do |text|
376
+ write_archive archive_path, text, compressor_options
377
+
378
+ get_compatible_decompressor_options compressor_options do |decompressor_options|
379
+ decompressed_text = Target.open archive_path, decompressor_options, &:read
380
+ decompressed_text.force_encoding text.encoding
381
+
382
+ assert_equal text, decompressed_text
383
+ end
384
+ end
385
+ end
386
+ end
387
+
388
+ def test_open_with_large_texts
389
+ Common.parallel LARGE_TEXTS do |text, worker_index|
390
+ archive_path = Common.get_path ARCHIVE_PATH, worker_index
391
+ write_archive archive_path, text
392
+
393
+ decompressed_text = Target.open archive_path, &:read
394
+ decompressed_text.force_encoding text.encoding
395
+
396
+ assert_equal text, decompressed_text
397
+ end
398
+ end
399
+
400
+ # -----
401
+
402
+ protected def write_archive(archive_path, text, compressor_options = {})
403
+ compressed_text = String.compress text, compressor_options
404
+ ::File.write archive_path, compressed_text, :mode => "wb"
405
+ end
406
+
407
+ def parallel_compressor_options(&block)
408
+ Common.parallel_options Option.get_compressor_options_generator(BUFFER_LENGTH_NAMES), &block
409
+ end
410
+
411
+ def get_compatible_decompressor_options(compressor_options, &block)
412
+ Option.get_compatible_decompressor_options compressor_options, BUFFER_LENGTH_MAPPING, &block
413
+ end
414
+
415
+ protected def target
416
+ self.class::Target
417
+ end
418
+ end
419
+ end
420
+ end
421
+ end