|
1 | 1 | package com.jalgoarena.judge
|
2 | 2 |
|
3 |
| -import java.math.RoundingMode |
4 |
| -import java.text.DecimalFormat |
| 3 | +import com.jalgoarena.type.Pair |
5 | 4 | import java.util.*
|
6 |
| -import com.jalgoarena.type.* |
7 | 5 |
|
8 | 6 | class JudgeResultVerifier {
|
9 |
| - fun isValidAnswer(a: Any?, b: Any?): Boolean { |
10 |
| - return equalForObjectsOrArrays(a, b) |
| 7 | + fun isValidAnswer(expected: Any?, toCheck: Any?): Boolean { |
| 8 | + return equalForObjectsOrArrays(expected, toCheck) |
11 | 9 | }
|
12 | 10 |
|
13 |
| - private fun equalForObjectsOrArrays(a: Any?, b: Any?): Boolean { |
| 11 | + private fun equalForObjectsOrArrays(expected: Any?, toCheck: Any?): Boolean { |
14 | 12 | return when {
|
15 |
| - a === b -> return true |
16 |
| - a == null || b == null -> return false |
17 |
| - a is Pair && b is Pair -> return comparePairs(a, b) |
18 |
| - a is Array<*> && b is Array<*> -> return Arrays.deepEquals(a, b) |
19 |
| - a is IntArray && b is IntArray -> Arrays.equals(a, b) |
20 |
| - a is Double && b is Double -> equalDouble(a, b) |
21 |
| - a is Int && b is Double -> equalDouble(a.toDouble(), b) |
22 |
| - a is Double && b is Int -> equalDouble(a, b.toDouble()) |
23 |
| - a == b -> return true |
24 |
| - else -> return a.toString() == b.toString() |
| 13 | + expected === toCheck -> return true |
| 14 | + expected == null || toCheck == null -> return false |
| 15 | + expected is Pair && toCheck is Pair -> return comparePairs(expected, toCheck) |
| 16 | + expected is Array<*> && toCheck is Array<*> -> return Arrays.deepEquals(expected, toCheck) |
| 17 | + expected is IntArray && toCheck is IntArray -> Arrays.equals(expected, toCheck) |
| 18 | + expected is Double && toCheck is Double -> equalDouble(expected, toCheck) |
| 19 | + expected is Int && toCheck is Double -> equalDouble(expected.toDouble(), toCheck) |
| 20 | + expected is Double && toCheck is Int -> equalDouble(expected, toCheck.toDouble()) |
| 21 | + expected == toCheck -> return true |
| 22 | + else -> return expected.toString() == toCheck.toString() |
25 | 23 | }
|
26 | 24 | }
|
27 | 25 |
|
28 |
| - private fun comparePairs(a: Pair, b: Pair): Boolean { |
29 |
| - return equalForObjectsOrArrays(a.first, b.first) && |
30 |
| - equalForObjectsOrArrays(a.second, b.second) |
| 26 | + private fun comparePairs(expected: Pair, toCheck: Pair): Boolean { |
| 27 | + return equalForObjectsOrArrays(expected.first, toCheck.first) && |
| 28 | + equalForObjectsOrArrays(expected.second, toCheck.second) |
31 | 29 | }
|
32 | 30 |
|
33 |
| - private fun equalDouble(a: Double, b: Double): Boolean { |
34 |
| - val df = DecimalFormat("#.#####") |
35 |
| - df.roundingMode = RoundingMode.HALF_UP |
36 |
| - |
37 |
| - return df.format(a) == df.format(b) |
| 31 | + private fun equalDouble(expected: Double, toCheck: Double): Boolean { |
| 32 | + return Math.abs(toCheck - expected) / Math.max(1.0, expected) < 1e-5 |
38 | 33 | }
|
39 | 34 | }
|
0 commit comments