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

View File

@ -24,22 +24,144 @@ public class MainActivityTest {
@Test
public void addSimpleDigit() {
activity.addDigit(2);
assertEquals(2.0, activity.getDisplayedNumberAsDouble());
assertEquals("2", getDisplayedNumber());
}
@Test
public void removeLeadingZero() {
activity.addDigit(0);
activity.addDigit(5);
assertEquals(5.0, activity.getDisplayedNumberAsDouble());
assertEquals("5", getDisplayedNumber());
}
@Test
public void addition_test() {
activity.setResult("-1.2");
activity.handleOperation(MainActivity.PLUS);
activity.setResult("3.4");
activity.getResult();
assertEquals(2.2, activity.getDisplayedNumberAsDouble());
public void additionTest() {
String res = calcResult(-1.2, MainActivity.PLUS, 3.4);
assertEquals("2.2", res);
}
@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();
}
}