package org.eclipse.viatra.solver.data.util; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static org.eclipse.viatra.solver.data.util.CollectionsUtil.*; import static org.junit.jupiter.api.Assertions.assertEquals; class CollectionsUtilTests { List list10 = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List listTen = List.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); private static void compare(Iterable a, Iterable b) { List listA = toList(a); List listB = toList(b); assertEquals(listA, listB); } private static List toList(Iterable a) { List result = new ArrayList(); Iterator iterator = a.iterator(); while (iterator.hasNext()) { result.add(iterator.next()); } return result; } @Test void testFilterEven() { compare(List.of(2, 4, 6, 8, 10), filter(list10, (x -> x % 2 == 0))); } @Test void testFilterOdd() { compare(List.of(1, 3, 5, 7, 9), filter(list10, (x -> x % 2 == 1))); } @Test void testFilterFalse() { compare(List.of(), filter(list10, (x -> false))); } @Test void testFilterTrue() { compare(list10, filter(list10, (x -> true))); } @Test void testFilterEmpty() { compare(List.of(), filter(List.of(), (x -> true))); } @Test() void testNoSuchElement() { Iterable iterable = filter(list10, (x -> x % 2 == 0)); Iterator iterator = iterable.iterator(); while (iterator.hasNext()) { iterator.next(); } Assertions.assertThrows(NoSuchElementException.class, () -> iterator.next()); } @Test() void mapTest() { compare(listTen, map(list10, x -> x.toString())); } @Test() void mapEmtyTest() { compare(List.of(), map(List.of(), x -> x.toString())); } }