feat: uploaded versions for jack
This commit is contained in:
@@ -0,0 +1,328 @@
|
||||
|
||||
/**
|
||||
* 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.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
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 ReactMain {
|
||||
|
||||
@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")
|
||||
@Disabled
|
||||
public void testInputCellValueCanBeSet() {
|
||||
runTest(() -> {
|
||||
var input = React.inputCell(4);
|
||||
input.setValue(20);
|
||||
assertIntEquals(20, input.getValue());
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "compute cells calculate initial value")
|
||||
@Disabled
|
||||
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")
|
||||
@Disabled
|
||||
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")
|
||||
@Disabled
|
||||
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")
|
||||
@Disabled
|
||||
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")
|
||||
@Disabled
|
||||
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<Integer>();
|
||||
output.addCallback(values::add);
|
||||
input.setValue(3);
|
||||
assertListContainsExactly(values, 4);
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "callback cells only fire on change")
|
||||
@Disabled
|
||||
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<Integer>();
|
||||
output.addCallback(values::add);
|
||||
input.setValue(2);
|
||||
assertListEmpty(values);
|
||||
input.setValue(4);
|
||||
assertListContainsExactly(values, 222);
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "callbacks do not report already reported values")
|
||||
@Disabled
|
||||
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<Integer>();
|
||||
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")
|
||||
@Disabled
|
||||
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<Integer>();
|
||||
plusOne.addCallback(values1::add);
|
||||
var values2 = new java.util.ArrayList<Integer>();
|
||||
minusOne.addCallback(values2::add);
|
||||
input.setValue(10);
|
||||
assertListContainsExactly(values1, 11);
|
||||
assertListContainsExactly(values2, 9);
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "callbacks can be added and removed")
|
||||
@Disabled
|
||||
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<Integer>();
|
||||
Consumer<Integer> callback1 = values1::add;
|
||||
output.addCallback(callback1);
|
||||
var values2 = new java.util.ArrayList<Integer>();
|
||||
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<Integer>();
|
||||
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")
|
||||
@Disabled
|
||||
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<Integer>();
|
||||
Consumer<Integer> callback1 = values1::add;
|
||||
output.addCallback(callback1);
|
||||
var values2 = new java.util.ArrayList<Integer>();
|
||||
Consumer<Integer> 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")
|
||||
@Disabled
|
||||
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<Integer>();
|
||||
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")
|
||||
@Disabled
|
||||
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<Integer>();
|
||||
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();
|
||||
System.out.println("TEST PASSED");
|
||||
} catch (Exception | AssertionError t) {
|
||||
System.out.println("TEST FAILED: " + t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void assertIntEquals(int expected, int actual) {
|
||||
if (expected != actual) {
|
||||
throw new AssertionError("Expected '" + expected + "' but got '" + actual + "'");
|
||||
}
|
||||
}
|
||||
|
||||
private void assertListContainsExactly(List<Integer> actual, Integer... expected) {
|
||||
List<Integer> 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<Integer> actual) {
|
||||
if (actual != null && !actual.isEmpty()) {
|
||||
throw new AssertionError("Expected empty list but got " + actual);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new ReactMain().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<Method> allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class))
|
||||
.collect(Collectors.toList());
|
||||
List<Method> 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user