package interfaces;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class BinaryComputingIteratorTest{
	
	private Iterator<Double> iterator1, iterator2, iteratorShort;
	
	@BeforeEach
	public void setup() {
		iterator1 = Arrays.asList(0.5, -2.0).iterator();
		iterator2 = Arrays.asList(5.0, 3.0).iterator();
		iteratorShort = Arrays.asList(5.0).iterator();
	}
	
	@Test
	public void testMultiplication() {
		BinaryComputingIterator binaryIt = new BinaryComputingIterator(iterator1, iterator2, (a, b) -> a * b);
		Assertions.assertEquals(2.5, binaryIt.next());
		Assertions.assertTrue(binaryIt.hasNext());
		Assertions.assertEquals(-6.0, binaryIt.next());
		Assertions.assertFalse(binaryIt.hasNext());
	}
	
	@Test
	public void testAddition() {
		BinaryComputingIterator binaryIt = new BinaryComputingIterator(iterator1, iterator2, (a, b) -> a + b);
		Assertions.assertEquals(5.5, binaryIt.next());
		Assertions.assertTrue(binaryIt.hasNext());
		Assertions.assertEquals(1.0, binaryIt.next());
		Assertions.assertFalse(binaryIt.hasNext());
	}
	
	@Test
	public void testShortIterator() {
		BinaryComputingIterator binaryIt = new BinaryComputingIterator(iterator1, iteratorShort, (a, b) -> a * b);
		Assertions.assertEquals(2.5, binaryIt.next());
		Assertions.assertFalse(binaryIt.hasNext());
	}
	
	@Test
	public void testShortIteratorAndDefault() {
		BinaryComputingIterator binaryIt = new BinaryComputingIterator(iterator1, iteratorShort, null, 2.0, (a, b) -> a * b);
		Assertions.assertEquals(2.5, binaryIt.next());
		Assertions.assertTrue(binaryIt.hasNext());
		Assertions.assertEquals(-4.0, binaryIt.next());
		Assertions.assertFalse(binaryIt.hasNext());
	}
	
	@Test
	public void testEmptyIterator() {
		BinaryComputingIterator binaryIt = new BinaryComputingIterator(new ArrayList().iterator(), new ArrayList().iterator(), (a, b) -> a * b);
		Assertions.assertFalse(binaryIt.hasNext());
	}
	
	@Test
	public void testEmptyIteratorAndDefault() {
		BinaryComputingIterator binaryIt = new BinaryComputingIterator(new ArrayList().iterator(), new ArrayList().iterator(), 1.0, 2.0, (a, b) -> a * b);
		Assertions.assertFalse(binaryIt.hasNext());
	}
}
