Implement invert, clone and alternate
This commit is contained in:
		
							parent
							
								
									9b19dea6e5
								
							
						
					
					
						commit
						9dd65d203e
					
				
					 2 changed files with 75 additions and 3 deletions
				
			
		|  | @ -346,17 +346,45 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse | |||
| 
 | ||||
|     @Override | ||||
|     public FullyUsedArray<T> invert(FullyUsedArray<T> 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<T> clone(FullyUsedArray<T> array) { | ||||
|         return null; | ||||
|         var newArray = new FullyUsedArray<T>(); | ||||
|         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<T> alternate(FullyUsedArray<T> a, FullyUsedArray<T> b) { | ||||
|         throw new UnsupportedOperationException(); | ||||
|         var newArray = new FullyUsedArray<T>(); | ||||
|         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 | ||||
|  |  | |||
|  | @ -438,4 +438,48 @@ public abstract class SequenceProcessorTest<S> { | |||
|         assertTrue(processor.check(s)); | ||||
|         assertIterableEquals(noDuplicatesList, processor.iterate(s)); | ||||
|     } | ||||
| 
 | ||||
|     @ParameterizedTest | ||||
|     @ArgumentsSource(ListProvider.class) | ||||
|     void testThat_invertWorks(List<Integer> 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<Integer> 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<Integer> listA, List<Integer> listB) { | ||||
|         S a = processor.create(listA); | ||||
|         S b = processor.create(listB); | ||||
| 
 | ||||
|         var list = new ArrayList<Integer>(); | ||||
|         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)); | ||||
|     } | ||||
| } | ||||
		Reference in a new issue