如何用java编写简单的txt测试用例

发布于 2024-12-22 19:23:31 字数 1009 浏览 1 评论 0原文

您好,我正在编写我的第一个 Java 应用程序,我有一些测试用例(*.tc 文件),我想通过此脚本直接访问该应用程序:

for f in `ls *.tc`; do
  echo "Current Testcase: $f"
  x=${f%.*}
  java Main < $x.tc > $x.out

  if diff "$x.out" "$x.should"; then
    echo "passed testcase $f"
    let PASSED=PASSED+1
  else
    echo "failed testcase $f"
    let FAILED=FAILED+1
 fi
done

问题是我无法弄清楚为什么一旦 < code>tc 文件 包含不止一行,应用程序变得疯狂。例如: quit.tc 包含

quit

并工作就像我手动输入“quit”时一样,因此测试用例通过。 但是,当我编写另一个 tc: quit2.tc 时,其中包含

lala
test
quit

应用程序在第一个命令后退出(因为 readString 函数似乎随后返回 null)。 这是负责读取的函数:

public String readString(){

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String answer = null;

try {
  answer = br.readLine();
  return answer;
}
catch(IOException ioe) {
  System.out.println("IO Error");
}
return answer;
}   

当我重定向到应用程序时,我不知道为什么或何时此函数返回 null,这似乎是问题所在。你能帮我让 tc 脚本工作吗?谢谢

Hi I'm writing my first Java app and I've got a few Testcases (*.tc files) I want to direct to the app via this script:

for f in `ls *.tc`; do
  echo "Current Testcase: $f"
  x=${f%.*}
  java Main < $x.tc > $x.out

  if diff "$x.out" "$x.should"; then
    echo "passed testcase $f"
    let PASSED=PASSED+1
  else
    echo "failed testcase $f"
    let FAILED=FAILED+1
 fi
done

The Problem is I can't quite figure out why as soon as the tc file contains more than one line the app goes nuts. For example: quit.tc contains

quit

and works just like when I manually enter "quit", therfore the testcase passes.
However when I write another tc: quit2.tc which contains

lala
test
quit

The app quits after the first command (because the readString function seems to return null afterwards).
Here is the function responsible for reading:

public String readString(){

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String answer = null;

try {
  answer = br.readLine();
  return answer;
}
catch(IOException ioe) {
  System.out.println("IO Error");
}
return answer;
}   

I dont know why or when this function returns null when I redirect to the app, which seems to be the problem. Can you help out so I can get the tc script working? thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海 2024-12-29 19:23:31

如果您是 java 新手,并且仍在塑造自己的风格和做事方式,我会向您推荐 2 个提示:

1) 使用 扫描仪来读取您的输入。使用 nextLine()< Scanner 对象的 /a> 方法可能就是您正在寻找的。

2) 设计您的代码,以便可以通过 JUnit 进行测试。

不过,在您的 readString() 方法中,从 try 块中删除 return answer;

更新:尝试在您的函数中实现以下内容:

a),而扫描仪实例 hasNextLine() 为 true ->

b) 调用扫描器实例的 nextLine() 方法 ->

c) 解析该行,看看它是否等于 'quit' ->

d) 实现相应的逻辑 if 情况。

If you're new to java, and you still shape your style and way of doing things, I will recommend you 2 tips:

1) use Scanner to read your input. Using nextLine() method of the Scanner object is probably what you're looking for.

2) design your code, so that it's testable via JUnit.

Still, in your readString() method, remove the return answer; from the try block.

UPDATE: try to implement the following in your function:

a) while the scanner instance hasNextLine() is true ->

b) call scanner instance nextLine() method ->

c) parse the line, and see if it equals 'quit' ->

d) implement the corresponding logical if cases.

书间行客 2024-12-29 19:23:31

您的问题是 BufferedReader:它将立即读取整个文件(出于性能原因)。

由于每次调用 readString() 时都会创建一个新的 BufferedReader,因此第一个读取器将吞下文件的大部分(或全部),而第二个读取器将结束- 文件。

解决方案是将读取器放入一个字段中,创建一次一次,然后始终使用相同的读取器来调用readLine()

另外,您永远不会关闭读取器。

Java 会忘记未使用的对象,但您必须正确关闭操作系统资源,例如 FileInputStream

Your problem is BufferedReader: It will read the whole file at once (for performance reasons).

Since you create a new BufferedReader each time readString() is called, the first reader will swallow most of the file (or all of it) and the second reader will hit end-of-file.

The solution is to put the reader into a field, create it once and then always use the same reader to call readLine()

Plus you never close the reader.

Java will forget unused objects but you must properly close OS resources like FileInputStreams

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文