import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.SortedMap; import java.util.stream.Collectors; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; public class PalindromeCalculatorTest { private PalindromeCalculator palindromeCalculator; @Before public void setup() { palindromeCalculator = new PalindromeCalculator(); } @Test public void largestPalindromeFromSingleDigitFactors() { final List> expected = Collections.unmodifiableList( Arrays.asList( Arrays.asList(1, 9), Arrays.asList(3, 3) ) ); final long expectedValue = 9l; final SortedMap>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1, 9); checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey()); } @Ignore("Remove to run test") @Test public void largestPalindromeFromDoubleDigitFactors() { final List> expected = Collections.unmodifiableList( Arrays.asList( Arrays.asList(91, 99) ) ); final long expectedValue = 9009l; final SortedMap>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99); checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey()); } @Ignore("Remove to run test") @Test public void smallestPalindromeFromDoubleDigitFactors() { final List> expected = Collections.unmodifiableList( Arrays.asList( Arrays.asList(11, 11) ) ); final long expectedValue = 121l; final SortedMap>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99); checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey()); } @Ignore("Remove to run test") @Test public void largestPalindromeFromTripleDigitFactors() { final List> expected = Collections.unmodifiableList( Arrays.asList( Arrays.asList(913, 993) ) ); final long expectedValue = 906609l; final SortedMap>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999); checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey()); } @Ignore("Remove to run test") @Test public void smallestPalindromeFromTripleDigitFactors() { final List> expected = Collections.unmodifiableList( Arrays.asList( Arrays.asList(101, 101) ) ); final long expectedValue = 10201l; final SortedMap>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999); checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey()); } private void checkPalindromeWithFactorsMatchesExpected(List> expectedPalindromeFactors, long expectedValueOfPalindrome, SortedMap>> actualPalindromes, long actualValueOfPalindrome) { assertNotNull(actualPalindromes); assertFalse(actualPalindromes.isEmpty()); assertEquals(expectedValueOfPalindrome, actualValueOfPalindrome); List> actualPalindromeFactors = actualPalindromes .get(actualValueOfPalindrome) .stream() .sorted((a, b) -> Integer.compare(a.get(0), b.get(0))) .collect(Collectors.toList()); assertEquals(expectedPalindromeFactors, actualPalindromeFactors); } }