forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisjointSetTest.java
More file actions
52 lines (40 loc) · 1.98 KB
/
DisjointSetTest.java
File metadata and controls
52 lines (40 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.thealgorithms.graph;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.*;
public class DisjointSetTest {
@Test
public void testAccountsMerge() {
// Input data setup
List<List<String>> list = new ArrayList<>();
list.add(new ArrayList<>(List.of("abc", "abc@mail.com", "abx@mail.com")));
list.add(new ArrayList<>(List.of("abc", "abc@mail.com", "aby@mail.com")));
list.add(new ArrayList<>(List.of("Mary", "mary@mail.com")));
list.add(new ArrayList<>(List.of("John", "johnnybravo@mail.com")));
list.add(new ArrayList<>(List.of("John", "johnnybravo@mail.com", "john@mail.com")));
// Create instance of your Solution/DisjointSet class
DisjointSet disjointSet = new DisjointSet(list.size()); // or DisjointSet if that’s where accountsMerge() lives
// Execute the method
List<List<String>> result = disjointSet.accountsMerge(list);
// Expected output (order of accounts may vary)
List<List<String>> expected = new ArrayList<>();
expected.add(Arrays.asList("abc", "abc@mail.com", "abx@mail.com", "aby@mail.com"));
expected.add(Arrays.asList("Mary", "mary@mail.com"));
expected.add(Arrays.asList("John", "john@mail.com", "johnnybravo@mail.com"));
// Sort both results for deterministic comparison
sortAccounts(result);
sortAccounts(expected);
// Verify results
assertEquals(expected, result);
}
// Helper method to sort emails and accounts for deterministic comparison
private static void sortAccounts(List<List<String>> accounts) {
for (List<String> acc : accounts) {
// Sort all emails (leave name first)
List<String> emails = acc.subList(1, acc.size());
Collections.sort(emails);
}
// Sort accounts lexicographically by name
accounts.sort(Comparator.comparing(a -> a.get(0)));
}
}