JeffGrigg

FibonacciAndPrimeStreamsTest

Sep 9th, 2018
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.40 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6. import java.util.stream.Stream;
  7.  
  8. public class FibonacciAndPrimeStreamsTest extends TestCase {
  9.  
  10.     public void testFibonacciStreamOfBigIntegerValues() {
  11.  
  12.         final Iterator<BigInteger> fibonacciStreamIterator = NumericStreamFactory.getFibonacciStreamOfBigIntegerValues().iterator();
  13.  
  14.         final Stream<BigInteger> fibonacciKnownValuesStream = TestCombinedNumberSequenceLists.getFibonacciKnownValuesAsStreamOfBigInteger();
  15.         final int[] elementNumber = new int[]{0};
  16.         fibonacciKnownValuesStream.forEach((knownValue) -> {
  17.             ++elementNumber[0];
  18.             assertEquals("Fibonacci element #" + elementNumber[0] + ";", knownValue, fibonacciStreamIterator.next());
  19.         });
  20.     }
  21.  
  22.     public void testFibonacciStreamOfLongValues() {
  23.  
  24.         final Iterator<Long> fibonacciStreamIterator = NumericStreamFactory.getFibonacciStreamOfLongValues().iterator();
  25.  
  26.         int elementNumber = 0;
  27.  
  28.         for (final long knownValue : TestFibonacciNumberLists.FIBONACCI_KNOWN_LONG_VALUES) {
  29.             ++elementNumber;
  30.             assertEquals("Fibonacci element #" + elementNumber + ";", knownValue, fibonacciStreamIterator.next().longValue());
  31.         }
  32.  
  33.         for (int i = 0; i < 10000; ++i) {
  34.             ++elementNumber;
  35.             try {
  36.                 fibonacciStreamIterator.next();
  37.                 fail("Expected NoSuchElementException at element number " + elementNumber);
  38.             } catch (final NoSuchElementException ex) {
  39.                 assertEquals("Fibonacci element number " + elementNumber + " exceeds the maximum value that can be held in a Java 'long' type.", ex.getMessage());
  40.             }
  41.         }
  42.     }
  43.  
  44.     public void testPrimeStream() {
  45.  
  46.         final Stream<Integer> primeStream = NumericStreamFactory.getPrimeStream();
  47.  
  48.         final Iterator<Integer> primeStreamIterator = primeStream.iterator();
  49.  
  50.         int elementNumber = 0;
  51.         for (final long primeNumber : TestPrimeNumberLists.KNOWN_PRIME_NUMBERS) {
  52.             ++elementNumber;
  53.             assertEquals("Prime Number element #" + elementNumber + ";", primeNumber, primeStreamIterator.next().intValue());
  54.         }
  55.     }
  56.  
  57.     public void testCombinedStreamsOfLongValues() {
  58.  
  59.         final Iterator<Long> combinedStreamIterator = NumericStreamFactory.getCombinedStreamOfLongValues().iterator();
  60.  
  61.         int elementNumber = 0;
  62.         for (final long knownValue : TestCombinedNumberSequenceLists.EXAMPLE_COMBINED_SEQUENCE_OF_LONG_VALUES) {
  63.             ++elementNumber;
  64.             final String elementNumberMessage = "Combined element #" + elementNumber + ";";
  65.             if (knownValue > 0) {
  66.                 assertEquals(elementNumberMessage, knownValue, combinedStreamIterator.next().longValue());
  67.             } else {
  68.                 assertEquals(-1, knownValue);
  69.                 try {
  70.                     combinedStreamIterator.next();
  71.                     fail("Expected NoSuchElementException on " + elementNumberMessage);
  72.                 } catch (final NoSuchElementException ex) {
  73.                     assertEquals(elementNumberMessage,
  74.                             "Fibonacci element number 93 exceeds the maximum value that can be held in a Java 'long' type.",
  75.                             ex.getMessage());
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.     }
  81.  
  82.     public void testCombinedStreamsOfBigIntegerValues() {
  83.  
  84.         final Iterator<BigInteger> combinedStreamIterator = NumericStreamFactory.getCombinedStreamOfBigIntegerValues().iterator();
  85.  
  86.         int elementNumber = 0;
  87.         for (final Object testValue : TestCombinedNumberSequenceLists.EXAMPLE_COMBINED_SEQUENCE_OF_FIBONACCI_AND_PRIME_VALUES) {
  88.             ++elementNumber;
  89.             final BigInteger knownValue;
  90.             if (testValue instanceof String) {
  91.                 knownValue = new BigInteger((String) testValue);
  92.             } else if (testValue instanceof Integer) {
  93.                 knownValue = new BigInteger(String.valueOf(((Integer) testValue).intValue()));
  94.             } else {
  95.                 throw new IllegalArgumentException("Unreognized value of type " + testValue.getClass().getName());
  96.             }
  97.             assertEquals("Combined element #" + elementNumber + ";", knownValue, combinedStreamIterator.next());
  98.         }
  99.     }
  100.  
  101. }
Add Comment
Please, Sign In to add comment