/** * 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; public class CustomSetTest { private static final int MAX_POINTS = 100; private static final int TEST_CASES = 40; 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 = "Returns true if the set contains no elements") public void setsWithNoElementsAreEmpty() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.emptyList()); assertTrue(customSet.isEmpty()); }); } @Test(name = "sets with elements are not empty") public void setsWithElementsAreNotEmpty() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.singletonList('1')); assertFalse(customSet.isEmpty()); }); } @Test(name = "nothing is contained in an empty set") public void nothingIsContainedInAnEmptySet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.emptyList()); assertFalse(customSet.contains("1")); }); } @Test(name = "when the element is in the set") public void whenTheElementIsInTheSet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList(1, 2, 3)); assertTrue(customSet.contains(1)); }); } @Test(name = "when the element is not in the set") public void whenTheElementIsNotInTheSet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList('1', '2', '3')); assertFalse(customSet.contains('4')); }); } @Test(name = "empty set is a subset of another empty set") public void emptySetIsASubsetOfAnotherEmptySet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.emptyList()); CustomSet secondCustomSet = new CustomSet<>(Collections.emptyList()); assertTrue(customSet.isSubset(secondCustomSet)); }); } @Test(name = "empty set is a subset of non-empty set") public void emptySetIsASubsetOfNonEmptySet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.singletonList(1)); CustomSet secondCustomSet = new CustomSet<>(Collections.emptyList()); assertTrue(customSet.isSubset(secondCustomSet)); }); } @Test(name = "non-empty set is not a subset of empty set") public void nonEmptySetIsNotASubsetOfEmptySet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.emptyList()); CustomSet secondCustomSet = new CustomSet<>(Collections.singletonList('1')); assertFalse(customSet.isSubset(secondCustomSet)); }); } @Test(name = "set is a subset of set with exact same elements") public void setIsASubsetOfSetWithExactSameElements() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList("1", "2", "3")); CustomSet secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "3")); assertTrue(customSet.isSubset(secondCustomSet)); }); } @Test(name = "set is a subset of larger set with same elements") public void setIsASubsetOfLargerSetWithSameElements() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList(4, 1, 2, 3)); CustomSet 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") public void setIsNotASubsetOfSetThatDoesNotContainItsElements() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList('4', '1', '3')); CustomSet secondCustomSet = new CustomSet<>(Arrays.asList('1', '2', '3')); assertFalse(customSet.isSubset(secondCustomSet)); }); } @Test(name = "the empty set is disjoint with itself") public void theEmptySetIsDisjointWithItself() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.emptyList()); CustomSet secondCustomSet = new CustomSet<>(Collections.emptyList()); assertTrue(customSet.isDisjoint(secondCustomSet)); }); } @Test(name = "empty set is disjoint with non-empty set") public void emptySetIsDisjointWithNonEmptySet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.emptyList()); CustomSet secondCustomSet = new CustomSet<>(Collections.singletonList(1)); assertTrue(customSet.isDisjoint(secondCustomSet)); }); } @Test(name = "non-empty set is disjoint with empty set") public void nonEmptySetIsDisjointWithEmptySet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.singletonList('1')); CustomSet secondCustomSet = new CustomSet<>(Collections.emptyList()); assertTrue(customSet.isDisjoint(secondCustomSet)); }); } @Test(name = "sets are not disjoint if they share an element") public void setsAreNotDisjointIfTheyShareAnElement() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList("1", "2")); CustomSet secondCustomSet = new CustomSet<>(Arrays.asList("2", "3")); assertFalse(customSet.isDisjoint(secondCustomSet)); }); } @Test(name = "sets are disjoint if they share no elements") public void setsAreDisjointIfTheyShareNoElements() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList(1, 2)); CustomSet secondCustomSet = new CustomSet<>(Arrays.asList(3, 4)); assertTrue(customSet.isDisjoint(secondCustomSet)); }); } @Test(name = "empty sets are equal") public void emptySetsAreEqual() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.emptyList()); CustomSet secondCustomSet = new CustomSet<>(Collections.emptyList()); assertTrue(customSet.equals(secondCustomSet)); }); } @Test(name = "empty set is not equal to non-empty set") public void emptySetIsNotEqualToNonEmptySet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.emptyList()); CustomSet secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "3")); assertFalse(customSet.equals(secondCustomSet)); }); } @Test(name = "non-empty set is not equal to empty set") public void nonEmptySetIsNotEqualToEmptySet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList(1, 2, 3)); CustomSet secondCustomSet = new CustomSet<>(Collections.emptyList()); assertFalse(customSet.equals(secondCustomSet)); }); } @Test(name = "sets with the same elements are equal") public void setsWithTheSameElementsAreEqual() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList('1', '2')); CustomSet secondCustomSet = new CustomSet<>(Arrays.asList('2', '1')); assertTrue(customSet.equals(secondCustomSet)); }); } @Test(name = "sets with different elements are not equal") public void setsWithDifferentElementsAreNotEqual() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList("1", "2", "3")); CustomSet secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "4")); assertFalse(customSet.equals(secondCustomSet)); }); } @Test(name = "set is not equal to larger set with same elements") public void setIsNotEqualToLargerSetWithSameElements() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList("1", "2", "3")); CustomSet 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") public void secondSetWithDuplicatesIsEqualToFirstSet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Collections.singletonList("1")); CustomSet secondCustomSet = new CustomSet<>(Arrays.asList("1", "1")); assertTrue(customSet.equals(secondCustomSet)); }); } @Test(name = "difference removes all duplicates in the first set") public void firstSetWithDuplicatesIsEqualToSecondSet() { runTest(() -> { CustomSet customSet = new CustomSet<>(Arrays.asList("1", "1")); CustomSet secondCustomSet = new CustomSet<>(Collections.singletonList("1")); assertTrue(customSet.equals(secondCustomSet)); }); } @Test(name = "add to empty set") public void addToEmptySet() { runTest(() -> { int element = 3; CustomSet expected = new CustomSet<>( Collections.unmodifiableList(Collections.singletonList(element))); CustomSet actual = new CustomSet<>(Collections.emptyList()); actual.add(element); assertNotNull(actual); assertTrue(actual.equals(expected)); assertFalse(actual.isEmpty()); }); } @Test(name = "add to non-empty set") public void addToNonEmptySet() { runTest(() -> { char element = '3'; CustomSet expected = new CustomSet<>(Collections.unmodifiableList( Arrays.asList('1', '2', '3', '4'))); CustomSet 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") public void addingAnExistingElementDoesNotChangeTheSet() { runTest(() -> { String element = "3"; CustomSet expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "2", "3"))); CustomSet 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") public void intersectionOfTwoEmptySetsIsAnEmptySet() { runTest(() -> { CustomSet actual = new CustomSet(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") public void intersectionOfAnEmptySetAndNonEmptySetIsAnEmptySet() { runTest(() -> { CustomSet actual = new CustomSet(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") public void intersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet() { runTest(() -> { CustomSet 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") public void intersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet() { runTest(() -> { CustomSet 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") public void intersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements() { runTest(() -> { CustomSet expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList('2', '3'))); CustomSet 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") public void differenceOfTwoEmptySetsIsAnEmptySet() { runTest(() -> { CustomSet actual = new CustomSet(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") public void differenceOfAnEmptySetAndNonEmptySetIsAnEmptySet() { runTest(() -> { CustomSet actual = new CustomSet(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") public void differenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() { runTest(() -> { CustomSet expected = new CustomSet<>(Collections.unmodifiableList( Arrays.asList('1', '2', '3', '4'))); CustomSet 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") public void differenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet() { runTest(() -> { CustomSet expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "3"))); CustomSet 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") public void unionOfTwoEmptySetsIsAnEmptySet() { runTest(() -> { CustomSet actual = new CustomSet(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") public void unionOfAnEmptySetAndNonEmptySetIsTheNonEmptySet() { runTest(() -> { CustomSet expected = new CustomSet<>( Collections.unmodifiableList(Collections.singletonList('2'))); CustomSet actual = new CustomSet(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") public void unionOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() { runTest(() -> { CustomSet expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "3"))); CustomSet 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") public void unionOfTwoNonEmptySetsContainsAllUniqueElements() { runTest(() -> { CustomSet expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList(3, 2, 1))); CustomSet 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(); testPoints.add(SUCCESS_POINTS); } catch (Exception | AssertionError t) { TracingFramework.printError(t.getMessage()); testPoints.add(FAILURE_POINTS); } } 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 int getResult() { int sum = testPoints.stream().mapToInt(Integer::intValue).sum(); return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum; } }