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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
|
require 'test/unit'
require 'fileutils'
require 'rbconfig'
# FIXME: This needs platform-specific stuff changed
class TestIO < Test::Unit::TestCase
IS19 = RUBY_VERSION =~ /1\.9/
WIN32 = RbConfig::CONFIG['host_os'] =~ /mswin/
SAMPLE = "08: This is a line\n"
LINE_LENGTH = WIN32 ? SAMPLE.length + 1 : SAMPLE.length
def setup_test_dir
Dir.mkdir "_test"
end
def teardown_test_dir
FileUtils.rm_rf "_test"
end
def setup
setup_test_dir
@file = "_test/_10lines"
@file1 = "_test/_99lines"
File.open(@file, "w") do |f|
10.times { |i| f.printf "%02d: This is a line\n", i }
end
File.open(@file1, "w") do |f|
99.times { |i| f.printf "Line %02d\n", i }
end
end
def teardown
teardown_test_dir
end
unless WIN32
def stdin_copy_pipe
IO.popen("#$interpreter -e '$stdout.sync=true;while gets;puts $_;end'", "r+")
end
end
# ---------------------------------------------------------------
def test_s_foreach
assert_raise(Errno::ENOENT) { File.foreach("gumby") {} }
# No longer a valid test in 1.8.7
=begin
assert_raise(LocalJumpError) { File.foreach(@file) }
=end
count = 0
IO.foreach(@file) do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
assert_equal(10, count)
count = 0
IO.foreach(@file, nil) do |file|
file.split(/\n/).each do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
end
assert_equal(10, count)
count = 0
IO.foreach(@file, ' ') do |thing|
count += 1
end
assert_equal(41, count)
end
def test_s_new
f = File.open(@file)
io = IO.new(f.fileno, "r")
begin
count = 0
io.each { count += 1 }
assert_equal(10, count)
ensure
io.close
begin
f.close
rescue Exception
end
end
f = File.open(@file)
io = IO.new(f.fileno, "r")
begin
f.close
assert_raise(Errno::EBADF) { io.gets }
ensure
assert_raise(Errno::EBADF) { io.close }
begin
f.close
rescue Exception
end
end
f = File.open(@file, "r")
f.sysread(3*LINE_LENGTH)
io = IO.new(f.fileno, "r")
begin
assert_equal(3*LINE_LENGTH, io.tell)
count = 0
io.each { count += 1 }
assert_equal(7, count)
ensure
io.close
begin
f.close
rescue Exception
end
end
end
def test_s_pipe
p = IO.pipe
begin
assert_equal(2, p.size)
r, w = *p
assert_instance_of(IO, r)
assert_instance_of(IO, w)
w.puts "Hello World"
assert_equal("Hello World\n", r.gets)
ensure
r.close
w.close
end
end
# TODO: fails on 1.8.6
=begin
def test_s_popen
if WIN32
cmd = "type"
fname = @file.tr '/', '\\'
else
cmd = "cat"
fname = @file
end
# READ
IO.popen("#{cmd} #{fname}") do |p|
count = 0
p.each do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
assert_equal(10, count)
end
# READ with block
res = IO.popen("#{cmd} #{fname}") do |p|
count = 0
p.each do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
assert_equal(10, count)
end
assert_nil(res)
# WRITE
IO.popen("#$interpreter -e 'puts readlines' >#{fname}", "w") do |p|
5.times { |i| p.printf "Line %d\n", i }
end
count = 0
IO.foreach(@file) do |line|
num = line.chomp[-1,1].to_i
assert_equal(count, num)
count += 1
end
assert_equal(5, count)
unless WIN32
# Spawn an interpreter
parent = $$
p = IO.popen("-")
if p
begin
assert_equal(parent, $$)
assert_equal("Hello\n", p.gets)
ensure
p.close
end
else
assert_equal(parent, Process.ppid)
puts "Hello"
exit
end
end
end
=end
# disabled due to JRUBY-1895
def XXXtest_s_popen_spawn
unless WIN32
# Spawn an interpreter - WRITE
parent = $$
pipe = IO.popen("-", "w")
if pipe
begin
assert_equal(parent, $$)
pipe.puts "12"
Process.wait pipe.pid
assert_equal(12, $?>>8)
ensure
pipe.close
end
else
buff = $stdin.gets
exit buff.to_i
end
# Spawn an interpreter - READWRITE
parent = $$
p = IO.popen("-", "w+")
if p
begin
assert_equal(parent, $$)
p.puts "Hello\n"
assert_equal("Goodbye\n", p.gets)
Process.wait
ensure
p.close
end
else
puts "Goodbye" if $stdin.gets == "Hello\n"
exit
end
end
end
def test_s_readlines
assert_raise(Errno::ENOENT) { IO.readlines('gumby') }
lines = IO.readlines(@file)
assert_equal(10, lines.size)
lines = IO.readlines(@file, nil)
assert_equal(1, lines.size)
assert_equal(SAMPLE.length*10, lines[0].size)
end
# disabled since std streams are not selectable under Java.
def XXXtest_s_select
assert_nil(select(nil, nil, nil, 0))
assert_raise(ArgumentError) { IO.select(nil, nil, nil, -1) }
File.open(@file) do |file|
res = IO.select([file], [$stdout, $stderr], [file,$stdout,$stderr], 1)
assert_equal( 3, res.length )
assert_equal( [file], res[0] )
assert_equal( [$stdout, $stderr], res[1] )
# TODO: not sure how to handle this
=begin
case
when $os == Solaris || $os == MacOS
# select is documented to work this way on Solaris.
# From the select(3C) man page:
#
# File descriptors associated with regular files always
# select true for ready to read, ready to write, and error
# conditions.
#
# MacOS seem to work the same way.
#
assert_equal( [file], res[2] )
when $os ==
# seems to work like this on Windows, but I have not found any
# documentation supporting it.
#
assert_equal( [file, $stdout, $stderr], res[2])
else
# The "normal" case for Linux, FreeBSD, etc.
#
assert_equal( [], res[2] )
end
=end
end
# read, write = *IO.pipe
# read.fcntl(F_SETFL, File::NONBLOCK)
# assert_nil(select([read], nil, [read], .1))
# write.puts "Hello"
# assert_equal([[read],[],[]], select([read], nil, [read], .1))
# read.gets
# assert_nil(select([read], nil, [read], .1))
# write.close
# assert_equal([[read],[],[]], select([read], nil, [read], .1))
# assert_nil(read.gets)
# read.close
end
class Dummy
def to_s
"dummy"
end
end
def test_LSHIFT # '<<'
file = File.open(@file, "w")
io = IO.new(file.fileno, "w")
io << 1 << "\n" << Dummy.new << "\n" << "cat\n"
io.close
assert_raise(Errno::EBADF) { file.close }
expected = [ "1\n", "dummy\n", "cat\n"]
IO.foreach(@file) do |line|
assert_equal(expected.shift, line)
end
assert_equal([], expected)
end
def test_binmode
# TODO: needs impl
end
def test_clone
# check file position shared
file = File.open(@file, "r")
io = []
io[0] = IO.new(file.fileno, "r")
begin
io[1] = io[0].clone
begin
count = 0
io[count & 1].each do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
assert_equal(10, count)
ensure
io[1].close
end
ensure
io[0].close
end
assert_raise(Errno::EBADF) { file.close }
end
def test_close
read, write = *IO.pipe
begin
read.close
assert_raise(IOError) { read.gets }
ensure
begin
read.close
rescue Exception
end
write.close
end
end
# TODO: fails on 1.8.6
=begin
def test_close_read
unless WIN32
pipe = stdin_copy_pipe
begin
pipe.puts "Hello"
assert_equal("Hello\n", pipe.gets)
pipe.close_read
assert_raise(IOError) { pipe.gets }
ensure
pipe.close_write
end
end
end
=end
# TODO: fails on 1.8.6
=begin
def test_close_write
unless WIN32
pipe = stdin_copy_pipe
pipe.puts "Hello"
assert_equal("Hello\n", pipe.gets)
pipe.close_write
assert_raise(IOError) { pipe.puts "Hello" }
pipe.close
end
end
=end
def test_closed?
f = File.open(@file)
assert(!f.closed?)
f.close
assert(f.closed?)
# TODO: stdin_copy_pipe produces a warning on 1.8.6
=begin
unless WIN32
pipe = stdin_copy_pipe
assert(!pipe.closed?)
pipe.close_read
assert(!pipe.closed?)
pipe.close_write
assert(pipe.closed?)
end
=end
end
def test_each
# count = 0
# # File.open(@file) do |file|
# file.each do |line|
# num = line[0..1].to_i
# assert_equal(count, num)
# count += 1
# end
# # assert_equal(10, count)
# end
# count = 0
# File.open(@file) do |file|
# file.each(nil) do |contents|
# contents.split(/\n/).each do |line|
# num = line[0..1].to_i
# assert_equal(count, num)
# count += 1
## end
# end
# end
# assert_equal(10, count)
count = 0
File.open(@file) do |file|
file.each(' ') do |thing|
count += 1
end
end
assert_equal(41, count)
end
def test_each_byte
count = 0
data =
"00: This is a line\n" +
"01: This is a line\n" +
"02: This is a line\n" +
"03: This is a line\n" +
"04: This is a line\n" +
"05: This is a line\n" +
"06: This is a line\n" +
"07: This is a line\n" +
"08: This is a line\n" +
"09: This is a line\n"
File.open(@file) do |file|
file.each_byte do |b|
assert_equal(IS19 ? data.getbyte(count) : data[count], b)
count += 1
end
end
assert_equal(SAMPLE.length*10, count)
end
def test_each_line
count = 0
File.open(@file) do |file|
file.each_line do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
assert_equal(10, count)
end
count = 0
File.open(@file) do |file|
file.each_line(nil) do |contents|
contents.split(/\n/).each do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
end
end
assert_equal(10, count)
count = 0
File.open(@file) do |file|
file.each_line(' ') do |thing|
count += 1
end
end
assert_equal(41, count)
end
def test_eof
File.open(@file) do |file|
10.times do
assert(!file.eof)
assert(!file.eof?)
file.gets
end
assert(file.eof)
assert(file.eof?)
end
end
def test_fcntl
# TODO: needs impl
end
def test_fileno
assert_equal(0, $stdin.fileno)
assert_equal(1, $stdout.fileno)
assert_equal(2, $stderr.fileno)
end
def test_flush
unless WIN32
read, write = IO.pipe
write.sync = false
write.print "hello"
assert_nil(select([read], nil, [read], 0.1))
write.flush
assert_equal([[read],[],[]], select([read], nil, [read], 0.1))
read.close
write.close
end
end
def test_getc
count = 0
data =
"00: This is a line\n" +
"01: This is a line\n" +
"02: This is a line\n" +
"03: This is a line\n" +
"04: This is a line\n" +
"05: This is a line\n" +
"06: This is a line\n" +
"07: This is a line\n" +
"08: This is a line\n" +
"09: This is a line\n"
File.open(@file) do |file|
while (ch = file.getc)
assert_equal(data[count], ch)
count += 1
end
assert_equal(nil, file.getc)
end
assert_equal(SAMPLE.length*10, count)
end
def test_gets
count = 0
File.open(@file) do |file|
while (line = file.gets)
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
assert_equal(nil, file.gets)
assert_equal(10, count)
end
count = 0
File.open(@file) do |file|
while (contents = file.gets(nil))
contents.split(/\n/).each do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
end
end
assert_equal(10, count)
count = 0
File.open(@file) do |file|
while (thing = file.gets(' '))
count += 1
end
end
assert_equal(41, count)
end
def test_gets_para
File.open(@file, "w") do |file|
file.print "foo\n"*4096, "\n"*4096, "bar"*4096, "\n"*4096, "zot\n"*1024
end
File.open(@file) do |file|
assert_equal("foo\n"*4096+"\n", file.gets(""))
assert_equal("bar"*4096+"\n\n", file.gets(""))
assert_equal("zot\n"*1024, file.gets(""))
end
end
def test_ioctl
# TODO: needs impl
end
# see tty?
def test_isatty
File.open(@file) { |f| assert(!f.isatty) }
if WIN32
# FIXME: JRUBY-4186
return
File.open("con") { |f| assert(f.isatty) }
end
unless WIN32
begin
File.open("/dev/tty") { |f| assert(f.isatty) }
rescue
# in run from (say) cron, /dev/tty can't be opened
end
end
end
def test_lineno
count = 1
File.open(@file) do |file|
while (line = file.gets)
assert_equal(count, file.lineno)
count += 1
end
assert_equal(11, count)
file.rewind
assert_equal(0, file.lineno)
end
count = 1
File.open(@file) do |file|
while (line = file.gets('i'))
assert_equal(count, file.lineno)
count += 1
end
assert_equal(32, count)
end
end
def test_lineno=
File.open(@file) do |f|
assert_equal(0, f.lineno)
assert_equal(123, f.lineno = 123)
assert_equal(123, f.lineno)
f.gets
assert_equal(124, f.lineno)
f.lineno = 0
f.gets
assert_equal(1, f.lineno)
f.gets
assert_equal(2, f.lineno)
end
end
# TODO: fails on 1.8.6
=begin
def test_pid
assert_nil($stdin.pid)
pipe = nil
if WIN32
pipe = IO.popen("#$interpreter -e 'p $$'", "r")
else
pipe = IO.popen("exec #$interpreter -e 'p $$'", "r")
end
pid = pipe.gets
assert_equal(pid.to_i, pipe.pid)
pipe.close
end
=end
def test_pos
pos = 0
File.open(@file, "rb") do |file|
assert_equal(0, file.pos)
while (line = file.gets)
pos += line.length
assert_equal(pos, file.pos)
end
end
end
def test_pos=
# FIXME: JRUBY-61
return if WIN32
nums = [ 5, 8, 0, 1, 0 ]
File.open(@file) do |file|
file.pos = 999
assert_nil(file.gets)
#
# Errno::EFBIG added below for FreeBSD, because of
# fseeko(3) behaviour there.
#
assert_raise(Errno::EFBIG, Errno::EINVAL, SystemCallError) {
file.pos = -1
}
for pos in nums
assert_equal(LINE_LENGTH*pos, file.pos = LINE_LENGTH*pos)
line = file.gets
assert_equal(pos, line[0..1].to_i)
end
end
end
def test_print
File.open(@file, "w") do |file|
file.print "hello"
file.print 1,2
$_ = "wombat\n"
file.print
$\ = ":"
$, = ","
file.print 3, 4
file.print 5, 6
$\ = nil
file.print "\n"
$, = nil
end
File.open(@file) do |file|
content = file.gets(nil)
assert_equal("hello12wombat\n3,4:5,6:\n", content)
end
end
def test_printf
# tested under Kernel.sprintf
end
def test_putc
File.open(@file, "wb") do |file|
file.putc "A"
0.upto(255) { |ch| file.putc ch }
end
File.open(@file, "rb") do |file|
assert_equal(?A, file.getc)
0.upto(255) { |ch| assert_equal(IS19 ? ch.chr : ch, file.getc) }
end
end
def test_puts
File.open(@file, "w") do |file|
file.puts "line 1", "line 2"
file.puts [ Dummy.new, 4 ]
end
File.open(@file) do |file|
assert_equal("line 1\n", file.gets)
assert_equal("line 2\n", file.gets)
assert_equal("dummy\n", file.gets)
assert_equal("4\n", file.gets)
end
end
def test_read
File.open(@file) do |file|
content = file.read
assert_equal(SAMPLE.length*10, content.length)
count = 0
content.split(/\n/).each do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
end
File.open(@file) do |file|
assert_equal("00: This is ", file.read(12))
assert_equal("a line\n01: T", file.read(12))
end
end
def test_readchar
count = 0
data =
"00: This is a line\n" +
"01: This is a line\n" +
"02: This is a line\n" +
"03: This is a line\n" +
"04: This is a line\n" +
"05: This is a line\n" +
"06: This is a line\n" +
"07: This is a line\n" +
"08: This is a line\n" +
"09: This is a line\n"
File.open(@file) do |file|
190.times do |count|
ch = file.readchar
assert_equal(data[count], ch)
count += 1
end
assert_raise(EOFError) { file.readchar }
end
end
def test_readline
count = 0
File.open(@file) do |file|
10.times do |count|
line = file.readline
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
assert_raise(EOFError) { file.readline }
end
count = 0
File.open(@file) do |file|
contents = file.readline(nil)
contents.split(/\n/).each do |line|
num = line[0..1].to_i
assert_equal(count, num)
count += 1
end
assert_raise(EOFError) { file.readline }
end
assert_equal(10, count)
count = 0
File.open(@file) do |file|
41.times do |count|
thing = file.readline(' ')
count += 1
end
assert_raise(EOFError) { file.readline }
end
end
def test_readlines
File.open(@file) do |file|
lines = file.readlines
assert_equal(10, lines.size)
end
File.open(@file) do |file|
lines = file.readlines(nil)
assert_equal(1, lines.size)
assert_equal(SAMPLE.length*10, lines[0].size)
end
end
def test_reopen1
f1 = File.new(@file)
assert_equal("00: This is a line\n", f1.gets)
assert_equal("01: This is a line\n", f1.gets)
f2 = File.new(@file1)
assert_equal("Line 00\n", f2.gets)
assert_equal("Line 01\n", f2.gets)
f2.reopen(@file)
assert_equal("00: This is a line\n", f2.gets)
assert_equal("01: This is a line\n", f2.gets)
ensure
f1.close if f1
f2.close if f2
end
def test_reopen2
f1 = File.new(@file)
assert_equal("00: This is a line\n", f1.read(SAMPLE.length))
assert_equal("01: This is a line\n", f1.read(SAMPLE.length))
f2 = File.new(@file1)
assert_equal("Line 00\n", f2.read(8))
assert_equal("Line 01\n", f2.read(8))
f2.reopen(f1)
assert_equal("02: This is a line\n", f2.read(SAMPLE.length))
assert_equal("03: This is a line\n", f2.read(SAMPLE.length))
ensure
f1.close if f1
f2.close if f2
end
def test_rewind
f1 = File.new(@file)
assert_equal("00: This is a line\n", f1.gets)
assert_equal("01: This is a line\n", f1.gets)
f1.rewind
assert_equal("00: This is a line\n", f1.gets)
f1.readlines
assert_nil(f1.gets)
f1.rewind
assert_equal("00: This is a line\n", f1.gets)
f1.close
end
def test_seek
# FIXME: JRUBY-61
return if WIN32
nums = [ 5, 8, 0, 1, 0 ]
File.open(@file, "rb") do |file|
file.seek(999, IO::SEEK_SET)
assert_nil(file.gets)
#
# Errno::EFBIG added below for FreeBSD, because of
# fseeko(3) behaviour there.
#
assert_raise(Errno::EFBIG, Errno::EINVAL, SystemCallError) {
file.seek(-1)
}
for pos in nums
assert_equal(0, file.seek(LINE_LENGTH*pos))
line = file.gets
assert_equal(pos, line[0..1].to_i)
end
end
nums = [5, -2, 4, -7, 0 ]
File.open(@file) do |file|
count = -1
file.seek(0)
for pos in nums
assert_equal(0, file.seek(LINE_LENGTH*pos, IO::SEEK_CUR))
line = file.gets
count = count + pos + 1
assert_equal(count, line[0..1].to_i)
end
end
nums = [ 5, 8, 1, 10, 1 ]
File.open(@file) do |file|
file.seek(0)
for pos in nums
assert_equal(0, file.seek(-LINE_LENGTH*pos, IO::SEEK_END))
line = file.gets
assert_equal(10-pos, line[0..1].to_i)
end
end
end
# Stat is pretty much tested elsewhere, so we're minimal here
# Excluded due to JRUBY-2665
def XXXtest_stat
io = IO.new($stdin.fileno)
assert_instance_of(File::Stat, io.stat)
io.close
end
def test_sync
$stderr.sync = false
assert(!$stderr.sync)
$stderr.sync = true
assert($stderr.sync)
end
def test_sync=()
unless WIN32
read, write = IO.pipe
write.sync = false
write.print "hello"
assert_nil(select([read], nil, [read], 0.1))
write.sync = true
write.print "there"
assert_equal([[read],[],[]], select([read], nil, [read], 0.1))
read.close
write.close
end
end
def test_sysread
File.open(@file, 'rb') do |file|
assert_equal("", file.sysread(0))
assert_equal("0", file.sysread(1))
assert_equal("0:", file.sysread(2))
assert_equal(" Thi", file.sysread(4))
rest = file.sysread(100000)
assert_equal(LINE_LENGTH*10 - (1+2+4), rest.length)
assert_raise(EOFError) { file.sysread(1) }
end
end
def test_syswrite
File.open(@file, "w") do |file|
file.syswrite ""
file.syswrite "hello"
file.syswrite 1
file.syswrite "\n"
end
File.open(@file) do |file|
assert_equal("hello1\n", file.gets)
end
end
# see also pos
def test_tell
pos = 0
File.open(@file, "rb") do |file|
assert_equal(0, file.tell)
while (line = file.gets)
pos += line.length
assert_equal(pos, file.tell)
end
end
end
def test_to_i
assert_equal(0, $stdin.to_i)
assert_equal(1, $stdout.to_i)
assert_equal(2, $stderr.to_i)
end
# see isatty
def test_tty?
File.open(@file) { |f| assert(!f.tty?) }
if WIN32
# FIXME: JRUBY-4186
return
File.open("con") { |f| assert(f.tty?) }
end
unless WIN32
begin
File.open("/dev/tty") { |f| assert(f.isatty) }
rescue
# Can't open from crontab jobs
end
end
end
def test_ungetc
File.open(@file) do |file|
assert_equal(?0, file.getc)
assert_equal(?0, file.getc)
assert_equal(?:, file.getc)
assert_equal(?\s, file.getc)
assert_nil(file.ungetc(?:))
assert_equal(?:, file.getc)
1 while file.getc
assert_nil(file.ungetc(?A))
assert_equal(?A, file.getc)
end
end
def test_write
File.open(@file, "wb") do |file|
assert_equal(10, file.write('*' * 10))
assert_equal(5, file.write('!' * 5))
assert_equal(0, file.write(''))
assert_equal(1, file.write(1))
assert_equal(3, file.write(2.30000))
assert_equal(1, file.write("\n"))
end
File.open(@file) do |file|
assert_equal("**********!!!!!12.3\n", file.gets)
end
end
end
|