|
1 /* |
|
2 * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved. |
|
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 */ |
|
23 package org.graalvm.util.test; |
|
24 |
|
25 import java.util.ArrayList; |
|
26 import java.util.Arrays; |
|
27 import java.util.Collections; |
|
28 import java.util.Iterator; |
|
29 import java.util.List; |
|
30 import java.util.NoSuchElementException; |
|
31 |
|
32 import org.graalvm.util.CollectionsUtil; |
|
33 import org.junit.Assert; |
|
34 import org.junit.Test; |
|
35 |
|
36 public class CollectionUtilTest { |
|
37 |
|
38 private static int sum(Iterable<Integer> iterable) { |
|
39 int sum = 0; |
|
40 for (int i : iterable) { |
|
41 sum += i; |
|
42 } |
|
43 return sum; |
|
44 } |
|
45 |
|
46 private static int indexOf(Iterable<Integer> iterable, int element) { |
|
47 int index = 0; |
|
48 for (int i : iterable) { |
|
49 if (i == element) { |
|
50 return index; |
|
51 } |
|
52 index++; |
|
53 } |
|
54 return -1; |
|
55 } |
|
56 |
|
57 @Test |
|
58 public void testConcat() { |
|
59 List<Integer> a = Arrays.asList(1, 2); |
|
60 List<Integer> b = Arrays.asList(3, 4, 5); |
|
61 Assert.assertEquals(sum(CollectionsUtil.concat(a, b)), 15); |
|
62 Assert.assertEquals(sum(CollectionsUtil.concat(b, a)), 15); |
|
63 Assert.assertEquals(indexOf(CollectionsUtil.concat(a, b), 5), 4); |
|
64 Assert.assertEquals(indexOf(CollectionsUtil.concat(b, a), 5), 2); |
|
65 } |
|
66 |
|
67 @Test |
|
68 public void testMatch() { |
|
69 String[] array = {"a", "b", "c", "d", "e"}; |
|
70 Assert.assertTrue(CollectionsUtil.allMatch(array, s -> !s.isEmpty())); |
|
71 Assert.assertFalse(CollectionsUtil.allMatch(array, s -> !s.startsWith("c"))); |
|
72 Assert.assertFalse(CollectionsUtil.anyMatch(array, String::isEmpty)); |
|
73 Assert.assertTrue(CollectionsUtil.anyMatch(array, s -> s.startsWith("c"))); |
|
74 } |
|
75 |
|
76 @Test |
|
77 public void testFilterToList() { |
|
78 String[] array = {"a", "b", "", "d", "e"}; |
|
79 Assert.assertEquals(CollectionsUtil.filterToList(Arrays.asList(array), String::isEmpty).size(), 1); |
|
80 } |
|
81 |
|
82 @Test |
|
83 public void testFilterAndMapToArray() { |
|
84 String[] array = {"a", "b", "", "d", "e"}; |
|
85 String[] newArray = CollectionsUtil.filterAndMapToArray(array, s -> !s.isEmpty(), String::toUpperCase, String[]::new); |
|
86 Assert.assertArrayEquals(newArray, new String[]{"A", "B", "D", "E"}); |
|
87 } |
|
88 |
|
89 @Test |
|
90 public void testMapToArray() { |
|
91 String[] array = {"a", "b", "c", "d", "e"}; |
|
92 String[] newArray = CollectionsUtil.mapToArray(array, String::toUpperCase, String[]::new); |
|
93 Assert.assertArrayEquals(newArray, new String[]{"A", "B", "C", "D", "E"}); |
|
94 } |
|
95 |
|
96 @Test |
|
97 public void testMapAndJoin() { |
|
98 String[] array = {"a", "b", "c", "d", "e"}; |
|
99 Assert.assertEquals(CollectionsUtil.mapAndJoin(array, String::toUpperCase, ", "), "A, B, C, D, E"); |
|
100 Assert.assertEquals(CollectionsUtil.mapAndJoin(array, String::toUpperCase, ", ", "'"), "'A, 'B, 'C, 'D, 'E"); |
|
101 Assert.assertEquals(CollectionsUtil.mapAndJoin(array, String::toUpperCase, ", ", "'", "'"), "'A', 'B', 'C', 'D', 'E'"); |
|
102 |
|
103 Assert.assertEquals(CollectionsUtil.mapAndJoin(Arrays.asList(array), String::toUpperCase, ", "), "A, B, C, D, E"); |
|
104 Assert.assertEquals(CollectionsUtil.mapAndJoin(Arrays.asList(array), String::toUpperCase, ", ", "'"), "'A, 'B, 'C, 'D, 'E"); |
|
105 } |
|
106 |
|
107 @Test |
|
108 public void testIterableConcat() { |
|
109 List<String> i1 = Arrays.asList("1", "2", "3"); |
|
110 List<String> i2 = Arrays.asList(); |
|
111 List<String> i3 = Arrays.asList("4", "5"); |
|
112 List<String> i4 = Arrays.asList(); |
|
113 List<String> i5 = Arrays.asList("6"); |
|
114 List<String> iNull = null; |
|
115 |
|
116 List<String> actual = new ArrayList<>(); |
|
117 List<String> expected = new ArrayList<>(); |
|
118 expected.addAll(i1); |
|
119 expected.addAll(i2); |
|
120 expected.addAll(i3); |
|
121 expected.addAll(i4); |
|
122 expected.addAll(i5); |
|
123 Iterable<String> iterable = CollectionsUtil.concat(Arrays.asList(i1, i2, i3, i4, i5)); |
|
124 for (String s : iterable) { |
|
125 actual.add(s); |
|
126 } |
|
127 Assert.assertEquals(expected, actual); |
|
128 |
|
129 Iterator<String> iter = iterable.iterator(); |
|
130 while (iter.hasNext()) { |
|
131 iter.next(); |
|
132 } |
|
133 try { |
|
134 iter.next(); |
|
135 Assert.fail("Expected NoSuchElementException"); |
|
136 } catch (NoSuchElementException e) { |
|
137 // Expected |
|
138 } |
|
139 try { |
|
140 CollectionsUtil.concat(i1, iNull); |
|
141 Assert.fail("Expected NullPointerException"); |
|
142 } catch (NullPointerException e) { |
|
143 // Expected |
|
144 } |
|
145 |
|
146 Iterable<Object> emptyIterable = CollectionsUtil.concat(Collections.emptyList()); |
|
147 Assert.assertFalse(emptyIterable.iterator().hasNext()); |
|
148 } |
|
149 |
|
150 } |