Files
jack/custom-set/CustomSetMain.java
T

554 lines
22 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 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 CustomSetMain {
@Test(name = "Returns true if the set contains no elements")
public void setsWithNoElementsAreEmpty() {
runTest(() -> {
CustomSet<Integer> customSet = new CustomSet<>(Collections.emptyList());
assertTrue(customSet.isEmpty());
});
}
@Test(name = "sets with elements are not empty")
@Disabled
public void setsWithElementsAreNotEmpty() {
runTest(() -> {
CustomSet<Character> customSet = new CustomSet<>(Collections.singletonList('1'));
assertFalse(customSet.isEmpty());
});
}
@Test(name = "nothing is contained in an empty set")
@Disabled
public void nothingIsContainedInAnEmptySet() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Collections.emptyList());
assertFalse(customSet.contains("1"));
});
}
@Test(name = "when the element is in the set")
@Disabled
public void whenTheElementIsInTheSet() {
runTest(() -> {
CustomSet<Integer> customSet = new CustomSet<>(Arrays.asList(1, 2, 3));
assertTrue(customSet.contains(1));
});
}
@Test(name = "when the element is not in the set")
@Disabled
public void whenTheElementIsNotInTheSet() {
runTest(() -> {
CustomSet<Character> customSet = new CustomSet<>(Arrays.asList('1', '2', '3'));
assertFalse(customSet.contains('4'));
});
}
@Test(name = "empty set is a subset of another empty set")
@Disabled
public void emptySetIsASubsetOfAnotherEmptySet() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Collections.emptyList());
CustomSet<String> secondCustomSet = new CustomSet<>(Collections.emptyList());
assertTrue(customSet.isSubset(secondCustomSet));
});
}
@Test(name = "empty set is a subset of non-empty set")
@Disabled
public void emptySetIsASubsetOfNonEmptySet() {
runTest(() -> {
CustomSet<Integer> customSet = new CustomSet<>(Collections.singletonList(1));
CustomSet<Integer> secondCustomSet = new CustomSet<>(Collections.emptyList());
assertTrue(customSet.isSubset(secondCustomSet));
});
}
@Test(name = "non-empty set is not a subset of empty set")
@Disabled
public void nonEmptySetIsNotASubsetOfEmptySet() {
runTest(() -> {
CustomSet<Character> customSet = new CustomSet<>(Collections.emptyList());
CustomSet<Character> secondCustomSet = new CustomSet<>(Collections.singletonList('1'));
assertFalse(customSet.isSubset(secondCustomSet));
});
}
@Test(name = "set is a subset of set with exact same elements")
@Disabled
public void setIsASubsetOfSetWithExactSameElements() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
assertTrue(customSet.isSubset(secondCustomSet));
});
}
@Test(name = "set is a subset of larger set with same elements")
@Disabled
public void setIsASubsetOfLargerSetWithSameElements() {
runTest(() -> {
CustomSet<Integer> customSet = new CustomSet<>(Arrays.asList(4, 1, 2, 3));
CustomSet<Integer> secondCustomSet = new CustomSet<>(Arrays.asList(1, 2, 3));
assertTrue(customSet.isSubset(secondCustomSet));
});
}
@Test(name = "set is not a subset of set that does not contain its elements")
@Disabled
public void setIsNotASubsetOfSetThatDoesNotContainItsElements() {
runTest(() -> {
CustomSet<Character> customSet = new CustomSet<>(Arrays.asList('4', '1', '3'));
CustomSet<Character> secondCustomSet = new CustomSet<>(Arrays.asList('1', '2', '3'));
assertFalse(customSet.isSubset(secondCustomSet));
});
}
@Test(name = "the empty set is disjoint with itself")
@Disabled
public void theEmptySetIsDisjointWithItself() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Collections.emptyList());
CustomSet<String> secondCustomSet = new CustomSet<>(Collections.emptyList());
assertTrue(customSet.isDisjoint(secondCustomSet));
});
}
@Test(name = "empty set is disjoint with non-empty set")
@Disabled
public void emptySetIsDisjointWithNonEmptySet() {
runTest(() -> {
CustomSet<Integer> customSet = new CustomSet<>(Collections.emptyList());
CustomSet<Integer> secondCustomSet = new CustomSet<>(Collections.singletonList(1));
assertTrue(customSet.isDisjoint(secondCustomSet));
});
}
@Test(name = "non-empty set is disjoint with empty set")
@Disabled
public void nonEmptySetIsDisjointWithEmptySet() {
runTest(() -> {
CustomSet<Character> customSet = new CustomSet<>(Collections.singletonList('1'));
CustomSet<Character> secondCustomSet = new CustomSet<>(Collections.emptyList());
assertTrue(customSet.isDisjoint(secondCustomSet));
});
}
@Test(name = "sets are not disjoint if they share an element")
@Disabled
public void setsAreNotDisjointIfTheyShareAnElement() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "2"));
CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("2", "3"));
assertFalse(customSet.isDisjoint(secondCustomSet));
});
}
@Test(name = "sets are disjoint if they share no elements")
@Disabled
public void setsAreDisjointIfTheyShareNoElements() {
runTest(() -> {
CustomSet<Integer> customSet = new CustomSet<>(Arrays.asList(1, 2));
CustomSet<Integer> secondCustomSet = new CustomSet<>(Arrays.asList(3, 4));
assertTrue(customSet.isDisjoint(secondCustomSet));
});
}
@Test(name = "empty sets are equal")
@Disabled
public void emptySetsAreEqual() {
runTest(() -> {
CustomSet<Character> customSet = new CustomSet<>(Collections.emptyList());
CustomSet<Character> secondCustomSet = new CustomSet<>(Collections.emptyList());
assertTrue(customSet.equals(secondCustomSet));
});
}
@Test(name = "empty set is not equal to non-empty set")
@Disabled
public void emptySetIsNotEqualToNonEmptySet() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Collections.emptyList());
CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
assertFalse(customSet.equals(secondCustomSet));
});
}
@Test(name = "non-empty set is not equal to empty set")
@Disabled
public void nonEmptySetIsNotEqualToEmptySet() {
runTest(() -> {
CustomSet<Integer> customSet = new CustomSet<>(Arrays.asList(1, 2, 3));
CustomSet<Integer> secondCustomSet = new CustomSet<>(Collections.emptyList());
assertFalse(customSet.equals(secondCustomSet));
});
}
@Test(name = "sets with the same elements are equal")
@Disabled
public void setsWithTheSameElementsAreEqual() {
runTest(() -> {
CustomSet<Character> customSet = new CustomSet<>(Arrays.asList('1', '2'));
CustomSet<Character> secondCustomSet = new CustomSet<>(Arrays.asList('2', '1'));
assertTrue(customSet.equals(secondCustomSet));
});
}
@Test(name = "sets with different elements are not equal")
@Disabled
public void setsWithDifferentElementsAreNotEqual() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "4"));
assertFalse(customSet.equals(secondCustomSet));
});
}
@Test(name = "set is not equal to larger set with same elements")
@Disabled
public void setIsNotEqualToLargerSetWithSameElements() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "3", "4"));
assertFalse(customSet.equals(secondCustomSet));
});
}
@Test(name = "set is equal to a set constructed from an array with duplicates")
@Disabled
public void secondSetWithDuplicatesIsEqualToFirstSet() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Collections.singletonList("1"));
CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "1"));
assertTrue(customSet.equals(secondCustomSet));
});
}
@Test(name = "difference removes all duplicates in the first set")
@Disabled
public void firstSetWithDuplicatesIsEqualToSecondSet() {
runTest(() -> {
CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "1"));
CustomSet<String> secondCustomSet = new CustomSet<>(Collections.singletonList("1"));
assertTrue(customSet.equals(secondCustomSet));
});
}
@Test(name = "add to empty set")
@Disabled
public void addToEmptySet() {
runTest(() -> {
int element = 3;
CustomSet<Integer> expected = new CustomSet<>(
Collections.unmodifiableList(Collections.singletonList(element)));
CustomSet<Integer> actual = new CustomSet<>(Collections.emptyList());
actual.add(element);
assertNotNull(actual);
assertTrue(actual.equals(expected));
assertFalse(actual.isEmpty());
});
}
@Test(name = "add to non-empty set")
@Disabled
public void addToNonEmptySet() {
runTest(() -> {
char element = '3';
CustomSet<Character> expected = new CustomSet<>(
Collections.unmodifiableList(Arrays.asList('1', '2', '3', '4')));
CustomSet<Character> actual = new CustomSet<>(Arrays.asList('1', '2', '4'));
actual.add(element);
assertNotNull(actual);
assertTrue(actual.equals(expected));
assertFalse(actual.isEmpty());
});
}
@Test(name = "adding an existing element does not change the set")
@Disabled
public void addingAnExistingElementDoesNotChangeTheSet() {
runTest(() -> {
String element = "3";
CustomSet<String> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "2", "3")));
CustomSet<String> actual = new CustomSet<>(Arrays.asList("1", "2", "3"));
actual.add(element);
assertNotNull(actual);
assertTrue(actual.equals(expected));
});
}
@Test(name = "intersection of two empty sets is an empty set")
@Disabled
public void intersectionOfTwoEmptySetsIsAnEmptySet() {
runTest(() -> {
CustomSet<Integer> actual = new CustomSet<Integer>(Collections.emptyList())
.getIntersection(new CustomSet<>(Collections.emptyList()));
assertNotNull(actual);
assertTrue(actual.isEmpty());
});
}
@Test(name = "intersection of an empty set and non-empty set is an empty set")
@Disabled
public void intersectionOfAnEmptySetAndNonEmptySetIsAnEmptySet() {
runTest(() -> {
CustomSet<Character> actual = new CustomSet<Character>(Collections.emptyList())
.getIntersection(new CustomSet<>(Arrays.asList('3', '2', '5')));
assertNotNull(actual);
assertTrue(actual.isEmpty());
});
}
@Test(name = "intersection of a non-empty set and an empty set is an empty set")
@Disabled
public void intersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet() {
runTest(() -> {
CustomSet<String> actual = new CustomSet<>(Arrays.asList("1", "2", "3", "4"))
.getIntersection(new CustomSet<>(Collections.emptyList()));
assertNotNull(actual);
assertTrue(actual.isEmpty());
});
}
@Test(name = "intersection of two sets with no shared elements is an empty set")
@Disabled
public void intersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet() {
runTest(() -> {
CustomSet<Integer> actual = new CustomSet<>(Arrays.asList(1, 2, 3))
.getIntersection(new CustomSet<>(Arrays.asList(4, 5, 6)));
assertNotNull(actual);
assertTrue(actual.isEmpty());
});
}
@Test(name = "intersection of two sets with shared elements is a set of the shared elements")
@Disabled
public void intersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements() {
runTest(() -> {
CustomSet<Character> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList('2', '3')));
CustomSet<Character> actual = new CustomSet<>(Arrays.asList('1', '2', '3', '4'))
.getIntersection(new CustomSet<>(Arrays.asList('3', '2', '5')));
assertNotNull(actual);
assertFalse(actual.isEmpty());
assertTrue(actual.equals(expected));
});
}
@Test(name = "difference of two empty sets is an empty set")
@Disabled
public void differenceOfTwoEmptySetsIsAnEmptySet() {
runTest(() -> {
CustomSet<String> actual = new CustomSet<String>(Collections.emptyList())
.getDifference(new CustomSet<>(Collections.emptyList()));
assertNotNull(actual);
assertTrue(actual.isEmpty());
});
}
@Test(name = "difference of empty set and non-empty set is an empty set")
@Disabled
public void differenceOfAnEmptySetAndNonEmptySetIsAnEmptySet() {
runTest(() -> {
CustomSet<Integer> actual = new CustomSet<Integer>(Collections.emptyList())
.getDifference(new CustomSet<>(Arrays.asList(3, 2, 5)));
assertNotNull(actual);
assertTrue(actual.isEmpty());
});
}
@Test(name = "difference of a non-empty set and an empty set is the non-empty set")
@Disabled
public void differenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() {
runTest(() -> {
CustomSet<Character> expected = new CustomSet<>(
Collections.unmodifiableList(Arrays.asList('1', '2', '3', '4')));
CustomSet<Character> actual = new CustomSet<>(Arrays.asList('1', '2', '3', '4'))
.getDifference(new CustomSet<>(Collections.emptyList()));
assertNotNull(actual);
assertFalse(actual.isEmpty());
assertTrue(actual.equals(expected));
});
}
@Test(name = "difference of two non-empty sets is a set of elements that are only in the first set")
@Disabled
public void differenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet() {
runTest(() -> {
CustomSet<String> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "3")));
CustomSet<String> actual = new CustomSet<>(Arrays.asList("3", "2", "1"))
.getDifference(new CustomSet<>(Arrays.asList("2", "4")));
assertNotNull(actual);
assertFalse(actual.isEmpty());
assertTrue(actual.equals(expected));
});
}
@Test(name = "union of empty sets is an empty set")
@Disabled
public void unionOfTwoEmptySetsIsAnEmptySet() {
runTest(() -> {
CustomSet<Integer> actual = new CustomSet<Integer>(Collections.emptyList())
.getUnion(new CustomSet<>(Collections.emptyList()));
assertNotNull(actual);
assertTrue(actual.isEmpty());
});
}
@Test(name = "union of an empty set and non-empty set is the non-empty set")
@Disabled
public void unionOfAnEmptySetAndNonEmptySetIsTheNonEmptySet() {
runTest(() -> {
CustomSet<Character> expected = new CustomSet<>(
Collections.unmodifiableList(Collections.singletonList('2')));
CustomSet<Character> actual = new CustomSet<Character>(Collections.emptyList())
.getUnion(new CustomSet<>(Collections.singletonList('2')));
assertNotNull(actual);
assertFalse(actual.isEmpty());
assertTrue(actual.equals(expected));
});
}
@Test(name = "union of a non-empty set and empty set is the non-empty set")
@Disabled
public void unionOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() {
runTest(() -> {
CustomSet<String> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "3")));
CustomSet<String> actual = new CustomSet<>(Arrays.asList("1", "3"))
.getUnion(new CustomSet<>(Collections.emptyList()));
assertNotNull(actual);
assertFalse(actual.isEmpty());
assertTrue(actual.equals(expected));
});
}
@Test(name = "union of non-empty sets contains all unique elements")
@Disabled
public void unionOfTwoNonEmptySetsContainsAllUniqueElements() {
runTest(() -> {
CustomSet<Integer> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList(3, 2, 1)));
CustomSet<Integer> actual = new CustomSet<>(Arrays.asList(1, 3))
.getUnion(new CustomSet<>(Arrays.asList(2, 3)));
assertNotNull(actual);
assertFalse(actual.isEmpty());
assertTrue(actual.equals(expected));
});
}
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 assertTrue(boolean condition) {
if (!condition) {
throw new AssertionError("Expected condition to be true");
}
}
private void assertFalse(boolean condition) {
if (condition) {
throw new AssertionError("Expected condition to be false");
}
}
private void assertNotNull(Object value) {
if (value == null) {
throw new AssertionError("Expected value to be non-null");
}
}
public static void main(String[] args) {
new CustomSetMain().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.");
}
}
}