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 2 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
3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/set/SetOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.cactoos.Proc;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.And;

Expand Down Expand Up @@ -60,7 +61,7 @@ public SetOf(final T... array) {
public SetOf(final Iterable<T> src) {
super(() -> {
final Set<T> tmp = new HashSet<>();
new And(tmp::add, src)
new And((Proc<T>) tmp::add, src)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanifieiev I think it would be way better to use src.forEach(tmp::add), that's what Iterable is made for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@victornoel Sure, that one looks better. But just wondering what was the And(final Proc<X> proc, final Iterable<X> src) created for ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanifieiev I think it is here only for completeness. And is more about the logical assertion (which does not rely on side effect as the Proc version does).

I created #1226 in reaction to this discussion :)

.value();
return Collections.unmodifiableSet(tmp);
});
Expand Down
86 changes: 76 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,93 @@
*/
package org.cactoos.set;

import org.hamcrest.MatcherAssert;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Joined;
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 numbers",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanifieiev please, change "Must keep unique numbers" to "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 numbers",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanifieiev please, change "Must keep unique numbers" to "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 numbers",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanifieiev please, change "Must keep unique numbers" to "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 behaveAsSetWithOriginalMergedCollectionsWithDuplicates() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanifieiev please, add more tests for:

  • chars
  • double numbers
  • other type object different of primitives/boxed and string.

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();
}
}