|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | + |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class DisjointSetTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void testAccountsMerge() { |
| 12 | + // Input data setup |
| 13 | + List<List<String>> list = new ArrayList<>(); |
| 14 | + list. add( new ArrayList<>( List. of( "abc", "[email protected]", "[email protected]"))); |
| 15 | + list. add( new ArrayList<>( List. of( "abc", "[email protected]", "[email protected]"))); |
| 16 | + list. add( new ArrayList<>( List. of( "Mary", "[email protected]"))); |
| 17 | + list. add( new ArrayList<>( List. of( "John", "[email protected]"))); |
| 18 | + list. add( new ArrayList<>( List. of( "John", "[email protected]", "[email protected]"))); |
| 19 | + |
| 20 | + // Create instance of your Solution/DisjointSet class |
| 21 | + DisjointSet disjointSet = new DisjointSet(list.size()); // or DisjointSet if that’s where accountsMerge() lives |
| 22 | + |
| 23 | + // Execute the method |
| 24 | + List<List<String>> result = disjointSet.accountsMerge(list); |
| 25 | + |
| 26 | + // Expected output (order of accounts may vary) |
| 27 | + List<List<String>> expected = new ArrayList<>(); |
| 28 | + |
| 29 | + expected. add( Arrays. asList( "Mary", "[email protected]")); |
| 30 | + expected. add( Arrays. asList( "John", "[email protected]", "[email protected]")); |
| 31 | + |
| 32 | + // Sort both results for deterministic comparison |
| 33 | + sortAccounts(result); |
| 34 | + sortAccounts(expected); |
| 35 | + |
| 36 | + // Verify results |
| 37 | + assertEquals(expected, result); |
| 38 | + } |
| 39 | + |
| 40 | + // Helper method to sort emails and accounts for deterministic comparison |
| 41 | + private static void sortAccounts(List<List<String>> accounts) { |
| 42 | + for (List<String> acc : accounts) { |
| 43 | + // Sort all emails (leave name first) |
| 44 | + List<String> emails = acc.subList(1, acc.size()); |
| 45 | + Collections.sort(emails); |
| 46 | + } |
| 47 | + // Sort accounts lexicographically by name |
| 48 | + accounts.sort(Comparator.comparing(a -> a.get(0))); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | + |
0 commit comments