/** * 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.Arrays; import java.util.Collections; import java.util.List; public class ForthEvaluatorTest { private static final int MAX_POINTS = 100; private static final int TEST_CASES = 55; private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES; private static final int FAILURE_POINTS = 0; private final java.util.List testPoints = new java.util.ArrayList<>(); @Test(name = "numbers just get pushed onto the stack") public void testNumbersAreJustPushedOntoTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 2, 3, 4, 5), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 4 5"))); }); } @Test(name = "pushes negative numbers onto the stack") public void testNegativeNumbersArePushedOntoTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(-1, -2, -3, -4, -5), forthEvaluator.evaluateProgram(Collections.singletonList("-1 -2 -3 -4 -5"))); }); } @Test(name = "can add two numbers") public void testTwoNumbersCanBeAdded() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(3), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 +"))); }); } @Test(name = "errors if there is nothing on the stack") public void testErrorIfAdditionAttemptedWithNothingOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("+"))); }); } @Test(name = "errors if there is only one value on the stack") public void testErrorIfAdditionAttemptedWithOneNumberOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("1 +"))); }); } @Test(name = "more than two values on the stack") public void testAdditionForMoreThanTwoValuesOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 5), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 +"))); }); } @Test(name = "can subtract two numbers") public void testTwoNumbersCanBeSubtracted() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(-1), forthEvaluator.evaluateProgram(Collections.singletonList("3 4 -"))); }); } @Test(name = "errors if there is nothing on the stack") public void testErrorIfSubtractionAttemptedWithNothingOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("-"))); }); } @Test(name = "errors if there is only one value on the stack") public void testErrorIfSubtractionAttemptedWithOneNumberOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("1 -"))); }); } @Test(name = "more than two values on the stack") public void testSubtractionForMoreThanTwoValuesOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 9), forthEvaluator.evaluateProgram(Collections.singletonList("1 12 3 -"))); }); } @Test(name = "can multiply two numbers") public void testTwoNumbersCanBeMultiplied() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(8), forthEvaluator.evaluateProgram(Collections.singletonList("2 4 *"))); }); } @Test(name = "errors if there is nothing on the stack") public void testErrorIfMultiplicationAttemptedWithNothingOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("*"))); }); } @Test(name = "errors if there is only one value on the stack") public void testErrorIfMultiplicationAttemptedWithOneNumberOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("1 *"))); }); } @Test(name = "more than two values on the stack") public void testMultiplicationForMoreThanTwoValuesOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 6), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 *"))); }); } @Test(name = "can divide two numbers") public void testTwoNumbersCanBeDivided() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(4), forthEvaluator.evaluateProgram(Collections.singletonList("12 3 /"))); }); } @Test(name = "performs integer division") public void testThatIntegerDivisionIsUsed() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(2), forthEvaluator.evaluateProgram(Collections.singletonList("8 3 /"))); }); } @Test(name = "errors if dividing by zero") public void testErrorIfDividingByZero() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("4 0 /"))); }); } @Test(name = "errors if there is nothing on the stack") public void testErrorIfDivisionAttemptedWithNothingOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("/"))); }); } @Test(name = "errors if there is only one value on the stack") public void testErrorIfDivisionAttemptedWithOneNumberOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("1 /"))); }); } @Test(name = "more than two values on the stack") public void testDivisionForMoreThanTwoValuesOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 4), forthEvaluator.evaluateProgram(Collections.singletonList("1 12 3 /"))); }); } @Test(name = "addition and subtraction") public void testCombinedAdditionAndSubtraction() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(-1), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 + 4 -"))); }); } @Test(name = "multiplication and division") public void testCombinedMultiplicationAndDivision() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(2), forthEvaluator.evaluateProgram(Collections.singletonList("2 4 * 3 /"))); }); } @Test(name = "multiplication and addition") public void testCombinedMultiplicationAndAddition() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(13), forthEvaluator.evaluateProgram(Collections.singletonList("1 3 4 * +"))); }); } @Test(name = "addition and multiplication") public void testCombinedAdditionAndMultiplication() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(7), forthEvaluator.evaluateProgram(Collections.singletonList("1 3 4 + *"))); }); } @Test(name = "copies a value on the stack") public void testDupCopiesAValueOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 1), forthEvaluator.evaluateProgram(Collections.singletonList("1 dup"))); }); } @Test(name = "copies the top value on the stack") public void testDupCopiesTopValueOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 2, 2), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 dup"))); }); } @Test(name = "errors if there is nothing on the stack") public void testErrorIfDuplicatingAttemptedWithNothingOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("dup"))); }); } @Test(name = "removes the top value on the stack if it is the only one") public void testDropRemovesTheTopValueOnTheStackIfItIsTheOnlyOne() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertIsEmpty(forthEvaluator.evaluateProgram(Collections.singletonList("1 drop"))); }); } @Test(name = "removes the top value on the stack if it is not the only one") public void testDropRemovesTheTopValueOnTheStackIfItIsNotTheOnlyOne() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 drop"))); }); } @Test(name = "errors if there is nothing on the stack") public void testErrorIfDroppingAttemptedWithNothingOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("drop"))); }); } @Test(name = "swaps the top two values on the stack if they are the only ones") public void testSwapSwapsTheTopTwosValueOnTheStackIfTheyAreTheOnlyOnes() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(2, 1), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 swap"))); }); } @Test(name = "swaps the top two values on the stack if they are not the only ones") public void testSwapSwapsTheTopTwosValueOnTheStackIfTheyAreNotTheOnlyOnes() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 3, 2), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 swap"))); }); } @Test(name = "errors if there is nothing on the stack") public void testErrorIfSwappingAttemptedWithNothingOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("swap"))); }); } @Test(name = "errors if there is only one value on the stack") public void testErrorIfSwappingAttemptedWithOneNumberOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("1 swap"))); }); } @Test(name = "copies the second element if there are only two") public void testOverCopiesTheSecondElementIfThereAreOnlyTwo() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 2, 1), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 over"))); }); } @Test(name = "copies the second element if there are more than two") public void testOverCopiesTheSecondElementIfThereAreMoreThanTwo() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 2, 3, 2), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 over"))); }); } @Test(name = "errors if there is nothing on the stack") public void testErrorIfOveringAttemptedWithNothingOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("over"))); }); } @Test(name = "errors if there is only one value on the stack") public void testErrorIfOveringAttemptedWithOneNumberOnTheStack() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("1 over"))); }); } @Test(name = "can consist of built-in words") public void testUserDefinedOperatorsCanConsistOfBuiltInOperators() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 1, 1), forthEvaluator.evaluateProgram(Arrays.asList(": dup-twice dup dup ;", "1 dup-twice"))); }); } @Test(name = "execute in the right order") public void testUserDefinedOperatorsAreEvaluatedInTheCorrectOrder() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 2, 3), forthEvaluator.evaluateProgram(Arrays.asList(": countup 1 2 3 ;", "countup"))); }); } @Test(name = "can override other user-defined words") public void testCanRedefineAUserDefinedOperator() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 1, 1), forthEvaluator.evaluateProgram(Arrays.asList(": foo dup ;", ": foo dup dup ;", "1 foo"))); }); } @Test(name = "can override built-in words") public void testCanOverrideBuiltInWordOperators() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 1), forthEvaluator.evaluateProgram(Arrays.asList(": swap dup ;", "1 swap"))); }); } @Test(name = "can override built-in operators") public void testCanOverrideBuiltInArithmeticOperators() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(12), forthEvaluator.evaluateProgram(Arrays.asList(": + * ;", "3 4 +"))); }); } @Test(name = "can use different words with the same name") public void testCanUseDifferentWordsWithTheSameName() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(5, 6), forthEvaluator.evaluateProgram(Arrays.asList(": foo 5 ;", ": bar foo ;", ": foo 6 ;", "bar foo"))); }); } @Test(name = "can define word that uses word with the same name") public void testCanDefineWordThatUsesWordWithTheSameName() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(11), forthEvaluator.evaluateProgram(Arrays.asList(": foo 10 ;", ": foo foo 1 + ;", "foo"))); }); } @Test(name = "cannot redefine non-negative numbers") public void testCannotRedefineNonNegativeNumbers() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList(": 1 2 ;"))); }); } @Test(name = "cannot redefine negative numbers") public void testCannotRedefineNegativeNumbers() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList(": -1 2 ;"))); }); } @Test(name = "errors if executing a non-existent word") public void testErrorIfEvaluatingAnUndefinedOperator() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("foo"))); }); } @Test(name = "DUP is case-insensitive") public void testDupIsCaseInsensitive() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 1, 1, 1), forthEvaluator.evaluateProgram(Collections.singletonList("1 DUP Dup dup"))); }); } @Test(name = "DROP is case-insensitive") public void testDropIsCaseInsensitive() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 4 DROP Drop drop"))); }); } @Test(name = "SWAP is case-insensitive") public void testSwapIsCaseInsensitive() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(2, 3, 4, 1), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 SWAP 3 Swap 4 swap"))); }); } @Test(name = "OVER is case-insensitive") public void testOverIsCaseInsensitive() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 2, 1, 2, 1), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 OVER Over over"))); }); } @Test(name = "user-defined words are case-insensitive") public void testUserDefinedWordsAreCaseInsensitive() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 1, 1, 1), forthEvaluator.evaluateProgram(Arrays.asList(": foo dup ;", "1 FOO Foo foo"))); }); } @Test(name = "definitions are case-insensitive") public void testDefinitionsAreCaseInsensitive() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(1, 1, 1, 1), forthEvaluator.evaluateProgram(Arrays.asList(": SWAP DUP Dup dup ;", "1 swap"))); }); } @Test(name = "only defines locally") public void testDefinitionsAreOnlyDefinedLocally() { runTest(() -> { ForthEvaluator firstInstance = new ForthEvaluator(); ForthEvaluator secondInstance = new ForthEvaluator(); List firstOutput = firstInstance.evaluateProgram(Arrays.asList(": + - ;", "1 1 +")); List secondOutput = secondInstance.evaluateProgram(Collections.singletonList("1 1 +")); assertListEquals(Arrays.asList(0), firstOutput); assertListEquals(Arrays.asList(2), secondOutput); }); } private void runTest(Runnable body) { try { body.run(); testPoints.add(SUCCESS_POINTS); } catch (Exception | AssertionError t) { TracingFramework.printError(t.getMessage()); testPoints.add(FAILURE_POINTS); } } private void assertListEquals(List expected, List actual) { if (expected == null) { if (actual != null) throw new AssertionError("expected null but was: " + actual); return; } if (!expected.equals(actual)) throw new AssertionError("expected: " + expected + " but was: " + actual); } private void assertIsEmpty(List actual) { if (actual == null || actual.isEmpty()) return; throw new AssertionError("expected empty list but was: " + actual); } private void assertThrows(Class exceptionType, Runnable body) { try { body.run(); throw new AssertionError("expected " + exceptionType.getSimpleName() + " to be thrown"); } catch (Exception e) { if (!exceptionType.isInstance(e)) { throw new AssertionError( "expected " + exceptionType.getSimpleName() + " but was: " + e.getClass().getSimpleName()); } } } public int getResult() { int sum = testPoints.stream().mapToInt(Integer::intValue).sum(); return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum; } }