/*
* Copyright 2014 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.impl.set.immutable;
import java.util.Collections;
import java.util.List;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.collection.ImmutableCollection;
import com.gs.collections.api.collection.MutableCollection;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.multimap.set.ImmutableSetMultimap;
import com.gs.collections.api.set.ImmutableSet;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.UnsortedSetIterable;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.factory.Functions;
import com.gs.collections.impl.block.factory.Predicates;
import com.gs.collections.impl.block.function.NegativeIntervalFunction;
import com.gs.collections.impl.block.procedure.CollectionAddProcedure;
import com.gs.collections.impl.collection.immutable.AbstractImmutableCollectionTestCase;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.factory.Sets;
import com.gs.collections.impl.list.Interval;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.multimap.set.UnifiedSetMultimap;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
public abstract class AbstractImmutableSetTestCase extends AbstractImmutableCollectionTestCase
{
@Override
protected abstract ImmutableSet<Integer> classUnderTest();
@Override
protected <T> MutableSet<T> newMutable()
{
return UnifiedSet.newSet();
}
@Test
public void equalsAndHashCode()
{
ImmutableSet<Integer> immutable = this.classUnderTest();
MutableSet<Integer> mutable = UnifiedSet.newSet(immutable);
Verify.assertEqualsAndHashCode(immutable, mutable);
Verify.assertPostSerializedEqualsAndHashCode(immutable);
Assert.assertNotEquals(immutable, FastList.newList(mutable));
}
@Test
public void newWith()
{
ImmutableSet<Integer> immutable = this.classUnderTest();
Assert.assertSame(immutable, immutable.newWith(immutable.size()));
Verify.assertSize(immutable.size() + 1, immutable.newWith(immutable.size() + 1).castToSet());
}
@Test
public void newWithout()
{
ImmutableSet<Integer> immutable = this.classUnderTest();
Verify.assertSize(Math.max(0, immutable.size() - 1), immutable.newWithout(immutable.size()).castToSet());
Verify.assertSize(immutable.size(), immutable.newWithout(immutable.size() + 1).castToSet());
}
@Test
public void newWithAll()
{
ImmutableSet<Integer> set = this.classUnderTest();
ImmutableSet<Integer> withAll = set.newWithAll(UnifiedSet.newSetWith(0));
Assert.assertNotEquals(set, withAll);
Assert.assertEquals(UnifiedSet.newSet(set).with(0), withAll);
}
@Test
public void newWithoutAll()
{
ImmutableSet<Integer> set = this.classUnderTest();
ImmutableSet<Integer> withoutAll = set.newWithoutAll(UnifiedSet.newSet(this.classUnderTest()));
Assert.assertEquals(Sets.immutable.<Integer>of(), withoutAll);
ImmutableSet<Integer> largeWithoutAll = set.newWithoutAll(Interval.fromTo(101, 150));
Assert.assertEquals(set, largeWithoutAll);
ImmutableSet<Integer> largeWithoutAll2 = set.newWithoutAll(UnifiedSet.newSet(Interval.fromTo(151, 199)));
Assert.assertEquals(set, largeWithoutAll2);
ImmutableSet<Integer> largeWithoutAll3 = set.newWithoutAll(FastList.newList(Interval.fromTo(151, 199)));
Assert.assertEquals(set, largeWithoutAll3);
}
@Test
public void contains()
{
ImmutableSet<Integer> set = this.classUnderTest();
for (int i = 1; i <= set.size(); i++)
{
Assert.assertTrue(set.contains(i));
}
Assert.assertFalse(set.contains(set.size() + 1));
}
@Test
public void containsAllArray()
{
Assert.assertTrue(this.classUnderTest().containsAllArguments(this.classUnderTest().toArray()));
}
@Test
public void containsAllIterable()
{
Assert.assertTrue(this.classUnderTest().containsAllIterable(this.classUnderTest()));
}
@Test
public void forEach()
{
MutableSet<Integer> result = UnifiedSet.newSet();
ImmutableSet<Integer> collection = this.classUnderTest();
collection.forEach(CollectionAddProcedure.on(result));
Assert.assertEquals(collection, result);
}
@Test
public void forEachWith()
{
MutableCollection<Integer> result = UnifiedSet.newSet();
this.classUnderTest().forEachWith((argument1, argument2) -> result.add(argument1 + argument2), 0);
Assert.assertEquals(this.classUnderTest(), result);
}
@Test
public void forEachWithIndex()
{
MutableCollection<Integer> result = UnifiedSet.newSet();
this.classUnderTest().forEachWithIndex((object, index) -> result.add(object));
Assert.assertEquals(this.classUnderTest(), result);
}
@Test
public void select_target()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(integers, integers.select(Predicates.lessThan(integers.size() + 1), UnifiedSet.<Integer>newSet()));
Verify.assertEmpty(integers.select(Predicates.greaterThan(integers.size()), FastList.<Integer>newList()));
}
@Test
public void reject_target()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Verify.assertEmpty(integers.reject(Predicates.lessThan(integers.size() + 1), FastList.<Integer>newList()));
Assert.assertEquals(integers, integers.reject(Predicates.greaterThan(integers.size()), UnifiedSet.<Integer>newSet()));
}
@Test
public void flatCollectWithTarget()
{
MutableCollection<String> actual = this.classUnderTest().flatCollect(integer -> Lists.fixedSize.of(String.valueOf(integer)), UnifiedSet.<String>newSet());
ImmutableCollection<String> expected = this.classUnderTest().collect(String::valueOf);
Assert.assertEquals(expected, actual);
}
@Test
public void zip()
{
ImmutableCollection<Integer> immutableCollection = this.classUnderTest();
List<Object> nulls = Collections.nCopies(immutableCollection.size(), null);
List<Object> nullsPlusOne = Collections.nCopies(immutableCollection.size() + 1, null);
List<Object> nullsMinusOne = Collections.nCopies(immutableCollection.size() - 1, null);
ImmutableCollection<Pair<Integer, Object>> pairs = immutableCollection.zip(nulls);
Assert.assertEquals(immutableCollection, pairs.collect((Function<Pair<Integer, ?>, Integer>) Pair::getOne));
Assert.assertEquals(UnifiedSet.newSet(nulls), pairs.collect((Function<Pair<?, Object>, Object>) Pair::getTwo));
ImmutableCollection<Pair<Integer, Object>> pairsPlusOne = immutableCollection.zip(nullsPlusOne);
Assert.assertEquals(immutableCollection, pairsPlusOne.collect((Function<Pair<Integer, ?>, Integer>) Pair::getOne));
Assert.assertEquals(UnifiedSet.newSet(nulls), pairsPlusOne.collect((Function<Pair<?, Object>, Object>) Pair::getTwo));
ImmutableCollection<Pair<Integer, Object>> pairsMinusOne = immutableCollection.zip(nullsMinusOne);
Assert.assertEquals(immutableCollection.size() - 1, pairsMinusOne.size());
Assert.assertTrue(immutableCollection.containsAllIterable(pairsMinusOne.collect((Function<Pair<Integer, ?>, Integer>) Pair::getOne)));
Assert.assertEquals(immutableCollection.zip(nulls), immutableCollection.zip(nulls, UnifiedSet.<Pair<Integer, Object>>newSet()));
}
@Test
public void zipWithIndex()
{
ImmutableCollection<Integer> immutableCollection = this.classUnderTest();
ImmutableCollection<Pair<Integer, Integer>> pairs = immutableCollection.zipWithIndex();
Assert.assertEquals(immutableCollection, pairs.collect((Function<Pair<Integer, ?>, Integer>) Pair::getOne));
Assert.assertEquals(
Interval.zeroTo(immutableCollection.size() - 1).toSet(),
pairs.collect((Function<Pair<?, Integer>, Integer>) Pair::getTwo));
Assert.assertEquals(
immutableCollection.zipWithIndex(),
immutableCollection.zipWithIndex(UnifiedSet.<Pair<Integer, Integer>>newSet()));
}
@Test
public void chunk_large_size()
{
Assert.assertEquals(this.classUnderTest(), this.classUnderTest().chunk(10).getFirst());
Verify.assertInstanceOf(ImmutableSet.class, this.classUnderTest().chunk(10).getFirst());
}
@Test
public void collectIfWithTarget()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
Assert.assertEquals(integers, integers.collectIf(Integer.class::isInstance, Functions.getIntegerPassThru(), UnifiedSet.<Integer>newSet()));
}
@Test
public void toList()
{
ImmutableCollection<Integer> integers = this.classUnderTest();
MutableList<Integer> list = integers.toList();
Verify.assertEqualsAndHashCode(FastList.newList(integers), list);
}
@Test
public void toSortedListBy()
{
ImmutableSet<Integer> integers = this.classUnderTest();
MutableList<Integer> list = integers.toSortedListBy(String::valueOf);
Assert.assertEquals(this.classUnderTest().toList(), list);
}
@Test
public void groupBy()
{
ImmutableSet<Integer> undertest = this.classUnderTest();
ImmutableSetMultimap<Integer, Integer> actual = undertest.groupBy(Functions.<Integer>getPassThru());
UnifiedSetMultimap<Integer, Integer> expected = UnifiedSet.newSet(undertest).groupBy(Functions.<Integer>getPassThru());
Assert.assertEquals(expected, actual);
}
@Test
public void groupByEach()
{
ImmutableSet<Integer> undertest = this.classUnderTest();
NegativeIntervalFunction function = new NegativeIntervalFunction();
ImmutableSetMultimap<Integer, Integer> actual = undertest.groupByEach(function);
UnifiedSetMultimap<Integer, Integer> expected = UnifiedSet.newSet(undertest).groupByEach(function);
Assert.assertEquals(expected, actual);
}
@Test
public void groupByWithTarget()
{
ImmutableSet<Integer> undertest = this.classUnderTest();
UnifiedSetMultimap<Integer, Integer> actual = undertest.groupBy(Functions.<Integer>getPassThru(), UnifiedSetMultimap.<Integer, Integer>newMultimap());
UnifiedSetMultimap<Integer, Integer> expected = UnifiedSet.newSet(undertest).groupBy(Functions.<Integer>getPassThru());
Assert.assertEquals(expected, actual);
}
@Test
public void groupByEachWithTarget()
{
ImmutableSet<Integer> undertest = this.classUnderTest();
NegativeIntervalFunction function = new NegativeIntervalFunction();
UnifiedSetMultimap<Integer, Integer> actual = undertest.groupByEach(function, UnifiedSetMultimap.<Integer, Integer>newMultimap());
UnifiedSetMultimap<Integer, Integer> expected = UnifiedSet.newSet(undertest).groupByEach(function);
Assert.assertEquals(expected, actual);
}
@Test
public void union()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
ImmutableSet<String> union = set.union(UnifiedSet.newSetWith("a", "b", "c", "1"));
Verify.assertSize(set.size() + 3, union);
Assert.assertTrue(union.containsAllIterable(Interval.oneTo(set.size()).collect(String::valueOf)));
Verify.assertContainsAll(union, "a", "b", "c");
Assert.assertEquals(set, set.union(UnifiedSet.newSetWith("1")));
}
@Test
public void unionInto()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
MutableSet<String> union = set.unionInto(UnifiedSet.newSetWith("a", "b", "c", "1"), UnifiedSet.<String>newSet());
Verify.assertSize(set.size() + 3, union);
Assert.assertTrue(union.containsAllIterable(Interval.oneTo(set.size()).collect(String::valueOf)));
Verify.assertContainsAll(union, "a", "b", "c");
Assert.assertEquals(set, set.unionInto(UnifiedSet.newSetWith("1"), UnifiedSet.<String>newSet()));
}
@Test
public void intersect()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
ImmutableSet<String> intersect = set.intersect(UnifiedSet.newSetWith("a", "b", "c", "1"));
Verify.assertSize(1, intersect);
Assert.assertEquals(UnifiedSet.newSetWith("1"), intersect);
Verify.assertIterableEmpty(set.intersect(UnifiedSet.newSetWith("not present")));
}
@Test
public void intersectInto()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
MutableSet<String> intersect = set.intersectInto(UnifiedSet.newSetWith("a", "b", "c", "1"), UnifiedSet.<String>newSet());
Verify.assertSize(1, intersect);
Assert.assertEquals(UnifiedSet.newSetWith("1"), intersect);
Verify.assertEmpty(set.intersectInto(UnifiedSet.newSetWith("not present"), UnifiedSet.<String>newSet()));
}
@Test
public void difference()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
ImmutableSet<String> difference = set.difference(UnifiedSet.newSetWith("2", "3", "4", "not present"));
Assert.assertEquals(UnifiedSet.newSetWith("1"), difference);
Assert.assertEquals(set, set.difference(UnifiedSet.newSetWith("not present")));
}
@Test
public void differenceInto()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
MutableSet<String> difference = set.differenceInto(UnifiedSet.newSetWith("2", "3", "4", "not present"), UnifiedSet.<String>newSet());
Assert.assertEquals(UnifiedSet.newSetWith("1"), difference);
Assert.assertEquals(set, set.differenceInto(UnifiedSet.newSetWith("not present"), UnifiedSet.<String>newSet()));
}
@Test
public void symmetricDifference()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
ImmutableSet<String> difference = set.symmetricDifference(UnifiedSet.newSetWith("2", "3", "4", "5", "not present"));
Verify.assertContains("1", difference);
Assert.assertTrue(difference.containsAllIterable(Interval.fromTo(set.size() + 1, 5).collect(String::valueOf)));
for (int i = 2; i <= set.size(); i++)
{
Verify.assertNotContains(String.valueOf(i), difference);
}
Verify.assertSize(set.size() + 1, set.symmetricDifference(UnifiedSet.newSetWith("not present")));
}
@Test
public void symmetricDifferenceInto()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
MutableSet<String> difference = set.symmetricDifferenceInto(
UnifiedSet.newSetWith("2", "3", "4", "5", "not present"),
UnifiedSet.<String>newSet());
Verify.assertContains("1", difference);
Assert.assertTrue(difference.containsAllIterable(Interval.fromTo(set.size() + 1, 5).collect(String::valueOf)));
for (int i = 2; i <= set.size(); i++)
{
Verify.assertNotContains(String.valueOf(i), difference);
}
Verify.assertSize(
set.size() + 1,
set.symmetricDifferenceInto(UnifiedSet.newSetWith("not present"), UnifiedSet.<String>newSet()));
}
@Test
public void isSubsetOf()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
Assert.assertTrue(set.isSubsetOf(UnifiedSet.newSetWith("1", "2", "3", "4", "5")));
}
@Test
public void isProperSubsetOf()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
Assert.assertTrue(set.isProperSubsetOf(UnifiedSet.newSetWith("1", "2", "3", "4", "5")));
Assert.assertFalse(set.isProperSubsetOf(set));
}
@Test
public void powerSet()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
ImmutableSet<UnsortedSetIterable<String>> powerSet = set.powerSet();
Verify.assertSize((int) StrictMath.pow(2, set.size()), powerSet);
Verify.assertContains(UnifiedSet.<String>newSet(), powerSet);
Verify.assertContains(set, powerSet);
Verify.assertInstanceOf(ImmutableSet.class, powerSet.getFirst());
Verify.assertInstanceOf(ImmutableSet.class, powerSet.getLast());
}
@Test
public void cartesianProduct()
{
ImmutableSet<String> set = this.classUnderTest().collect(String::valueOf);
LazyIterable<Pair<String, String>> cartesianProduct = set.cartesianProduct(UnifiedSet.newSetWith("One", "Two"));
Verify.assertIterableSize(set.size() * 2, cartesianProduct);
Assert.assertEquals(
set,
cartesianProduct
.select(Predicates.attributeEqual((Function<Pair<?, String>, String>) Pair::getTwo, "One"))
.collect((Function<Pair<String, ?>, String>) Pair::getOne).toSet());
}
@Test
public void toImmutable()
{
ImmutableSet<Integer> integers = this.classUnderTest();
ImmutableSet<Integer> actual = integers.toImmutable();
Assert.assertEquals(integers, actual);
Assert.assertSame(integers, actual);
}
}