add some more tests for the other buttons

This commit is contained in:
tibbi 2015-12-28 22:15:26 +01:00
parent 95d3b31102
commit cc856b3d05
2 changed files with 194 additions and 50 deletions

View File

@ -27,6 +27,7 @@ public class MainActivity extends AppCompatActivity {
public static final int DIVIDE = 5; public static final int DIVIDE = 5;
public static final int MODULO = 6; public static final int MODULO = 6;
public static final int POWER = 7; public static final int POWER = 7;
public static final int ROOT = 8;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -36,41 +37,46 @@ public class MainActivity extends AppCompatActivity {
resetValues(); resetValues();
} }
private void resetValues() { public void resetValues() {
baseValue = 0; baseValue = 0;
secondValue = 0; secondValue = 0;
resetValue = false; resetValue = false;
lastKey = 0; lastKey = 0;
lastOperation = 0; lastOperation = 0;
setResult("0"); setValueDouble(0);
} }
public void addDigit(int number) { public void addDigit(int number) {
final String currentValue = getDisplayedNumber(); final String currentValue = getDisplayedNumber();
final String newValue = removeLeadingZero(currentValue + number); final String newValue = removeLeadingZero(currentValue + number);
setResult(newValue); setValue(newValue);
} }
public void setResult(String value) { public void setValue(String value) {
result.setText(value); result.setText(value);
} }
public void setValueDouble(double d) {
setValue(Formatter.doubleToString(d));
lastKey = DIGIT;
}
private String removeLeadingZero(String str) { private String removeLeadingZero(String str) {
final double doubleValue = Double.parseDouble(str); final double doubleValue = Double.parseDouble(str);
return Formatter.doubleToString(doubleValue); return Formatter.doubleToString(doubleValue);
} }
private String getDisplayedNumber() { public String getDisplayedNumber() {
return result.getText().toString(); return result.getText().toString();
} }
public double getDisplayedNumberAsDouble() { private double getDisplayedNumberAsDouble() {
return Double.parseDouble(getDisplayedNumber()); return Double.parseDouble(getDisplayedNumber());
} }
private void resetValueIfNeeded() { private void resetValueIfNeeded() {
if (resetValue) if (resetValue)
setResult("0"); setValueDouble(0);
resetValue = false; resetValue = false;
} }
@ -103,11 +109,50 @@ public class MainActivity extends AppCompatActivity {
return; return;
if (lastKey == DIGIT) if (lastKey == DIGIT)
getResult(); handleResult();
resetValue = true; resetValue = true;
lastKey = operation; lastKey = operation;
lastOperation = operation; lastOperation = operation;
if (operation == ROOT)
calculateResult();
}
public void handleClear() {
final String oldValue = getDisplayedNumber();
String newValue;
final int len = oldValue.length();
int minLen = 1;
if (oldValue.contains("-"))
minLen++;
if (len > minLen)
newValue = oldValue.substring(0, len - 1);
else
newValue = "0";
if (newValue.equals("-0"))
newValue = "0";
setValue(newValue);
baseValue = Double.parseDouble(newValue);
}
public void handleLongClear() {
resetValues();
}
public void handleEquals() {
if (lastKey == EQUALS)
calculateResult();
if (lastKey != DIGIT)
return;
secondValue = getDisplayedNumberAsDouble();
calculateResult();
lastKey = EQUALS;
} }
@OnClick(R.id.btn_plus) @OnClick(R.id.btn_plus)
@ -142,71 +187,45 @@ public class MainActivity extends AppCompatActivity {
@OnClick(R.id.btn_root) @OnClick(R.id.btn_root)
public void rootClicked() { public void rootClicked() {
getResult(); handleOperation(ROOT);
lastOperation = EQUALS;
updateResult(Math.sqrt(baseValue));
} }
@OnClick(R.id.btn_clear) @OnClick(R.id.btn_clear)
public void clearClicked() { public void clearClicked() {
final String oldValue = getDisplayedNumber(); handleClear();
String newValue;
final int len = oldValue.length();
int minLen = 1;
if (oldValue.contains("-"))
minLen++;
if (len > minLen)
newValue = oldValue.substring(0, len - 1);
else
newValue = "0";
if (newValue.equals("-0"))
newValue = "0";
setResult(newValue);
baseValue = Double.parseDouble(newValue);
} }
@OnLongClick(R.id.btn_clear) @OnLongClick(R.id.btn_clear)
public boolean clearLongClicked() { public boolean clearLongClicked() {
resetValues(); handleLongClear();
return true; return true;
} }
@OnClick(R.id.btn_equals) @OnClick(R.id.btn_equals)
public void equalsClicked() { public void equalsClicked() {
if (lastKey == EQUALS) handleEquals();
calculateResult();
if (lastKey != DIGIT)
return;
secondValue = getDisplayedNumberAsDouble();
calculateResult();
lastKey = EQUALS;
} }
public void decimalClicked() { public void decimalClicked() {
String value = getDisplayedNumber(); String value = getDisplayedNumber();
if (!value.contains(".")) if (!value.contains("."))
value += "."; value += ".";
setResult(value); setValue(value);
} }
public void zeroClicked() { public void zeroClicked() {
String value = getDisplayedNumber(); String value = getDisplayedNumber();
if (!value.equals("0")) if (!value.equals("0"))
value += "0"; value += "0";
setResult(value); setValue(value);
} }
private void updateResult(double value) { private void updateResult(double value) {
setResult(Formatter.doubleToString(value)); setValue(Formatter.doubleToString(value));
baseValue = value; baseValue = value;
} }
public void getResult() { public void handleResult() {
secondValue = getDisplayedNumberAsDouble(); secondValue = getDisplayedNumberAsDouble();
calculateResult(); calculateResult();
baseValue = getDisplayedNumberAsDouble(); baseValue = getDisplayedNumberAsDouble();
@ -232,6 +251,9 @@ public class MainActivity extends AppCompatActivity {
case POWER: case POWER:
powerNumbers(); powerNumbers();
break; break;
case ROOT:
updateResult(Math.sqrt(baseValue));
break;
default: default:
break; break;
} }

View File

@ -24,22 +24,144 @@ public class MainActivityTest {
@Test @Test
public void addSimpleDigit() { public void addSimpleDigit() {
activity.addDigit(2); activity.addDigit(2);
assertEquals(2.0, activity.getDisplayedNumberAsDouble()); assertEquals("2", getDisplayedNumber());
} }
@Test @Test
public void removeLeadingZero() { public void removeLeadingZero() {
activity.addDigit(0); activity.addDigit(0);
activity.addDigit(5); activity.addDigit(5);
assertEquals(5.0, activity.getDisplayedNumberAsDouble()); assertEquals("5", getDisplayedNumber());
} }
@Test @Test
public void addition_test() { public void additionTest() {
activity.setResult("-1.2"); String res = calcResult(-1.2, MainActivity.PLUS, 3.4);
activity.handleOperation(MainActivity.PLUS); assertEquals("2.2", res);
activity.setResult("3.4"); }
activity.getResult();
assertEquals(2.2, activity.getDisplayedNumberAsDouble()); @Test
public void subtractionTest() {
String res = calcResult(7.8, MainActivity.MINUS, 2.5);
assertEquals("5.3", res);
}
@Test
public void multiplyTest() {
String res = calcResult(-3.2, MainActivity.MULTIPLY, 6.6);
assertEquals("-21.12", res);
}
@Test
public void divisionTest() {
String res = calcResult(18.25, MainActivity.DIVIDE, 5);
assertEquals("3.65", res);
}
@Test
public void divisionByZero_returnsZero() {
String res = calcResult(6, MainActivity.DIVIDE, 0);
assertEquals("0", res);
}
@Test
public void moduloTest() {
String res = calcResult(6.5, MainActivity.MODULO, 3);
assertEquals("0.5", res);
}
@Test
public void powerTest() {
String res = calcResult(3, MainActivity.POWER, 6);
assertEquals("729", res);
}
@Test
public void rootTest() {
setDouble(16);
handleOperation(MainActivity.ROOT);
activity.handleResult();
assertEquals("4", getDisplayedNumber());
}
@Test
public void clearBtnSimpleTest() {
setDouble(156);
activity.handleClear();
assertEquals("15", getDisplayedNumber());
}
@Test
public void clearBtnComplexTest() {
setDouble(-26);
activity.handleClear();
assertEquals("-2", getDisplayedNumber());
activity.handleClear();
assertEquals("0", getDisplayedNumber());
}
@Test
public void clearBtnLongClick_resetsEverything() {
calcResult(-1.2, MainActivity.PLUS, 3.4);
activity.handleLongClear();
handleOperation(MainActivity.PLUS);
setDouble(3);
activity.handleResult();
assertEquals("3", getDisplayedNumber());
}
@Test
public void complexTest() {
setDouble(-12.2);
handleOperation(MainActivity.PLUS);
setDouble(21);
handleOperation(MainActivity.MINUS);
assertEquals("8.8", getDisplayedNumber());
setDouble(1.6);
activity.handleEquals();
assertEquals("7.2", getDisplayedNumber());
activity.handleEquals();
assertEquals("5.6", getDisplayedNumber());
handleOperation(MainActivity.MULTIPLY);
setDouble(5);
handleOperation(MainActivity.DIVIDE);
assertEquals("28", getDisplayedNumber());
setDouble(4);
handleOperation(MainActivity.MODULO);
assertEquals("7", getDisplayedNumber());
setDouble(5);
handleOperation(MainActivity.POWER);
assertEquals("2", getDisplayedNumber());
setDouble(8);
handleOperation(MainActivity.ROOT);
assertEquals("16", getDisplayedNumber());
activity.handleClear();
assertEquals("1", getDisplayedNumber());
}
private void setDouble(double d) {
activity.setValueDouble(d);
}
private void handleOperation(int operation) {
activity.handleOperation(operation);
}
private String calcResult(double baseValue, int operation, double secondValue) {
setDouble(baseValue);
handleOperation(operation);
setDouble(secondValue);
activity.handleResult();
return getDisplayedNumber();
}
private String getDisplayedNumber() {
return activity.getDisplayedNumber();
} }
} }