1 # -*- encoding: binary -*-
7 class Test_IO_Splice < Test::Unit::TestCase
13 tmp = Tempfile.new('ruby_io_splice')
15 assert_nothing_raised {
20 nr = IO.splice(tmp.fileno, nil, wr.fileno, nil, size, 0)
22 assert_equal str, rd.sysread(size)
25 def test_splice_in_offset
30 tmp = Tempfile.new('ruby_io_splice')
32 assert_nothing_raised {
37 nr = IO.splice(tmp.fileno, off, wr.fileno, nil, len, 0)
39 assert_equal 'de', rd.sysread(len)
42 def test_splice_out_offset
45 tmp = Tempfile.new('ruby_io_splice')
47 assert_nothing_raised { wr.syswrite(str) }
48 nr = IO.splice(rd.fileno, nil, tmp.fileno, 3, str.size, 0)
50 assert_nothing_raised { tmp.sysseek(0) }
51 assert_equal "\0\0\0abcde", tmp.sysread(9)
54 def test_splice_nonblock
56 tmp = Tempfile.new('ruby_io_splice')
58 assert_raises(Errno::EAGAIN) {
59 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
65 tmp = Tempfile.new('ruby_io_splice')
69 nr = IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
71 assert_raises(EOFError) {
72 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
82 assert_nothing_raised { wra.syswrite(str) }
83 nr = IO.tee(rda.fileno, wrb.fileno, size, 0)
85 assert_equal str, rdb.sysread(5)
86 assert_equal str, rda.sysread(5)
93 assert_raises(EOFError) { IO.tee(rda.fileno, wrb.fileno, 4096, 0) }
99 assert_raises(Errno::EAGAIN) {
100 IO.tee(rda.fileno, wrb.fileno, 4096, IO::Splice::F_NONBLOCK)
104 def test_vmsplice_array
105 data = %w(hello world how are you today)
107 n = IO.vmsplice(w.fileno, data, 0)
108 assert_equal data.join('').size, n
109 assert_equal data.join(''), r.readpartial(16384)
112 def test_vmsplice_nonblock
113 data = %w(hello world how are you today)
115 w.syswrite('.' * IO::Splice::PIPE_CAPA)
116 assert_raises(Errno::EAGAIN) {
117 IO.vmsplice(w.fileno, data, IO::Splice::F_NONBLOCK)
121 def test_vmsplice_in_full
124 # bs * count should be > PIPE_BUF
125 [ [ 512, 512 ], [ 131073, 3 ], [ 4098, 64 ] ].each do |(bs,count)|
127 buf = File.open('/dev/urandom', 'rb') { |fp| fp.sysread(bs) }
129 vec = (1..count).map { buf }
135 tmp << rd.readpartial(8192, buf)
139 ok = (vec.join(empty) == tmp.join(empty))
142 assert_nothing_raised { rd.close }
143 assert_equal(bs * count, IO.vmsplice(wr.fileno, vec, 0))
144 assert_nothing_raised { wr.close }
145 _, status = Process.waitpid2(pid)
146 assert status.success?
151 assert IO::Splice::PIPE_BUF > 0
152 %w(move nonblock more gift).each { |x|
153 assert Integer === IO::Splice.const_get("F_#{x.upcase}")
155 assert IO::Splice::PIPE_CAPA >= IO::Splice::PIPE_BUF