tests added

This commit is contained in:
serso
2011-12-14 16:32:23 +04:00
parent 848012615e
commit fb825089b4
12 changed files with 184 additions and 26 deletions

View File

@@ -285,7 +285,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
int cursorPositionOffset = 0;
final StringBuilder textToBeInserted = new StringBuilder(text);
final MathType.Result mathType = MathType.getType(text, 0);
final MathType.Result mathType = MathType.getType(text, 0, false);
switch (mathType.getMathType()) {
case function:
textToBeInserted.append("()");

View File

@@ -233,7 +233,7 @@ public class CalculatorVarsActivity extends ListActivity {
}
if (canBeSaved) {
final MathType.Result mathType = MathType.getType(name, 0);
final MathType.Result mathType = MathType.getType(name, 0, false);
if (mathType.getMathType() == MathType.text || mathType.getMathType() == MathType.constant) {

View File

@@ -102,7 +102,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
final NumberBuilder numberBuilder = new NumberBuilder(simpleFormat, mathContext.getNumeralBase());
for (int i = 0; i < text.length(); i++) {
MathType.Result mathType = MathType.getType(text, i);
MathType.Result mathType = MathType.getType(text, i, numberBuilder.isHexMode());
final MutableObject<Integer> localNumberOffset = new MutableObject<Integer>(0);
numberBuilder.process(text1, mathType, localNumberOffset);

View File

@@ -318,12 +318,14 @@ public enum MathType {
/**
* Method determines mathematical entity type for text substring starting from ith index
*
*
* @param text analyzed text
* @param i index which points to start of substring
* @param hexMode
* @return math entity type of substring starting from ith index of specified text
*/
@NotNull
public static Result getType(@NotNull String text, int i) {
public static Result getType(@NotNull String text, int i, boolean hexMode) {
if (i < 0) {
throw new IllegalArgumentException("I must be more or equals to 0.");
} else if (i >= text.length() && i != 0) {
@@ -338,7 +340,7 @@ public enum MathType {
final String s = CollectionsUtils.find(mathType.getTokens(), startsWithFinder);
if (s != null) {
if ( s.length() == 1 ) {
if (JsclMathEngine.instance.getNumeralBase() == NumeralBase.hex) {
if (hexMode || JsclMathEngine.instance.getNumeralBase() == NumeralBase.hex) {
final Character ch = s.charAt(0);
if ( NumeralBase.hex.getAcceptableCharacters().contains(ch) ) {
return new Result(MathType.digit, s);

View File

@@ -35,7 +35,7 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Gene
final NumberBuilder numberBuilder = new NumberBuilder(true, mathContext.getNumeralBase());
for (int i = 0; i < s.length(); i++) {
final MathType.Result mathTypeResult = MathType.getType(s, i);
final MathType.Result mathTypeResult = MathType.getType(s, i, numberBuilder.isHexMode());
numberBuilder.process(sb, mathTypeResult, null);
@@ -58,7 +58,7 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Gene
for (int i = 0; i < s.length(); i++) {
mathTypeBefore = mathType;
if (mathTypeAfter == null) {
mathType = MathType.getType(s, i);
mathType = MathType.getType(s, i, false);
} else {
mathType = mathTypeAfter;
}
@@ -66,7 +66,7 @@ public class FromJsclSimplifyTextProcessor implements TextProcessor<String, Gene
char ch = s.charAt(i);
if (ch == '*') {
if (i + 1 < s.length()) {
mathTypeAfter = MathType.getType(s, i + 1);
mathTypeAfter = MathType.getType(s, i + 1, false);
} else {
mathTypeAfter = null;
}

View File

@@ -190,4 +190,8 @@ public class NumberBuilder {
return result;
}
public boolean isHexMode() {
return nb == NumeralBase.hex;
}
}

View File

@@ -6,6 +6,7 @@
package org.solovyev.android.calculator.model;
import jscl.JsclMathEngine;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.StartsWithFinder;
import org.solovyev.android.calculator.jscl.JsclOperation;
@@ -38,12 +39,18 @@ class ToJsclTextProcessor implements TextProcessor<PreparedExpression, String> {
MathType.Result mathTypeResult = null;
MathType.Result mathTypeBefore = null;
final StringBuilder sb = new StringBuilder(s);
final NumberBuilder nb = new NumberBuilder(true, CalculatorEngine.instance.getEngine().getNumeralBase());
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') continue;
startsWithFinder.setI(i);
mathTypeBefore = mathTypeResult == null ? null : mathTypeResult;
mathTypeResult = MathType.getType(s, i);
mathTypeResult = MathType.getType(s, i, nb.isHexMode());
nb.process(sb, mathTypeResult, null);
if (mathTypeBefore != null) {