65 lines
1.7 KiB
Java
65 lines
1.7 KiB
Java
package org.usi.sq.util;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
public class CollectionUtilsTest {
|
|
|
|
private static <T extends Comparable<T>> void checkSort(List<T> source, List<T> expected) {
|
|
List<T> target = new ArrayList<>(source);
|
|
CollectionUtils.sort(target);
|
|
assertEquals(expected, target);
|
|
}
|
|
|
|
@SafeVarargs
|
|
private static <T> List<T> list(T... elements) {
|
|
return Arrays.asList(elements);
|
|
}
|
|
|
|
@Test
|
|
public void checkSorted() {
|
|
assertThrows(IllegalArgumentException.class, () -> CollectionUtils.sort(null));
|
|
checkSort(new ArrayList<Integer>(), new ArrayList<>());
|
|
checkSort(list(0, 1, 2, 3, 4), list(0, 1, 2, 3, 4));
|
|
checkSort(list(8, 4, 5, 6, 7), list(4, 5, 6, 7, 8));
|
|
checkSort(list(8, 7), list(7, 8));
|
|
}
|
|
|
|
@Test
|
|
public void checkStability() {
|
|
class DummyAllSame implements Comparable<DummyAllSame> {
|
|
@Override
|
|
public int compareTo(DummyAllSame o) {
|
|
return 0; // all are same
|
|
}
|
|
}
|
|
|
|
final DummyAllSame a = new DummyAllSame();
|
|
final DummyAllSame b = new DummyAllSame();
|
|
final DummyAllSame c = new DummyAllSame();
|
|
|
|
final List<DummyAllSame> list = Arrays.asList(a, b, c);
|
|
CollectionUtils.sort(list);
|
|
|
|
assertSame(a, list.get(0));
|
|
assertSame(b, list.get(1));
|
|
assertSame(c, list.get(2));
|
|
|
|
assertEquals(3, list.size());
|
|
}
|
|
|
|
@Test
|
|
public void checkConstructor() {
|
|
try {
|
|
new CollectionUtils();
|
|
} catch (final Exception ignored) {
|
|
fail(ignored);
|
|
}
|
|
}
|
|
}
|