Skip to content

Commit b596020

Browse files
Merge branch 'master' into #6336/enhancement/ComparisionLogicStandardization
2 parents bdd8085 + 2eb80fb commit b596020

File tree

5 files changed

+399
-25
lines changed

5 files changed

+399
-25
lines changed

src/main/java/com/thealgorithms/sorts/SimpleSort.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/test/java/com/thealgorithms/datastructures/bag/BagTest.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import static org.junit.jupiter.api.Assertions.assertTrue;
77

88
import com.thealgorithms.datastructures.bags.Bag;
9+
import java.util.ArrayList;
910
import java.util.Iterator;
11+
import java.util.List;
12+
import java.util.NoSuchElementException;
1013
import org.junit.jupiter.api.Test;
1114

1215
class BagTest {
@@ -156,4 +159,116 @@ void testIteratorWithDuplicates() {
156159
}
157160
assertEquals(3, count, "Iterator should traverse all 3 items including duplicates");
158161
}
162+
163+
@Test
164+
void testCollectionElements() {
165+
Bag<List<String>> bag = new Bag<>();
166+
List<String> list1 = new ArrayList<>();
167+
list1.add("a");
168+
list1.add("b");
169+
170+
List<String> list2 = new ArrayList<>();
171+
list2.add("c");
172+
173+
List<String> emptyList = new ArrayList<>();
174+
175+
bag.add(list1);
176+
bag.add(list2);
177+
bag.add(emptyList);
178+
bag.add(list1); // Duplicate
179+
180+
assertEquals(4, bag.size(), "Bag should contain 4 list elements");
181+
assertTrue(bag.contains(list1), "Bag should contain list1");
182+
assertTrue(bag.contains(list2), "Bag should contain list2");
183+
assertTrue(bag.contains(emptyList), "Bag should contain empty list");
184+
}
185+
186+
@Test
187+
void testIteratorConsistency() {
188+
Bag<String> bag = new Bag<>();
189+
bag.add("first");
190+
bag.add("second");
191+
bag.add("third");
192+
193+
// Multiple iterations should return same elements
194+
List<String> firstIteration = new ArrayList<>();
195+
for (String item : bag) {
196+
firstIteration.add(item);
197+
}
198+
199+
List<String> secondIteration = new ArrayList<>();
200+
for (String item : bag) {
201+
secondIteration.add(item);
202+
}
203+
204+
assertEquals(firstIteration.size(), secondIteration.size(), "Both iterations should have same size");
205+
assertEquals(3, firstIteration.size(), "First iteration should have 3 elements");
206+
assertEquals(3, secondIteration.size(), "Second iteration should have 3 elements");
207+
}
208+
209+
@Test
210+
void testMultipleIterators() {
211+
Bag<String> bag = new Bag<>();
212+
bag.add("item1");
213+
bag.add("item2");
214+
215+
Iterator<String> iter1 = bag.iterator();
216+
Iterator<String> iter2 = bag.iterator();
217+
218+
assertTrue(iter1.hasNext(), "First iterator should have next element");
219+
assertTrue(iter2.hasNext(), "Second iterator should have next element");
220+
221+
String first1 = iter1.next();
222+
String first2 = iter2.next();
223+
224+
org.junit.jupiter.api.Assertions.assertNotNull(first1, "First iterator should return non-null element");
225+
org.junit.jupiter.api.Assertions.assertNotNull(first2, "Second iterator should return non-null element");
226+
}
227+
228+
@Test
229+
void testIteratorHasNextConsistency() {
230+
Bag<String> bag = new Bag<>();
231+
bag.add("single");
232+
233+
Iterator<String> iter = bag.iterator();
234+
assertTrue(iter.hasNext(), "hasNext should return true");
235+
assertTrue(iter.hasNext(), "hasNext should still return true after multiple calls");
236+
237+
String item = iter.next();
238+
assertEquals("single", item, "Next should return the single item");
239+
240+
assertFalse(iter.hasNext(), "hasNext should return false after consuming element");
241+
assertFalse(iter.hasNext(), "hasNext should still return false");
242+
}
243+
244+
@Test
245+
void testIteratorNextOnEmptyBag() {
246+
Bag<String> bag = new Bag<>();
247+
Iterator<String> iter = bag.iterator();
248+
249+
assertFalse(iter.hasNext(), "hasNext should return false for empty bag");
250+
assertThrows(NoSuchElementException.class, iter::next, "next() should throw NoSuchElementException on empty bag");
251+
}
252+
253+
@Test
254+
void testBagOrderIndependence() {
255+
Bag<String> bag1 = new Bag<>();
256+
Bag<String> bag2 = new Bag<>();
257+
258+
// Add same elements in different order
259+
bag1.add("first");
260+
bag1.add("second");
261+
bag1.add("third");
262+
263+
bag2.add("third");
264+
bag2.add("first");
265+
bag2.add("second");
266+
267+
assertEquals(bag1.size(), bag2.size(), "Bags should have same size");
268+
269+
// Both bags should contain all elements
270+
assertTrue(bag1.contains("first") && bag2.contains("first"));
271+
assertTrue(bag1.contains("second") && bag2.contains("second"));
272+
assertTrue(bag1.contains("third") && bag2.contains("third"));
273+
}
159274
}

src/test/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnionTest.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,109 @@ public void testPathCompression() {
122122
assertEquals(root, node1);
123123
assertEquals(node1, node3.parent);
124124
}
125+
126+
@Test
127+
public void testUnionByRankSmallerToLarger() {
128+
DisjointSetUnion<Integer> dsu = new DisjointSetUnion<>();
129+
Node<Integer> node1 = dsu.makeSet(1);
130+
Node<Integer> node2 = dsu.makeSet(2);
131+
Node<Integer> node3 = dsu.makeSet(3);
132+
133+
// Create tree with node1 as root and rank 1
134+
dsu.unionSets(node1, node2);
135+
assertEquals(1, node1.rank);
136+
assertEquals(0, node2.rank);
137+
138+
// Union single node (rank 0) with tree (rank 1)
139+
// Smaller rank tree should attach to larger rank tree
140+
dsu.unionSets(node3, node1);
141+
assertEquals(node1, dsu.findSet(node3));
142+
assertEquals(1, node1.rank); // Rank should not increase
143+
}
144+
145+
@Test
146+
public void testUnionByRankEqualRanks() {
147+
DisjointSetUnion<Integer> dsu = new DisjointSetUnion<>();
148+
Node<Integer> node1 = dsu.makeSet(1);
149+
Node<Integer> node2 = dsu.makeSet(2);
150+
Node<Integer> node3 = dsu.makeSet(3);
151+
Node<Integer> node4 = dsu.makeSet(4);
152+
153+
// Create two trees of equal rank (1)
154+
dsu.unionSets(node1, node2);
155+
dsu.unionSets(node3, node4);
156+
assertEquals(1, node1.rank);
157+
assertEquals(1, node3.rank);
158+
159+
// Union two trees of equal rank
160+
dsu.unionSets(node1, node3);
161+
Node<Integer> root = dsu.findSet(node1);
162+
assertEquals(2, root.rank); // Rank should increase by 1
163+
}
164+
165+
@Test
166+
public void testLargeChainPathCompression() {
167+
DisjointSetUnion<Integer> dsu = new DisjointSetUnion<>();
168+
Node<Integer> node1 = dsu.makeSet(1);
169+
Node<Integer> node2 = dsu.makeSet(2);
170+
Node<Integer> node3 = dsu.makeSet(3);
171+
Node<Integer> node4 = dsu.makeSet(4);
172+
Node<Integer> node5 = dsu.makeSet(5);
173+
174+
// Create a long chain: 1 -> 2 -> 3 -> 4 -> 5
175+
dsu.unionSets(node1, node2);
176+
dsu.unionSets(node2, node3);
177+
dsu.unionSets(node3, node4);
178+
dsu.unionSets(node4, node5);
179+
180+
// Find from the deepest node
181+
Node<Integer> root = dsu.findSet(node5);
182+
183+
// Path compression should make all nodes point directly to root
184+
assertEquals(root, node5.parent);
185+
assertEquals(root, node4.parent);
186+
assertEquals(root, node3.parent);
187+
assertEquals(root, node2.parent);
188+
assertEquals(root, node1.parent);
189+
}
190+
191+
@Test
192+
public void testMultipleDisjointSets() {
193+
DisjointSetUnion<Integer> dsu = new DisjointSetUnion<>();
194+
Node<Integer> node1 = dsu.makeSet(1);
195+
Node<Integer> node2 = dsu.makeSet(2);
196+
Node<Integer> node3 = dsu.makeSet(3);
197+
Node<Integer> node4 = dsu.makeSet(4);
198+
Node<Integer> node5 = dsu.makeSet(5);
199+
Node<Integer> node6 = dsu.makeSet(6);
200+
201+
// Create two separate components
202+
dsu.unionSets(node1, node2);
203+
dsu.unionSets(node2, node3);
204+
205+
dsu.unionSets(node4, node5);
206+
dsu.unionSets(node5, node6);
207+
208+
// Verify they are separate
209+
assertEquals(dsu.findSet(node1), dsu.findSet(node2));
210+
assertEquals(dsu.findSet(node2), dsu.findSet(node3));
211+
assertEquals(dsu.findSet(node4), dsu.findSet(node5));
212+
assertEquals(dsu.findSet(node5), dsu.findSet(node6));
213+
214+
assertNotEquals(dsu.findSet(node1), dsu.findSet(node4));
215+
assertNotEquals(dsu.findSet(node3), dsu.findSet(node6));
216+
}
217+
218+
@Test
219+
public void testEmptyValues() {
220+
DisjointSetUnion<String> dsu = new DisjointSetUnion<>();
221+
Node<String> emptyNode = dsu.makeSet("");
222+
Node<String> nullNode = dsu.makeSet(null);
223+
224+
assertEquals(emptyNode, dsu.findSet(emptyNode));
225+
assertEquals(nullNode, dsu.findSet(nullNode));
226+
227+
dsu.unionSets(emptyNode, nullNode);
228+
assertEquals(dsu.findSet(emptyNode), dsu.findSet(nullNode));
229+
}
125230
}

0 commit comments

Comments
 (0)