BinaryOperationTest.java 3.99 KB
package kernel.test;

import kernel.Cell;
import kernel.Grid;
import kernel.LanguageEnum;
import kernel.exception.CreateCycleException;
import kernel.exception.InvalidIntervalException;
import kernel.function.Sum;
import kernel.operation.Addition;
import kernel.operation.Division;
import kernel.operation.Multiplication;
import kernel.operation.Subtraction;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class BinaryOperationTest {
	
	private List<Cell> cells;
	
	@Before
	public void initData() throws CreateCycleException, InvalidIntervalException {
		Grid.language = LanguageEnum.EN;
		this.cells = new ArrayList<>();
		
		Cell a1 = new Cell("A", 1, 10.);
		Cell a2 = new Cell("A", 2, 0.);
		Cell a3 = new Cell("A", 3, 20.);
		
		List<Cell> sumList = new ArrayList<>();
		sumList.add(a1);
		sumList.add(a2);
		sumList.add(a3);
		
		Cell b1 = new Cell("B", 1, new Sum(sumList));
		
		this.cells.add(a1);
		this.cells.add(a2);
		this.cells.add(a3);
		this.cells.add(b1);
	}
	
	@Test
	public void testAddition() {
		Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null);
		Cell a2 = this.cells.stream().filter(c -> c.getId().equals("A2")).findFirst().orElse(null);
		
		Addition addition = new Addition(a1, a2);
		
		assertEquals(10, addition.eval(), 0);
		assertEquals("(A1+A2)", addition.toString());
		assertEquals("(A1+A2)", addition.getDevelopedFormula());
		
		Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null);
		
		addition = new Addition(a1, b1);
		
		assertEquals(40, addition.eval(), 0);
		assertEquals("(A1+B1)", addition.toString());
		assertEquals("(A1+SUM(A1,A2,A3))", addition.getDevelopedFormula());
	}
	
	@Test
	public void testMultiplication() {
		Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null);
		Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null);
		
		Multiplication multiplication = new Multiplication(a1, a3);
		
		assertEquals(200, multiplication.eval(), 0);
		assertEquals("(A1*A3)", multiplication.toString());
		assertEquals("(A1*A3)", multiplication.getDevelopedFormula());
		
		Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null);
		
		multiplication = new Multiplication(a1, b1);
		
		assertEquals(300, multiplication.eval(), 0);
		assertEquals("(A1*B1)", multiplication.toString());
		assertEquals("(A1*SUM(A1,A2,A3))", multiplication.getDevelopedFormula());
	}
	
	@Test
	public void testSubtraction() {
		Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null);
		Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null);
		
		Subtraction subtraction = new Subtraction(a3, a1);
		
		assertEquals(10, subtraction.eval(), 0);
		assertEquals("(A3-A1)", subtraction.toString());
		assertEquals("(A3-A1)", subtraction.getDevelopedFormula());
		
		Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null);
		
		subtraction = new Subtraction(a1, b1);
		
		assertEquals(-20, subtraction.eval(), 0);
		assertEquals("(A1-B1)", subtraction.toString());
		assertEquals("(A1-SUM(A1,A2,A3))", subtraction.getDevelopedFormula());
	}
	
	@Test
	public void testDivision() {
		Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null);
		Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null);
		
		Division division = new Division(a3, a1);
		
		assertEquals(2, division.eval(), 0);
		assertEquals("(A3/A1)", division.toString());
		assertEquals("(A3/A1)", division.getDevelopedFormula());
		
		Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null);
		
		division = new Division(b1, a1);
		
		assertEquals(3, division.eval(), 0);
		assertEquals("(B1/A1)", division.toString());
		assertEquals("(SUM(A1,A2,A3)/A1)", division.getDevelopedFormula());
	}
}