File: test_buffer.rb

package info (click to toggle)
ruby-bio 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,108 kB
  • sloc: ruby: 68,331; perl: 13; makefile: 11; sh: 1
file content (399 lines) | stat: -rw-r--r-- 11,800 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#
# = test/unit/bio/io/flatfile/test_buffer.rb - unit test for Bio::FlatFile::BufferedInputStream
#
#   Copyright (C) 2006 Naohisa Goto <ng@bioruby.org>
#
# License:: The Ruby License
#
#  $Id:$
#

# loading helper routine for testing bioruby
require 'pathname'
load Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 4,
                            'bioruby_test_helper.rb')).cleanpath.to_s

# libraries needed for the tests
require 'test/unit'
require 'stringio'
require 'bio/io/flatfile/buffer'

module Bio::TestFlatFileBufferedInputStream

  TestDataPath = BioRubyTestDataPath
  TestDataFastaFormat01 = File.join(TestDataPath, 'fasta', 'example1.txt')

  class TestBufferedInputStreamParseFileOpenArg < Test::Unit::TestCase

    K = Bio::FlatFile::BufferedInputStream

    def _parse_file_open_mode(mode)
      K.module_eval { _parse_file_open_mode(mode) }
    end
    private :_parse_file_open_mode

    def _parse_file_open_arg(*arg)
      K.module_eval { _parse_file_open_arg(*arg) }
    end
    private :_parse_file_open_arg

    def test_parse_file_open_mode_nil
      assert_equal(nil, _parse_file_open_mode(nil))
    end

    def test_parse_file_open_mode_integer
      assert_equal({ :fmode_integer => 127 },
                   _parse_file_open_mode(127))
    end

    def test_parse_file_open_mode_str
      assert_equal({ :fmode_string => "r+b" },
                   _parse_file_open_mode("r+b"))
    end

    def test_parse_file_open_mode_str_with_ext_enc
      assert_equal({ :fmode_string => "r+t",
                     :external_encoding => "UTF-8" },
                   _parse_file_open_mode("r+t:UTF-8"))
    end

    def test_parse_file_open_mode_str_with_enc
      assert_equal({ :fmode_string => "rb",
                     :external_encoding => "EUC-JP",
                     :internal_encoding => "UTF-8" },
                   _parse_file_open_mode("rb:EUC-JP:UTF-8"))
    end

    def test_parse_file_open_arg_nil
      assert_equal({}, _parse_file_open_arg(nil))
    end

    def test_parse_file_open_arg_integer
      assert_equal({ :fmode_integer => 127 },
                   _parse_file_open_arg(127))
    end

    def test_parse_file_open_arg_str
      assert_equal({ :fmode_string => "r+b" },
                   _parse_file_open_arg("r+b"))
    end

    def test_parse_file_open_arg_str_with_ext_enc
      assert_equal({ :fmode_string => "r+t",
                     :external_encoding => "UTF-8" },
                   _parse_file_open_arg("r+t:UTF-8"))
    end

    def test_parse_file_open_arg_str_with_enc
      assert_equal({ :fmode_string => "rb",
                     :external_encoding => "EUC-JP",
                     :internal_encoding => "UTF-8" },
                   _parse_file_open_arg("rb:EUC-JP:UTF-8"))
    end

    def test_parse_file_open_arg_str_perm
      assert_equal({ :fmode_string => "r+b",
                     :perm => 0644 },
                   _parse_file_open_arg("r+b", 0644))
    end

    def test_parse_file_open_arg_int_perm
      assert_equal({ :fmode_integer => 255,
                     :perm => 0755 },
                   _parse_file_open_arg(255, 0755))
    end

    def test_parse_file_open_arg_int_perm_opt
      assert_equal({ :fmode_integer => 191,
                     :perm => 0600,
                     :textmode => true,
                     :internal_encoding => "EUC-JP" },
                   _parse_file_open_arg(191, 0600,
                                        :textmode => true,
                                        :internal_encoding => "EUC-JP"))
    end

    def test_parse_file_open_arg_int_opt
      assert_equal({ :fmode_integer => 191,
                     :textmode => true,
                     :internal_encoding => "EUC-JP" },
                   _parse_file_open_arg(191, 
                                        :textmode => true,
                                        :internal_encoding => "EUC-JP"))
    end

    def test_parse_file_open_arg_str_perm_opt
      assert_equal({ :fmode_string => "a",
                     :perm => 0644,
                     :binmode => true,
                     :external_encoding => "UTF-8" },
                   _parse_file_open_arg("a", 0644,
                                        :binmode => true,
                                        :external_encoding => "UTF-8"))
    end

    def test_parse_file_open_arg_str_opt
      assert_equal({ :fmode_string => "a",
                     :binmode => true,
                     :external_encoding => "UTF-8" },
                   _parse_file_open_arg("a",
                                        :binmode => true,
                                        :external_encoding => "UTF-8"))
    end

    def test_parse_file_open_arg_opt
      assert_equal({ :fmode_string => "r",
                     :binmode => true,
                     :external_encoding => "UTF-8" },
                   _parse_file_open_arg(:mode => "r",
                                        :binmode => true,
                                        :external_encoding => "UTF-8"))
    end

    def test_parse_file_open_arg_opt_with_integer_mode
      assert_equal({ :fmode_integer => 123,
                     :perm => 0600,
                     :textmode => true,
                     :external_encoding => "EUC-JP" },
                   _parse_file_open_arg(:mode => 123,
                                        :perm => 0600,
                                        :textmode => true,
                                        :external_encoding => "EUC-JP"))
    end
  end #class TestBufferedInputStreamParseFileOpenArg

  class TestBufferedInputStreamClassMethod < Test::Unit::TestCase

    def test_self_for_io
      io = File.open(TestDataFastaFormat01)
      obj = Bio::FlatFile::BufferedInputStream.for_io(io)
      assert_instance_of(Bio::FlatFile::BufferedInputStream, obj)
      assert_equal(TestDataFastaFormat01, obj.path)
    end

    def test_self_open_file
      obj = Bio::FlatFile::BufferedInputStream.open_file(TestDataFastaFormat01)
      assert_instance_of(Bio::FlatFile::BufferedInputStream, obj)
      assert_equal(TestDataFastaFormat01, obj.path)
    end

    def test_self_open_file_with_block
      obj2 = nil
      Bio::FlatFile::BufferedInputStream.open_file(TestDataFastaFormat01) do |obj|
        assert_instance_of(Bio::FlatFile::BufferedInputStream, obj)
        assert_equal(TestDataFastaFormat01, obj.path)
        obj2 = obj
      end
      # Since Ruby 2.3.0, calling IO#close to closed IO object is allowed
      # without error.
      if RUBY_VERSION >= "2.3.0"
        assert_nothing_raised { obj2.close }
      else
        assert_raise(IOError) { obj2.close }
      end
    end
  end #class TestBufferedInputStreamClassMethod

  class TestBufferedInputStream < Test::Unit::TestCase
    def setup
      io = File.open(TestDataFastaFormat01)
      io.binmode
      path = TestDataFastaFormat01
      @obj = Bio::FlatFile::BufferedInputStream.new(io, path)
    end

    def test_to_io
      assert_kind_of(IO, @obj.to_io)
    end

    def test_close
      assert_nil(@obj.close)
    end

    def test_rewind
      @obj.prefetch_gets
      @obj.rewind
      assert_equal('', @obj.prefetch_buffer)
    end

    def test_pos
      @obj.gets
      @obj.gets
      @obj.prefetch_gets
      assert_equal(117, @obj.pos) #the number depends on original data
    end

    def test_pos=()
      str = @obj.gets
      assert_equal(0, @obj.pos = 0)
      assert_equal(str, @obj.gets)
    end
      
    def test_eof_false_first
      assert_equal(false, @obj.eof?)
    end

    def test_eof_false_after_prefetch
      while @obj.prefetch_gets; nil; end
      assert_equal(false, @obj.eof?)
    end

    def test_eof_true
      while @obj.gets; nil; end
      assert_equal(true, @obj.eof?)
    end

    def test_gets
      @obj.gets
      @obj.gets
      assert_equal("gagcaaatcgaaaaggagagatttctgcatatcaagagaaaattcgagctgagatacattccaagtgtggctactc", @obj.gets.chomp)
    end

    def test_gets_equal_prefetch_gets
      @obj.prefetch_gets
      str = @obj.prefetch_gets
      @obj.prefetch_gets
      @obj.gets
      assert_equal(@obj.gets, str)
    end

    def test_gets_rs
      rs = 'tggtg'
      str = <<__END_OF_STR__
aggcactagaattgagcagtgaa
gaagatgaggaagatgaagaagaagatgaggaagaaatcaagaaagaaaaatgcgaattttctgaagatgtagacc
gatttatatggacggttgggcaggactatggtttggatgatctggtcgtgcggcgtgctctcgccaagtacctcga
agtggatgtttcggacatattggaaagatacaatgaactcaagcttaagaatgatggaactgctggtg
__END_OF_STR__
      @obj.gets(rs)
      @obj.gets(rs)
      assert_equal(str.chomp, @obj.gets(rs))
    end

    def test_gets_rs_equal_prefetch_gets
      rs = 'tggtg'
      @obj.prefetch_gets(rs)
      str = @obj.prefetch_gets(rs)
      @obj.prefetch_gets(rs)
      @obj.gets(rs)
      assert_equal(@obj.gets(rs), str)
    end

    def test_gets_rs_within_buffer
      rs = 'tggtg'
      a = []
      20.times {a.push @obj.gets }
      @obj.ungets(a.join(''))
      
      assert_equal(">At1g02580 mRNA (2291 bp) UTR's and CDS\naggcgagtggttaatggagaaggaaaaccatgaggacgatggtg", @obj.gets(rs))

      assert_equal('ggctgaaagtgattctgtgattggtaagagacaaatctattatttgaatggtg',
                   @obj.gets(rs).split(/\n/)[-1])

      assert_equal('aggcactagaattgagcagtgaa',
                   @obj.gets(rs).split(/\n/)[0])

      assert_equal('aggcttct', @obj.gets(rs).split(/\n/)[0])

      assert_equal('agacacc', @obj.gets(rs).split(/\n/)[0])
    end

    def test_gets_paragraph_mode
      @obj.gets('')
      @obj.gets('')
      assert_equal('>At1g65300: mRNA 837bp (shortened at end)',
                   @obj.gets('').split(/\n/)[0])
    end

    def test_gets_paragraph_mode_equal_prefetch_gets
      rs = ''
      @obj.prefetch_gets(rs)
      str = @obj.prefetch_gets(rs)
      @obj.prefetch_gets(rs)
      @obj.gets(rs)
      assert_equal(@obj.gets(rs), str)
    end

    def test_gets_paragraph_mode_within_buffer
      @obj.gets('')
      a = []
      20.times {a.push @obj.gets }
      @obj.ungets(a.join(''))
     
      assert_equal('>At1g65300: mRNA 837bp',
                   @obj.gets('').split(/\n/)[0])

      assert_equal('>At1g65300: mRNA 837bp (shortened at end)',
                   @obj.gets('').split(/\n/)[0])

      assert_equal('>At1g65300: mRNA 837bp (shortened from start)',
                   @obj.gets('').split(/\n/)[0])
    end

    def test_ungets
      @obj.gets
      @obj.gets
      str1 = @obj.gets
      str2 = @obj.gets
      assert_nil(@obj.ungets(str2))
      assert_nil(@obj.ungets(str1))
      assert_equal(str1, @obj.gets)
      assert_equal(str2, @obj.gets)
    end

    def test_getc
      assert_equal(?>, @obj.getc)
    end

    def test_getc_after_prefetch
      @obj.prefetch_gets
      assert_equal(?>, @obj.getc)
    end

    def test_ungetc
      c = @obj.getc
      assert_nil(@obj.ungetc(c))
      assert_equal(c, @obj.getc)
    end

    def test_ungetc_after_prefetch
      str = @obj.prefetch_gets
      c = @obj.getc
      assert_nil(@obj.ungetc(c))
      assert_equal(str, @obj.gets)
    end

    def test_prefetch_buffer
      str = @obj.prefetch_gets
      str += @obj.prefetch_gets
      assert_equal(str, @obj.prefetch_buffer)
    end

    def test_prefetch_gets
      @obj.prefetch_gets
      @obj.prefetch_gets
      @obj.gets
      str = @obj.prefetch_gets
      @obj.gets
      assert_equal(str, @obj.gets)
    end

    def test_prefetch_gets_with_arg
      # test @obj.gets
      str = @obj.prefetch_gets("\n>")
      assert_equal(str, @obj.gets("\n>"))
      # test using IO object
      io = @obj.to_io
      io.rewind
      assert_equal(str, io.gets("\n>"))
    end

    def test_skip_spaces
      @obj.gets('CDS')
      assert_nil(@obj.skip_spaces)
      assert_equal(?a, @obj.getc)
    end
                   
  end #class TestBufferedInputStream
end #module Bio::TestFlatFile