CellTest.java 4.97 KB
package kernel.test;

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

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

import static org.junit.Assert.*;


public class CellTest {
	
	@Before
	public void initData() {
		Grid.language = LanguageEnum.EN;
	}
	
	@Test
	public void CreateCellValueTest() {
		Cell A1 = new Cell("A", 1, 25.);
		assertEquals(A1.getValue(), 25., 0);
		assertFalse(A1.containFormula());
		assertEquals(A1.getUsedIn().size(), 0);
		assertEquals(A1.getId(), "A1");
	}
	
	@Test
	public void CreateCellBinaryOperationTest() throws CreateCycleException {
		Cell A1 = new Cell("A", 1, 25.);
		Cell A2 = new Cell("A", 2, 35.);
		Cell A3 = new Cell("A", 3, new Addition(A1, A2));
		
		assertEquals(A3.getValue(), 60., 0);
		assertTrue(A3.containFormula());
		assertEquals(A3.getUsedIn().size(), 0);
		assertEquals(A3.getId(), "A3");
		
		assertEquals(A3.toString(), "(A1+A2)");
		assertEquals(A1.getUsedIn().size(), 1);
		assertEquals(A2.getUsedIn().size(), 1);
	}
	
	@Test
	public void CreateCellFunctionTest() throws CreateCycleException {
		Cell A1 = new Cell("A", 1, 25.);
		Cell A2 = new Cell("A", 2, 35.);
		Cell A3 = new Cell("A", 3, new Addition(A1, A2));
		Cell A5 = new Cell("A", 5, 45.);
		List<Cell> sumList = new ArrayList<>();
		sumList.add(A1);
		sumList.add(A2);
		sumList.add(A3);
		sumList.add(A5);
		Cell A4 = new Cell("A", 4, new Sum(sumList));
		
		assertEquals(A4.getValue(), 165., 0);
		assertTrue(A4.containFormula());
		assertEquals(A4.getUsedIn().size(), 0);
		
		assertEquals(A4.toString(), "SUM(A1,A2,A3,A5)");
		assertEquals(A4.getDevelopedFormula(), "SUM(A1,A2,(A1+A2),A5)");
		assertEquals(A1.getUsedIn().size(), 2);
		assertEquals(A2.getUsedIn().size(), 2);
		assertEquals(A3.getUsedIn().size(), 1);
		assertEquals(A5.getUsedIn().size(), 1);
	}
	
	@Test
	public void ModifyCellValueTest() throws CreateCycleException {
		Cell A1 = new Cell("A", 1, 25.);
		Cell A2 = new Cell("A", 2, 35.);
		Cell A3 = new Cell("A", 3, new Addition(A1, A2));
		assertEquals(A1.getValue(), 25., 0);
		assertEquals(A2.getValue(), 35., 0);
		assertEquals(A3.getValue(), 60., 0);
		A1.setValue(45.);
		assertEquals(A1.getValue(), 45., 0);
		assertEquals(A3.getValue(), 80., 0);
	}
	
	@Test
	public void ModifyCellFormulaTest() throws CreateCycleException {
		Cell A1 = new Cell("A", 1, 25.);
		Cell A2 = new Cell("A", 2, 35.);
		Cell A3 = new Cell("A", 3, new Addition(A1, A2));
		Cell A5 = new Cell("A", 5, 45.);
		List<Cell> sumList = new ArrayList<>();
		sumList.add(A1);
		sumList.add(A2);
		sumList.add(A3);
		sumList.add(A5);
		Cell A4 = new Cell("A", 4, new Sum(sumList));
		
		assertEquals(A4.getValue(), 165., 0);
		assertTrue(A4.containFormula());
		assertEquals(A4.getUsedIn().size(), 0);
		
		assertEquals(A1.getUsedIn().size(), 2);
		assertEquals(A2.getUsedIn().size(), 2);
		assertEquals(A3.getUsedIn().size(), 1);
		assertEquals(A5.getUsedIn().size(), 1);
		
		assertFalse(A1.containFormula());
		A1.setFormula(new Addition(A2, A5));
		assertTrue(A1.containFormula());
		
		assertEquals(A1.getValue(), 80., 0);
		assertEquals(A3.getValue(), 115., 0);
		assertEquals(A4.getValue(), 275., 0);
		
		assertEquals(A1.toString(), "(A2+A5)");
		assertEquals(A1.getDevelopedFormula(), "(A2+A5)");
		assertEquals(A4.toString(), "SUM(A1,A2,A3,A5)");
		assertEquals(A4.getDevelopedFormula(), "SUM((A2+A5),A2,((A2+A5)+A2),A5)");
		
		assertEquals(A1.getUsedIn().size(), 2);
		assertEquals(A2.getUsedIn().size(), 3);
		assertEquals(A3.getUsedIn().size(), 1);
		assertEquals(A5.getUsedIn().size(), 2);
	}
	
	@Test(expected = CreateCycleException.class)
	public void DirectCycleBynaryOperation() throws CreateCycleException {
		Cell A1 = new Cell("A", 1, 25.);
		Cell A2 = new Cell("A", 2, 35.);
		A1.setFormula(new Addition(A1, A2));
	}
	
	
	@Test(expected = CreateCycleException.class)
	public void DirectCycleFunction() throws CreateCycleException {
		Cell A2 = new Cell("A", 2, 25.);
		Cell B2 = new Cell("B", 2, 35.);
		Cell A3 = new Cell("A", 3, 0.5);
		List<Cell> sumList = new ArrayList<>();
		sumList.add(A2);
		sumList.add(B2);
		sumList.add(A3);
		
		B2.setFormula(new Sum(sumList));
	}
	
	@Test(expected = CreateCycleException.class)
	public void IndirectCycle() throws CreateCycleException {
		Cell A1 = new Cell("A", 1, 25.);
		Cell A2 = new Cell("A", 2, 5.);
		Cell B4 = new Cell("B", 4, new Addition(A1, A2));
		Cell A4 = new Cell("A", 4, 0.);
		Cell A3 = new Cell("A", 3, 0.5);
		Cell B2 = new Cell("B", 2, 12.);
		
		List<Cell> sumList = new ArrayList<>();
		sumList.add(A2);
		sumList.add(B2);
		sumList.add(A3);
		Cell B6 = new Cell("B", 6, new Sum(sumList));
		
		List<Cell> averageList = new ArrayList<>();
		averageList.add(B6);
		averageList.add(B4);
		averageList.add(A4);
		Cell C6 = new Cell("C", 6, new Average(averageList));
		
		B2.setFormula(new Addition(A1, C6));
	}
}