Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- import java.math.BigInteger;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- import java.util.stream.Stream;
- public class FibonacciAndPrimeStreamsTest extends TestCase {
- public void testFibonacciStreamOfBigIntegerValues() {
- final Iterator<BigInteger> fibonacciStreamIterator = NumericStreamFactory.getFibonacciStreamOfBigIntegerValues().iterator();
- final Stream<BigInteger> fibonacciKnownValuesStream = TestCombinedNumberSequenceLists.getFibonacciKnownValuesAsStreamOfBigInteger();
- final int[] elementNumber = new int[]{0};
- fibonacciKnownValuesStream.forEach((knownValue) -> {
- ++elementNumber[0];
- assertEquals("Fibonacci element #" + elementNumber[0] + ";", knownValue, fibonacciStreamIterator.next());
- });
- }
- public void testFibonacciStreamOfLongValues() {
- final Iterator<Long> fibonacciStreamIterator = NumericStreamFactory.getFibonacciStreamOfLongValues().iterator();
- int elementNumber = 0;
- for (final long knownValue : TestFibonacciNumberLists.FIBONACCI_KNOWN_LONG_VALUES) {
- ++elementNumber;
- assertEquals("Fibonacci element #" + elementNumber + ";", knownValue, fibonacciStreamIterator.next().longValue());
- }
- for (int i = 0; i < 10000; ++i) {
- ++elementNumber;
- try {
- fibonacciStreamIterator.next();
- fail("Expected NoSuchElementException at element number " + elementNumber);
- } catch (final NoSuchElementException ex) {
- assertEquals("Fibonacci element number " + elementNumber + " exceeds the maximum value that can be held in a Java 'long' type.", ex.getMessage());
- }
- }
- }
- public void testPrimeStream() {
- final Stream<Integer> primeStream = NumericStreamFactory.getPrimeStream();
- final Iterator<Integer> primeStreamIterator = primeStream.iterator();
- int elementNumber = 0;
- for (final long primeNumber : TestPrimeNumberLists.KNOWN_PRIME_NUMBERS) {
- ++elementNumber;
- assertEquals("Prime Number element #" + elementNumber + ";", primeNumber, primeStreamIterator.next().intValue());
- }
- }
- public void testCombinedStreamsOfLongValues() {
- final Iterator<Long> combinedStreamIterator = NumericStreamFactory.getCombinedStreamOfLongValues().iterator();
- int elementNumber = 0;
- for (final long knownValue : TestCombinedNumberSequenceLists.EXAMPLE_COMBINED_SEQUENCE_OF_LONG_VALUES) {
- ++elementNumber;
- final String elementNumberMessage = "Combined element #" + elementNumber + ";";
- if (knownValue > 0) {
- assertEquals(elementNumberMessage, knownValue, combinedStreamIterator.next().longValue());
- } else {
- assertEquals(-1, knownValue);
- try {
- combinedStreamIterator.next();
- fail("Expected NoSuchElementException on " + elementNumberMessage);
- } catch (final NoSuchElementException ex) {
- assertEquals(elementNumberMessage,
- "Fibonacci element number 93 exceeds the maximum value that can be held in a Java 'long' type.",
- ex.getMessage());
- break;
- }
- }
- }
- }
- public void testCombinedStreamsOfBigIntegerValues() {
- final Iterator<BigInteger> combinedStreamIterator = NumericStreamFactory.getCombinedStreamOfBigIntegerValues().iterator();
- int elementNumber = 0;
- for (final Object testValue : TestCombinedNumberSequenceLists.EXAMPLE_COMBINED_SEQUENCE_OF_FIBONACCI_AND_PRIME_VALUES) {
- ++elementNumber;
- final BigInteger knownValue;
- if (testValue instanceof String) {
- knownValue = new BigInteger((String) testValue);
- } else if (testValue instanceof Integer) {
- knownValue = new BigInteger(String.valueOf(((Integer) testValue).intValue()));
- } else {
- throw new IllegalArgumentException("Unreognized value of type " + testValue.getClass().getName());
- }
- assertEquals("Combined element #" + elementNumber + ";", knownValue, combinedStreamIterator.next());
- }
- }
- }
Add Comment
Please, Sign In to add comment