summaryrefslogtreecommitdiff
path: root/test/ruby/ut_eof.rb
blob: fcd7a63988954cd1004672161d736d1da379c6c8 (plain)
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
# frozen_string_literal: false
require 'test/unit'

module TestEOF
  def test_eof_0
    open_file("") {|f|
      assert_equal("", f.read(0))
      assert_equal("", f.read(0))
      assert_equal("", f.read)
      assert_equal("", f.read(0))
      assert_equal("", f.read(0))
    }
    open_file("") {|f|
      assert_nil(f.read(1))
      assert_equal("", f.read)
      assert_nil(f.read(1))
    }
    open_file("") {|f|
      s = "x"
      assert_equal("", f.read(nil, s))
      assert_equal("", s)
    }
    open_file("") {|f|
      s = "x"
      assert_nil(f.read(10, s))
      assert_equal("", s)
    }
  end

  def test_eof_0_rw
    return unless respond_to? :open_file_rw
    open_file_rw("") {|f|
      assert_equal("", f.read)
      assert_equal("", f.read)
      assert_equal(0, f.syswrite(""))
      assert_equal("", f.read)
    }
  end

  def test_eof_1
    open_file("a") {|f|
      assert_equal("", f.read(0))
      assert_equal("a", f.read(1))
      assert_equal("" , f.read(0))
      assert_equal("" , f.read(0))
      assert_equal("", f.read)
      assert_equal("", f.read(0))
      assert_equal("", f.read(0))
    }
    open_file("a") {|f|
      assert_equal("a", f.read(1))
      assert_nil(f.read(1))
    }
    open_file("a") {|f|
      assert_equal("a", f.read(2))
      assert_nil(f.read(1))
      assert_equal("", f.read)
      assert_nil(f.read(1))
    }
    open_file("a") {|f|
      assert_equal("a", f.read)
      assert_nil(f.read(1))
      assert_equal("", f.read)
      assert_nil(f.read(1))
    }
    open_file("a") {|f|
      assert_equal("a", f.read(2))
      assert_equal("", f.read)
      assert_equal("", f.read)
    }
    open_file("a") {|f|
      assert_equal("a", f.read)
      assert_equal("", f.read(0))
    }
    open_file("a") {|f|
      s = "x"
      assert_equal("a", f.read(nil, s))
      assert_equal("a", s)
    }
    open_file("a") {|f|
      s = "x"
      assert_equal("a", f.read(10, s))
      assert_equal("a", s)
    }
  end

  def test_eof_2
    open_file("") {|f|
      assert_equal("", f.read)
      assert_predicate(f, :eof?)
    }
  end

  def test_eof_3
    open_file("") {|f|
      assert_predicate(f, :eof?)
    }
  end

  module Seek
    def open_file_seek(content, pos)
      open_file(content) do |f|
        f.seek(pos)
        yield f
      end
    end

    def test_eof_0_seek
      open_file_seek("", 10) {|f|
        assert_equal(10, f.pos)
        assert_equal("", f.read(0))
        assert_equal("", f.read)
        assert_equal("", f.read(0))
        assert_equal("", f.read)
      }
    end

    def test_eof_1_seek
      open_file_seek("a", 10) {|f|
        assert_equal("", f.read)
        assert_equal("", f.read)
      }
      open_file_seek("a", 1) {|f|
        assert_equal("", f.read)
        assert_equal("", f.read)
      }
    end
  end
end