adsp 1.0.0

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