Move cmp -> static CMP

This commit is contained in:
2021-09-03 11:00:13 +02:00
parent 0292a88ea5
commit 5661e3dbd2
2 changed files with 37 additions and 36 deletions

View File

@ -8,13 +8,12 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
import static aud.exam.prep.Tests.CMP;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue;
public abstract class SequenceProcessorTest<S> { public abstract class SequenceProcessorTest<S> {
protected final Comparator<Integer> cmp = Integer::compareTo;
protected final SequenceProcessor<Integer, S> processor; protected final SequenceProcessor<Integer, S> processor;
protected SequenceProcessorTest(SequenceProcessor<Integer, S> processor) { protected SequenceProcessorTest(SequenceProcessor<Integer, S> processor) {
@ -50,10 +49,10 @@ public abstract class SequenceProcessorTest<S> {
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(ListProvider.class) @ArgumentsSource(ListProvider.class)
void testThat_findBinaryOfNonExistsIsFalse(List<Integer> list) { void testThat_findBinaryOfNonExistsIsFalse(List<Integer> list) {
list.sort(cmp); list.sort(CMP);
S s = processor.create(list); S s = processor.create(list);
assertFalse(processor.findBinary(s, -1, cmp)); assertFalse(processor.findBinary(s, -1, CMP));
assertFalse(processor.findBinary(s, 999, cmp)); assertFalse(processor.findBinary(s, 999, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ -62,7 +61,7 @@ public abstract class SequenceProcessorTest<S> {
list.sort(Comparable::compareTo); list.sort(Comparable::compareTo);
S s = processor.create(list); S s = processor.create(list);
for (var n : list) { for (var n : list) {
assertTrue(processor.findBinary(s, n, cmp)); assertTrue(processor.findBinary(s, n, CMP));
} }
} }
@ -149,14 +148,14 @@ public abstract class SequenceProcessorTest<S> {
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(ListProvider.class) @ArgumentsSource(ListProvider.class)
void testThat_insertInOrderWorks(List<Integer> list) { void testThat_insertInOrderWorks(List<Integer> list) {
list.sort(cmp); list.sort(CMP);
S s = processor.create(list); S s = processor.create(list);
for (int i = -1; i < 110; i++) { for (int i = -1; i < 110; i++) {
processor.insertInOrder(s, i, cmp); processor.insertInOrder(s, i, CMP);
list.add(i); list.add(i);
list.sort(cmp); list.sort(CMP);
assertTrue(processor.check(s)); assertTrue(processor.check(s));
assertIterableEquals(list, processor.iterate(s)); assertIterableEquals(list, processor.iterate(s));
@ -224,15 +223,15 @@ public abstract class SequenceProcessorTest<S> {
list.add(list.size()/2, -1); list.add(list.size()/2, -1);
S s = processor.create(list); S s = processor.create(list);
assertFalse(processor.isAscending(s, cmp)); assertFalse(processor.isAscending(s, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(ListProvider.class) @ArgumentsSource(ListProvider.class)
void testThat_ifAscendingOfSortedIsTrue(List<Integer> list) { void testThat_ifAscendingOfSortedIsTrue(List<Integer> list) {
list.sort(cmp); list.sort(CMP);
S s = processor.create(list); S s = processor.create(list);
assertTrue(processor.isAscending(s, cmp)); assertTrue(processor.isAscending(s, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ -242,9 +241,9 @@ public abstract class SequenceProcessorTest<S> {
S s = processor.create(list); S s = processor.create(list);
var max = list var max = list
.stream() .stream()
.max(cmp) .max(CMP)
.orElseThrow(); .orElseThrow();
assertEquals(max, processor.max(s, cmp)); assertEquals(max, processor.max(s, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ -255,18 +254,18 @@ public abstract class SequenceProcessorTest<S> {
S s = processor.create(list); S s = processor.create(list);
var max = list var max = list
.stream() .stream()
.sorted(cmp.reversed()) .sorted(CMP.reversed())
.skip(1) .skip(1)
.findFirst() .findFirst()
.orElseThrow(); .orElseThrow();
assertEquals(max, processor.secondMax(s, cmp)); assertEquals(max, processor.secondMax(s, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(ListProvider.class) @ArgumentsSource(ListProvider.class)
void testThat_isItemWiseLessOrEqualOfSameIsTrue(List<Integer> list) { void testThat_isItemWiseLessOrEqualOfSameIsTrue(List<Integer> list) {
S s = processor.create(list); S s = processor.create(list);
assertTrue(processor.isItemWiseLessOrEqual(s, s, cmp)); assertTrue(processor.isItemWiseLessOrEqual(s, s, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ -281,10 +280,10 @@ public abstract class SequenceProcessorTest<S> {
} }
S b = processor.create(list); S b = processor.create(list);
assertTrue(processor.isItemWiseLessOrEqual(a, b, cmp)); assertTrue(processor.isItemWiseLessOrEqual(a, b, CMP));
assumeTrue(!list.isEmpty(), "List must not be empty to be less then"); assumeTrue(!list.isEmpty(), "List must not be empty to be less then");
assertFalse(processor.isItemWiseLessOrEqual(b, a, cmp)); assertFalse(processor.isItemWiseLessOrEqual(b, a, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ -297,15 +296,15 @@ public abstract class SequenceProcessorTest<S> {
} }
S b = processor.create(list); S b = processor.create(list);
assertTrue(processor.isItemWiseLessOrEqual(a, b, cmp)); assertTrue(processor.isItemWiseLessOrEqual(a, b, CMP));
assertFalse(processor.isItemWiseLessOrEqual(b, a, cmp)); assertFalse(processor.isItemWiseLessOrEqual(b, a, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(ListProvider.class) @ArgumentsSource(ListProvider.class)
void testThat_isItemWiseLessOfSameIsFalse(List<Integer> list) { void testThat_isItemWiseLessOfSameIsFalse(List<Integer> list) {
S s = processor.create(list); S s = processor.create(list);
assertFalse(processor.isItemWiseLess(s, s, cmp)); assertFalse(processor.isItemWiseLess(s, s, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ -321,8 +320,8 @@ public abstract class SequenceProcessorTest<S> {
} }
S b = processor.create(list); S b = processor.create(list);
assertTrue(processor.isItemWiseLess(a, b, cmp)); assertTrue(processor.isItemWiseLess(a, b, CMP));
assertFalse(processor.isItemWiseLess(b, a, cmp)); assertFalse(processor.isItemWiseLess(b, a, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ -341,15 +340,15 @@ public abstract class SequenceProcessorTest<S> {
} }
S b = processor.create(list); S b = processor.create(list);
assertTrue(processor.isItemWiseLess(a, b, cmp)); assertTrue(processor.isItemWiseLess(a, b, CMP));
assertFalse(processor.isItemWiseLess(b, a, cmp)); assertFalse(processor.isItemWiseLess(b, a, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(ListProvider.class) @ArgumentsSource(ListProvider.class)
void testThat_isLexSmallerOfSameIfFalse(List<Integer> list) { void testThat_isLexSmallerOfSameIfFalse(List<Integer> list) {
S s = processor.create(list); S s = processor.create(list);
assertFalse(processor.isLexSmaller(s, s, cmp)); assertFalse(processor.isLexSmaller(s, s, CMP));
} }
@ParameterizedTest @ParameterizedTest
@ -360,11 +359,11 @@ public abstract class SequenceProcessorTest<S> {
assertEquals( assertEquals(
stringA.compareTo(stringB) < 0, stringA.compareTo(stringB) < 0,
processor.isLexSmaller(a, b, cmp)); processor.isLexSmaller(a, b, CMP));
assertEquals( assertEquals(
stringB.compareTo(stringA) < 0, stringB.compareTo(stringA) < 0,
processor.isLexSmaller(b, a, cmp)); processor.isLexSmaller(b, a, CMP));
} }
protected S toSequenceByAscii(String string) { protected S toSequenceByAscii(String string) {
@ -536,16 +535,16 @@ public abstract class SequenceProcessorTest<S> {
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(DoubleListProvider.class) @ArgumentsSource(DoubleListProvider.class)
void testThat_mergeWorks(List<Integer> listA, List<Integer> listB) { void testThat_mergeWorks(List<Integer> listA, List<Integer> listB) {
listA.sort(cmp); listA.sort(CMP);
S a = processor.create(listA); S a = processor.create(listA);
listB.sort(cmp); listB.sort(CMP);
S b = processor.create(listB); S b = processor.create(listB);
listA.addAll(listB); listA.addAll(listB);
listA.sort(cmp); listA.sort(CMP);
S s = processor.merge(a, b, cmp); S s = processor.merge(a, b, CMP);
assertTrue(processor.check(s)); assertTrue(processor.check(s));
assertIterableEquals(listA, processor.iterate(s)); assertIterableEquals(listA, processor.iterate(s));
@ -584,7 +583,7 @@ public abstract class SequenceProcessorTest<S> {
div.get(i % 2).addAll(runs.get(i)); div.get(i % 2).addAll(runs.get(i));
} }
var p = processor.divideAlternatingByRuns(s, cmp); var p = processor.divideAlternatingByRuns(s, CMP);
assertTrue(processor.check(p.fst)); assertTrue(processor.check(p.fst));
assertIterableEquals(div.get(0), processor.iterate(p.fst)); assertIterableEquals(div.get(0), processor.iterate(p.fst));
@ -602,12 +601,12 @@ public abstract class SequenceProcessorTest<S> {
var div = list var div = list
.stream() .stream()
.collect(Collectors.groupingBy(n -> .collect(Collectors.groupingBy(n ->
(int) Math.signum(cmp.compare(n, pivot)))); (int) Math.signum(CMP.compare(n, pivot))));
div.putIfAbsent(-1, List.of()); div.putIfAbsent(-1, List.of());
div.putIfAbsent(+1, List.of()); div.putIfAbsent(+1, List.of());
var p = processor.divideByPivot(s, pivot, cmp); var p = processor.divideByPivot(s, pivot, CMP);
assertTrue(processor.check(p.fst)); assertTrue(processor.check(p.fst));
assertIterableEquals(div.get(-1), processor.iterate(p.fst)); assertIterableEquals(div.get(-1), processor.iterate(p.fst));

View File

@ -1,5 +1,6 @@
package aud.exam.prep; package aud.exam.prep;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -9,6 +10,7 @@ public class Tests {
public static final int STEAM_SIZE = 50; public static final int STEAM_SIZE = 50;
public static final Random RANDOM = new Random(); public static final Random RANDOM = new Random();
public static final Comparator<Integer> CMP = Integer::compareTo;
static List<Integer> randomList() { static List<Integer> randomList() {
var size = RANDOM.nextInt(50); var size = RANDOM.nextInt(50);