001package csheets.core.formula.compiler; 002 003import static org.junit.Assert.*; 004 005import org.junit.*; 006 007import csheets.core.*; 008 009/** 010 * Class to test the Number Sign Compiler 011 * 012 * @author Andre 013 * 014 */ 015public class NumberSignCompilerTest { 016 017 private static Workbook workbook; 018 private static Spreadsheet sheetAttri; 019 private static Spreadsheet sheetSeq; 020 private static Spreadsheet sheetWhile; 021 private static Spreadsheet sheetDo; 022 private static Spreadsheet sheetEval; 023 024 /** 025 * Create a new workbook for the program 026 * 027 * @throws Exception 028 * any Exception can occur 029 */ 030 @BeforeClass 031 public static void setUpClass() throws Exception { 032 033 /** create a new instance of workbook */ 034 workbook = new Workbook(5); 035 036 /** Change to the first spreadsheet */ 037 sheetAttri = workbook.getSpreadsheet(0); 038 039 /** Change to the second spreadsheet */ 040 sheetSeq = workbook.getSpreadsheet(1); 041 042 /** Change to the third spreadsheet */ 043 sheetWhile = workbook.getSpreadsheet(2); 044 045 /** Change to the fourth spreadsheet */ 046 sheetDo = workbook.getSpreadsheet(3); 047 048 /** Change to the fifth spreadsheet */ 049 sheetEval = workbook.getSpreadsheet(4); 050 051 } 052 053 /** 054 * Tests the attribution function 055 */ 056 @Test 057 public void testAttribution() { 058 059 try { 060 // test 1 -> test in cell a1, to modify cell a2 to be 3 061 String expression1 = "#a2:=3"; 062 Cell cellOri1 = sheetAttri.getCell(new Address(0, 0)); 063 cellOri1.setContent(expression1); 064 Cell cellFim1 = sheetAttri.getCell(new Address(0, 1)); 065 066 // test 2 -> test in cell f5, to modify cell c3 to be 3 067 String expression2 = "#c3:=4"; 068 Cell cellOri2 = sheetAttri.getCell(new Address(5, 4)); 069 cellOri2.setContent(expression2); 070 Cell cellFim2 = sheetAttri.getCell(new Address(2, 2)); 071 072 // test 3 -> test in cell h3, to modify cell f2 to be 7 073 String expression3 = "#f2:=7"; 074 Cell cellOri3 = sheetAttri.getCell(new Address(7, 2)); 075 cellOri3.setContent(expression3); 076 Cell cellFim3 = sheetAttri.getCell(new Address(5, 1)); 077 078 assertEquals("3", cellFim1.getContent()); 079 assertEquals("4", cellFim2.getContent()); 080 assertEquals("7", cellFim3.getContent()); 081 082 } catch (FormulaCompilationException e) { 083 fail("Exception error!"); 084 } 085 } 086 087 /** 088 * Tests the sequence of expressions in the cell 089 */ 090 @Test 091 public void testSequence() { 092 try { 093 String expression = "#{a1:=1;a2:=2;a3:=3;b1:=a1=1;a4:=SUM(a1:a3)}"; 094 Cell cellOri1 = sheetSeq.getCell(new Address(0, 4)); 095 cellOri1.setContent(expression); 096 Cell cellFim1_1 = sheetSeq.getCell(new Address(0, 0)); 097 Cell cellFim1_2 = sheetSeq.getCell(new Address(0, 1)); 098 Cell cellFim1_3 = sheetSeq.getCell(new Address(0, 2)); 099 Cell cellFim1_4 = sheetSeq.getCell(new Address(0, 3)); 100 Cell cellFim1_5 = sheetSeq.getCell(new Address(1, 0)); 101 Value espected1 = new Value(6); 102 Value espected2 = new Value(true); 103 104 assertEquals("1", cellFim1_1.getContent()); 105 assertEquals("2", cellFim1_2.getContent()); 106 assertEquals("3", cellFim1_3.getContent()); 107 assertEquals("6", cellFim1_4.getContent()); 108 assertEquals(0, espected2.compareTo(cellFim1_5.getValue())); 109 assertEquals(0, espected1.compareTo(cellOri1.getValue())); 110 111 } catch (FormulaCompilationException e) { 112 fail("Exception error!"); 113 } 114 } 115 116 /** 117 * Tests the whiledo formula 118 */ 119 @Test 120 public void testWhileDo() { 121 try { 122 Cell cellFim_1 = sheetWhile.getCell(new Address(0, 0)); 123 Cell cellFim_2 = sheetWhile.getCell(new Address(2, 0)); 124 cellFim_1.setContent("1"); 125 cellFim_2.setContent("1"); 126 String expression = "#whiledo{a1<10;a1:=a1+1;c1:=a1+1}"; 127 Cell cellOri = sheetWhile.getCell(new Address(2, 2)); 128 cellOri.setContent(expression); 129 Value espected = new Value(11); 130 131 assertEquals("10", cellFim_1.getContent()); 132 assertEquals("11", cellFim_2.getContent()); 133 assertEquals(0, espected.compareTo(cellOri.getValue())); 134 } catch (FormulaCompilationException e) { 135 fail("Exception error!"); 136 } 137 } 138 139 /** 140 * Tests the dowhile function 141 */ 142 @Test 143 public void testDoWhile() { 144 try { 145 Cell cellFim_1 = sheetDo.getCell(new Address(0, 0)); 146 Cell cellFim_2 = sheetDo.getCell(new Address(2, 0)); 147 cellFim_1.setContent("1"); 148 cellFim_2.setContent("1"); 149 String expression = "#dowhile{a1:=a1+1;c1:=a1+1;a1<10}"; 150 Cell cellOri = sheetDo.getCell(new Address(2, 2)); 151 cellOri.setContent(expression); 152 Value espected = new Value(11); 153 154 assertEquals("10", cellFim_1.getContent()); 155 assertEquals("11", cellFim_2.getContent()); 156 assertEquals(0, espected.compareTo(cellOri.getValue())); 157 } catch (FormulaCompilationException e) { 158 fail("Exception error!"); 159 } 160 } 161 162 /** 163 * Tests the eval function 164 */ 165 @Test 166 public void testEval() { 167 try { 168 Cell cellFim = sheetEval.getCell(new Address(0, 0)); 169 String expression = "#eval(\"#a1:=2\")"; 170 Cell cellOri = sheetEval.getCell(new Address(2, 2)); 171 cellOri.setContent(expression); 172 Value espected = new Value("#a1:=2"); 173 174 assertEquals("2", cellFim.getContent()); 175 assertEquals(0, espected.compareTo(cellOri.getValue())); 176 } catch (FormulaCompilationException e) { 177 fail("Exception error!"); 178 } 179 } 180}