Skip to content

Commit 0f28479

Browse files
committed
Collection Utils test cases
1 parent 7be367c commit 0f28479

File tree

2 files changed

+108
-24
lines changed

2 files changed

+108
-24
lines changed

src/com/deepak/data/structures/Utils/CollectionUtils.java

+15-24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* Utilities for collections i.e List
14+
* Note : Finding min, max and rotate can also be added here
1415
*
1516
* @author Deepak
1617
*/
@@ -121,11 +122,19 @@ public static <T> List<T> shuffle(List<T> list) {
121122
*/
122123
public static <T> List<T> swap(List<T> list, int minIndex, int maxIndex) {
123124
if (list != null && list.size() > 2 && maxIndex > minIndex) {
125+
List<T> updatedList = new ArrayList<>();
124126
T elementAtMinIndex = list.get(minIndex);
125127
T elementAtMaxIndex = list.get(maxIndex);
126-
list.add(minIndex, elementAtMaxIndex);
127-
list.add(maxIndex, elementAtMinIndex);
128-
return list;
128+
for (int i = 0; i < list.size(); i++) {
129+
if (i != minIndex && i != maxIndex) {
130+
updatedList.add(i, list.get(i));
131+
} else if (i == minIndex) {
132+
updatedList.add(i, elementAtMaxIndex);
133+
} else if (i == maxIndex) {
134+
updatedList.add(i, elementAtMinIndex);
135+
}
136+
}
137+
return updatedList;
129138
}
130139
return null;
131140
}
@@ -139,31 +148,13 @@ public static <T> List<T> swap(List<T> list, int minIndex, int maxIndex) {
139148
*/
140149
public static <T> List<T> fill(List<T> list, T element) {
141150
if (list != null && list.size() > 0) {
151+
List<T> updatedList = new ArrayList<>();
142152
for (int i = 0; i < list.size(); i++) {
143-
list.add(i, element);
153+
updatedList.add(element);
144154
}
145-
return list;
155+
return updatedList;
146156
}
147157
return null;
148158
}
149159

150-
/**
151-
* Method to find the minimum from the list
152-
*
153-
* @param list
154-
* @param comparator
155-
* @return {@link T}
156-
*/
157-
public static <T> T min(List<T> list, Comparator<T> comparator) {
158-
return null;
159-
}
160-
161-
public static <T> T max(List<T> list, Comparator<T> comparator) {
162-
return null;
163-
}
164-
165-
public static <T> List<T> rotate(List<T> list, int n) {
166-
return null;
167-
}
168-
169160
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,98 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* CollectionUtilsTest.java
4+
*/
15
package com.deepak.data.structures.Utils;
26

7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import org.junit.Assert;
12+
import org.junit.Test;
13+
14+
/**
15+
* Test cases for collection utility methods
16+
*
17+
* @author Deepak
18+
*/
319
public class CollectionUtilsTest {
420

21+
/**
22+
* Test case to check if a collection is empty
23+
*/
24+
@Test
25+
public void testIsEmpty() {
26+
List<String> list = new ArrayList<>();
27+
Assert.assertTrue(CollectionUtils.isEmpty(list));
28+
list.add("A");
29+
Assert.assertFalse(CollectionUtils.isEmpty(list));
30+
}
31+
32+
/**
33+
* Test cases for sort
34+
*/
35+
@Test
36+
public void testSort() {
37+
List<String> list = Arrays.asList("C", "A", "F", "B");
38+
list = CollectionUtils.sort(list);
39+
Assert.assertTrue(list.get(0) == "A");
40+
Assert.assertTrue(list.get(1) == "B");
41+
Assert.assertTrue(list.get(2) == "C");
42+
Assert.assertTrue(list.get(3) == "F");
43+
}
44+
45+
/**
46+
* Test case to reverse the collection
47+
*/
48+
@Test
49+
public void testReverse() {
50+
List<String> list = Arrays.asList("C", "A", "F", "B");
51+
list = CollectionUtils.reverse(list);
52+
Assert.assertTrue(list.get(0) == "B");
53+
Assert.assertTrue(list.get(1) == "F");
54+
Assert.assertTrue(list.get(2) == "A");
55+
Assert.assertTrue(list.get(3) == "C");
56+
}
57+
58+
/**
59+
* Test case to shuffle the collection
60+
*/
61+
@Test
62+
public void testShuffle() {
63+
List<String> list = Arrays.asList("C", "A", "F", "B");
64+
List<String> updatedList = CollectionUtils.reverse(list);
65+
Assert.assertTrue(list.size() == updatedList.size());
66+
}
67+
68+
/**
69+
* Method to test swapping of elements when index is given
70+
*/
71+
@Test
72+
public void testSwap() {
73+
List<String> list = Arrays.asList("C", "A", "F", "B");
74+
Assert.assertTrue(list.get(0) == "C");
75+
Assert.assertTrue(list.get(1) == "A");
76+
Assert.assertTrue(list.get(2) == "F");
77+
Assert.assertTrue(list.get(3) == "B");
78+
List<String> updatedList = CollectionUtils.swap(list, 1, 2);
79+
Assert.assertTrue(updatedList.get(0) == "C");
80+
Assert.assertTrue(updatedList.get(1) == "F");
81+
Assert.assertTrue(updatedList.get(2) == "A");
82+
Assert.assertTrue(updatedList.get(3) == "B");
83+
}
84+
85+
/**
86+
* Test case to check fill functionality
87+
*/
88+
@Test
89+
public void testFill() {
90+
List<String> list = Arrays.asList("C", "A", "F", "B");
91+
list = CollectionUtils.fill(list, "M");
92+
Assert.assertTrue(list.get(0) == "M");
93+
Assert.assertTrue(list.get(1) == "M");
94+
Assert.assertTrue(list.get(2) == "M");
95+
Assert.assertTrue(list.get(3) == "M");
96+
}
97+
598
}

0 commit comments

Comments
 (0)