/* * 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.lazy.primitive; import com.gs.collections.api.InternalIterable; import com.gs.collections.api.LazyIterable; import com.gs.collections.api.block.procedure.Procedure; import com.gs.collections.impl.block.factory.Procedures; import com.gs.collections.impl.list.mutable.FastList; import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; public class CollectBooleanToObjectIterableTest { private LazyIterable<Boolean> newPrimitiveWith(boolean... elements) { return new CollectBooleanToObjectIterable<>(BooleanArrayList.newListWith(elements), Boolean::valueOf); } @Test public void forEach() { InternalIterable<Boolean> select = this.newPrimitiveWith(true, false, true, false, true); Appendable builder = new StringBuilder(); Procedure<Boolean> appendProcedure = Procedures.append(builder); select.forEach(appendProcedure); Assert.assertEquals("truefalsetruefalsetrue", builder.toString()); } @Test public void forEachWithIndex() { InternalIterable<Boolean> select = this.newPrimitiveWith(true, false, true, false, true); StringBuilder builder = new StringBuilder(""); select.forEachWithIndex((object, index) -> { builder.append(object); builder.append(index); }); Assert.assertEquals("true0false1true2false3true4", builder.toString()); } @Test public void iterator() { InternalIterable<Boolean> select = this.newPrimitiveWith(true, false, true, false, true); StringBuilder builder = new StringBuilder(""); for (Boolean each : select) { builder.append(each); } Assert.assertEquals("truefalsetruefalsetrue", builder.toString()); } @Test public void forEachWith() { InternalIterable<Boolean> select = this.newPrimitiveWith(true, false, true, false, true); StringBuilder builder = new StringBuilder(""); select.forEachWith((each, aBuilder) -> aBuilder.append(each), builder); Assert.assertEquals("truefalsetruefalsetrue", builder.toString()); } @Test public void selectInstancesOf() { Assert.assertEquals( FastList.newListWith(true, false, true, false, true), this.newPrimitiveWith(true, false, true, false, true).selectInstancesOf(Boolean.class).toList()); } @Test public void sizeEmptyNotEmpty() { Verify.assertIterableSize(2, this.newPrimitiveWith(true, false)); Verify.assertIterableEmpty(this.newPrimitiveWith()); Assert.assertTrue(this.newPrimitiveWith(true, false).notEmpty()); } @Test(expected = UnsupportedOperationException.class) public void removeThrows() { this.newPrimitiveWith().iterator().remove(); } }