Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs for comparing outputs with Wrong Answer and Presentation Error status #80

Merged
merged 2 commits into from
Nov 14, 2015

Conversation

hzxie
Copy link
Contributor

@hzxie hzxie commented Nov 13, 2015

As I mentioned in Issue #79, There's a bug in comparing outputs.
And here's the pull request to fix it.

Because when you compare the outputs, you stopped when reach the end of output of test case. And you didn't check if the rest of the stdout of user's program.

If there's some non-blank characters, the judge status should be Wrong Answer, but in the old code, the status would be Presentation Error.

Feel free to ask me questions and thanks for reviewing my PR.

@hzxie
Copy link
Contributor Author

hzxie commented Nov 13, 2015

Here's the code to help you review the PR.
The following code is copied from the file judgelib.php:

<?php

// trim tailing return chars which are meaning less
$output = rtrim("100 = 2 * 2 * 5 * 5", "\r\n");
$stdout = rtrim("100 = 2 * 2 * 5 * 5 * 1\n", "\r\n");

echo diff($output, $stdout);

function diff($output, $stdout) {
    if (strcmp($output, $stdout) == 0)
        return "ONLINEJUDGE_STATUS_ACCEPTED\n";
    else {
        $tokens = array();
        $tok = strtok($output, " \n\r\t");

        while ($tok !== false) {
            $tokens[] = $tok;
            $tok = strtok(" \n\r\t");
        }
        $tok = strtok($stdout, " \n\r\t");

        // Token is output (right answer)
        foreach ($tokens as $anstok) {
            if ($tok === false || $tok !== $anstok)
                return "ONLINEJUDGE_STATUS_WRONG_ANSWER\n";
            $tok = strtok(" \n\r\t");
        }
        // The following code from line 30 to line 35 is used to fix the bug
        while ($tok !== false) {
            if ( !empty(trim($tok)) ) {
                return "ONLINEJUDGE_STATUS_WRONG_ANSWER\n";
            }
            $tok = strtok(" \n\r\t");
        }
        return "ONLINEJUDGE_STATUS_PRESENTATION_ERROR\n";
    }
}

And it's obvious that if you remove the code from 30-35, the diff() function will return the wrong status code.

@hzxie hzxie changed the title Fix bugs for compare outputs with Wrong Answer and Presentation Error Fix bugs for comparing outputs with Wrong Answer and Presentation Error Nov 13, 2015
@hzxie hzxie changed the title Fix bugs for comparing outputs with Wrong Answer and Presentation Error Fix bugs for comparing outputs with Wrong Answer and Presentation Error status Nov 13, 2015
return ONLINEJUDGE_STATUS_WRONG_ANSWER;
}
$tok = strtok(" \n\r\t");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps,

            if ($tok !== false) {
                return ONLINEJUDGE_STATUS_WRONG_ANSWER;
            } else {
                return ONLINEJUDGE_STATUS_PRESENTATION_ERROR;
            }

Is this OK?

@hzxie
Copy link
Contributor Author

hzxie commented Nov 13, 2015

Yes, thanks for your advice @sunner.
I've updated the code. You can merge the PR directly.

@sunner
Copy link
Member

sunner commented Nov 13, 2015

Hi, have you tested the code with enough submissions? I can not test it now.

@hzxie
Copy link
Contributor Author

hzxie commented Nov 13, 2015

Yes. I tested following test cases:

Test Case 1

$output = "100 = 2 * 2 * 5 * 5";
$stdout = "100 = 2 * 2 * 5 * 5\n";

Returns ONLINEJUDGE_STATUS_ACCEPTED;

Test Case 2

$output = "100 = 2 * 2 * 5 * 5\n\n";
$stdout = "100 = 2 * 2 * 5 * 5\n";

Returns ONLINEJUDGE_STATUS_ACCEPTED;

Test Case 3

$output = "100 = 2 * 2 * 5 * 5";
$stdout = "100 = 2 * 2 * 5 * 5 \n";

Returns ONLINEJUDGE_STATUS_PRESENTATION_ERROR;

Test Case 4

$output = "100 = 2 * 2 * 5 * 5 \n";
$stdout = "100 = 2 * 2 * 5 * 5\n";

Returns ONLINEJUDGE_STATUS_PRESENTATION_ERROR;

Test Case 5

$output = "100 = 2 * 2 * 5 * 5";
$stdout = "100 = 2 * 2 * 5 * 5\t\n";

Returns ONLINEJUDGE_STATUS_PRESENTATION_ERROR;

Test Case 6

$output = "100 = 2 * 2 * 5 * 5";
$stdout = "100 = 2 * 2 * 5 * 5\t \n";

Returns ONLINEJUDGE_STATUS_PRESENTATION_ERROR;

Test Case 7

$output = "100 = 2 * 2 * 5 * 5";
$stdout = "100 = 2 * 2 * 5 * 5 * 1\n";

Returns ONLINEJUDGE_STATUS_WRONG_ANSWER;

Test Case 8

$output = "100 = 2 * 2 * 5 * 5";
$stdout = "100 = 2 * 2 * 5 * 5\n * 1\n";

Returns ONLINEJUDGE_STATUS_WRONG_ANSWER;

Test Case 9

$output = "100 = 2 * 2 * 5 * 5 * 1";
$stdout = "100 = 2 * 2 * 5 * 5\n";

Returns ONLINEJUDGE_STATUS_WRONG_ANSWER;

Test Case 10

$output = "100 = 2 * 2 * 5 * 5 * 1";
$stdout = "100 = 2 * 2 * 5 * 5\t * 1\n";

Returns ONLINEJUDGE_STATUS_WRONG_ANSWER;

sunner added a commit that referenced this pull request Nov 14, 2015
Fix bugs for comparing outputs with Wrong Answer and Presentation Error status
@sunner sunner merged commit faa5c7a into hit-moodle:master Nov 14, 2015
@sunner
Copy link
Member

sunner commented Nov 14, 2015

Merged. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants