|
| 1 | +package com.thealgorithms.dynamicprogramming; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Stream; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +public class LongestIncreasingSubsequenceTests { |
| 13 | + @FunctionalInterface |
| 14 | + public interface IntArrayToInt { |
| 15 | + int apply(int[] array); |
| 16 | + } |
| 17 | + |
| 18 | + @ParameterizedTest |
| 19 | + @MethodSource("testCases") |
| 20 | + public void testLongestIncreasingSubsequence(final int expected, final int[] input, final IntArrayToInt method) { |
| 21 | + assertEquals(expected, method.apply(input)); |
| 22 | + } |
| 23 | + |
| 24 | + private static Stream<Arguments> testCases() { |
| 25 | + final Object[][] testData = { |
| 26 | + {0, new int[] {}}, |
| 27 | + {1, new int[] {1}}, |
| 28 | + {1, new int[] {2, 2}}, |
| 29 | + {1, new int[] {3, 3, 3}}, |
| 30 | + {1, new int[] {4, 4, 4, 4}}, |
| 31 | + {1, new int[] {5, 5, 5, 5, 5}}, |
| 32 | + {2, new int[] {1, 2}}, |
| 33 | + {2, new int[] {1, 2, 2, 2, 2}}, |
| 34 | + {2, new int[] {1, 0, 2}}, |
| 35 | + {3, new int[] {1, 10, 2, 30}}, |
| 36 | + {3, new int[] {5, 8, 3, 7, 9, 1}}, |
| 37 | + {6, new int[] {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}}, |
| 38 | + {4, new int[] {10, 9, 2, 5, 3, 7, 101, 18}}, |
| 39 | + {4, new int[] {10, 10, 9, 9, 2, 2, 5, 5, 3, 3, 7, 7, 101, 101, 18, 18}}, |
| 40 | + {4, new int[] {0, 1, 0, 3, 2, 3}}, |
| 41 | + {2, new int[] {1, 1, 2, 2, 2}}, |
| 42 | + {3, new int[] {1, 1, 2, 2, 2, 3, 3, 3, 3}}, |
| 43 | + }; |
| 44 | + |
| 45 | + final List<IntArrayToInt> methods = Arrays.asList(LongestIncreasingSubsequence::LIS, LongestIncreasingSubsequence::findLISLen); |
| 46 | + |
| 47 | + return Stream.of(testData).flatMap(input -> methods.stream().map(method -> Arguments.of(input[0], input[1], method))); |
| 48 | + } |
| 49 | +} |
0 commit comments