我想爲學生的教程控制檯應用程序編寫一個簡單的檢查器。它應該作爲獨立應用程序在Windows下運行。Ruby IO不能像預期的那樣工作在Windows上,隨機在put上讀取並讀取
有使用Ruby一些經驗,我寫了下面的代碼:
require 'yaml'
progs = YAML.load_file "test.yaml"
progs.each do |prog|
puts "running #{prog}..."
pn = prog["program"]
tc = 0
prog["tests"].each do |i, o|
tc += 1
puts "Test #{tc}:\n" #{i}\n=>\n#{o}\n--\n"
IO.popen(pn, "r+") do |pie|
#pie.sync = true
pie.puts i
#pie.flush
#pie.close_write
a = pie.read
if a != o
puts "ERROR: wrong output on test #{tc}"
puts "Input: #{i.inspect}"
puts "Expected: #{o.inspect}"
puts "Actual: #{a.inspect}"
puts
else
puts "ok"
end
end
end
end
支持YAML文件看起來如下:
---
- program: summax.exe
tests:
"2\r\n 0 0\r\n": "0\r\n0\r\n"
"4\n 100 1000 -100 -1000": "0\n1001\n"
- program: ./summax
tests:
"2\n 5 6": "11\n6\n"
在Linux上,這只是工作,甚至當運行exe文件編譯ABC帕斯卡爾(通過葡萄酒和一些binfmt魔術我想)。在目標平臺Windows上,在打印「測試1:」行後,相同的代碼被卡住了,如果我用^ C分解它,它可以繼續測試2或輸出更多的字符串,然後用tester.rb:20:in 'puts': Interrupt
(或任何其他放入,甚至讀)。
對sync = true, flush and close_write
取消所有這些行的註釋似乎沒有幫助。也將read
更改爲read_lines.join
。即使用flush(output); close(output);
沖洗另一端也不能解決該問題。我還應該嘗試什麼,或者更簡單的方法是將本程序重寫爲Windows更原生的東西?
我可以依稀記得,如果進程是從繼承的句柄開始的,那麼你會得到這個問題。我不知道ABC Pascal(我更喜歡FPC),但是如果它允許你修改RTL,你可以修復它。 – 2014-09-08 07:43:12