@Test public void rejectToTarget() { ImmutableBag<String> strings = this.newBag(); Assert.assertEquals( strings, strings.reject(Predicates.lessThan("0"), FastList.<String>newList()).toBag()); Verify.assertEmpty(strings.reject(Predicates.greaterThan("0"), FastList.<String>newList())); }
@Test public void injectIntoLong() { ImmutableBag<Integer> integers = this.newBag().collect(Integer::valueOf); long result = integers.injectInto(0, AddFunction.INTEGER_TO_LONG); Assert.assertEquals( FastList.newList(integers).injectInto(0, AddFunction.INTEGER_TO_INT), result); }
@Test public void sumFloat() { ImmutableBag<Integer> integers = this.newBag().collect(Integer::valueOf); double result = integers.sumOfFloat(Integer::floatValue); float expected = FastList.newList(integers).injectInto(0, AddFunction.INTEGER_TO_FLOAT); Assert.assertEquals(expected, result, 0.001); }
@Test public void reject() { ImmutableBag<String> strings = this.newBag(); Verify.assertIterableEmpty(strings.reject(Predicates.greaterThan("0"))); Assert.assertEquals(strings, strings.reject(Predicates.lessThan("0"))); Verify.assertIterableSize(strings.size() - 1, strings.reject(Predicates.lessThan("2"))); }
@Test public void sumDouble() { ImmutableBag<Integer> integers = this.newBag().collect(Integer::valueOf); double result = integers.sumOfDouble(Integer::doubleValue); double expected = FastList.newList(integers).injectInto(0, AddFunction.INTEGER_TO_DOUBLE); Assert.assertEquals(expected, result, 0.001); }
@Test public void forEachWithIndex() { MutableBag<String> result = Bags.mutable.of(); ImmutableBag<String> strings = this.newBag(); strings.forEachWithIndex(ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(result))); Assert.assertEquals(strings, result); }
@Test public void sumLong() { ImmutableBag<Integer> integers = this.newBag().collect(Integer::valueOf); long result = integers.sumOfLong(Integer::longValue); long expected = FastList.newList(integers).injectInto(0, AddFunction.INTEGER_TO_LONG); Assert.assertEquals(expected, result); }
@Test public void forEach() { MutableBag<String> result = Bags.mutable.of(); ImmutableBag<String> collection = this.newBag(); collection.forEach(CollectionAddProcedure.on(result)); Assert.assertEquals(collection, result); }
@Override @Test public void isEmpty() { ImmutableBag<String> bag = this.newBag(); Assert.assertTrue(bag.isEmpty()); Assert.assertFalse(bag.notEmpty()); }
@Test public void asLazy() { ImmutableBag<String> bag = this.newBag(); LazyIterable<String> lazyIterable = bag.asLazy(); Verify.assertInstanceOf(LazyIterable.class, lazyIterable); Assert.assertEquals(bag, lazyIterable.toBag()); }
@Test public void newWithAll() { ImmutableBag<String> bag = this.newBag(); ImmutableBag<String> newBag = bag.newWithAll(Bags.mutable.of("0")); Assert.assertNotEquals(bag, newBag); Assert.assertEquals(HashBag.newBag(bag).with("0"), newBag); Assert.assertEquals(newBag.size(), bag.size() + 1); }
@Test public void appendString_with_start_separator_end() { ImmutableBag<String> bag = this.newBag(); Appendable builder = new StringBuilder(); bag.appendString(builder, "[", ", ", "]"); Assert.assertEquals(bag.toString(), builder.toString()); }
@Test public void appendString_with_separator() { ImmutableBag<String> bag = this.newBag(); Appendable builder = new StringBuilder(); bag.appendString(builder, ", "); Assert.assertEquals(bag.toString(), '[' + builder.toString() + ']'); }
@Override @Test public void selectInstancesOf() { ImmutableBag<Number> numbers = Bags.immutable.of(); Assert.assertEquals(iBag(), numbers.selectInstancesOf(Integer.class)); Assert.assertEquals(iBag(), numbers.selectInstancesOf(Double.class)); Assert.assertEquals(iBag(), numbers.selectInstancesOf(Number.class)); }
@Test public void collectIfWithTarget() { ImmutableBag<String> strings = this.newBag(); Assert.assertEquals( strings, strings.collectIf( String.class::isInstance, Functions.getStringPassThru(), HashBag.<String>newBag())); }
@Test public void toSortedListBy() { MutableList<String> expected = this.newBag().toList(); Collections.sort(expected); ImmutableBag<String> immutableBag = this.newBag(); MutableList<String> sortedList = immutableBag.toSortedListBy(String::valueOf); Assert.assertEquals(expected, sortedList); }
@Test public void appendString() { ImmutableBag<String> bag = this.newBag(); Appendable builder = new StringBuilder(); bag.appendString(builder); Assert.assertEquals(FastList.newList(bag).makeString(), builder.toString()); }
@Test public void collectWith() { ImmutableBag<String> strings = this.newBag(); String argument = "thing"; Assert.assertEquals( strings, strings.collectWith(this.generateAssertingPassThroughFunction2(argument), argument)); }
@Test public void toSortedList() { ImmutableBag<String> strings = this.newBag(); MutableList<String> copy = FastList.newList(strings); MutableList<String> list = strings.toSortedList(Collections.<String>reverseOrder()); Assert.assertEquals(copy.sortThis(Collections.<String>reverseOrder()), list); MutableList<String> list2 = strings.toSortedList(); Assert.assertEquals(copy.sortThis(), list2); }
@Test public void injectInto() { ImmutableBag<Integer> integers = this.newBag().collect(Integer::valueOf); Integer result = integers.injectInto(0, AddFunction.INTEGER); Assert.assertEquals( FastList.newList(integers).injectInto(0, AddFunction.INTEGER_TO_INT), result.intValue()); String result1 = this.newBag().injectInto("0", String::concat); Assert.assertEquals(FastList.newList(this.newBag()).injectInto("0", String::concat), result1); }
@Test public void detectIfNone() { ImmutableBag<String> strings = this.newBag(); Function0<String> function = new PassThruFunction0<String>(String.valueOf(this.numKeys() + 1)); Assert.assertEquals("1", strings.detectIfNone("1"::equals, function)); Assert.assertEquals( String.valueOf(this.numKeys() + 1), strings.detectIfNone(String.valueOf(this.numKeys() + 1)::equals, function)); }
@Override @Test public void toSortedBagBy() { ImmutableBag<String> immutableBag = this.newBag(); MutableSortedBag<String> sortedBag = immutableBag.toSortedBagBy(String::valueOf); TreeBag<Object> expectedBag = TreeBag.newBag(Comparators.byFunction(String::valueOf)); Verify.assertSortedBagsEqual(expectedBag, sortedBag); }
@Test public void selectWithToTarget() { ImmutableBag<String> strings = this.newBag(); Assert.assertEquals( strings, strings .selectWith(Predicates2.<String>greaterThan(), "0", FastList.<String>newList()) .toBag()); }
@Test public void partition() { ImmutableBag<String> strings = this.newBag(); PartitionImmutableBag<String> partition = strings.partition(Predicates.greaterThan("0")); Assert.assertEquals(strings, partition.getSelected()); Verify.assertIterableEmpty(partition.getRejected()); Verify.assertIterableSize( strings.size() - 1, strings.partition(Predicates.greaterThan("1")).getSelected()); }
@Test public void newWithoutAll() { ImmutableBag<String> bag = this.newBag(); ImmutableBag<String> withoutAll = bag.newWithoutAll(UnifiedSet.newSet(this.newBag())); Assert.assertEquals(Bags.immutable.of(), withoutAll); ImmutableBag<String> newBag = bag.newWithAll(Lists.fixedSize.of("0", "0", "0")).newWithoutAll(Lists.fixedSize.of("0")); Assert.assertEquals(0, newBag.occurrencesOf("0")); }
@Test public void forEachWith() { MutableBag<String> result = Bags.mutable.of(); ImmutableBag<String> bag = this.newBag(); bag.forEachWith( (argument1, argument2) -> { result.add(argument1 + argument2); }, ""); Assert.assertEquals(bag, result); }
@Test public void collect_target() { ImmutableBag<String> strings = this.newBag(); HashBag<String> target = HashBag.<String>newBag(); HashBag<String> actual = strings.collect(Functions.getStringPassThru(), target); Assert.assertEquals(strings, actual); Assert.assertSame(target, actual); Assert.assertEquals( strings, strings.collect(Functions.getStringPassThru(), FastList.<String>newList()).toBag()); }
@Test public void toSortedBagBy_empty() { ImmutableBag<Integer> immutableBag = Bags.immutable.of(); Function<Integer, Integer> function = object -> object * -1; MutableSortedBag<Integer> sortedBag = immutableBag.toSortedBagBy(function); sortedBag.addOccurrences(1, 3); sortedBag.addOccurrences(10, 2); Verify.assertSortedBagsEqual( TreeBag.newBagWith(Comparators.byFunction(function), 10, 10, 1, 1, 1), sortedBag); }
@Test @Override public void newWith() { ImmutableBag<String> bag = this.newBag(); ImmutableBag<String> newBag = bag.newWith("1"); Assert.assertNotEquals(bag, newBag); Assert.assertEquals(newBag.size(), bag.size() + 1); ImmutableBag<String> newBag2 = bag.newWith("5"); Assert.assertNotEquals(bag, newBag2); Assert.assertEquals(newBag2.size(), bag.size() + 1); Assert.assertEquals(1, newBag2.sizeDistinct()); }
@Test public void detectWithIfNone() { ImmutableBag<String> immutableStrings = this.newBag(); Assert.assertEquals( "1", immutableStrings.detectWithIfNone( Object::equals, "1", new PassThruFunction0<String>("Not Found"))); Assert.assertEquals( "Not Found", immutableStrings.detectWithIfNone( Object::equals, "10000", new PassThruFunction0<String>("Not Found"))); }