@Test public void testForEachWithIndexToArrayUsingImmutableList() { Integer[] array = new Integer[200]; ImmutableList<Integer> list = Interval.oneTo(200).toList().toImmutable(); Assert.assertTrue(ArrayIterate.allSatisfy(array, Predicates.isNull())); ParallelIterate.forEachWithIndex(list, (each, index) -> array[index] = each, 10, 10); Assert.assertArrayEquals(array, list.toArray(new Integer[] {})); }
@Test public void serialization() { int size = COLLISIONS.size(); for (int i = 1; i < size; i++) { MutableSet<Integer> set = UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, SIZE) .withAll(COLLISIONS.subList(0, i)); Verify.assertPostSerializedEqualsAndHashCode(set); set.add(null); Verify.assertPostSerializedEqualsAndHashCode(set); } UnifiedSetWithHashingStrategy<Integer> nullBucketZero = UnifiedSetWithHashingStrategy.newSetWith( INTEGER_HASHING_STRATEGY, null, COLLISION_1, COLLISION_2); Verify.assertPostSerializedEqualsAndHashCode(nullBucketZero); UnifiedSetWithHashingStrategy<Integer> simpleSetWithNull = UnifiedSetWithHashingStrategy.newSetWith(INTEGER_HASHING_STRATEGY, null, 1, 2); Verify.assertPostSerializedEqualsAndHashCode(simpleSetWithNull); UnifiedSetWithHashingStrategy<Person> people = UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY, PEOPLE); Verify.assertPostSerializedEqualsAndHashCode(people); // Testing the hashingStrategy is serialized correctly by making sure it is still hashing by // last name Verify.assertSetsEqual(LAST_NAME_HASHED_SET.castToSet(), people.withAll(PEOPLE.castToList())); }
@Test public void put_with_hashingStrategy() { UnifiedSetWithHashingStrategy<Person> people = UnifiedSetWithHashingStrategy.newSet( HashingStrategies.nullSafeHashingStrategy(LAST_NAME_HASHING_STRATEGY), 2) .withAll(PEOPLE.castToList()); // Testing if element already exists, returns the instance in the set Assert.assertSame(JOHNSMITH, people.put(new Person("Anything", "Smith"))); Verify.assertSize(2, people); // Testing if the element doesn't exist, returns the element itself Person notInSet = new Person("Not", "inSet"); Assert.assertSame(notInSet, people.put(notInSet)); Verify.assertSize(3, people); // Testing putting a null to force a rehash Assert.assertNull(people.put(null)); Verify.assertSize(4, people); // Testing put throws NullPointerException if the hashingStrategy is not null safe Verify.assertThrows( NullPointerException.class, () -> { UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY).put(null); }); }
@Test public void get_with_hashingStrategy() { UnifiedSetWithHashingStrategy<Person> people = UnifiedSetWithHashingStrategy.newSet( HashingStrategies.nullSafeHashingStrategy(LAST_NAME_HASHING_STRATEGY), 2) .withAll(PEOPLE.castToList()); // Putting null then testing geting a null Verify.assertSize(3, people.with((Person) null)); Assert.assertNull(people.get(null)); // Testing it is getting the same reference Assert.assertSame(JOHNSMITH, people.get(JANESMITH)); Assert.assertSame(JOHNSMITH, people.get(JOHNSMITH)); Assert.assertSame(JOHNDOE, people.get(JANEDOE)); Assert.assertSame(JOHNDOE, people.get(JOHNDOE)); Assert.assertSame(JOHNSMITH, people.get(new Person("Anything", "Smith"))); Assert.assertNull(people.get(new Person("John", "NotHere"))); // Testing get throws NullPointerException if the hashingStrategy is not null safe Verify.assertThrows( NullPointerException.class, () -> { UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY).get(null); }); }
@Override @Test public void reject() { super.reject(); UnifiedSetWithHashingStrategy<Person> people = UnifiedSetWithHashingStrategy.newSet(LAST_NAME_HASHING_STRATEGY) .withAll(PEOPLE.castToList()); Verify.assertSetsEqual( UnifiedSet.newSetWith(JOHNSMITH), people.reject(each -> "Doe".equals(each.getLastName())).with(JANESMITH)); }
@Test public void testForEachImmutableList() { IntegerSum sum1 = new IntegerSum(0); ImmutableList<Integer> list1 = Lists.immutable.ofAll(ParallelIterateAcceptanceTest.createIntegerList(16)); ParallelIterate.forEach( list1, new SumProcedure(sum1), new SumCombiner(sum1), 1, list1.size() / 2); Assert.assertEquals(16, sum1.getSum()); IntegerSum sum2 = new IntegerSum(0); ImmutableList<Integer> list2 = Lists.immutable.ofAll(ParallelIterateAcceptanceTest.createIntegerList(7)); ParallelIterate.forEach(list2, new SumProcedure(sum2), new SumCombiner(sum2)); Assert.assertEquals(7, sum2.getSum()); IntegerSum sum3 = new IntegerSum(0); ImmutableList<Integer> list3 = Lists.immutable.ofAll(ParallelIterateAcceptanceTest.createIntegerList(15)); ParallelIterate.forEach( list3, new SumProcedure(sum3), new SumCombiner(sum3), 1, list3.size() / 2); Assert.assertEquals(15, sum3.getSum()); IntegerSum sum4 = new IntegerSum(0); ImmutableList<Integer> list4 = Lists.immutable.ofAll(ParallelIterateAcceptanceTest.createIntegerList(35)); ParallelIterate.forEach(list4, new SumProcedure(sum4), new SumCombiner(sum4)); Assert.assertEquals(35, sum4.getSum()); IntegerSum sum5 = new IntegerSum(0); ImmutableList<Integer> list5 = FastList.newList(list4).toImmutable(); ParallelIterate.forEach(list5, new SumProcedure(sum5), new SumCombiner(sum5)); Assert.assertEquals(35, sum5.getSum()); IntegerSum sum6 = new IntegerSum(0); ImmutableList<Integer> list6 = Lists.immutable.ofAll(ParallelIterateAcceptanceTest.createIntegerList(40)); ParallelIterate.forEach( list6, new SumProcedure(sum6), new SumCombiner(sum6), 1, list6.size() / 2); Assert.assertEquals(40, sum6.getSum()); IntegerSum sum7 = new IntegerSum(0); ImmutableList<Integer> list7 = FastList.newList(list6).toImmutable(); ParallelIterate.forEach( list7, new SumProcedure(sum7), new SumCombiner(sum7), 1, list6.size() / 2); Assert.assertEquals(40, sum7.getSum()); }