import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.util.Collections; import java.util.List; import static java.util.Arrays.*; import static org.testng.Assert.assertEquals; public class ComparatorAlgTest { List<Integer> list1, list2; @BeforeMethod public void init(){ list1 = asList(1,2); list2 = asList(Integer.MIN_VALUE, Integer.MAX_VALUE); } void check(String name){ System.out.println(name+" list1 = " + list1); System.out.println(name+" list2 = " + list2); assertEquals(list1, asList(1,2) ); assertEquals(list2, asList(Integer.MIN_VALUE, Integer.MAX_VALUE) ); } @Test public void testWrong() { Collections.sort(list1, (Integer a, Integer b) -> ab ); Collections.sort(list2, (Integer a, Integer b) -> ab ); check("wrong"); } @Test public void testFine() { Collections.sort(list1, (Integer a, Integer b) -> a.equals(b)? 0: a>b ? 1:-1 ); Collections.sort(list2, (Integer a, Integer b) -> a.equals(b)? 0: a>b ? 1:-1 ); check("fine"); } }
fine list1 = [1, 2]
fine list2 = [-2147483648, 2147483647]
wrong list1 = [1, 2]
wrong list2 = [2147483647, -2147483648]
Source: https://habr.com/ru/post/278329/
All Articles