Implement exchangePairs and rotateTriples

This commit is contained in:
2021-08-30 14:56:00 +02:00
parent b09f3d11db
commit 5a3e2b295c
2 changed files with 47 additions and 7 deletions

View File

@ -201,11 +201,6 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse
return secondMax; return secondMax;
} }
@Override
public boolean check(FullyUsedArray<T> array) {
return true;
}
@Override @Override
public boolean isItemWiseLessOrEqual(FullyUsedArray<T> a, FullyUsedArray<T> b, Comparator<T> cmp) { public boolean isItemWiseLessOrEqual(FullyUsedArray<T> a, FullyUsedArray<T> b, Comparator<T> cmp) {
if (b.theArray.length < a.theArray.length) { if (b.theArray.length < a.theArray.length) {
@ -258,12 +253,21 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse
@Override @Override
public void exchangePairs(FullyUsedArray<T> array) { public void exchangePairs(FullyUsedArray<T> array) {
for (int i = 1; i < array.theArray.length-1; i += 2) {
T t = array.theArray[i];
array.theArray[i] = array.theArray[i+1];
array.theArray[i+1] = t;
}
} }
@Override @Override
public void rotateTriples(FullyUsedArray<T> array) { public void rotateTriples(FullyUsedArray<T> array) {
for (int i = 1; i < array.theArray.length-2; i += 3) {
T t = array.theArray[i+2];
array.theArray[i+2] = array.theArray[i+1];
array.theArray[i+1] = array.theArray[i];
array.theArray[i] = t;
}
} }
@Override @Override
@ -514,6 +518,11 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse
return p; return p;
} }
@Override
public boolean check(FullyUsedArray<T> array) {
return true;
}
@Override @Override
public FullyUsedArray<T> create(Iterable<T> iterable) { public FullyUsedArray<T> create(Iterable<T> iterable) {
var array = new FullyUsedArray<T>(); var array = new FullyUsedArray<T>();

View File

@ -373,6 +373,37 @@ public abstract class SequenceProcessorTest<S> {
return processor.create(list); return processor.create(list);
} }
@ParameterizedTest
@ArgumentsSource(ListProvider.class)
void testThat_exchangePairsWorks(List<Integer> list) {
S s = processor.create(list);
for (int i = 1; i < list.size()-1; i += 2) {
Collections.swap(list, i, i+1);
}
processor.exchangePairs(s);
assertTrue(processor.check(s));
assertIterableEquals(list, processor.iterate(s));
}
@ParameterizedTest
@ArgumentsSource(ListProvider.class)
void testThat_rotateTriplesWorks(List<Integer> list) {
S s = processor.create(list);
for (int i = 1; i < list.size()-2; i += 3) {
Collections.swap(list, i+1, i+2);
Collections.swap(list, i, i+1);
}
processor.rotateTriples(s);
assertTrue(processor.check(s));
assertIterableEquals(list, processor.iterate(s));
}
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(ListProvider.class) @ArgumentsSource(ListProvider.class)
void testThat_doubleAllKeysWorks(List<Integer> list) { void testThat_doubleAllKeysWorks(List<Integer> list) {