Implement divideAlternating

This commit is contained in:
2021-08-29 23:12:18 +02:00
parent effe0a39f9
commit 7416f94dbc
2 changed files with 35 additions and 1 deletions

View File

@ -417,7 +417,22 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse
@Override @Override
public Pair<FullyUsedArray<T>, FullyUsedArray<T>> divideAlternating(FullyUsedArray<T> array) { public Pair<FullyUsedArray<T>, FullyUsedArray<T>> divideAlternating(FullyUsedArray<T> array) {
throw new UnsupportedOperationException(); var p = new Pair<FullyUsedArray<T>, FullyUsedArray<T>>();
p.fst = new FullyUsedArray<>();
p.snd = new FullyUsedArray<>();
p.snd.theArray = Arrays.newArray(array.theArray.length / 2);
p.fst.theArray = Arrays.newArray(array.theArray.length - p.snd.theArray.length);
for (int i = 0; i < array.theArray.length; i++) {
if (i % 2 == 0) {
p.fst.theArray[i/2] = array.theArray[i];
} else {
p.snd.theArray[i/2] = array.theArray[i];
}
}
return p;
} }
@Override @Override

View File

@ -501,4 +501,23 @@ public abstract class SequenceProcessorTest<S> {
assertTrue(processor.check(s)); assertTrue(processor.check(s));
assertIterableEquals(listA, processor.iterate(s)); assertIterableEquals(listA, processor.iterate(s));
} }
@ParameterizedTest
@ArgumentsSource(DoubleListProvider.class)
void testThat_divideAlternatingWorks(List<Integer> list) {
S s = processor.create(list);
var div = List.of(new ArrayList<Integer>(), new ArrayList<Integer>());
for (int i = 0; i < list.size(); i++) {
div.get(i % 2).add(list.get(i));
}
var p = processor.divideAlternating(s);
assertTrue(processor.check(p.fst));
assertIterableEquals(div.get(0), processor.iterate(p.fst));
assertTrue(processor.check(p.snd));
assertIterableEquals(div.get(1), processor.iterate(p.snd));
}
} }