/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/test/java/com/thealgorithms/greedyalgorithms/GaleShapleyTest.java
72 строки
3 KB
B Karthik
Add Gale-Shapley Algorithm and Tests (#5494)
02 окт 2024, 16:50
Не верифицирован
02 окт 2024, 16:50
e6f597a
Код
Авторство
О чём код?
package com.thealgorithms.greedyalgorithms; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; public class GaleShapleyTest { @Test public void testStableMatch() { Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); womenPrefs.put("A", new LinkedList<>(List.of("X", "Y", "Z"))); womenPrefs.put("B", new LinkedList<>(List.of("Y", "X", "Z"))); womenPrefs.put("C", new LinkedList<>(List.of("X", "Y", "Z"))); Map<String, LinkedList<String>> menPrefs = new HashMap<>(); menPrefs.put("X", new LinkedList<>(List.of("A", "B", "C"))); menPrefs.put("Y", new LinkedList<>(List.of("B", "A", "C"))); menPrefs.put("Z", new LinkedList<>(List.of("A", "B", "C"))); Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); Map<String, String> expected = new HashMap<>(); expected.put("A", "X"); expected.put("B", "Y"); expected.put("C", "Z"); assertEquals(expected, result); } @Test public void testSinglePair() { Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); womenPrefs.put("A", new LinkedList<>(List.of("X"))); Map<String, LinkedList<String>> menPrefs = new HashMap<>(); menPrefs.put("X", new LinkedList<>(List.of("A"))); Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); Map<String, String> expected = new HashMap<>(); expected.put("A", "X"); assertEquals(expected, result); } @Test public void testEqualPreferences() { Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); womenPrefs.put("A", new LinkedList<>(List.of("X", "Y", "Z"))); womenPrefs.put("B", new LinkedList<>(List.of("X", "Y", "Z"))); womenPrefs.put("C", new LinkedList<>(List.of("X", "Y", "Z"))); Map<String, LinkedList<String>> menPrefs = new HashMap<>(); menPrefs.put("X", new LinkedList<>(List.of("A", "B", "C"))); menPrefs.put("Y", new LinkedList<>(List.of("A", "B", "C"))); menPrefs.put("Z", new LinkedList<>(List.of("A", "B", "C"))); Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); Map<String, String> expected = new HashMap<>(); expected.put("A", "X"); expected.put("B", "Y"); expected.put("C", "Z"); assertEquals(expected, result); } }