diff --git a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java index 76ab91c..e63b8d3 100644 --- a/src/aud/exam/prep/array/FullyUsedArrayProcessor.java +++ b/src/aud/exam/prep/array/FullyUsedArrayProcessor.java @@ -346,17 +346,45 @@ public class FullyUsedArrayProcessor implements SequenceProcessor invert(FullyUsedArray array) { - return null; + for (int i = 0; i < array.theArray.length/2; i++) { + T t = array.theArray[i]; + array.theArray[i] = array.theArray[array.theArray.length-i-1]; + array.theArray[array.theArray.length-i-1] = t; + } + return array; } @Override public FullyUsedArray clone(FullyUsedArray array) { - return null; + var newArray = new FullyUsedArray(); + newArray.theArray = Arrays.newArray(array.theArray.length); + + for (int i = 0; i < array.theArray.length; i++) { + newArray.theArray[i] = array.theArray[i]; + } + + return newArray; } @Override public FullyUsedArray alternate(FullyUsedArray a, FullyUsedArray b) { - throw new UnsupportedOperationException(); + var newArray = new FullyUsedArray(); + newArray.theArray = Arrays.newArray(a.theArray.length + b.theArray.length); + + var index = 0; + var indexA = 0; + var indexB = 0; + + while (indexA < a.theArray.length || indexB < b.theArray.length) { + if (indexA < a.theArray.length) { + newArray.theArray[index++] = a.theArray[indexA++]; + } + if (indexB < b.theArray.length) { + newArray.theArray[index++] = b.theArray[indexB++]; + } + } + + return newArray; } @Override diff --git a/test/aud/exam/prep/SequenceProcessorTest.java b/test/aud/exam/prep/SequenceProcessorTest.java index 2c4e02f..74114d4 100644 --- a/test/aud/exam/prep/SequenceProcessorTest.java +++ b/test/aud/exam/prep/SequenceProcessorTest.java @@ -438,4 +438,48 @@ public abstract class SequenceProcessorTest { assertTrue(processor.check(s)); assertIterableEquals(noDuplicatesList, processor.iterate(s)); } + + @ParameterizedTest + @ArgumentsSource(ListProvider.class) + void testThat_invertWorks(List list) { + S s = processor.create(list); + + Collections.reverse(list); + s = processor.invert(s); + + assertTrue(processor.check(s)); + assertIterableEquals(list, processor.iterate(s)); + } + + @ParameterizedTest + @ArgumentsSource(ListProvider.class) + void testThat_cloneWorks(List list) { + S a = processor.create(list); + S b = processor.clone(a); + + assertNotSame(a, b); + assertTrue(processor.check(b)); + assertIterableEquals(list, processor.iterate(b)); + } + + @ParameterizedTest + @ArgumentsSource(DoubleListProvider.class) + void testThat_cloneWorks(List listA, List listB) { + S a = processor.create(listA); + S b = processor.create(listB); + + var list = new ArrayList(); + while(!listA.isEmpty() || !listB.isEmpty()) { + if (!listA.isEmpty()) { + list.add(listA.remove(0)); + } + if (!listB.isEmpty()) { + list.add(listB.remove(0)); + } + } + + S s = processor.alternate(a, b); + + assertIterableEquals(list, processor.iterate(s)); + } } \ No newline at end of file