|
| 1 | +package com.thealgorithms.greedyalgorithms; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.LinkedList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +public class GaleShapleyTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void testStableMatch() { |
| 15 | + Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); |
| 16 | + womenPrefs.put("A", new LinkedList<>(List.of("X", "Y", "Z"))); |
| 17 | + womenPrefs.put("B", new LinkedList<>(List.of("Y", "X", "Z"))); |
| 18 | + womenPrefs.put("C", new LinkedList<>(List.of("X", "Y", "Z"))); |
| 19 | + |
| 20 | + Map<String, LinkedList<String>> menPrefs = new HashMap<>(); |
| 21 | + menPrefs.put("X", new LinkedList<>(List.of("A", "B", "C"))); |
| 22 | + menPrefs.put("Y", new LinkedList<>(List.of("B", "A", "C"))); |
| 23 | + menPrefs.put("Z", new LinkedList<>(List.of("A", "B", "C"))); |
| 24 | + |
| 25 | + Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); |
| 26 | + |
| 27 | + Map<String, String> expected = new HashMap<>(); |
| 28 | + expected.put("A", "X"); |
| 29 | + expected.put("B", "Y"); |
| 30 | + expected.put("C", "Z"); |
| 31 | + |
| 32 | + assertEquals(expected, result); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testSinglePair() { |
| 37 | + Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); |
| 38 | + womenPrefs.put("A", new LinkedList<>(List.of("X"))); |
| 39 | + |
| 40 | + Map<String, LinkedList<String>> menPrefs = new HashMap<>(); |
| 41 | + menPrefs.put("X", new LinkedList<>(List.of("A"))); |
| 42 | + |
| 43 | + Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); |
| 44 | + |
| 45 | + Map<String, String> expected = new HashMap<>(); |
| 46 | + expected.put("A", "X"); |
| 47 | + |
| 48 | + assertEquals(expected, result); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testEqualPreferences() { |
| 53 | + Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); |
| 54 | + womenPrefs.put("A", new LinkedList<>(List.of("X", "Y", "Z"))); |
| 55 | + womenPrefs.put("B", new LinkedList<>(List.of("X", "Y", "Z"))); |
| 56 | + womenPrefs.put("C", new LinkedList<>(List.of("X", "Y", "Z"))); |
| 57 | + |
| 58 | + Map<String, LinkedList<String>> menPrefs = new HashMap<>(); |
| 59 | + menPrefs.put("X", new LinkedList<>(List.of("A", "B", "C"))); |
| 60 | + menPrefs.put("Y", new LinkedList<>(List.of("A", "B", "C"))); |
| 61 | + menPrefs.put("Z", new LinkedList<>(List.of("A", "B", "C"))); |
| 62 | + |
| 63 | + Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); |
| 64 | + |
| 65 | + Map<String, String> expected = new HashMap<>(); |
| 66 | + expected.put("A", "X"); |
| 67 | + expected.put("B", "Y"); |
| 68 | + expected.put("C", "Z"); |
| 69 | + |
| 70 | + assertEquals(expected, result); |
| 71 | + } |
| 72 | +} |
0 commit comments