/** * 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.List; import java.util.function.Consumer; public class ReactTest { private static final int MAX_POINTS = 100; private static final int TEST_CASES = 14; 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 = "input cells have a value") public void testInputCellHasValue() { runTest(() -> { var input = React.inputCell(10); assertIntEquals(10, input.getValue()); }); } @Test(name = "an input cell's value can be set") public void testInputCellValueCanBeSet() { runTest(() -> { var input = React.inputCell(4); input.setValue(20); assertIntEquals(20, input.getValue()); }); } @Test(name = "compute cells calculate initial value") public void testComputeCellCalculateInitialValue() { runTest(() -> { var input = React.inputCell(1); var output = React.computeCell(list -> list.get(0) + 1, List.of(input)); assertIntEquals(2, output.getValue()); }); } @Test(name = "compute cells take inputs in the right order") public void testComputeCellsInTheRightOrder() { runTest(() -> { var first = React.inputCell(1); var second = React.inputCell(2); var output = React.computeCell(list -> list.get(0) + list.get(1) * 10, List.of(first, second)); assertIntEquals(21, output.getValue()); }); } @Test(name = "compute cells update value when dependencies are changed") public void testComputeCellsUpdateValueWhenDependenciesAreChanged() { runTest(() -> { var input = React.inputCell(1); var output = React.computeCell(list -> list.get(0) + 1, List.of(input)); input.setValue(3); assertIntEquals(4, output.getValue()); }); } @Test(name = "compute cells can depend on other compute cells") public void testComputeCellsCanDependOnOtherComputeCells() { runTest(() -> { var input = React.inputCell(1); var timesTwo = React.computeCell(list -> list.get(0) * 2, List.of(input)); var timesThirty = React.computeCell(list -> list.get(0) * 30, List.of(input)); var output = React.computeCell(list -> list.get(0) + list.get(1), List.of(timesTwo, timesThirty)); assertIntEquals(32, output.getValue()); input.setValue(3); assertIntEquals(96, output.getValue()); }); } @Test(name = "compute cells fire callbacks") public void testComputeCellsFireCallbacks() { runTest(() -> { var input = React.inputCell(1); var output = React.computeCell(list -> list.get(0) + 1, List.of(input)); var values = new java.util.ArrayList(); output.addCallback(values::add); input.setValue(3); assertListContainsExactly(values, 4); }); } @Test(name = "callback cells only fire on change") public void testCallbacksOnlyFireOnChange() { runTest(() -> { var input = React.inputCell(1); var output = React.computeCell(list -> list.get(0) < 3 ? 111 : 222, List.of(input)); var values = new java.util.ArrayList(); output.addCallback(values::add); input.setValue(2); assertListEmpty(values); input.setValue(4); assertListContainsExactly(values, 222); }); } @Test(name = "callbacks do not report already reported values") public void testCallbacksDoNotReportAlreadyReportedValues() { runTest(() -> { var input = React.inputCell(1); var output = React.computeCell(list -> list.get(0) + 1, List.of(input)); var values = new java.util.ArrayList(); output.addCallback(values::add); input.setValue(2); assertListContainsExactly(values, 3); values.clear(); input.setValue(3); assertListContainsExactly(values, 4); }); } @Test(name = "callbacks can fire from multiple cells") public void testCallbacksCanFireFromMultipleCells() { runTest(() -> { var input = React.inputCell(1); var plusOne = React.computeCell(list -> list.get(0) + 1, List.of(input)); var minusOne = React.computeCell(list -> list.get(0) - 1, List.of(input)); var values1 = new java.util.ArrayList(); plusOne.addCallback(values1::add); var values2 = new java.util.ArrayList(); minusOne.addCallback(values2::add); input.setValue(10); assertListContainsExactly(values1, 11); assertListContainsExactly(values2, 9); }); } @Test(name = "callbacks can be added and removed") public void testCallbacksCanBeAddedAndRemoved() { runTest(() -> { var input = React.inputCell(11); var output = React.computeCell(list -> list.get(0) + 1, List.of(input)); var values1 = new java.util.ArrayList(); Consumer callback1 = values1::add; output.addCallback(callback1); var values2 = new java.util.ArrayList(); output.addCallback(values2::add); input.setValue(31); assertListContainsExactly(values1, 32); assertListContainsExactly(values2, 32); values1.clear(); values2.clear(); output.removeCallback(callback1); var values3 = new java.util.ArrayList(); output.addCallback(values3::add); input.setValue(41); assertListEmpty(values1); assertListContainsExactly(values2, 42); assertListContainsExactly(values3, 42); }); } @Test(name = "removing a callback multiple times doesn't interfere with other callbacks") public void testRemovingACallbackMultipleTimesDoesntInterfereWithOtherCallbacks() { runTest(() -> { var input = React.inputCell(1); var output = React.computeCell(list -> list.get(0) + 1, List.of(input)); var values1 = new java.util.ArrayList(); Consumer callback1 = values1::add; output.addCallback(callback1); var values2 = new java.util.ArrayList(); Consumer callback2 = values2::add; output.addCallback(callback2); output.removeCallback(callback1); output.removeCallback(callback1); input.setValue(2); assertListContainsExactly(values2, 3); assertListEmpty(values1); }); } @Test(name = "callbacks should only be called once even if multiple dependencies change") public void testCallbacksShouldOnlyBeCalledOnceEvenIfMultipleDependenciesChange() { runTest(() -> { var input = React.inputCell(1); var plusOne = React.computeCell(list -> list.get(0) + 1, List.of(input)); var minusOne1 = React.computeCell(list -> list.get(0) - 1, List.of(input)); var minusOne2 = React.computeCell(list -> list.get(0) - 1, List.of(minusOne1)); var output = React.computeCell(list -> list.get(0) * list.get(1), List.of(plusOne, minusOne2)); var values = new java.util.ArrayList(); output.addCallback(values::add); input.setValue(4); assertListContainsExactly(values, 10); }); } @Test(name = "callbacks should not be called if dependencies change but output value doesn't change") public void testCallbacksShouldNotBeCalledIfDependenciesChangeButOutputValueDoesntChange() { runTest(() -> { var input = React.inputCell(1); var plusOne = React.computeCell(list -> list.get(0) + 1, List.of(input)); var minusOne = React.computeCell(list -> list.get(0) - 1, List.of(input)); var alwaysTwo = React.computeCell(list -> list.get(0) - list.get(1), List.of(plusOne, minusOne)); var values = new java.util.ArrayList(); alwaysTwo.addCallback(values::add); input.setValue(2); assertListEmpty(values); input.setValue(3); assertListEmpty(values); input.setValue(4); assertListEmpty(values); input.setValue(5); assertListEmpty(values); }); } 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 assertIntEquals(int expected, int actual) { if (expected != actual) { throw new AssertionError("Expected '" + expected + "' but got '" + actual + "'"); } } private void assertListContainsExactly(List actual, Integer... expected) { List expectedList = List.of(expected); if (actual == null || actual.size() != expectedList.size() || !actual.equals(expectedList)) { throw new AssertionError("Expected list " + expectedList + " but got " + actual); } } private void assertListEmpty(List actual) { if (actual != null && !actual.isEmpty()) { throw new AssertionError("Expected empty list but got " + actual); } } public int getResult() { int sum = testPoints.stream().mapToInt(Integer::intValue).sum(); return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum; } }