Implement removeEverySecond
This commit is contained in:
		
							parent
							
								
									5a3e2b295c
								
							
						
					
					
						commit
						dda0bcd654
					
				
					 2 changed files with 24 additions and 1 deletions
				
			
		|  | @ -272,7 +272,13 @@ public class FullyUsedArrayProcessor<T> implements SequenceProcessor<T, FullyUse | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void removeEverySecond(FullyUsedArray<T> array) { |     public void removeEverySecond(FullyUsedArray<T> array) { | ||||||
|         throw new UnsupportedOperationException(); |         T[] newArray = Arrays.newArray(array.theArray.length - array.theArray.length / 2); | ||||||
|  | 
 | ||||||
|  |         for (int i = 0; i < newArray.length; i++) { | ||||||
|  |             newArray[i] = array.theArray[2*i]; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         array.theArray = newArray; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ import org.junit.jupiter.params.provider.ArgumentsSource; | ||||||
| 
 | 
 | ||||||
| import java.util.*; | import java.util.*; | ||||||
| import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||||
|  | import java.util.stream.IntStream; | ||||||
| import java.util.stream.Stream; | import java.util.stream.Stream; | ||||||
| 
 | 
 | ||||||
| import static org.junit.jupiter.api.Assertions.*; | import static org.junit.jupiter.api.Assertions.*; | ||||||
|  | @ -404,6 +405,22 @@ public abstract class SequenceProcessorTest<S> { | ||||||
|         assertIterableEquals(list, processor.iterate(s)); |         assertIterableEquals(list, processor.iterate(s)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @ParameterizedTest | ||||||
|  |     @ArgumentsSource(ListProvider.class) | ||||||
|  |     void testThat_removeEverySecondWorks(List<Integer> list) { | ||||||
|  |         S s = processor.create(list); | ||||||
|  | 
 | ||||||
|  |         var everyFirst = IntStream | ||||||
|  |             .iterate(0, i -> i < list.size(), i -> i+2) | ||||||
|  |             .mapToObj(list::get) | ||||||
|  |             .collect(Collectors.toList()); | ||||||
|  | 
 | ||||||
|  |         processor.removeEverySecond(s); | ||||||
|  | 
 | ||||||
|  |         assertTrue(processor.check(s)); | ||||||
|  |         assertIterableEquals(everyFirst, processor.iterate(s)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @ParameterizedTest |     @ParameterizedTest | ||||||
|     @ArgumentsSource(ListProvider.class) |     @ArgumentsSource(ListProvider.class) | ||||||
|     void testThat_doubleAllKeysWorks(List<Integer> list) { |     void testThat_doubleAllKeysWorks(List<Integer> list) { | ||||||
|  |  | ||||||
		Reference in a new issue