210 lines
12 KiB
Java
210 lines
12 KiB
Java
|
|
/**
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2021 Exercism
|
|
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
|
|
|
|
import java.util.Map;
|
|
|
|
public class AlphameticsTest {
|
|
|
|
private static final int MAX_POINTS = 100;
|
|
private static final int TEST_CASES = 10;
|
|
private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES;
|
|
private static final int FAILURE_POINTS = 0;
|
|
|
|
private final java.util.List<Integer> testPoints = new java.util.ArrayList<>();
|
|
|
|
@Test(name = "puzzle with three letters")
|
|
public void testThreeLetters() {
|
|
runTest(() -> {
|
|
Map<Character, Integer> result = new Alphametics("I + BB == ILL").solve();
|
|
assertMapEquals(Map.ofEntries(Map.entry('I', 1), Map.entry('B', 9), Map.entry('L', 0)), result);
|
|
});
|
|
}
|
|
|
|
@Test(name = "solution must have unique value for each letter")
|
|
public void testUniqueValue() {
|
|
runTest(() -> {
|
|
Alphametics alphametics = new Alphametics("A == B");
|
|
assertThrows(UnsolvablePuzzleException.class, alphametics::solve, null);
|
|
});
|
|
}
|
|
|
|
@Test(name = "leading zero solution is invalid")
|
|
public void testLeadingZero() {
|
|
runTest(() -> {
|
|
Alphametics alphametics = new Alphametics("ACA + DD == BD");
|
|
assertThrows(UnsolvablePuzzleException.class, alphametics::solve, null);
|
|
});
|
|
}
|
|
|
|
@Test(name = "puzzle with two digits final carry")
|
|
public void testTwoDigitsFinalCarry() {
|
|
runTest(() -> {
|
|
Map<Character, Integer> result = new Alphametics(
|
|
"A + A + A + A + A + A + A + A + A + A + A + B == BCC").solve();
|
|
assertMapEquals(Map.ofEntries(Map.entry('A', 9), Map.entry('B', 1), Map.entry('C', 0)), result);
|
|
});
|
|
}
|
|
|
|
@Test(name = "puzzle with four letters")
|
|
public void testFourLetters() {
|
|
runTest(() -> {
|
|
Map<Character, Integer> result = new Alphametics("AS + A == MOM").solve();
|
|
assertMapEquals(Map.ofEntries(Map.entry('A', 9), Map.entry('S', 2), Map.entry('M', 1),
|
|
Map.entry('O', 0)), result);
|
|
});
|
|
}
|
|
|
|
@Test(name = "puzzle with six letters")
|
|
public void testSixLetters() {
|
|
runTest(() -> {
|
|
Map<Character, Integer> result = new Alphametics("NO + NO + TOO == LATE").solve();
|
|
assertMapEquals(Map.ofEntries(Map.entry('N', 7), Map.entry('O', 4), Map.entry('T', 9),
|
|
Map.entry('L', 1), Map.entry('A', 0), Map.entry('E', 2)), result);
|
|
});
|
|
}
|
|
|
|
@Test(name = "puzzle with seven letters")
|
|
public void testSevenLetters() {
|
|
runTest(() -> {
|
|
Map<Character, Integer> result = new Alphametics("HE + SEES + THE == LIGHT").solve();
|
|
assertMapEquals(Map.ofEntries(Map.entry('E', 4), Map.entry('G', 2), Map.entry('H', 5),
|
|
Map.entry('I', 0), Map.entry('L', 1), Map.entry('S', 9), Map.entry('T', 7)),
|
|
result);
|
|
});
|
|
}
|
|
|
|
@Test(name = "puzzle with eight letters")
|
|
public void testEightLetters() {
|
|
runTest(() -> {
|
|
Map<Character, Integer> result = new Alphametics("SEND + MORE == MONEY").solve();
|
|
assertMapEquals(Map.ofEntries(Map.entry('S', 9), Map.entry('E', 5), Map.entry('N', 6),
|
|
Map.entry('D', 7), Map.entry('M', 1), Map.entry('O', 0), Map.entry('R', 8),
|
|
Map.entry('Y', 2)), result);
|
|
});
|
|
}
|
|
|
|
@Test(name = "puzzle with ten letters")
|
|
public void testTenLetters() {
|
|
runTest(() -> {
|
|
Map<Character, Integer> result = new Alphametics(
|
|
"AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE").solve();
|
|
assertMapEquals(Map.ofEntries(Map.entry('A', 5), Map.entry('D', 3), Map.entry('E', 4),
|
|
Map.entry('F', 7), Map.entry('G', 8), Map.entry('N', 0), Map.entry('O', 2),
|
|
Map.entry('R', 1), Map.entry('S', 6), Map.entry('T', 9)), result);
|
|
});
|
|
}
|
|
|
|
@Test(name = "puzzle with ten letters and 199 addends")
|
|
public void testTenLetters199Addends() {
|
|
runTest(() -> {
|
|
Map<Character, Integer> result = new Alphametics(
|
|
"THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL + A + " +
|
|
"TALE + THAT + FALSIFIES + ITS + TITLE + TIS + A + LIE + THE + TALE + OF + THE + LAST + FIRE + "
|
|
+
|
|
"HORSES + LATE + AFTER + THE + FIRST + FATHERS + FORESEE + THE + HORRORS + THE + LAST + FREE + "
|
|
+
|
|
"TROLL + TERRIFIES + THE + HORSES + OF + FIRE + THE + TROLL + RESTS + AT + THE + HOLE + OF + "
|
|
+
|
|
"LOSSES + IT + IS + THERE + THAT + SHE + STORES + ROLES + OF + LEATHERS + AFTER + SHE + SATISFIES + "
|
|
+
|
|
"HER + HATE + OFF + THOSE + FEARS + A + TASTE + RISES + AS + SHE + HEARS + THE + LEAST + FAR + "
|
|
+
|
|
"HORSE + THOSE + FAST + HORSES + THAT + FIRST + HEAR + THE + TROLL + FLEE + OFF + TO + THE + "
|
|
+
|
|
"FOREST + THE + HORSES + THAT + ALERTS + RAISE + THE + STARES + OF + THE + OTHERS + AS + THE + "
|
|
+
|
|
"TROLL + ASSAILS + AT + THE + TOTAL + SHIFT + HER + TEETH + TEAR + HOOF + OFF + TORSO + AS + THE + "
|
|
+
|
|
"LAST + HORSE + FORFEITS + ITS + LIFE + THE + FIRST + FATHERS + HEAR + OF + THE + HORRORS + THEIR + "
|
|
+
|
|
"FEARS + THAT + THE + FIRES + FOR + THEIR + FEASTS + ARREST + AS + THE + FIRST + FATHERS + "
|
|
+
|
|
"RESETTLE + THE + LAST + OF + THE + FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE + FOREST + "
|
|
+
|
|
"HEART + FREE + AT + LAST + OF + THE + LAST + TROLL + ALL + OFFER + THEIR + FIRE + HEAT + TO + THE + "
|
|
+
|
|
"ASSISTERS + FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + STARS + RISE + THE + "
|
|
+
|
|
"HORSES + REST + SAFE + AFTER + ALL + SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A + "
|
|
+
|
|
"ROOFS + FOR + THEIR + SAFE == FORTRESSES")
|
|
.solve();
|
|
assertMapEquals(Map.ofEntries(Map.entry('A', 1), Map.entry('E', 0), Map.entry('F', 5),
|
|
Map.entry('H', 8), Map.entry('I', 7), Map.entry('L', 2), Map.entry('O', 6),
|
|
Map.entry('R', 3), Map.entry('S', 4), Map.entry('T', 9)), result);
|
|
});
|
|
}
|
|
|
|
private void assertMapEquals(Map<?, ?> expected, Map<?, ?> actual) {
|
|
if (expected == null ? actual == null : expected.equals(actual)) {
|
|
return;
|
|
}
|
|
throw new AssertionError("Expected: " + expected + ", but got: " + actual);
|
|
}
|
|
|
|
private void assertThrows(Class<? extends Throwable> expectedType, ThrowingRunnable runnable,
|
|
String expectedMessage) {
|
|
try {
|
|
runnable.run();
|
|
throw new AssertionError("Expected exception: " + expectedType.getSimpleName());
|
|
} catch (UnsolvablePuzzleException t) {
|
|
if (expectedType.isInstance(t)
|
|
&& (expectedMessage == null || expectedMessage.equals(t.getMessage()))) {
|
|
return;
|
|
}
|
|
throw new AssertionError("Expected: " + expectedType.getSimpleName()
|
|
+ " with message: " + expectedMessage
|
|
+ ", but got: " + t.getClass().getSimpleName() + " with message: "
|
|
+ t.getMessage());
|
|
}
|
|
}
|
|
|
|
private void runTest(Runnable test) {
|
|
try {
|
|
test.run();
|
|
testPoints.add(SUCCESS_POINTS);
|
|
} catch (AssertionError | Exception e) {
|
|
TracingFramework.printError(e.getMessage());
|
|
testPoints.add(FAILURE_POINTS);
|
|
}
|
|
}
|
|
|
|
@FunctionalInterface
|
|
private interface ThrowingRunnable {
|
|
void run() throws UnsolvablePuzzleException;
|
|
}
|
|
|
|
public int getResult() {
|
|
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
|
|
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
|
|
}
|
|
|
|
}
|