| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "problemcasevalidator.h" |
| 18 | #include "../utils.h" |
| 19 | |
| 20 | ProblemCaseValidator::ProblemCaseValidator() |
| 21 | { |
| 22 | |
| 23 | } |
| 24 | |
| 25 | bool ProblemCaseValidator::validate(POJProblemCase problemCase, bool ignoreSpaces) |
| 26 | { |
| 27 | if (!problemCase) |
| 28 | return false; |
| 29 | QStringList output = textToLines(problemCase->output); |
| 30 | QStringList expected; |
| 31 | if (fileExists(problemCase->expectedOutputFileName)) |
| 32 | expected = readFileToLines(problemCase->expectedOutputFileName); |
| 33 | else |
| 34 | expected = textToLines(problemCase->expected); |
| 35 | problemCase->outputLineCounts = output.count(); |
| 36 | problemCase->expectedLineCounts = expected.count(); |
| 37 | if (output.count()!=expected.count()) |
| 38 | return false; |
| 39 | for (int i=0;i<output.count();i++) { |
| 40 | if (ignoreSpaces) { |
| 41 | if (!equalIgnoringSpaces(output[i],expected[i])) { |
| 42 | problemCase->firstDiffLine = i; |
| 43 | return false; |
| 44 | } |
| 45 | } else { |
| 46 | if (output[i]!=expected[i]) { |
| 47 | problemCase->firstDiffLine = i; |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | bool ProblemCaseValidator::equalIgnoringSpaces(const QString &s1, const QString &s2) |
| 56 | { |
| 57 | QStringList strList1=split(s1); |
| 58 | QStringList strList2=split(s2); |
| 59 | return (strList1==strList2); |
| 60 | } |
| 61 | |
| 62 | QStringList ProblemCaseValidator::split(const QString &s) |
| 63 | { |
| 64 | QStringList result; |
| 65 | const QChar* p = s.data(); |
| 66 | const QChar* start = p; |
| 67 | while (p->unicode()!=0) { |
| 68 | if (p->isSpace()) { |
| 69 | if (!start->isSpace()) { |
| 70 | result.append(QString(start,p-start)); |
| 71 | } |
| 72 | start = p; |
| 73 | } else if (start->isSpace()) { |
| 74 | start = p; |
| 75 | } |
| 76 | p++; |
| 77 | } |
| 78 | if (!start->isSpace()) { |
| 79 | result.append(QString(start,p-start)); |
| 80 | } |
| 81 | return result; |
| 82 | } |
| 83 |