Skip to content

Commit d753bd3

Browse files
BAEL-2725 Lists in Groovy
1 parent 192e49f commit d753bd3

1 file changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package com.baeldung.groovy.lists
2+
3+
import static groovy.test.GroovyAssert.*
4+
import org.junit.Test
5+
6+
class ListTest{
7+
8+
@Test
9+
void testCreateList() {
10+
11+
def list = [1, 2, 3]
12+
assertNotNull(list)
13+
14+
def listMix = ['A', "b", 1, true]
15+
assertTrue(listMix == ['A', "b", 1, true])
16+
17+
def linkedList = [1, 2, 3] as LinkedList
18+
assertTrue(linkedList instanceof LinkedList)
19+
20+
ArrayList arrList = [1, 2, 3]
21+
assertTrue(arrList.class == ArrayList)
22+
23+
def list2 = new ArrayList(arrList)
24+
assertTrue(list2 == arrList)
25+
26+
def list3 = arrList.clone()
27+
assertTrue(list3 == arrList)
28+
}
29+
30+
@Test
31+
void testCreateEmptyList() {
32+
33+
def emptyList = []
34+
assertTrue(emptyList.size() == 0)
35+
}
36+
37+
@Test
38+
void testCompareTwoLists() {
39+
40+
def list1 = [5, 6.0, 'p']
41+
def list2 = [5, 6.0, 'p']
42+
assertTrue(list1 == list2)
43+
}
44+
45+
@Test
46+
void testGetItemsFromList(){
47+
48+
def list = ["Hello", "World"]
49+
50+
assertTrue(list.get(1) == "World")
51+
assertTrue(list[1] == "World")
52+
assertTrue(list[-1] == "World")
53+
assertTrue(list.getAt(1) == "World")
54+
assertTrue(list.getAt(-2) == "Hello")
55+
}
56+
57+
@Test
58+
void testAddItemsToList() {
59+
60+
def list = []
61+
62+
list << 1
63+
list.add("Apple")
64+
assertTrue(list == [1, "Apple"])
65+
66+
list[2] = "Box"
67+
list[4] = true
68+
assertTrue(list == [1, "Apple", "Box", null, true])
69+
70+
list.add(1, 6.0)
71+
assertTrue(list == [1, 6.0, "Apple", "Box", null, true])
72+
73+
def list2 = [1, 2]
74+
list += list2
75+
list += 12
76+
assertTrue(list == [1, 6.0, "Apple", "Box", null, true, 1, 2, 12])
77+
}
78+
79+
@Test
80+
void testUpdateItemsInList() {
81+
82+
def list =[1, "Apple", 80, "App"]
83+
list[1] = "Box"
84+
list.set(2,90)
85+
assertTrue(list == [1, "Box", 90, "App"])
86+
}
87+
88+
@Test
89+
void testRemoveItemsFromList(){
90+
91+
def list = [1, 2, 3, 4, 5, 5, 6, 6, 7]
92+
93+
list.remove(3)
94+
assertTrue(list == [1, 2, 3, 5, 5, 6, 6, 7])
95+
96+
list.removeElement(5)
97+
assertTrue(list == [1, 2, 3, 5, 6, 6, 7])
98+
99+
assertTrue(list - 6 == [1, 2, 3, 5, 7])
100+
}
101+
102+
@Test
103+
void testIteratingOnAList(){
104+
105+
def list = [1, "App", 3, 4]
106+
list.each{ println it * 2}
107+
108+
list.eachWithIndex{ it, i -> println "$i : $it" }
109+
}
110+
111+
@Test
112+
void testCollectingToAnotherList(){
113+
114+
def list = ["Kay", "Henry", "Justin", "Tom"]
115+
assertTrue(list.collect{"Hi " + it} == ["Hi Kay", "Hi Henry", "Hi Justin", "Hi Tom"])
116+
}
117+
118+
@Test
119+
void testJoinItemsInAList(){
120+
assertTrue(["One", "Two", "Three"].join(",") == "One,Two,Three")
121+
}
122+
123+
@Test
124+
void testFilteringOnLists(){
125+
def list = [2, 1, 3, 4, 5, 6, 76]
126+
127+
assertTrue(list.find{it > 3} == 4)
128+
129+
assertTrue(list.findAll{it > 3} == [4, 5, 6, 76])
130+
131+
assertTrue(list.findAll{ it instanceof Number} == [2, 1, 3, 4, 5, 6, 76])
132+
133+
assertFalse(list.every{ it < 6})
134+
135+
assertTrue(list.any{ it%2 == 0})
136+
137+
assertTrue(list.grep( Number )== [2, 1, 3, 4, 5, 6, 76])
138+
139+
assertTrue(list.grep{ it> 6 }== [76])
140+
}
141+
142+
@Test
143+
void testGetUniqueItemsInAList(){
144+
assertTrue([1, 3, 3, 4].toUnique() == [1, 3, 4])
145+
146+
def list = [1, 3, 3, 4]
147+
list.unique()
148+
assertTrue(list == [1, 3, 4])
149+
150+
assertTrue(["A", "B", "Ba", "Bat", "Cat"].toUnique{ it.size()} == ["A", "Ba", "Bat"])
151+
}
152+
153+
@Test
154+
void testSorting(){
155+
156+
assertTrue([1, 2, 1, 0].sort() == [0, 1, 1, 2])
157+
Comparator mc = {a,b -> a == b? 0: a < b? 1 : -1}
158+
159+
def list = [1, 2, 1, 0]
160+
list.sort(mc)
161+
assertTrue(list == [2, 1, 1, 0])
162+
163+
def list1 = ["na", "ppp", "as"]
164+
assertTrue(list1.max() == "ppp")
165+
166+
Comparator minc = {a,b -> a == b? 0: a < b? -1 : 1}
167+
def list2 = [3, 2, 0, 7]
168+
assertTrue(list2.min(minc) == 0)
169+
}
170+
}

0 commit comments

Comments
 (0)