Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1212) Fixed SetOf, which discarded all elements after duplicated occurrence #1215

Merged
merged 4 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/main/java/org/cactoos/set/SetOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.HashSet;
import java.util.Set;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.And;

/**
* Iterable as {@link Set}.
Expand Down Expand Up @@ -60,8 +59,7 @@ public SetOf(final T... array) {
public SetOf(final Iterable<T> src) {
super(() -> {
final Set<T> tmp = new HashSet<>();
new And(tmp::add, src)
.value();
src.forEach(tmp::add);
return Collections.unmodifiableSet(tmp);
});
}
Expand Down
146 changes: 136 additions & 10 deletions src/test/java/org/cactoos/set/SetOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,153 @@
*/
package org.cactoos.set;

import org.hamcrest.MatcherAssert;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
import org.cactoos.text.TextOf;
import org.hamcrest.Matcher;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.IsCollectionContaining;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasSize;

/**
* Test case for {@link SetOf}.
*
* @since 0.49.2
* @checkstyle MagicNumber (500 line)
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class SetOfTest {

/**
* Ensures that SetOf behaves as set, which means no duplicates.
*/
@Test
public void behavesAsSet() {
MatcherAssert.assertThat(
"Can't behave as a set",
public void behaveAsSetWithOriginalDuplicationsInTheTail() {
new Assertion<>(
"Must keep unique integer numbers",
new SetOf<>(1, 2, 2),
new BehavesAsSet<>(2)
);
new AllOf<>(
new IterableOf<Matcher<? super SetOf<Integer>>>(
new HasSize(2),
new IsCollectionContaining<>(new IsEqual<>(1)),
new IsCollectionContaining<>(new IsEqual<>(2))
)
)
).affirm();
}

@Test
public void behaveAsSetWithOriginalDuplicationsInTheHead() {
new Assertion<>(
"Must keep unique integer numbers",
new SetOf<>(1, 1, 2, 3),
new AllOf<>(
new IterableOf<Matcher<? super SetOf<Integer>>>(
new HasSize(3),
new IsCollectionContaining<>(new IsEqual<>(1)),
new IsCollectionContaining<>(new IsEqual<>(2)),
new IsCollectionContaining<>(new IsEqual<>(3))
)
)
).affirm();
}

@Test
public void behaveAsSetWithOriginalDuplicationsInTheMiddle() {
new Assertion<>(
"Must keep unique integer numbers",
new SetOf<>(1, 2, 2, 3),
new AllOf<>(
new IterableOf<Matcher<? super SetOf<Integer>>>(
new HasSize(3),
new IsCollectionContaining<>(new IsEqual<>(1)),
new IsCollectionContaining<>(new IsEqual<>(2)),
new IsCollectionContaining<>(new IsEqual<>(3))
)
)
).affirm();
}

@Test
public void behaveAsSetMergedCollectionsOfLiteralsWithDuplicates() {
new Assertion<>(
"Must keep unique string literals",
new SetOf<String>(
new Joined<String>(
new IterableOf<>("cc", "ff"),
new IterableOf<>("aa", "bb", "cc", "dd")
)
),
new AllOf<>(
new IterableOf<Matcher<? super SetOf<String>>>(
new HasSize(5),
new IsCollectionContaining<>(new IsEqual<>("aa")),
new IsCollectionContaining<>(new IsEqual<>("bb")),
new IsCollectionContaining<>(new IsEqual<>("cc")),
new IsCollectionContaining<>(new IsEqual<>("dd")),
new IsCollectionContaining<>(new IsEqual<>("ff"))
)
)
).affirm();
}

@Test
public void behaveAsSetWithOriginalDuplicationsOfCharsInTheMiddle() {
new Assertion<>(
"Must keep unique characters",
new SetOf<>('a', 'b', 'b', 'c', 'a', 'b', 'd'),
new AllOf<>(
new IterableOf<Matcher<? super SetOf<Character>>>(
new HasSize(4),
new IsCollectionContaining<>(new IsEqual<>('a')),
new IsCollectionContaining<>(new IsEqual<>('b')),
new IsCollectionContaining<>(new IsEqual<>('c')),
new IsCollectionContaining<>(new IsEqual<>('d'))
)
)
).affirm();
}

@Test
public void behaveAsSetWithOriginalDuplicationsOfDoublesInTheMiddle() {
new Assertion<>(
"Must keep unique double numbers",
new SetOf<>(1.5d, 2.4d, 1.5d, 2.4d, 2.4d, 1.5d),
new AllOf<>(
new IterableOf<Matcher<? super SetOf<Double>>>(
new HasSize(2),
new IsCollectionContaining<>(new IsEqual<>(1.5d)),
new IsCollectionContaining<>(new IsEqual<>(2.4d))
)
)
).affirm();
}

@Test
public void behaveAsSetWithOriginalDuplicationsOfTextsInTheMiddle() {
new Assertion<>(
"Must keep unique TextOf objects",
new SetOf<>(
new TextOf("12345"),
new TextOf("67890"),
new TextOf("12345"),
new TextOf("00000")
),
new AllOf<>(
new IterableOf<Matcher<? super SetOf<TextOf>>>(
new HasSize(3),
new IsCollectionContaining<>(
new IsEqual<>(new TextOf("12345"))
),
new IsCollectionContaining<>(
new IsEqual<>(new TextOf("67890"))
),
new IsCollectionContaining<>(
new IsEqual<>(new TextOf("00000"))
)
)
)
).affirm();
}
}