2
* Copyright 2014 Goldman Sachs.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.gs.collections.impl.lazy.primitive;
19
import java.util.NoSuchElementException;
21
import com.gs.collections.api.CharIterable;
22
import com.gs.collections.api.LazyCharIterable;
23
import com.gs.collections.api.iterator.CharIterator;
24
import com.gs.collections.impl.bag.mutable.primitive.CharHashBag;
25
import com.gs.collections.impl.block.factory.PrimitiveFunctions;
26
import com.gs.collections.impl.block.factory.primitive.CharPredicates;
27
import com.gs.collections.impl.factory.Lists;
28
import com.gs.collections.impl.list.Interval;
29
import com.gs.collections.impl.list.mutable.FastList;
30
import com.gs.collections.impl.list.mutable.primitive.CharArrayList;
31
import com.gs.collections.impl.set.mutable.primitive.CharHashSet;
32
import com.gs.collections.impl.test.Verify;
33
import org.junit.Assert;
34
import org.junit.Test;
36
public class CollectCharIterableTest
38
private final CharIterable charIterable = Interval.oneTo(3).collectChar(PrimitiveFunctions.unboxIntegerToChar());
41
public void iterator()
44
CharIterator iterator = this.charIterable.charIterator();
45
while (iterator.hasNext())
47
sum += iterator.next();
49
Assert.assertEquals(6L, sum);
55
Assert.assertEquals(3L, this.charIterable.size());
61
Assert.assertTrue(this.charIterable.notEmpty());
62
Assert.assertFalse(this.charIterable.isEmpty());
68
char[] value = new char[1];
69
this.charIterable.forEach(each -> { value[0] += each; });
70
Assert.assertEquals(6, value[0]);
76
Assert.assertEquals(1, this.charIterable.count(CharPredicates.equal((char) 1)));
77
Assert.assertEquals(3, this.charIterable.count(CharPredicates.lessThan((char) 4)));
78
Assert.assertEquals(2, this.charIterable.count(CharPredicates.greaterThan((char) 1)));
82
public void anySatisfy()
84
Assert.assertTrue(this.charIterable.anySatisfy(CharPredicates.greaterThan((char) 1)));
85
Assert.assertTrue(this.charIterable.anySatisfy(CharPredicates.equal((char) 1)));
86
Assert.assertFalse(this.charIterable.anySatisfy(CharPredicates.greaterThan((char) 4)));
90
public void noneSatisfy()
92
Assert.assertFalse(this.charIterable.noneSatisfy(CharPredicates.greaterThan((char) 2)));
93
Assert.assertTrue(this.charIterable.noneSatisfy(CharPredicates.greaterThan((char) 4)));
97
public void allSatisfy()
99
Assert.assertTrue(this.charIterable.allSatisfy(CharPredicates.lessThan((char) 4)));
100
Assert.assertFalse(this.charIterable.allSatisfy(CharPredicates.lessThan((char) 3)));
106
Assert.assertEquals(3L, this.charIterable.select(CharPredicates.lessThan((char) 4)).size());
107
Assert.assertEquals(2L, this.charIterable.select(CharPredicates.lessThan((char) 3)).size());
113
Assert.assertEquals(0L, this.charIterable.reject(CharPredicates.lessThan((char) 4)).size());
114
Assert.assertEquals(1L, this.charIterable.reject(CharPredicates.lessThan((char) 3)).size());
118
public void detectIfNone()
120
Assert.assertEquals((char) 1, this.charIterable.detectIfNone(CharPredicates.lessThan((char) 4), (char) 0));
121
Assert.assertEquals((char) 0, this.charIterable.detectIfNone(CharPredicates.greaterThan((char) 3), (char) 0));
127
Assert.assertEquals(6L, this.charIterable.sum());
133
Assert.assertEquals((char) 3, Interval.oneTo(3).collectChar(PrimitiveFunctions.unboxIntegerToChar()).max());
139
Assert.assertEquals((char) 1, Interval.oneTo(3).collectChar(PrimitiveFunctions.unboxIntegerToChar()).min());
143
public void minIfEmpty()
145
Assert.assertEquals((char) 1, Interval.oneTo(3).collectChar(PrimitiveFunctions.unboxIntegerToChar()).minIfEmpty((char) 0));
146
Assert.assertEquals((char) 0, FastList.<Integer>newList().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).minIfEmpty((char) 0));
150
public void maxIfEmpty()
152
Assert.assertEquals((char) 3, Interval.oneTo(3).collectChar(PrimitiveFunctions.unboxIntegerToChar()).maxIfEmpty((char) 0));
153
Assert.assertEquals((char) 0, FastList.<Integer>newList().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).maxIfEmpty((char) 0));
156
@Test(expected = NoSuchElementException.class)
157
public void maxThrowsOnEmpty()
159
Lists.mutable.<Integer>of().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).max();
162
@Test(expected = NoSuchElementException.class)
163
public void minThrowsOnEmpty()
165
Lists.mutable.<Integer>of().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).min();
169
public void average()
171
Assert.assertEquals(2.5, Interval.oneTo(4).collectChar(PrimitiveFunctions.unboxIntegerToChar()).average(), 0.001);
174
@Test(expected = ArithmeticException.class)
175
public void averageThrowsOnEmpty()
177
Lists.mutable.<Integer>of().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).average();
183
Assert.assertEquals(2.5d, Interval.oneTo(4).collectChar(PrimitiveFunctions.unboxIntegerToChar()).median(), 0.001);
184
Assert.assertEquals(4.0d, Interval.oneTo(7).collectChar(PrimitiveFunctions.unboxIntegerToChar()).median(), 0.001);
187
@Test(expected = ArithmeticException.class)
188
public void medianThrowsOnEmpty()
190
Lists.mutable.<Integer>of().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).median();
194
public void toArray()
196
Assert.assertArrayEquals(new char[]{(char) 1, (char) 2, (char) 3, (char) 4},
197
Interval.oneTo(4).collectChar(PrimitiveFunctions.unboxIntegerToChar()).toArray());
201
public void toSortedArray()
203
Assert.assertArrayEquals(new char[]{(char) 1, (char) 2, (char) 3, (char) 4},
204
Interval.fromTo(4, 1).collectChar(PrimitiveFunctions.unboxIntegerToChar()).toSortedArray());
208
public void contains()
210
CharIterable charIterable = Interval.fromTo(4, 1).collectChar(PrimitiveFunctions.unboxIntegerToChar());
211
Assert.assertTrue(charIterable.contains((char) 1));
212
Assert.assertTrue(charIterable.contains((char) 3));
213
Assert.assertTrue(charIterable.contains((char) 4));
214
Assert.assertFalse(charIterable.contains((char) 5));
218
public void containsAllArray()
220
CharIterable charIterable = Interval.fromTo(4, 1).collectChar(PrimitiveFunctions.unboxIntegerToChar());
221
Assert.assertTrue(charIterable.containsAll((char) 1));
222
Assert.assertTrue(charIterable.containsAll((char) 1, (char) 2, (char) 3, (char) 4));
223
Assert.assertFalse(charIterable.containsAll((char) 1, (char) 2, (char) 3, (char) 4, (char) 5));
224
Assert.assertFalse(charIterable.containsAll((char) 7, (char) 6, (char) 5));
228
public void containsAllIterable()
230
CharIterable charIterable = Interval.fromTo(4, 1).collectChar(PrimitiveFunctions.unboxIntegerToChar());
231
Assert.assertTrue(charIterable.containsAll(CharArrayList.newListWith((char) 1)));
232
Assert.assertTrue(charIterable.containsAll(CharArrayList.newListWith((char) 1, (char) 2, (char) 3, (char) 4)));
233
Assert.assertFalse(charIterable.containsAll(CharArrayList.newListWith((char) 1, (char) 2, (char) 3, (char) 4, (char) 5)));
234
Assert.assertFalse(charIterable.containsAll(CharArrayList.newListWith((char) 7, (char) 6, (char) 5)));
238
public void collect()
240
Assert.assertEquals(FastList.newListWith("\u0001", "\u0002", "\u0003"), this.charIterable.collect(String::valueOf).toList());
244
public void testToString()
246
Assert.assertEquals("[\u0001, \u0002, \u0003]", this.charIterable.toString());
250
public void makeString()
252
Assert.assertEquals("\u0001, \u0002, \u0003", this.charIterable.makeString());
253
Assert.assertEquals("\u0001/\u0002/\u0003", this.charIterable.makeString("/"));
254
Assert.assertEquals("[\u0001, \u0002, \u0003]", this.charIterable.makeString("[", ", ", "]"));
258
public void appendString()
260
StringBuilder appendable = new StringBuilder();
261
this.charIterable.appendString(appendable);
262
Assert.assertEquals("\u0001, \u0002, \u0003", appendable.toString());
263
StringBuilder appendable2 = new StringBuilder();
264
this.charIterable.appendString(appendable2, "/");
265
Assert.assertEquals("\u0001/\u0002/\u0003", appendable2.toString());
266
StringBuilder appendable3 = new StringBuilder();
267
this.charIterable.appendString(appendable3, "[", ", ", "]");
268
Assert.assertEquals(this.charIterable.toString(), appendable3.toString());
274
Assert.assertEquals(CharArrayList.newListWith((char) 1, (char) 2, (char) 3), this.charIterable.toList());
278
public void toSortedList()
280
Assert.assertEquals(CharArrayList.newListWith((char) 1, (char) 2, (char) 3), this.charIterable.toSortedList());
286
Assert.assertEquals(CharHashSet.newSetWith((char) 1, (char) 2, (char) 3), this.charIterable.toSet());
292
Assert.assertEquals(CharHashBag.newBagWith((char) 1, (char) 2, (char) 3), this.charIterable.toBag());
298
Assert.assertEquals(this.charIterable.toSet(), this.charIterable.asLazy().toSet());
299
Verify.assertInstanceOf(LazyCharIterable.class, this.charIterable.asLazy());