Ad
  • Default User Avatar

    I did this in Java.

    In my opinion, the sample tests for this kata should be rewritten.

    Some of the test failure messages are the same even though what is being tested is different, making it difficult to tell which test you have failed. Critically, the tests do not test for arrays where values are repeated but are not plateaus; this isn't mentioned in the description either, so it's not clear that this is a problem until you go to attempt the kata and get failures there. At the very least, the description should be updated to account for this.

    I can understand why the tests have been conducted in a single @Test block, but I don't think it results in a well written test. Sure, it makes the code shorter, but shorter isn't always better if the result is confusing to anyone who has to look at the code later. Splitting them into seperate tests means the logs are also split, making debugging problems a whole lot easier, especially with the Codewars UI we are using here.

    I have included the tests I wrote below, and you can judge for yourself.

    import static org.junit.Assert.assertEquals;
    import org.junit.runners.JUnit4;
    import org.junit.FixMethodOrder;
    import org.junit.runners.MethodSorters;
    import java.util.*;
    import java.util.stream.Collectors;
    
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class SolutionTest {
    
      @Test
      public void a_shouldHandleSimplePeak(){
        int[] input = {1, 2, 1};
        int[] pos = {1};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
    
      }
    
      @Test
      public void b_shouldIgnorePeakAtBeginning(){
        int[] input = {2, 1, 2, 1};
        int[] pos = {2};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
    
      }
    
      @Test
      public void c_shouldIgnorePeakAtEnd(){
        int[] input = {1, 2, 1, 2};
        int[] pos = {1};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
    
      }
    
      @Test
      public void d_shouldHandleMultiplePeaks(){
        int[] input = {1, 2, 1, 2, 1};
        int[] pos = {1, 3};
        int[] peaks = {2, 2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
    
      }
      
      @Test
      public void e_shouldHandleSimplePlateau(){
        int[] input = {1, 2, 2, 2, 1};
        int[] pos = {1};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
    
      @Test
      public void f_shouldIgnorePlateauAtBeginning(){
        int[] input = {2, 2, 1, 2, 1};
        int[] pos = {3};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
      
      @Test
      public void g_shouldIgnorePlateauAtEnd(){
        int[] input = {1, 2, 1, 2, 2};
        int[] pos = {1};
        int[] peaks = {2};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
      
      @Test
      public void h_shouldHandleRepeatingValuesGoingUp(){
        int[] input = {1, 2, 2, 3, 2};
        int[] pos = {3};
        int[] peaks = {3};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
      
      @Test
      public void i_shouldHandleRepeatingValuesGoingDown(){
        int[] input = {1, 2, 2, 3, 2};
        int[] pos = {3};
        int[] peaks = {3};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
      
      @Test
      public void j_shouldHandleComplexPeaksAndPlateaus(){
        int[] input = {1, 2, 2, 3, 2, 2, 1, 0, 0};
        int[] pos = {3};
        int[] peaks = {3};
    
        assertEquals(getExpected(pos, peaks), PickPeaks.getPeaks(input));
        
      }
    
      public Map<String, List<Integer>> getExpected (int[] pos, int[] peaks){
        return Map.of("pos", arrToList(pos), "peaks", arrToList(peaks));
      }
      public List<Integer> arrToList(int[] arr){
        return Arrays.stream(arr).boxed().collect(Collectors.toList());
      }
    
    }