/** * 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 java.util.Arrays; import java.util.Collections; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; public class ForthEvaluatorMain { @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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled public void testTwoNumbersCanBeDivided() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(4), forthEvaluator.evaluateProgram(Collections.singletonList("12 3 /"))); }); } @Test(name = "performs integer division") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled public void testCombinedAdditionAndSubtraction() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(-1), forthEvaluator.evaluateProgram(Collections.singletonList("1 2 + 4 -"))); }); } @Test(name = "multiplication and division") @Disabled public void testCombinedMultiplicationAndDivision() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(2), forthEvaluator.evaluateProgram(Collections.singletonList("2 4 * 3 /"))); }); } @Test(name = "multiplication and addition") @Disabled public void testCombinedMultiplicationAndAddition() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertListEquals(Arrays.asList(13), forthEvaluator.evaluateProgram(Collections.singletonList("1 3 4 * +"))); }); } @Test(name = "addition and multiplication") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled public void testCannotRedefineNonNegativeNumbers() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList(": 1 2 ;"))); }); } @Test(name = "cannot redefine negative numbers") @Disabled 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") @Disabled public void testErrorIfEvaluatingAnUndefinedOperator() { runTest(() -> { ForthEvaluator forthEvaluator = new ForthEvaluator(); assertThrows(IllegalArgumentException.class, () -> forthEvaluator.evaluateProgram(Collections.singletonList("foo"))); }); } @Test(name = "DUP is case-insensitive") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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") @Disabled 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(); System.out.println("TEST PASSED"); } catch (Exception | AssertionError t) { System.out.println("TEST FAILED: " + t.getMessage()); } } 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 static void main(String[] args) { new ForthEvaluatorMain().runTests(); } /** * Annotation to mark test methods. The name parameter is used to identify the * test in the output. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Test { String name(); } /** * Annotation to mark test methods as disabled. Disabled tests will not be * executed when running the tests. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Disabled { } private void runTests() { Method[] methods = this.getClass().getDeclaredMethods(); List allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class)) .collect(Collectors.toList()); List enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class)) .collect(Collectors.toList()); System.out .println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n"); for (Method method : enabledTestMethods) { try { System.out.println("Running test: " + method.getAnnotation(Test.class).name()); method.invoke(this); } catch (Exception e) { System.out.println("Test " + method.getAnnotation(Test.class).name() + " failed with an unexpected exception: " + e.getCause()); } } System.out.println("Finished running tests."); if (allTestMethods.size() != enabledTestMethods.size()) { System.out.println("\nThe following tests are disabled and were not executed:"); for (Method method : allTestMethods) { if (method.isAnnotationPresent(Disabled.class)) { System.out.println("- " + method.getAnnotation(Test.class).name()); } } System.out.println( "To enable these tests, remove the @Disabled annotation from the corresponding test methods."); } } }