Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
a4e0333
besouro automatic message
Oct 12, 2016
8c0082f
besouro automatic message
Oct 12, 2016
35cfa19
besouro automatic message
Oct 12, 2016
d11d124
besouro automatic message
Oct 12, 2016
aa57e4d
besouro automatic message
Oct 12, 2016
d073f85
besouro automatic message
Oct 12, 2016
25bbdf8
besouro automatic message
Oct 12, 2016
4f8afbb
besouro automatic message
Oct 12, 2016
31e2293
besouro automatic message
Oct 12, 2016
d4a99ea
besouro automatic message
Oct 12, 2016
5a74483
besouro automatic message
Oct 12, 2016
b71fb36
besouro automatic message
Oct 12, 2016
ac070c0
besouro automatic message
Oct 12, 2016
fe5c444
besouro automatic message
Oct 12, 2016
516a6d6
besouro automatic message
Oct 12, 2016
b5558ff
besouro automatic message
Oct 12, 2016
f9ea277
besouro automatic message
Oct 12, 2016
afa307a
besouro automatic message
Oct 12, 2016
1126b5d
besouro automatic message
Oct 12, 2016
dc96cd3
besouro automatic message
Oct 12, 2016
882ea3e
besouro automatic message
Oct 12, 2016
a8b727c
besouro automatic message
Oct 12, 2016
49be231
besouro automatic message
Oct 12, 2016
5916153
besouro automatic message
Oct 12, 2016
1efa201
besouro automatic message
Oct 12, 2016
1bfe024
besouro automatic message
Oct 12, 2016
e17f8e3
besouro automatic message
Oct 12, 2016
3a7ecf0
besouro automatic message
Oct 12, 2016
5273350
besouro automatic message
Oct 12, 2016
7b9bdea
besouro automatic message
Oct 12, 2016
46e46ee
besouro automatic message
Oct 12, 2016
92354b0
besouro automatic message
Oct 12, 2016
b677b69
besouro automatic message
Oct 12, 2016
6a8a9e6
besouro automatic message
Oct 12, 2016
0bd684c
besouro automatic message
Oct 12, 2016
2a74399
besouro automatic message
Oct 12, 2016
0db3452
besouro automatic message
Oct 12, 2016
4c01852
besouro automatic message
Oct 12, 2016
ef3cf22
besouro automatic message
Oct 12, 2016
b4a236e
besouro automatic message
Oct 12, 2016
c12de5a
besouro automatic message
Oct 12, 2016
a1f09bd
besouro automatic message
Oct 12, 2016
36986c0
besouro automatic message
Oct 12, 2016
bb6f0cf
besouro automatic message
Oct 12, 2016
de6784b
besouro automatic message
Oct 12, 2016
5415322
besouro automatic message
Oct 12, 2016
c4f7583
besouro automatic message
Oct 12, 2016
a85e801
besouro automatic message
Oct 12, 2016
d477ea9
besouro automatic message
Oct 12, 2016
c678f9b
besouro automatic message
Oct 12, 2016
35bbd04
besouro automatic message
Oct 12, 2016
c9e0855
besouro automatic message
Oct 12, 2016
41c2fd4
besouro automatic message
Oct 12, 2016
b4ac568
besouro automatic message
Oct 12, 2016
22b8afd
Until formula evaluation
Oct 12, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
308 changes: 308 additions & 0 deletions .besouro/20161012152902644/actions.txt

Large diffs are not rendered by default.

956 changes: 956 additions & 0 deletions .besouro/20161012152902644/besouroEpisodes.txt

Large diffs are not rendered by default.

Empty file.
10 changes: 10 additions & 0 deletions .besouro/20161012152902644/randomHeuristicEpisodes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1476275744044 test-addition 1 401 true
1476275779118 regression 2 24 true
1476275783928 regression 1 1 true
1476278081246 test-first 1 2150 true
1476279098216 refactoring 1A 990 true
1476279180667 refactoring 1A 71 false
1476279198452 regression 1 0 false
1476279373374 test-addition 1 124 true
1476280842261 test-addition 2 1138 true
1476281016391 regression 1 0 false
Empty file.
10 changes: 10 additions & 0 deletions .besouro/20161012152902644/zorroEpisodes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1476275744044 test-addition 1 401 true
1476275779118 regression 2 35 true
1476275783928 regression 1 4 true
1476278081246 test-first 1 2297 true
1476279098216 refactoring 1A 1016 true
1476279180667 refactoring 1A 82 true
1476279198452 regression 1 17 true
1476279373374 test-addition 1 174 true
1476280842261 test-addition 2 1468 true
1476281016391 regression 1 174 true
3 changes: 3 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Spreadsheet.class
/SpreadsheetTest.class
/SpreadsheetException.class
Binary file modified bin/Spreadsheet.class
Binary file not shown.
Binary file modified bin/SpreadsheetTest.class
Binary file not shown.
133 changes: 127 additions & 6 deletions src/Spreadsheet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,139 @@
import java.util.Hashtable;

public class Spreadsheet {
Hashtable<String, String> cells;

private String[] allowedIntegerChars = {"1","2","3","4","5","6","7","8","9","-" };
public final String STR_MARK_IN_THE_MIDDLE_ERROR_MSG =
"String contains ' character(s) that is not at the beginning or end of string";
public final String ERROR_MESSAGE = "#Error";
public final boolean STR_MARKS_ALLOWED_INSIDE_STRING = false;

public Spreadsheet() {
cells = new Hashtable<String,String>();
}

public String get(String cell) {
// to be implemented
return null;
public String get(String cell) throws SpreadsheetException{
if (null == cell)
throw new SpreadsheetException("Non null string expected for cell identifier");

return cells.get(cell);
}

public void set(String cell, String value) {
// to be implemented
cells.put(cell, value);
}

public String evaluate(String cell) {
// to be implemented
return null;
public String evaluate(String cell) throws SpreadsheetException {
return evaluateCell(cell);
}

private String evaluateCell(String cell) throws SpreadsheetException {
String rawVal = cells.get(cell);
System.out.println("rawVal == " + rawVal);

return evaluation(rawVal);
}

private String evaluateValue(String value) throws SpreadsheetException {
return evaluation(value);
}

private String evaluation(String value) throws SpreadsheetException {
if (containsFormulaExpression(value))
return evaluateValue(processFormula(value));

if (containsString(value))
return evaluatedString(value);

if (!containsAllowedIntegers(value))
return ERROR_MESSAGE;

return value;
}

private boolean containsFormulaExpression(String value) throws SpreadsheetException {
if (charAsStringFromPos(value, 0).equals("="))
return true;
return false;
}

private String processFormula(String toProcess) {
return toProcess.substring(1);
}

private boolean containsString(String value) throws SpreadsheetException {
if (!(charAsStringFromPos(value, 0).equals("'") && charAsStringFromPos(value, value.length() - 1).equals("'")))
return false;

if ( !STR_MARKS_ALLOWED_INSIDE_STRING)
if (2 == characterCount(value, "'"))
return true;

throw new SpreadsheetException(STR_MARK_IN_THE_MIDDLE_ERROR_MSG);
}

private int characterCount(String str, String targetChar) throws SpreadsheetException {
int count = 0;

if (targetChar.length() != 1)
throw new SpreadsheetException("Character count target length must be 1");

for (int i = 0; i < str.length(); i++) {
if (charAsStringFromPos(str, i).equals(targetChar)) {
count++;
}
}

return count;
}

private String evaluatedString(String unevaluatedString) throws SpreadsheetException {
if (false == STR_MARKS_ALLOWED_INSIDE_STRING)
return removeFirstAndLastChar(unevaluatedString);

throw new SpreadsheetException("String evaluation method not found");
}

private String removeFirstAndLastChar(String target) {
String processedTarget = target.substring(1, target.length() - 1);

assert (target.length() - 2) == processedTarget.length();

return processedTarget;
}

private String charAsStringFromPos(String str, int pos) throws SpreadsheetException {
if (pos < 0 || pos > (str.length() - 1))
throw new SpreadsheetException("Position out of bounds when getting string of 1 len from a string");

String ret = str.substring(pos, pos + 1);

assert 1 == ret.length();

return ret;
}

private boolean containsAllowedIntegers(String value) throws SpreadsheetException {
for (int i = 0; i < value.length(); i++) {
if (!isAllowedInteger(charAsStringFromPos(value, i)))
return false;
}

return true;
}

private boolean isAllowedInteger(String character) throws SpreadsheetException {
if (character.length() > 1)
throw new SpreadsheetException("Checking if integer is allowed possible for only strings with len of 1");

for (int i = 0; i < allowedIntegerChars.length; i++) {
if (character.equals(allowedIntegerChars[i]))
return true;
}

return false;
}

}
6 changes: 6 additions & 0 deletions src/SpreadsheetException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

public class SpreadsheetException extends Exception {
public SpreadsheetException(String msg) {
super(msg);
}
}
72 changes: 70 additions & 2 deletions tests/SpreadsheetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,79 @@


public class SpreadsheetTest {
Spreadsheet spreadsheet = new Spreadsheet();

@Test
public void test() {
fail("Not yet implemented");
public void testSetGet() throws SpreadsheetException {
spreadsheet.set("A1", "1");

assertEquals("Cell does not return correct value", "1" , spreadsheet.get("A1"));
}

@Test
public void testNegativeEvaluation() throws SpreadsheetException {
spreadsheet.set("A1", "-1");

assertEquals("Negative cell value evaluated incorrectly", "-1", spreadsheet.evaluate("A1"));
}

@Test
public void testWronglyFormattedInteger_expectFail() throws SpreadsheetException {
spreadsheet.set("A1", "5A");

assertEquals("Wrongly formatted integer not detected", "#Error", spreadsheet.evaluate("A1"));
}

@Test
public void testWronglyFormattedInteger_expectSuccess() throws SpreadsheetException {
spreadsheet.set("A1", "43_27");
spreadsheet.set("A2", "437d813++");
spreadsheet.set("A3", "23.05843570");
spreadsheet.set("A4", "574f139578490");
spreadsheet.set("A5", "-4719472819");
spreadsheet.set("A6", "-99378914327");

assertFalse("Correctly formatted integer is mistaken as incorrectly formatted",
spreadsheet.evaluate("A6").equals("#Error"));
}

@Test
public void evaluateString() throws SpreadsheetException {
spreadsheet.set("A1", "'a string'");

assertEquals("String not evaluated correctly", "a string", spreadsheet.evaluate("A1"));
}

@Test
public void evaluateString_TooManyStrMarks() {
boolean caughtException = false;
spreadsheet.set("A1", "'str with ' in the middle'");

try {
spreadsheet.evaluate("A1");
} catch (SpreadsheetException e) {
caughtException = true;
assertEquals("Caught different exception than expected",
e.getMessage(), spreadsheet.STR_MARK_IN_THE_MIDDLE_ERROR_MSG);
}

if (!caughtException)
fail("String mark not found in the middle of a string");
}

@Test
public void detectStringIncorrectFormatting() throws SpreadsheetException {
spreadsheet.set("A1", "'a string");

assertEquals("Did not detect incorrect string formatting",
"#Error", spreadsheet.evaluate("A1"));
}

@Test
public void formulaTestCorrectString() throws SpreadsheetException {
spreadsheet.set("A1", "='a string'");

assertEquals("Did not produce processed string from a formula expression",
"a string", spreadsheet.evaluate("A1"));
}
}