From 044e69f2076a68abac8ee8869cdafa52a2dfa820 Mon Sep 17 00:00:00 2001 From: Haozhe Xie Date: Tue, 30 Apr 2019 10:26:39 +0800 Subject: [PATCH] Fix issue #50. --- .../src/main/cpp/unix/Judger.Core.Runner.cpp | 9 ++----- .../voj/judger/core/Comparator.java | 17 +++++++++++-- .../voj/judger/core/ComparatorTest.java | 25 ++++++++++++++++--- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/judger/src/main/cpp/unix/Judger.Core.Runner.cpp b/judger/src/main/cpp/unix/Judger.Core.Runner.cpp index e016aa89..c58c8b9d 100644 --- a/judger/src/main/cpp/unix/Judger.Core.Runner.cpp +++ b/judger/src/main/cpp/unix/Judger.Core.Runner.cpp @@ -287,8 +287,7 @@ char** getCommandArgs(const std::string& commandLine) { */ bool isCurrentUsedMemoryIgnored(int currentUsedMemory) { int jvmUsedMemory = getCurrentUsedMemory(getpid()); - std::cout << "[DEBUG] Current Memory of JVM: " << jvmUsedMemory << " KB" << std::endl; - + // std::cout << "[DEBUG] Current Memory of JVM: " << jvmUsedMemory << " KB" << std::endl; if ( currentUsedMemory >= jvmUsedMemory / 2 && currentUsedMemory <= jvmUsedMemory * 2 ) { return true; @@ -307,8 +306,7 @@ int getMaxUsedMemory(pid_t pid, int memoryLimit) { currentUsedMemory = 0; do { currentUsedMemory = getCurrentUsedMemory(pid); - std::cout << "[DEBUG] Current Memory of PID# " << pid << ": " << currentUsedMemory << " KB" << std::endl; - + // std::cout << "[DEBUG] Current Memory of PID# " << pid << ": " << currentUsedMemory << " KB" << std::endl; if ( currentUsedMemory > maxUsedMemory && !isCurrentUsedMemoryIgnored(currentUsedMemory) ) { maxUsedMemory = currentUsedMemory; @@ -344,9 +342,6 @@ int getCurrentUsedMemory(pid_t pid) { } } fclose(fp); - } else { - std::cout << "[DEBUG] Memory file path = " << cFilePath << std::endl; - std::cout << "[DEBUG] fp == NULL in getCurrentUsedMemory(pid_t)" << std::endl; } return currentUsedMemory; } diff --git a/judger/src/main/java/org/verwandlung/voj/judger/core/Comparator.java b/judger/src/main/java/org/verwandlung/voj/judger/core/Comparator.java index 005460f6..1c223eeb 100644 --- a/judger/src/main/java/org/verwandlung/voj/judger/core/Comparator.java +++ b/judger/src/main/java/org/verwandlung/voj/judger/core/Comparator.java @@ -117,7 +117,8 @@ private boolean isFileOutputTheSame(LineIterator stdFileItr, LineIterator fileIt * @return 某行的标准输出和用户输出是否相同 */ private boolean isLineOutputTheSame(String stdLine, String line) { - for ( int i = 0, j = 0; i < stdLine.length() && j < line.length(); ++ i, ++ j ) { + int i = 0, j = 0; + for ( ; i < stdLine.length() && j < line.length(); ++ i, ++ j ) { if ( stdLine.charAt(i) != line.charAt(j) ) { if ( stdLine.charAt(i) == '\n' ) { if ( !isLineEmpty(line, j) ) { @@ -133,6 +134,18 @@ private boolean isLineOutputTheSame(String stdLine, String line) { return false; } } + while ( i < stdLine.length() ) { + if ( !isLineEmpty(stdLine, i) ) { + return false; + } + ++ i; + } + while ( j < line.length() ) { + if ( !isLineEmpty(line, j) ) { + return false; + } + ++ j; + } return true; } @@ -143,7 +156,7 @@ private boolean isLineOutputTheSame(String stdLine, String line) { * @return 该行内容中是否只包含空格和换行符 */ private boolean isLineEmpty(String line, int startIndex) { - for ( int i = 0; i < line.length(); ++ i ) { + for ( int i = startIndex; i < line.length(); ++ i ) { if ( !(line.charAt(i) == ' ' || line.charAt(i) == '\n') ) { return false; } diff --git a/judger/src/test/java/org/verwandlung/voj/judger/core/ComparatorTest.java b/judger/src/test/java/org/verwandlung/voj/judger/core/ComparatorTest.java index 07464a5c..a0ab1634 100644 --- a/judger/src/test/java/org/verwandlung/voj/judger/core/ComparatorTest.java +++ b/judger/src/test/java/org/verwandlung/voj/judger/core/ComparatorTest.java @@ -94,13 +94,18 @@ public static void setUp() throws IOException { IOUtils.write(matchWithSpacesString + " \n \n", outputStream); // TestCase: Mismatch stdOutputStream = new FileOutputStream(new File( - "/tmp/voj-matcher-tests/mimatch-std.txt")); + "/tmp/voj-matcher-tests/mismatch-std.txt")); outputStream = new FileOutputStream(new File( - "/tmp/voj-matcher-tests/mimatch.txt")); + "/tmp/voj-matcher-tests/mismatch.txt")); String mismatchString1 = "45652 \n\n"; String mismatchString2 = "24334"; IOUtils.write(mismatchString1, stdOutputStream); IOUtils.write(mismatchString2, outputStream); + // TestCase: MismatchEmptyString + outputStream = new FileOutputStream(new File( + "/tmp/voj-matcher-tests/mismatch-empty.txt")); + String mismatchString3 = "\n"; + IOUtils.write(mismatchString3, outputStream); } /** @@ -147,8 +152,20 @@ public void testMatchWithSpaces() throws IOException { */ @Test public void testMismatch() throws IOException { - String standardOutputFilePath = "/tmp/voj-matcher-tests/mimatch-std.txt"; - String outputFilePath = "/tmp/voj-matcher-tests/mimatch.txt"; + String standardOutputFilePath = "/tmp/voj-matcher-tests/mismatch-std.txt"; + String outputFilePath = "/tmp/voj-matcher-tests/mismatch.txt"; + Assertions.assertFalse(comparator.isOutputTheSame(standardOutputFilePath, outputFilePath)); + } + + /** + * 测试用例: 测试isOutputTheSame(String, String)方法 + * 测试数据: 输入和输出不完全相同 + * 测试结果: 返回false, 表示输出结果不正确 + */ + @Test + public void testMismatchEmptyString() throws IOException { + String standardOutputFilePath = "/tmp/voj-matcher-tests/mismatch-std.txt"; + String outputFilePath = "/tmp/voj-matcher-tests/mismatch-empty.txt"; Assertions.assertFalse(comparator.isOutputTheSame(standardOutputFilePath, outputFilePath)); }