~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to unit-tests/src/test/java/com/gs/collections/impl/lazy/primitive/CollectCharIterableTest.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Goldman Sachs.
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
package com.gs.collections.impl.lazy.primitive;
 
18
 
 
19
import java.util.NoSuchElementException;
 
20
 
 
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;
 
35
 
 
36
public class CollectCharIterableTest
 
37
{
 
38
    private final CharIterable charIterable = Interval.oneTo(3).collectChar(PrimitiveFunctions.unboxIntegerToChar());
 
39
 
 
40
    @Test
 
41
    public void iterator()
 
42
    {
 
43
        long sum = 0;
 
44
        CharIterator iterator = this.charIterable.charIterator();
 
45
        while (iterator.hasNext())
 
46
        {
 
47
            sum += iterator.next();
 
48
        }
 
49
        Assert.assertEquals(6L, sum);
 
50
    }
 
51
 
 
52
    @Test
 
53
    public void size()
 
54
    {
 
55
        Assert.assertEquals(3L, this.charIterable.size());
 
56
    }
 
57
 
 
58
    @Test
 
59
    public void empty()
 
60
    {
 
61
        Assert.assertTrue(this.charIterable.notEmpty());
 
62
        Assert.assertFalse(this.charIterable.isEmpty());
 
63
    }
 
64
 
 
65
    @Test
 
66
    public void forEach()
 
67
    {
 
68
        char[] value = new char[1];
 
69
        this.charIterable.forEach(each -> { value[0] += each; });
 
70
        Assert.assertEquals(6, value[0]);
 
71
    }
 
72
 
 
73
    @Test
 
74
    public void count()
 
75
    {
 
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)));
 
79
    }
 
80
 
 
81
    @Test
 
82
    public void anySatisfy()
 
83
    {
 
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)));
 
87
    }
 
88
 
 
89
    @Test
 
90
    public void noneSatisfy()
 
91
    {
 
92
        Assert.assertFalse(this.charIterable.noneSatisfy(CharPredicates.greaterThan((char) 2)));
 
93
        Assert.assertTrue(this.charIterable.noneSatisfy(CharPredicates.greaterThan((char) 4)));
 
94
    }
 
95
 
 
96
    @Test
 
97
    public void allSatisfy()
 
98
    {
 
99
        Assert.assertTrue(this.charIterable.allSatisfy(CharPredicates.lessThan((char) 4)));
 
100
        Assert.assertFalse(this.charIterable.allSatisfy(CharPredicates.lessThan((char) 3)));
 
101
    }
 
102
 
 
103
    @Test
 
104
    public void select()
 
105
    {
 
106
        Assert.assertEquals(3L, this.charIterable.select(CharPredicates.lessThan((char) 4)).size());
 
107
        Assert.assertEquals(2L, this.charIterable.select(CharPredicates.lessThan((char) 3)).size());
 
108
    }
 
109
 
 
110
    @Test
 
111
    public void reject()
 
112
    {
 
113
        Assert.assertEquals(0L, this.charIterable.reject(CharPredicates.lessThan((char) 4)).size());
 
114
        Assert.assertEquals(1L, this.charIterable.reject(CharPredicates.lessThan((char) 3)).size());
 
115
    }
 
116
 
 
117
    @Test
 
118
    public void detectIfNone()
 
119
    {
 
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));
 
122
    }
 
123
 
 
124
    @Test
 
125
    public void sum()
 
126
    {
 
127
        Assert.assertEquals(6L, this.charIterable.sum());
 
128
    }
 
129
 
 
130
    @Test
 
131
    public void max()
 
132
    {
 
133
        Assert.assertEquals((char) 3, Interval.oneTo(3).collectChar(PrimitiveFunctions.unboxIntegerToChar()).max());
 
134
    }
 
135
 
 
136
    @Test
 
137
    public void min()
 
138
    {
 
139
        Assert.assertEquals((char) 1, Interval.oneTo(3).collectChar(PrimitiveFunctions.unboxIntegerToChar()).min());
 
140
    }
 
141
 
 
142
    @Test
 
143
    public void minIfEmpty()
 
144
    {
 
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));
 
147
    }
 
148
 
 
149
    @Test
 
150
    public void maxIfEmpty()
 
151
    {
 
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));
 
154
    }
 
155
 
 
156
    @Test(expected = NoSuchElementException.class)
 
157
    public void maxThrowsOnEmpty()
 
158
    {
 
159
        Lists.mutable.<Integer>of().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).max();
 
160
    }
 
161
 
 
162
    @Test(expected = NoSuchElementException.class)
 
163
    public void minThrowsOnEmpty()
 
164
    {
 
165
        Lists.mutable.<Integer>of().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).min();
 
166
    }
 
167
 
 
168
    @Test
 
169
    public void average()
 
170
    {
 
171
        Assert.assertEquals(2.5, Interval.oneTo(4).collectChar(PrimitiveFunctions.unboxIntegerToChar()).average(), 0.001);
 
172
    }
 
173
 
 
174
    @Test(expected = ArithmeticException.class)
 
175
    public void averageThrowsOnEmpty()
 
176
    {
 
177
        Lists.mutable.<Integer>of().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).average();
 
178
    }
 
179
 
 
180
    @Test
 
181
    public void median()
 
182
    {
 
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);
 
185
    }
 
186
 
 
187
    @Test(expected = ArithmeticException.class)
 
188
    public void medianThrowsOnEmpty()
 
189
    {
 
190
        Lists.mutable.<Integer>of().asLazy().collectChar(PrimitiveFunctions.unboxIntegerToChar()).median();
 
191
    }
 
192
 
 
193
    @Test
 
194
    public void toArray()
 
195
    {
 
196
        Assert.assertArrayEquals(new char[]{(char) 1, (char) 2, (char) 3, (char) 4},
 
197
                Interval.oneTo(4).collectChar(PrimitiveFunctions.unboxIntegerToChar()).toArray());
 
198
    }
 
199
 
 
200
    @Test
 
201
    public void toSortedArray()
 
202
    {
 
203
        Assert.assertArrayEquals(new char[]{(char) 1, (char) 2, (char) 3, (char) 4},
 
204
                Interval.fromTo(4, 1).collectChar(PrimitiveFunctions.unboxIntegerToChar()).toSortedArray());
 
205
    }
 
206
 
 
207
    @Test
 
208
    public void contains()
 
209
    {
 
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));
 
215
    }
 
216
 
 
217
    @Test
 
218
    public void containsAllArray()
 
219
    {
 
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));
 
225
    }
 
226
 
 
227
    @Test
 
228
    public void containsAllIterable()
 
229
    {
 
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)));
 
235
    }
 
236
 
 
237
    @Test
 
238
    public void collect()
 
239
    {
 
240
        Assert.assertEquals(FastList.newListWith("\u0001", "\u0002", "\u0003"), this.charIterable.collect(String::valueOf).toList());
 
241
    }
 
242
 
 
243
    @Test
 
244
    public void testToString()
 
245
    {
 
246
        Assert.assertEquals("[\u0001, \u0002, \u0003]", this.charIterable.toString());
 
247
    }
 
248
 
 
249
    @Test
 
250
    public void makeString()
 
251
    {
 
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("[", ", ", "]"));
 
255
    }
 
256
 
 
257
    @Test
 
258
    public void appendString()
 
259
    {
 
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());
 
269
    }
 
270
 
 
271
    @Test
 
272
    public void toList()
 
273
    {
 
274
        Assert.assertEquals(CharArrayList.newListWith((char) 1, (char) 2, (char) 3), this.charIterable.toList());
 
275
    }
 
276
 
 
277
    @Test
 
278
    public void toSortedList()
 
279
    {
 
280
        Assert.assertEquals(CharArrayList.newListWith((char) 1, (char) 2, (char) 3), this.charIterable.toSortedList());
 
281
    }
 
282
 
 
283
    @Test
 
284
    public void toSet()
 
285
    {
 
286
        Assert.assertEquals(CharHashSet.newSetWith((char) 1, (char) 2, (char) 3), this.charIterable.toSet());
 
287
    }
 
288
 
 
289
    @Test
 
290
    public void toBag()
 
291
    {
 
292
        Assert.assertEquals(CharHashBag.newBagWith((char) 1, (char) 2, (char) 3), this.charIterable.toBag());
 
293
    }
 
294
 
 
295
    @Test
 
296
    public void asLazy()
 
297
    {
 
298
        Assert.assertEquals(this.charIterable.toSet(), this.charIterable.asLazy().toSet());
 
299
        Verify.assertInstanceOf(LazyCharIterable.class, this.charIterable.asLazy());
 
300
    }
 
301
}