PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/rubicon/test_io.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1087 lines | 751 code | 117 blank | 219 comment | 25 complexity | 2b600bb77bb5781aaeb6dc743be38c1f MD5 | raw file
  1. require 'test/unit'
  2. require 'fileutils'
  3. require 'rbconfig'
  4. # FIXME: This needs platform-specific stuff changed
  5. class TestIO < Test::Unit::TestCase
  6. IS19 = RUBY_VERSION =~ /1\.9/
  7. WIN32 = RbConfig::CONFIG['host_os'] =~ /mswin/
  8. SAMPLE = "08: This is a line\n"
  9. LINE_LENGTH = WIN32 ? SAMPLE.length + 1 : SAMPLE.length
  10. def setup_test_dir
  11. Dir.mkdir "_test"
  12. end
  13. def teardown_test_dir
  14. FileUtils.rm_rf "_test"
  15. end
  16. def setup
  17. setup_test_dir
  18. @file = "_test/_10lines"
  19. @file1 = "_test/_99lines"
  20. File.open(@file, "w") do |f|
  21. 10.times { |i| f.printf "%02d: This is a line\n", i }
  22. end
  23. File.open(@file1, "w") do |f|
  24. 99.times { |i| f.printf "Line %02d\n", i }
  25. end
  26. end
  27. def teardown
  28. teardown_test_dir
  29. end
  30. unless WIN32
  31. def stdin_copy_pipe
  32. IO.popen("#$interpreter -e '$stdout.sync=true;while gets;puts $_;end'", "r+")
  33. end
  34. end
  35. # ---------------------------------------------------------------
  36. def test_s_foreach
  37. assert_raise(Errno::ENOENT) { File.foreach("gumby") {} }
  38. # No longer a valid test in 1.8.7
  39. =begin
  40. assert_raise(LocalJumpError) { File.foreach(@file) }
  41. =end
  42. count = 0
  43. IO.foreach(@file) do |line|
  44. num = line[0..1].to_i
  45. assert_equal(count, num)
  46. count += 1
  47. end
  48. assert_equal(10, count)
  49. count = 0
  50. IO.foreach(@file, nil) do |file|
  51. file.split(/\n/).each do |line|
  52. num = line[0..1].to_i
  53. assert_equal(count, num)
  54. count += 1
  55. end
  56. end
  57. assert_equal(10, count)
  58. count = 0
  59. IO.foreach(@file, ' ') do |thing|
  60. count += 1
  61. end
  62. assert_equal(41, count)
  63. end
  64. def test_s_new
  65. f = File.open(@file)
  66. io = IO.new(f.fileno, "r")
  67. begin
  68. count = 0
  69. io.each { count += 1 }
  70. assert_equal(10, count)
  71. ensure
  72. io.close
  73. begin
  74. f.close
  75. rescue Exception
  76. end
  77. end
  78. f = File.open(@file)
  79. io = IO.new(f.fileno, "r")
  80. begin
  81. f.close
  82. assert_raise(Errno::EBADF) { io.gets }
  83. ensure
  84. assert_raise(Errno::EBADF) { io.close }
  85. begin
  86. f.close
  87. rescue Exception
  88. end
  89. end
  90. f = File.open(@file, "r")
  91. f.sysread(3*LINE_LENGTH)
  92. io = IO.new(f.fileno, "r")
  93. begin
  94. assert_equal(3*LINE_LENGTH, io.tell)
  95. count = 0
  96. io.each { count += 1 }
  97. assert_equal(7, count)
  98. ensure
  99. io.close
  100. begin
  101. f.close
  102. rescue Exception
  103. end
  104. end
  105. end
  106. def test_s_pipe
  107. p = IO.pipe
  108. begin
  109. assert_equal(2, p.size)
  110. r, w = *p
  111. assert_instance_of(IO, r)
  112. assert_instance_of(IO, w)
  113. w.puts "Hello World"
  114. assert_equal("Hello World\n", r.gets)
  115. ensure
  116. r.close
  117. w.close
  118. end
  119. end
  120. # TODO: fails on 1.8.6
  121. =begin
  122. def test_s_popen
  123. if WIN32
  124. cmd = "type"
  125. fname = @file.tr '/', '\\'
  126. else
  127. cmd = "cat"
  128. fname = @file
  129. end
  130. # READ
  131. IO.popen("#{cmd} #{fname}") do |p|
  132. count = 0
  133. p.each do |line|
  134. num = line[0..1].to_i
  135. assert_equal(count, num)
  136. count += 1
  137. end
  138. assert_equal(10, count)
  139. end
  140. # READ with block
  141. res = IO.popen("#{cmd} #{fname}") do |p|
  142. count = 0
  143. p.each do |line|
  144. num = line[0..1].to_i
  145. assert_equal(count, num)
  146. count += 1
  147. end
  148. assert_equal(10, count)
  149. end
  150. assert_nil(res)
  151. # WRITE
  152. IO.popen("#$interpreter -e 'puts readlines' >#{fname}", "w") do |p|
  153. 5.times { |i| p.printf "Line %d\n", i }
  154. end
  155. count = 0
  156. IO.foreach(@file) do |line|
  157. num = line.chomp[-1,1].to_i
  158. assert_equal(count, num)
  159. count += 1
  160. end
  161. assert_equal(5, count)
  162. unless WIN32
  163. # Spawn an interpreter
  164. parent = $$
  165. p = IO.popen("-")
  166. if p
  167. begin
  168. assert_equal(parent, $$)
  169. assert_equal("Hello\n", p.gets)
  170. ensure
  171. p.close
  172. end
  173. else
  174. assert_equal(parent, Process.ppid)
  175. puts "Hello"
  176. exit
  177. end
  178. end
  179. end
  180. =end
  181. # disabled due to JRUBY-1895
  182. def XXXtest_s_popen_spawn
  183. unless WIN32
  184. # Spawn an interpreter - WRITE
  185. parent = $$
  186. pipe = IO.popen("-", "w")
  187. if pipe
  188. begin
  189. assert_equal(parent, $$)
  190. pipe.puts "12"
  191. Process.wait pipe.pid
  192. assert_equal(12, $?>>8)
  193. ensure
  194. pipe.close
  195. end
  196. else
  197. buff = $stdin.gets
  198. exit buff.to_i
  199. end
  200. # Spawn an interpreter - READWRITE
  201. parent = $$
  202. p = IO.popen("-", "w+")
  203. if p
  204. begin
  205. assert_equal(parent, $$)
  206. p.puts "Hello\n"
  207. assert_equal("Goodbye\n", p.gets)
  208. Process.wait
  209. ensure
  210. p.close
  211. end
  212. else
  213. puts "Goodbye" if $stdin.gets == "Hello\n"
  214. exit
  215. end
  216. end
  217. end
  218. def test_s_readlines
  219. assert_raise(Errno::ENOENT) { IO.readlines('gumby') }
  220. lines = IO.readlines(@file)
  221. assert_equal(10, lines.size)
  222. lines = IO.readlines(@file, nil)
  223. assert_equal(1, lines.size)
  224. assert_equal(SAMPLE.length*10, lines[0].size)
  225. end
  226. # disabled since std streams are not selectable under Java.
  227. def XXXtest_s_select
  228. assert_nil(select(nil, nil, nil, 0))
  229. assert_raise(ArgumentError) { IO.select(nil, nil, nil, -1) }
  230. File.open(@file) do |file|
  231. res = IO.select([file], [$stdout, $stderr], [file,$stdout,$stderr], 1)
  232. assert_equal( 3, res.length )
  233. assert_equal( [file], res[0] )
  234. assert_equal( [$stdout, $stderr], res[1] )
  235. # TODO: not sure how to handle this
  236. =begin
  237. case
  238. when $os == Solaris || $os == MacOS
  239. # select is documented to work this way on Solaris.
  240. # From the select(3C) man page:
  241. #
  242. # File descriptors associated with regular files always
  243. # select true for ready to read, ready to write, and error
  244. # conditions.
  245. #
  246. # MacOS seem to work the same way.
  247. #
  248. assert_equal( [file], res[2] )
  249. when $os ==
  250. # seems to work like this on Windows, but I have not found any
  251. # documentation supporting it.
  252. #
  253. assert_equal( [file, $stdout, $stderr], res[2])
  254. else
  255. # The "normal" case for Linux, FreeBSD, etc.
  256. #
  257. assert_equal( [], res[2] )
  258. end
  259. =end
  260. end
  261. # read, write = *IO.pipe
  262. # read.fcntl(F_SETFL, File::NONBLOCK)
  263. # assert_nil(select([read], nil, [read], .1))
  264. # write.puts "Hello"
  265. # assert_equal([[read],[],[]], select([read], nil, [read], .1))
  266. # read.gets
  267. # assert_nil(select([read], nil, [read], .1))
  268. # write.close
  269. # assert_equal([[read],[],[]], select([read], nil, [read], .1))
  270. # assert_nil(read.gets)
  271. # read.close
  272. end
  273. class Dummy
  274. def to_s
  275. "dummy"
  276. end
  277. end
  278. def test_LSHIFT # '<<'
  279. file = File.open(@file, "w")
  280. io = IO.new(file.fileno, "w")
  281. io << 1 << "\n" << Dummy.new << "\n" << "cat\n"
  282. io.close
  283. assert_raise(Errno::EBADF) { file.close }
  284. expected = [ "1\n", "dummy\n", "cat\n"]
  285. IO.foreach(@file) do |line|
  286. assert_equal(expected.shift, line)
  287. end
  288. assert_equal([], expected)
  289. end
  290. def test_binmode
  291. # TODO: needs impl
  292. end
  293. def test_clone
  294. # check file position shared
  295. file = File.open(@file, "r")
  296. io = []
  297. io[0] = IO.new(file.fileno, "r")
  298. begin
  299. io[1] = io[0].clone
  300. begin
  301. count = 0
  302. io[count & 1].each do |line|
  303. num = line[0..1].to_i
  304. assert_equal(count, num)
  305. count += 1
  306. end
  307. assert_equal(10, count)
  308. ensure
  309. io[1].close
  310. end
  311. ensure
  312. io[0].close
  313. end
  314. assert_raise(Errno::EBADF) { file.close }
  315. end
  316. def test_close
  317. read, write = *IO.pipe
  318. begin
  319. read.close
  320. assert_raise(IOError) { read.gets }
  321. ensure
  322. begin
  323. read.close
  324. rescue Exception
  325. end
  326. write.close
  327. end
  328. end
  329. # TODO: fails on 1.8.6
  330. =begin
  331. def test_close_read
  332. unless WIN32
  333. pipe = stdin_copy_pipe
  334. begin
  335. pipe.puts "Hello"
  336. assert_equal("Hello\n", pipe.gets)
  337. pipe.close_read
  338. assert_raise(IOError) { pipe.gets }
  339. ensure
  340. pipe.close_write
  341. end
  342. end
  343. end
  344. =end
  345. # TODO: fails on 1.8.6
  346. =begin
  347. def test_close_write
  348. unless WIN32
  349. pipe = stdin_copy_pipe
  350. pipe.puts "Hello"
  351. assert_equal("Hello\n", pipe.gets)
  352. pipe.close_write
  353. assert_raise(IOError) { pipe.puts "Hello" }
  354. pipe.close
  355. end
  356. end
  357. =end
  358. def test_closed?
  359. f = File.open(@file)
  360. assert(!f.closed?)
  361. f.close
  362. assert(f.closed?)
  363. # TODO: stdin_copy_pipe produces a warning on 1.8.6
  364. =begin
  365. unless WIN32
  366. pipe = stdin_copy_pipe
  367. assert(!pipe.closed?)
  368. pipe.close_read
  369. assert(!pipe.closed?)
  370. pipe.close_write
  371. assert(pipe.closed?)
  372. end
  373. =end
  374. end
  375. def test_each
  376. # count = 0
  377. # # File.open(@file) do |file|
  378. # file.each do |line|
  379. # num = line[0..1].to_i
  380. # assert_equal(count, num)
  381. # count += 1
  382. # end
  383. # # assert_equal(10, count)
  384. # end
  385. # count = 0
  386. # File.open(@file) do |file|
  387. # file.each(nil) do |contents|
  388. # contents.split(/\n/).each do |line|
  389. # num = line[0..1].to_i
  390. # assert_equal(count, num)
  391. # count += 1
  392. ## end
  393. # end
  394. # end
  395. # assert_equal(10, count)
  396. count = 0
  397. File.open(@file) do |file|
  398. file.each(' ') do |thing|
  399. count += 1
  400. end
  401. end
  402. assert_equal(41, count)
  403. end
  404. def test_each_byte
  405. count = 0
  406. data =
  407. "00: This is a line\n" +
  408. "01: This is a line\n" +
  409. "02: This is a line\n" +
  410. "03: This is a line\n" +
  411. "04: This is a line\n" +
  412. "05: This is a line\n" +
  413. "06: This is a line\n" +
  414. "07: This is a line\n" +
  415. "08: This is a line\n" +
  416. "09: This is a line\n"
  417. File.open(@file) do |file|
  418. file.each_byte do |b|
  419. assert_equal(IS19 ? data.getbyte(count) : data[count], b)
  420. count += 1
  421. end
  422. end
  423. assert_equal(SAMPLE.length*10, count)
  424. end
  425. def test_each_line
  426. count = 0
  427. File.open(@file) do |file|
  428. file.each_line do |line|
  429. num = line[0..1].to_i
  430. assert_equal(count, num)
  431. count += 1
  432. end
  433. assert_equal(10, count)
  434. end
  435. count = 0
  436. File.open(@file) do |file|
  437. file.each_line(nil) do |contents|
  438. contents.split(/\n/).each do |line|
  439. num = line[0..1].to_i
  440. assert_equal(count, num)
  441. count += 1
  442. end
  443. end
  444. end
  445. assert_equal(10, count)
  446. count = 0
  447. File.open(@file) do |file|
  448. file.each_line(' ') do |thing|
  449. count += 1
  450. end
  451. end
  452. assert_equal(41, count)
  453. end
  454. def test_eof
  455. File.open(@file) do |file|
  456. 10.times do
  457. assert(!file.eof)
  458. assert(!file.eof?)
  459. file.gets
  460. end
  461. assert(file.eof)
  462. assert(file.eof?)
  463. end
  464. end
  465. def test_fcntl
  466. # TODO: needs impl
  467. end
  468. def test_fileno
  469. assert_equal(0, $stdin.fileno)
  470. assert_equal(1, $stdout.fileno)
  471. assert_equal(2, $stderr.fileno)
  472. end
  473. def test_flush
  474. unless WIN32
  475. read, write = IO.pipe
  476. write.sync = false
  477. write.print "hello"
  478. assert_nil(select([read], nil, [read], 0.1))
  479. write.flush
  480. assert_equal([[read],[],[]], select([read], nil, [read], 0.1))
  481. read.close
  482. write.close
  483. end
  484. end
  485. def test_getc
  486. count = 0
  487. data =
  488. "00: This is a line\n" +
  489. "01: This is a line\n" +
  490. "02: This is a line\n" +
  491. "03: This is a line\n" +
  492. "04: This is a line\n" +
  493. "05: This is a line\n" +
  494. "06: This is a line\n" +
  495. "07: This is a line\n" +
  496. "08: This is a line\n" +
  497. "09: This is a line\n"
  498. File.open(@file) do |file|
  499. while (ch = file.getc)
  500. assert_equal(data[count], ch)
  501. count += 1
  502. end
  503. assert_equal(nil, file.getc)
  504. end
  505. assert_equal(SAMPLE.length*10, count)
  506. end
  507. def test_gets
  508. count = 0
  509. File.open(@file) do |file|
  510. while (line = file.gets)
  511. num = line[0..1].to_i
  512. assert_equal(count, num)
  513. count += 1
  514. end
  515. assert_equal(nil, file.gets)
  516. assert_equal(10, count)
  517. end
  518. count = 0
  519. File.open(@file) do |file|
  520. while (contents = file.gets(nil))
  521. contents.split(/\n/).each do |line|
  522. num = line[0..1].to_i
  523. assert_equal(count, num)
  524. count += 1
  525. end
  526. end
  527. end
  528. assert_equal(10, count)
  529. count = 0
  530. File.open(@file) do |file|
  531. while (thing = file.gets(' '))
  532. count += 1
  533. end
  534. end
  535. assert_equal(41, count)
  536. end
  537. def test_gets_para
  538. File.open(@file, "w") do |file|
  539. file.print "foo\n"*4096, "\n"*4096, "bar"*4096, "\n"*4096, "zot\n"*1024
  540. end
  541. File.open(@file) do |file|
  542. assert_equal("foo\n"*4096+"\n", file.gets(""))
  543. assert_equal("bar"*4096+"\n\n", file.gets(""))
  544. assert_equal("zot\n"*1024, file.gets(""))
  545. end
  546. end
  547. def test_ioctl
  548. # TODO: needs impl
  549. end
  550. # see tty?
  551. def test_isatty
  552. File.open(@file) { |f| assert(!f.isatty) }
  553. if WIN32
  554. # FIXME: JRUBY-4186
  555. return
  556. File.open("con") { |f| assert(f.isatty) }
  557. end
  558. unless WIN32
  559. begin
  560. File.open("/dev/tty") { |f| assert(f.isatty) }
  561. rescue
  562. # in run from (say) cron, /dev/tty can't be opened
  563. end
  564. end
  565. end
  566. def test_lineno
  567. count = 1
  568. File.open(@file) do |file|
  569. while (line = file.gets)
  570. assert_equal(count, file.lineno)
  571. count += 1
  572. end
  573. assert_equal(11, count)
  574. file.rewind
  575. assert_equal(0, file.lineno)
  576. end
  577. count = 1
  578. File.open(@file) do |file|
  579. while (line = file.gets('i'))
  580. assert_equal(count, file.lineno)
  581. count += 1
  582. end
  583. assert_equal(32, count)
  584. end
  585. end
  586. def test_lineno=
  587. File.open(@file) do |f|
  588. assert_equal(0, f.lineno)
  589. assert_equal(123, f.lineno = 123)
  590. assert_equal(123, f.lineno)
  591. f.gets
  592. assert_equal(124, f.lineno)
  593. f.lineno = 0
  594. f.gets
  595. assert_equal(1, f.lineno)
  596. f.gets
  597. assert_equal(2, f.lineno)
  598. end
  599. end
  600. # TODO: fails on 1.8.6
  601. =begin
  602. def test_pid
  603. assert_nil($stdin.pid)
  604. pipe = nil
  605. if WIN32
  606. pipe = IO.popen("#$interpreter -e 'p $$'", "r")
  607. else
  608. pipe = IO.popen("exec #$interpreter -e 'p $$'", "r")
  609. end
  610. pid = pipe.gets
  611. assert_equal(pid.to_i, pipe.pid)
  612. pipe.close
  613. end
  614. =end
  615. def test_pos
  616. pos = 0
  617. File.open(@file, "rb") do |file|
  618. assert_equal(0, file.pos)
  619. while (line = file.gets)
  620. pos += line.length
  621. assert_equal(pos, file.pos)
  622. end
  623. end
  624. end
  625. def test_pos=
  626. # FIXME: JRUBY-61
  627. return if WIN32
  628. nums = [ 5, 8, 0, 1, 0 ]
  629. File.open(@file) do |file|
  630. file.pos = 999
  631. assert_nil(file.gets)
  632. #
  633. # Errno::EFBIG added below for FreeBSD, because of
  634. # fseeko(3) behaviour there.
  635. #
  636. assert_raise(Errno::EFBIG, Errno::EINVAL, SystemCallError) {
  637. file.pos = -1
  638. }
  639. for pos in nums
  640. assert_equal(LINE_LENGTH*pos, file.pos = LINE_LENGTH*pos)
  641. line = file.gets
  642. assert_equal(pos, line[0..1].to_i)
  643. end
  644. end
  645. end
  646. def test_print
  647. File.open(@file, "w") do |file|
  648. file.print "hello"
  649. file.print 1,2
  650. $_ = "wombat\n"
  651. file.print
  652. $\ = ":"
  653. $, = ","
  654. file.print 3, 4
  655. file.print 5, 6
  656. $\ = nil
  657. file.print "\n"
  658. $, = nil
  659. end
  660. File.open(@file) do |file|
  661. content = file.gets(nil)
  662. assert_equal("hello12wombat\n3,4:5,6:\n", content)
  663. end
  664. end
  665. def test_printf
  666. # tested under Kernel.sprintf
  667. end
  668. def test_putc
  669. File.open(@file, "wb") do |file|
  670. file.putc "A"
  671. 0.upto(255) { |ch| file.putc ch }
  672. end
  673. File.open(@file, "rb") do |file|
  674. assert_equal(?A, file.getc)
  675. 0.upto(255) { |ch| assert_equal(IS19 ? ch.chr : ch, file.getc) }
  676. end
  677. end
  678. def test_puts
  679. File.open(@file, "w") do |file|
  680. file.puts "line 1", "line 2"
  681. file.puts [ Dummy.new, 4 ]
  682. end
  683. File.open(@file) do |file|
  684. assert_equal("line 1\n", file.gets)
  685. assert_equal("line 2\n", file.gets)
  686. assert_equal("dummy\n", file.gets)
  687. assert_equal("4\n", file.gets)
  688. end
  689. end
  690. def test_read
  691. File.open(@file) do |file|
  692. content = file.read
  693. assert_equal(SAMPLE.length*10, content.length)
  694. count = 0
  695. content.split(/\n/).each do |line|
  696. num = line[0..1].to_i
  697. assert_equal(count, num)
  698. count += 1
  699. end
  700. end
  701. File.open(@file) do |file|
  702. assert_equal("00: This is ", file.read(12))
  703. assert_equal("a line\n01: T", file.read(12))
  704. end
  705. end
  706. def test_readchar
  707. count = 0
  708. data =
  709. "00: This is a line\n" +
  710. "01: This is a line\n" +
  711. "02: This is a line\n" +
  712. "03: This is a line\n" +
  713. "04: This is a line\n" +
  714. "05: This is a line\n" +
  715. "06: This is a line\n" +
  716. "07: This is a line\n" +
  717. "08: This is a line\n" +
  718. "09: This is a line\n"
  719. File.open(@file) do |file|
  720. 190.times do |count|
  721. ch = file.readchar
  722. assert_equal(data[count], ch)
  723. count += 1
  724. end
  725. assert_raise(EOFError) { file.readchar }
  726. end
  727. end
  728. def test_readline
  729. count = 0
  730. File.open(@file) do |file|
  731. 10.times do |count|
  732. line = file.readline
  733. num = line[0..1].to_i
  734. assert_equal(count, num)
  735. count += 1
  736. end
  737. assert_raise(EOFError) { file.readline }
  738. end
  739. count = 0
  740. File.open(@file) do |file|
  741. contents = file.readline(nil)
  742. contents.split(/\n/).each do |line|
  743. num = line[0..1].to_i
  744. assert_equal(count, num)
  745. count += 1
  746. end
  747. assert_raise(EOFError) { file.readline }
  748. end
  749. assert_equal(10, count)
  750. count = 0
  751. File.open(@file) do |file|
  752. 41.times do |count|
  753. thing = file.readline(' ')
  754. count += 1
  755. end
  756. assert_raise(EOFError) { file.readline }
  757. end
  758. end
  759. def test_readlines
  760. File.open(@file) do |file|
  761. lines = file.readlines
  762. assert_equal(10, lines.size)
  763. end
  764. File.open(@file) do |file|
  765. lines = file.readlines(nil)
  766. assert_equal(1, lines.size)
  767. assert_equal(SAMPLE.length*10, lines[0].size)
  768. end
  769. end
  770. def test_reopen1
  771. f1 = File.new(@file)
  772. assert_equal("00: This is a line\n", f1.gets)
  773. assert_equal("01: This is a line\n", f1.gets)
  774. f2 = File.new(@file1)
  775. assert_equal("Line 00\n", f2.gets)
  776. assert_equal("Line 01\n", f2.gets)
  777. f2.reopen(@file)
  778. assert_equal("00: This is a line\n", f2.gets)
  779. assert_equal("01: This is a line\n", f2.gets)
  780. ensure
  781. f1.close if f1
  782. f2.close if f2
  783. end
  784. def test_reopen2
  785. f1 = File.new(@file)
  786. assert_equal("00: This is a line\n", f1.read(SAMPLE.length))
  787. assert_equal("01: This is a line\n", f1.read(SAMPLE.length))
  788. f2 = File.new(@file1)
  789. assert_equal("Line 00\n", f2.read(8))
  790. assert_equal("Line 01\n", f2.read(8))
  791. f2.reopen(f1)
  792. assert_equal("02: This is a line\n", f2.read(SAMPLE.length))
  793. assert_equal("03: This is a line\n", f2.read(SAMPLE.length))
  794. ensure
  795. f1.close if f1
  796. f2.close if f2
  797. end
  798. def test_rewind
  799. f1 = File.new(@file)
  800. assert_equal("00: This is a line\n", f1.gets)
  801. assert_equal("01: This is a line\n", f1.gets)
  802. f1.rewind
  803. assert_equal("00: This is a line\n", f1.gets)
  804. f1.readlines
  805. assert_nil(f1.gets)
  806. f1.rewind
  807. assert_equal("00: This is a line\n", f1.gets)
  808. f1.close
  809. end
  810. def test_seek
  811. # FIXME: JRUBY-61
  812. return if WIN32
  813. nums = [ 5, 8, 0, 1, 0 ]
  814. File.open(@file, "rb") do |file|
  815. file.seek(999, IO::SEEK_SET)
  816. assert_nil(file.gets)
  817. #
  818. # Errno::EFBIG added below for FreeBSD, because of
  819. # fseeko(3) behaviour there.
  820. #
  821. assert_raise(Errno::EFBIG, Errno::EINVAL, SystemCallError) {
  822. file.seek(-1)
  823. }
  824. for pos in nums
  825. assert_equal(0, file.seek(LINE_LENGTH*pos))
  826. line = file.gets
  827. assert_equal(pos, line[0..1].to_i)
  828. end
  829. end
  830. nums = [5, -2, 4, -7, 0 ]
  831. File.open(@file) do |file|
  832. count = -1
  833. file.seek(0)
  834. for pos in nums
  835. assert_equal(0, file.seek(LINE_LENGTH*pos, IO::SEEK_CUR))
  836. line = file.gets
  837. count = count + pos + 1
  838. assert_equal(count, line[0..1].to_i)
  839. end
  840. end
  841. nums = [ 5, 8, 1, 10, 1 ]
  842. File.open(@file) do |file|
  843. file.seek(0)
  844. for pos in nums
  845. assert_equal(0, file.seek(-LINE_LENGTH*pos, IO::SEEK_END))
  846. line = file.gets
  847. assert_equal(10-pos, line[0..1].to_i)
  848. end
  849. end
  850. end
  851. # Stat is pretty much tested elsewhere, so we're minimal here
  852. # Excluded due to JRUBY-2665
  853. def XXXtest_stat
  854. io = IO.new($stdin.fileno)
  855. assert_instance_of(File::Stat, io.stat)
  856. io.close
  857. end
  858. def test_sync
  859. $stderr.sync = false
  860. assert(!$stderr.sync)
  861. $stderr.sync = true
  862. assert($stderr.sync)
  863. end
  864. def test_sync=()
  865. unless WIN32
  866. read, write = IO.pipe
  867. write.sync = false
  868. write.print "hello"
  869. assert_nil(select([read], nil, [read], 0.1))
  870. write.sync = true
  871. write.print "there"
  872. assert_equal([[read],[],[]], select([read], nil, [read], 0.1))
  873. read.close
  874. write.close
  875. end
  876. end
  877. def test_sysread
  878. File.open(@file, 'rb') do |file|
  879. assert_equal("", file.sysread(0))
  880. assert_equal("0", file.sysread(1))
  881. assert_equal("0:", file.sysread(2))
  882. assert_equal(" Thi", file.sysread(4))
  883. rest = file.sysread(100000)
  884. assert_equal(LINE_LENGTH*10 - (1+2+4), rest.length)
  885. assert_raise(EOFError) { file.sysread(1) }
  886. end
  887. end
  888. def test_syswrite
  889. File.open(@file, "w") do |file|
  890. file.syswrite ""
  891. file.syswrite "hello"
  892. file.syswrite 1
  893. file.syswrite "\n"
  894. end
  895. File.open(@file) do |file|
  896. assert_equal("hello1\n", file.gets)
  897. end
  898. end
  899. # see also pos
  900. def test_tell
  901. pos = 0
  902. File.open(@file, "rb") do |file|
  903. assert_equal(0, file.tell)
  904. while (line = file.gets)
  905. pos += line.length
  906. assert_equal(pos, file.tell)
  907. end
  908. end
  909. end
  910. def test_to_i
  911. assert_equal(0, $stdin.to_i)
  912. assert_equal(1, $stdout.to_i)
  913. assert_equal(2, $stderr.to_i)
  914. end
  915. # see isatty
  916. def test_tty?
  917. File.open(@file) { |f| assert(!f.tty?) }
  918. if WIN32
  919. # FIXME: JRUBY-4186
  920. return
  921. File.open("con") { |f| assert(f.tty?) }
  922. end
  923. unless WIN32
  924. begin
  925. File.open("/dev/tty") { |f| assert(f.isatty) }
  926. rescue
  927. # Can't open from crontab jobs
  928. end
  929. end
  930. end
  931. def test_ungetc
  932. File.open(@file) do |file|
  933. assert_equal(?0, file.getc)
  934. assert_equal(?0, file.getc)
  935. assert_equal(?:, file.getc)
  936. assert_equal(?\s, file.getc)
  937. assert_nil(file.ungetc(?:))
  938. assert_equal(?:, file.getc)
  939. 1 while file.getc
  940. assert_nil(file.ungetc(?A))
  941. assert_equal(?A, file.getc)
  942. end
  943. end
  944. def test_write
  945. File.open(@file, "wb") do |file|
  946. assert_equal(10, file.write('*' * 10))
  947. assert_equal(5, file.write('!' * 5))
  948. assert_equal(0, file.write(''))
  949. assert_equal(1, file.write(1))
  950. assert_equal(3, file.write(2.30000))
  951. assert_equal(1, file.write("\n"))
  952. end
  953. File.open(@file) do |file|
  954. assert_equal("**********!!!!!12.3\n", file.gets)
  955. end
  956. end
  957. end