48861
|
1 |
/*
|
52910
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
|
48861
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
50858
|
23 |
|
|
24 |
|
48861
|
25 |
package org.graalvm.util.test;
|
|
26 |
|
|
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.Arrays;
|
|
29 |
import java.util.Collections;
|
|
30 |
import java.util.Iterator;
|
|
31 |
import java.util.List;
|
|
32 |
import java.util.NoSuchElementException;
|
|
33 |
|
|
34 |
import org.graalvm.util.CollectionsUtil;
|
|
35 |
import org.junit.Assert;
|
|
36 |
import org.junit.Test;
|
|
37 |
|
|
38 |
public class CollectionUtilTest {
|
|
39 |
|
|
40 |
private static int sum(Iterable<Integer> iterable) {
|
|
41 |
int sum = 0;
|
|
42 |
for (int i : iterable) {
|
|
43 |
sum += i;
|
|
44 |
}
|
|
45 |
return sum;
|
|
46 |
}
|
|
47 |
|
|
48 |
private static int indexOf(Iterable<Integer> iterable, int element) {
|
|
49 |
int index = 0;
|
|
50 |
for (int i : iterable) {
|
|
51 |
if (i == element) {
|
|
52 |
return index;
|
|
53 |
}
|
|
54 |
index++;
|
|
55 |
}
|
|
56 |
return -1;
|
|
57 |
}
|
|
58 |
|
|
59 |
@Test
|
|
60 |
public void testConcat() {
|
|
61 |
List<Integer> a = Arrays.asList(1, 2);
|
|
62 |
List<Integer> b = Arrays.asList(3, 4, 5);
|
|
63 |
Assert.assertEquals(sum(CollectionsUtil.concat(a, b)), 15);
|
|
64 |
Assert.assertEquals(sum(CollectionsUtil.concat(b, a)), 15);
|
|
65 |
Assert.assertEquals(indexOf(CollectionsUtil.concat(a, b), 5), 4);
|
|
66 |
Assert.assertEquals(indexOf(CollectionsUtil.concat(b, a), 5), 2);
|
|
67 |
}
|
|
68 |
|
|
69 |
@Test
|
|
70 |
public void testMatch() {
|
|
71 |
String[] array = {"a", "b", "c", "d", "e"};
|
|
72 |
Assert.assertTrue(CollectionsUtil.allMatch(array, s -> !s.isEmpty()));
|
|
73 |
Assert.assertFalse(CollectionsUtil.allMatch(array, s -> !s.startsWith("c")));
|
|
74 |
Assert.assertFalse(CollectionsUtil.anyMatch(array, String::isEmpty));
|
|
75 |
Assert.assertTrue(CollectionsUtil.anyMatch(array, s -> s.startsWith("c")));
|
|
76 |
}
|
|
77 |
|
|
78 |
@Test
|
|
79 |
public void testFilterToList() {
|
|
80 |
String[] array = {"a", "b", "", "d", "e"};
|
|
81 |
Assert.assertEquals(CollectionsUtil.filterToList(Arrays.asList(array), String::isEmpty).size(), 1);
|
|
82 |
}
|
|
83 |
|
|
84 |
@Test
|
|
85 |
public void testFilterAndMapToArray() {
|
|
86 |
String[] array = {"a", "b", "", "d", "e"};
|
|
87 |
String[] newArray = CollectionsUtil.filterAndMapToArray(array, s -> !s.isEmpty(), String::toUpperCase, String[]::new);
|
|
88 |
Assert.assertArrayEquals(newArray, new String[]{"A", "B", "D", "E"});
|
|
89 |
}
|
|
90 |
|
|
91 |
@Test
|
|
92 |
public void testMapToArray() {
|
|
93 |
String[] array = {"a", "b", "c", "d", "e"};
|
|
94 |
String[] newArray = CollectionsUtil.mapToArray(array, String::toUpperCase, String[]::new);
|
|
95 |
Assert.assertArrayEquals(newArray, new String[]{"A", "B", "C", "D", "E"});
|
|
96 |
}
|
|
97 |
|
|
98 |
@Test
|
|
99 |
public void testMapAndJoin() {
|
|
100 |
String[] array = {"a", "b", "c", "d", "e"};
|
|
101 |
Assert.assertEquals(CollectionsUtil.mapAndJoin(array, String::toUpperCase, ", "), "A, B, C, D, E");
|
|
102 |
Assert.assertEquals(CollectionsUtil.mapAndJoin(array, String::toUpperCase, ", ", "'"), "'A, 'B, 'C, 'D, 'E");
|
|
103 |
Assert.assertEquals(CollectionsUtil.mapAndJoin(array, String::toUpperCase, ", ", "'", "'"), "'A', 'B', 'C', 'D', 'E'");
|
|
104 |
|
|
105 |
Assert.assertEquals(CollectionsUtil.mapAndJoin(Arrays.asList(array), String::toUpperCase, ", "), "A, B, C, D, E");
|
|
106 |
Assert.assertEquals(CollectionsUtil.mapAndJoin(Arrays.asList(array), String::toUpperCase, ", ", "'"), "'A, 'B, 'C, 'D, 'E");
|
|
107 |
}
|
|
108 |
|
|
109 |
@Test
|
|
110 |
public void testIterableConcat() {
|
|
111 |
List<String> i1 = Arrays.asList("1", "2", "3");
|
|
112 |
List<String> i2 = Arrays.asList();
|
|
113 |
List<String> i3 = Arrays.asList("4", "5");
|
|
114 |
List<String> i4 = Arrays.asList();
|
|
115 |
List<String> i5 = Arrays.asList("6");
|
|
116 |
List<String> iNull = null;
|
|
117 |
|
|
118 |
List<String> actual = new ArrayList<>();
|
|
119 |
List<String> expected = new ArrayList<>();
|
|
120 |
expected.addAll(i1);
|
|
121 |
expected.addAll(i2);
|
|
122 |
expected.addAll(i3);
|
|
123 |
expected.addAll(i4);
|
|
124 |
expected.addAll(i5);
|
|
125 |
Iterable<String> iterable = CollectionsUtil.concat(Arrays.asList(i1, i2, i3, i4, i5));
|
|
126 |
for (String s : iterable) {
|
|
127 |
actual.add(s);
|
|
128 |
}
|
|
129 |
Assert.assertEquals(expected, actual);
|
|
130 |
|
|
131 |
Iterator<String> iter = iterable.iterator();
|
|
132 |
while (iter.hasNext()) {
|
|
133 |
iter.next();
|
|
134 |
}
|
|
135 |
try {
|
|
136 |
iter.next();
|
|
137 |
Assert.fail("Expected NoSuchElementException");
|
|
138 |
} catch (NoSuchElementException e) {
|
|
139 |
// Expected
|
|
140 |
}
|
|
141 |
try {
|
|
142 |
CollectionsUtil.concat(i1, iNull);
|
|
143 |
Assert.fail("Expected NullPointerException");
|
|
144 |
} catch (NullPointerException e) {
|
|
145 |
// Expected
|
|
146 |
}
|
|
147 |
|
|
148 |
Iterable<Object> emptyIterable = CollectionsUtil.concat(Collections.emptyList());
|
|
149 |
Assert.assertFalse(emptyIterable.iterator().hasNext());
|
|
150 |
}
|
|
151 |
|
|
152 |
}
|