changes
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package org.solovyev.android;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.units.CalculatorNumeralBase;
|
||||
import org.solovyev.math.units.Unit;
|
||||
import org.solovyev.math.units.UnitConverter;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 4/21/12
|
||||
* Time: 8:24 PM
|
||||
*/
|
||||
public class AndroidNumeralBaseTest {
|
||||
|
||||
@NotNull
|
||||
private final UnitConverter c = CalculatorNumeralBase.getConverter();
|
||||
|
||||
@Test
|
||||
public void testIsSupported() throws Exception {
|
||||
Assert.assertTrue(c.isSupported(CalculatorNumeralBase.bin, CalculatorNumeralBase.dec));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromDec() throws Exception {
|
||||
|
||||
Assert.assertEquals("101", c.convert(CalculatorNumeralBase.dec.createUnit("5"), CalculatorNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("1", c.convert(CalculatorNumeralBase.dec.createUnit("1"), CalculatorNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.bin).getValue());
|
||||
Assert.assertEquals("1111100111", c.convert(CalculatorNumeralBase.dec.createUnit("999"), CalculatorNumeralBase.bin).getValue());
|
||||
|
||||
Assert.assertEquals("A23", c.convert(CalculatorNumeralBase.dec.createUnit("2595"), CalculatorNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("AEE", c.convert(CalculatorNumeralBase.dec.createUnit("2798"), CalculatorNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("15", c.convert(CalculatorNumeralBase.dec.createUnit("21"), CalculatorNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.hex).getValue());
|
||||
Assert.assertEquals("3E7", c.convert(CalculatorNumeralBase.dec.createUnit("999"), CalculatorNumeralBase.hex).getValue());
|
||||
|
||||
Assert.assertEquals("76", c.convert(CalculatorNumeralBase.dec.createUnit("62"), CalculatorNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("12", c.convert(CalculatorNumeralBase.dec.createUnit("10"), CalculatorNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("15", c.convert(CalculatorNumeralBase.dec.createUnit("13"), CalculatorNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("0", c.convert(CalculatorNumeralBase.dec.createUnit("0"), CalculatorNumeralBase.oct).getValue());
|
||||
Assert.assertEquals("10445", c.convert(CalculatorNumeralBase.dec.createUnit("4389"), CalculatorNumeralBase.oct).getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomConvert() throws Exception {
|
||||
final Random random = new Random(new Date().getTime());
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
final String value = String.valueOf(random.nextInt());
|
||||
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.oct, CalculatorNumeralBase.oct, CalculatorNumeralBase.bin, CalculatorNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.bin, CalculatorNumeralBase.hex, CalculatorNumeralBase.dec, CalculatorNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.dec, CalculatorNumeralBase.hex, CalculatorNumeralBase.oct, CalculatorNumeralBase.dec));
|
||||
Assert.assertEquals(value, convertChain(value, CalculatorNumeralBase.dec, CalculatorNumeralBase.hex, CalculatorNumeralBase.bin, CalculatorNumeralBase.oct, CalculatorNumeralBase.dec));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String convertChain(@NotNull String value, @NotNull CalculatorNumeralBase baseAndroid, @NotNull CalculatorNumeralBase... typeAndroids) {
|
||||
Unit<String> unit = baseAndroid.createUnit(value);
|
||||
|
||||
for (CalculatorNumeralBase typeAndroid : typeAndroids) {
|
||||
unit = CalculatorNumeralBase.getConverter().convert(unit, typeAndroid);
|
||||
}
|
||||
|
||||
return unit.getValue();
|
||||
}
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/13/12
|
||||
* Time: 1:11 PM
|
||||
*/
|
||||
@RunWith(value = CalculatorppTestRunner.class)
|
||||
public class AndroidCalculatorEditorViewTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void staticSetUp() throws Exception {
|
||||
CalculatorTestUtils.staticSetUp(null);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
CalculatorActivity context = new CalculatorActivity();
|
||||
CalculatorTestUtils.initViews(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsyncWork() throws Exception {
|
||||
final int threadNum = 10;
|
||||
final int count = 10;
|
||||
final int maxTextLength = 100;
|
||||
|
||||
final Random random = new Random(new Date().getTime());
|
||||
final CountDownLatch startLatchLatch = new CountDownLatch(threadNum);
|
||||
final CountDownLatch finishLatch = new CountDownLatch(threadNum * count);
|
||||
final AtomicBoolean error = new AtomicBoolean(false);
|
||||
|
||||
for ( int i = 0; i < threadNum; i++ ) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
startLatchLatch.await();
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e);
|
||||
error.set(true);
|
||||
for ( int j = 0; j < count; j++ ) {
|
||||
finishLatch.countDown();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for ( int j = 0; j < count; j++ ) {
|
||||
try {
|
||||
int textLength = random.nextInt(maxTextLength);
|
||||
Locator.getInstance().getEditor().insert(StringUtils.generateRandomString(textLength), textLength);
|
||||
} catch (Throwable e) {
|
||||
System.out.println(e);
|
||||
error.set(true);
|
||||
} finally {
|
||||
finishLatch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
startLatchLatch.countDown();
|
||||
}
|
||||
|
||||
if ( finishLatch.await(60, TimeUnit.SECONDS) ) {
|
||||
Assert.assertFalse(error.get());
|
||||
} else {
|
||||
Assert.fail("Too long execution!");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.content.Context;
|
||||
import jscl.JsclMathEngine;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.mockito.Mockito;
|
||||
import org.solovyev.android.calculator.external.CalculatorExternalListenersContainer;
|
||||
import org.solovyev.android.calculator.history.CalculatorHistory;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 8:56 PM
|
||||
*/
|
||||
public class CalculatorTestUtils {
|
||||
|
||||
public static void staticSetUp(@Nullable Context context) throws Exception {
|
||||
Locator.getInstance().init(new CalculatorImpl(), newCalculatorEngine(), Mockito.mock(CalculatorClipboard.class), Mockito.mock(CalculatorNotifier.class), Mockito.mock(CalculatorHistory.class), new SystemOutCalculatorLogger(), Mockito.mock(CalculatorPreferenceService.class), Mockito.mock(CalculatorKeyboard.class), Mockito.mock(CalculatorExternalListenersContainer.class));
|
||||
Locator.getInstance().getEngine().init();
|
||||
|
||||
if ( context != null ) {
|
||||
initViews(context);
|
||||
}
|
||||
}
|
||||
|
||||
public static void initViews(@NotNull Context context) {
|
||||
final AndroidCalculatorEditorView editor = new AndroidCalculatorEditorView(context);
|
||||
editor.init(context);
|
||||
Locator.getInstance().getEditor().setView(editor);
|
||||
|
||||
final AndroidCalculatorDisplayView display = new AndroidCalculatorDisplayView(context);
|
||||
display.init(context);
|
||||
Locator.getInstance().getDisplay().setView(display);
|
||||
}
|
||||
|
||||
public static void staticSetUp() throws Exception {
|
||||
staticSetUp(null);
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
static CalculatorEngineImpl newCalculatorEngine() {
|
||||
final MathEntityDao mathEntityDao = Mockito.mock(MathEntityDao.class);
|
||||
|
||||
final JsclMathEngine jsclEngine = JsclMathEngine.getInstance();
|
||||
|
||||
final CalculatorVarsRegistry varsRegistry = new CalculatorVarsRegistry(jsclEngine.getConstantsRegistry(), mathEntityDao);
|
||||
final CalculatorFunctionsMathRegistry functionsRegistry = new CalculatorFunctionsMathRegistry(jsclEngine.getFunctionsRegistry(), mathEntityDao);
|
||||
final CalculatorOperatorsMathRegistry operatorsRegistry = new CalculatorOperatorsMathRegistry(jsclEngine.getOperatorsRegistry(), mathEntityDao);
|
||||
final CalculatorPostfixFunctionsRegistry postfixFunctionsRegistry = new CalculatorPostfixFunctionsRegistry(jsclEngine.getPostfixFunctionsRegistry(), mathEntityDao);
|
||||
|
||||
return new CalculatorEngineImpl(jsclEngine, varsRegistry, functionsRegistry, operatorsRegistry, postfixFunctionsRegistry, null);
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import com.xtremelabs.robolectric.RobolectricTestRunner;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/7/12
|
||||
* Time: 5:36 PM
|
||||
*/
|
||||
public class CalculatorppTestRunner extends RobolectricTestRunner {
|
||||
|
||||
public CalculatorppTestRunner(@NotNull Class<?> testClass) throws InitializationError {
|
||||
super(testClass, new File("."));
|
||||
}
|
||||
}
|
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.graphics.Color;
|
||||
import jscl.MathEngine;
|
||||
import jscl.NumeralBase;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.text.TextProcessor;
|
||||
import org.solovyev.android.calculator.view.TextHighlighter;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/12/11
|
||||
* Time: 10:07 PM
|
||||
*/
|
||||
public class TextHighlighterTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
CalculatorTestUtils.staticSetUp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcess() throws Exception {
|
||||
TextProcessor<?, String> textHighlighter = new TextHighlighter(0, false);
|
||||
|
||||
final Random random = new Random(new Date().getTime());
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
for (int j = 0; j < 1000; j++) {
|
||||
sb.append(random.nextBoolean() ? "(" : ")");
|
||||
}
|
||||
try {
|
||||
textHighlighter.process(sb.toString());
|
||||
} catch (Exception e) {
|
||||
System.out.println(sb.toString());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertEquals("<font color=\"#000000\"></font>)(((())())", textHighlighter.process(")(((())())").toString());
|
||||
Assert.assertEquals(")", textHighlighter.process(")").toString());
|
||||
Assert.assertEquals(")()(", textHighlighter.process(")()(").toString());
|
||||
|
||||
textHighlighter = new TextHighlighter(0, true);
|
||||
Assert.assertEquals("1 000 000", textHighlighter.process("1000000").toString());
|
||||
Assert.assertEquals("1 000 000", textHighlighter.process("1000000").toString());
|
||||
Assert.assertEquals("0.1E3", textHighlighter.process("0.1E3").toString());
|
||||
Assert.assertEquals("1E3", textHighlighter.process("1E3").toString());
|
||||
Assert.assertEquals("2<b>0x:</b>", textHighlighter.process("20x:").toString());
|
||||
Assert.assertEquals("20g", textHighlighter.process("20g").toString());
|
||||
Assert.assertEquals("22g", textHighlighter.process("22g").toString());
|
||||
Assert.assertEquals("20ю", textHighlighter.process("20ю").toString());
|
||||
Assert.assertEquals("20ъ", textHighlighter.process("20ъ").toString());
|
||||
Assert.assertEquals("3!!", textHighlighter.process("3!!").toString());
|
||||
Assert.assertEquals("2", textHighlighter.process("2").toString());
|
||||
Assert.assertEquals("21", textHighlighter.process("21").toString());
|
||||
Assert.assertEquals("214", textHighlighter.process("214").toString());
|
||||
Assert.assertEquals("2 145", textHighlighter.process("2 145").toString());
|
||||
Assert.assertEquals("1 000 000E3", textHighlighter.process("1000000E3").toString());
|
||||
Assert.assertEquals("-1 000 000E3", textHighlighter.process("-1000000E3").toString());
|
||||
Assert.assertEquals("-1 000 000E-3", textHighlighter.process("-1000000E-3").toString());
|
||||
Assert.assertEquals("-1 000 000E-30000", textHighlighter.process("-1000000E-30000").toString());
|
||||
textHighlighter = new TextHighlighter(0, false);
|
||||
|
||||
textHighlighter.process("cannot calculate 3^10^10 !!!\n" +
|
||||
" unable to enter 0. FIXED\n" +
|
||||
" empty display in Xperia Rayo\n" +
|
||||
" check привиденная FIXED\n" +
|
||||
" set display result only if text in editor was not changed FIXED\n" +
|
||||
" shift M text to the left\n" +
|
||||
" do not show SYNTAX ERROR always (may be show send clock?q) FIXED\n" +
|
||||
" ln(8)*log(8) => ln(8)*og(8) FIXED\n" +
|
||||
" copy/paste ln(8)*log(8)\n" +
|
||||
" 6!^2 ERROR");
|
||||
|
||||
Assert.assertEquals("<font color=\"#000000\"><i>sin</i>(</font><font color=\"#ffff9a\">2</font><font color=\"#000000\">)</font>", textHighlighter.process("sin(2)").toString());
|
||||
Assert.assertEquals("<font color=\"#000000\"><i>atanh</i>(</font><font color=\"#ffff9a\">2</font><font color=\"#000000\">)</font>", textHighlighter.process("atanh(2)").toString());
|
||||
|
||||
|
||||
Assert.assertEquals("<b>0x:</b>E", textHighlighter.process("0x:E").toString());
|
||||
Assert.assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:6F").toString());
|
||||
Assert.assertEquals("<b>0x:</b>6F.", textHighlighter.process("0x:6F.").toString());
|
||||
Assert.assertEquals("<b>0x:</b>6F.2", textHighlighter.process("0x:6F.2").toString());
|
||||
Assert.assertEquals("<b>0x:</b>6F.B", textHighlighter.process("0x:6F.B").toString());
|
||||
Assert.assertEquals("<b>0x:</b>006F.B", textHighlighter.process("0x:006F.B").toString());
|
||||
Assert.assertEquals("<b>0x:</b>0", textHighlighter.process("0x:0").toString());
|
||||
Assert.assertEquals("<b>0x:</b>FF33233FFE", textHighlighter.process("0x:FF33233FFE").toString());
|
||||
Assert.assertEquals("<b>0x:</b>FF33 233 FFE", textHighlighter.process("0x:FF33 233 FFE").toString());
|
||||
|
||||
final MathEngine me = Locator.getInstance().getEngine().getMathEngine0();
|
||||
try {
|
||||
me.setNumeralBase(NumeralBase.hex);
|
||||
Assert.assertEquals("E", textHighlighter.process("E").toString());
|
||||
Assert.assertEquals(".E", textHighlighter.process(".E").toString());
|
||||
Assert.assertEquals("E+", textHighlighter.process("E+").toString());
|
||||
Assert.assertEquals("E.", textHighlighter.process("E.").toString());
|
||||
Assert.assertEquals(".E.", textHighlighter.process(".E.").toString());
|
||||
Assert.assertEquals("6F", textHighlighter.process("6F").toString());
|
||||
Assert.assertEquals("6F", textHighlighter.process("6F").toString());
|
||||
Assert.assertEquals("6F.", textHighlighter.process("6F.").toString());
|
||||
Assert.assertEquals("6F.2", textHighlighter.process("6F.2").toString());
|
||||
Assert.assertEquals("6F.B", textHighlighter.process("6F.B").toString());
|
||||
Assert.assertEquals("006F.B", textHighlighter.process("006F.B").toString());
|
||||
} finally {
|
||||
me.setNumeralBase(NumeralBase.dec);
|
||||
}
|
||||
|
||||
Assert.assertEquals("<b>0b:</b>110101", textHighlighter.process("0b:110101").toString());
|
||||
Assert.assertEquals("<b>0b:</b>110101.", textHighlighter.process("0b:110101.").toString());
|
||||
Assert.assertEquals("<b>0b:</b>110101.101", textHighlighter.process("0b:110101.101").toString());
|
||||
Assert.assertEquals("<b>0b:</b>11010100.1", textHighlighter.process("0b:11010100.1").toString());
|
||||
Assert.assertEquals("<b>0b:</b>110101.0", textHighlighter.process("0b:110101.0").toString());
|
||||
Assert.assertEquals("<b>0b:</b>0", textHighlighter.process("0b:0").toString());
|
||||
Assert.assertEquals("<b>0b:</b>1010100101111010101001", textHighlighter.process("0b:1010100101111010101001").toString());
|
||||
Assert.assertEquals("<b>0b:</b>101 010 01 0 111 1 0 10101001", textHighlighter.process("0b:101 010 01 0 111 1 0 10101001").toString());
|
||||
|
||||
try {
|
||||
me.setNumeralBase(NumeralBase.bin);
|
||||
Assert.assertEquals("110101", textHighlighter.process("110101").toString());
|
||||
Assert.assertEquals("110101.", textHighlighter.process("110101.").toString());
|
||||
Assert.assertEquals("110101.101", textHighlighter.process("110101.101").toString());
|
||||
Assert.assertEquals("11010100.1", textHighlighter.process("11010100.1").toString());
|
||||
Assert.assertEquals("110101.0", textHighlighter.process("110101.0").toString());
|
||||
Assert.assertEquals("0", textHighlighter.process("0").toString());
|
||||
Assert.assertEquals("1010100101111010101001", textHighlighter.process("1010100101111010101001").toString());
|
||||
Assert.assertEquals("101 010 01 0 111 1 0 10101001", textHighlighter.process("101 010 01 0 111 1 0 10101001").toString());
|
||||
} finally {
|
||||
me.setNumeralBase(NumeralBase.dec);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTime() throws Exception {
|
||||
final TextProcessor<?, String> textHighlighter = new TextHighlighter(Color.WHITE, false);
|
||||
|
||||
final int count = 1000;
|
||||
final String subExpression = "cos(acos(t8ln(t5t85tln(8ln(5t55tln(5))))))+tln(88cos(tln(t)))+t√(ln(t))";
|
||||
final StringBuilder expression = new StringBuilder(subExpression.length() * count);
|
||||
for ( int i = 0; i < count; i++ ){
|
||||
expression.append(subExpression);
|
||||
expression.append("+");
|
||||
}
|
||||
expression.append(subExpression);
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
textHighlighter.process(expression.toString());
|
||||
long endTime = System.currentTimeMillis();
|
||||
System.out.println("Total time, ms: " + (endTime - startTime));
|
||||
}
|
||||
}
|
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewStateImpl;
|
||||
import org.solovyev.android.calculator.CalculatorEditorViewState;
|
||||
import org.solovyev.android.calculator.CalculatorEditorViewStateImpl;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.common.equals.CollectionEqualizer;
|
||||
import org.solovyev.common.equals.EqualsTool;
|
||||
import org.solovyev.common.history.HistoryHelper;
|
||||
import org.solovyev.common.history.SimpleHistoryHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/17/11
|
||||
* Time: 10:01 PM
|
||||
*/
|
||||
public class HistoryUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testFromXml() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
private static final String emptyHistory = "<history>\n" +
|
||||
" <historyItems class=\"java.util.ArrayList\"/>\n" +
|
||||
"</history>";
|
||||
|
||||
private static final String toXml1 = "<history>\n" +
|
||||
" <historyItems class=\"java.util.ArrayList\">\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>3</cursorPosition>\n" +
|
||||
" <text>1+1</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>0</cursorPosition>\n" +
|
||||
" <text>Error</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>simplify</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" </historyItems>\n" +
|
||||
"</history>";
|
||||
|
||||
private static final String toXml2 = "<history>\n" +
|
||||
" <historyItems class=\"java.util.ArrayList\">\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>3</cursorPosition>\n" +
|
||||
" <text>1+1</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>0</cursorPosition>\n" +
|
||||
" <text>Error</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>simplify</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>2</cursorPosition>\n" +
|
||||
" <text>5/6</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>3</cursorPosition>\n" +
|
||||
" <text>5/6</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>numeric</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>1</cursorPosition>\n" +
|
||||
" <text></text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>0</cursorPosition>\n" +
|
||||
" <text>Error</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>elementary</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" <calculatorHistoryState>\n" +
|
||||
" <time>100000000</time>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>0</cursorPosition>\n" +
|
||||
" <text>4+5/35sin(41)+dfdsfsdfs</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <displayState>\n" +
|
||||
" <editorState>\n" +
|
||||
" <cursorPosition>1</cursorPosition>\n" +
|
||||
" <text>4+5/35sin(41)+dfdsfsdfs</text>\n" +
|
||||
" </editorState>\n" +
|
||||
" <jsclOperation>numeric</jsclOperation>\n" +
|
||||
" </displayState>\n" +
|
||||
" </calculatorHistoryState>\n" +
|
||||
" </historyItems>\n" +
|
||||
"</history>";
|
||||
|
||||
@Test
|
||||
public void testToXml() throws Exception {
|
||||
final Date date = new Date(100000000);
|
||||
|
||||
HistoryHelper<CalculatorHistoryState> history = new SimpleHistoryHelper<CalculatorHistoryState>();
|
||||
|
||||
CalculatorDisplayViewState calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.simplify, "Error");
|
||||
|
||||
CalculatorEditorViewState calculatorEditor = CalculatorEditorViewStateImpl.newInstance("1+1", 3);
|
||||
|
||||
CalculatorHistoryState state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
Assert.assertEquals(emptyHistory, HistoryUtils.toXml(history.getStates()));
|
||||
|
||||
|
||||
state.setSaved(true);
|
||||
|
||||
Assert.assertEquals(toXml1, HistoryUtils.toXml(history.getStates()));
|
||||
|
||||
calculatorDisplay = CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "5/6", 3);
|
||||
|
||||
calculatorEditor = CalculatorEditorViewStateImpl.newInstance("5/6", 2);
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
calculatorDisplay = CalculatorDisplayViewStateImpl.newErrorState(JsclOperation.elementary, "Error");
|
||||
|
||||
calculatorEditor = CalculatorEditorViewStateImpl.newInstance("", 1);
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
calculatorDisplay = CalculatorDisplayViewStateImpl.newValidState(JsclOperation.numeric, null, "4+5/35sin(41)+dfdsfsdfs", 1);
|
||||
|
||||
calculatorEditor = CalculatorEditorViewStateImpl.newInstance("4+5/35sin(41)+dfdsfsdfs", 0);
|
||||
|
||||
state = CalculatorHistoryState.newInstance(calculatorEditor, calculatorDisplay);
|
||||
state.setSaved(true);
|
||||
state.setTime(date.getTime());
|
||||
history.addState(state);
|
||||
|
||||
String xml = HistoryUtils.toXml(history.getStates());
|
||||
Assert.assertEquals(toXml2, xml);
|
||||
|
||||
final List<CalculatorHistoryState> fromXml = new ArrayList<CalculatorHistoryState>();
|
||||
final HistoryHelper<CalculatorHistoryState> historyFromXml = new SimpleHistoryHelper<CalculatorHistoryState>();
|
||||
HistoryUtils.fromXml(xml, fromXml);
|
||||
for (CalculatorHistoryState historyState : fromXml) {
|
||||
historyFromXml.addState(historyState);
|
||||
}
|
||||
|
||||
Assert.assertEquals(history.getStates().size(), historyFromXml.getStates().size());
|
||||
|
||||
for (CalculatorHistoryState historyState : history.getStates()) {
|
||||
historyState.setId(0);
|
||||
historyState.setSaved(true);
|
||||
}
|
||||
for (CalculatorHistoryState historyState : historyFromXml.getStates()) {
|
||||
historyState.setId(0);
|
||||
historyState.setSaved(true);
|
||||
}
|
||||
Assert.assertTrue(EqualsTool.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer<CalculatorHistoryState>(null)));
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.math.function.IConstant;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/7/11
|
||||
* Time: 7:52 PM
|
||||
*/
|
||||
public class VarTest {
|
||||
|
||||
private static final String xml = "<vars>\n" +
|
||||
" <vars class=\"java.util.ArrayList\">\n" +
|
||||
" <var>\n" +
|
||||
" <name>e</name>\n" +
|
||||
" <value>2.718281828459045</value>\n" +
|
||||
" <system>true</system>\n" +
|
||||
" <description>description</description>\n" +
|
||||
" </var>\n" +
|
||||
" <var>\n" +
|
||||
" <name>;</name>\n" +
|
||||
" <value>3.0</value>\n" +
|
||||
" <system>true</system>\n" +
|
||||
" </var>\n" +
|
||||
" </vars>\n" +
|
||||
"</vars>";
|
||||
|
||||
@Test
|
||||
public void testXml() throws Exception {
|
||||
final Vars vars = new Vars();
|
||||
Var first = new Var.Builder("e", Math.E).setDescription("description").setSystem(true).create();
|
||||
vars.getEntities().add(first);
|
||||
Var second = new Var.Builder(";", 3d).setSystem(true).create();
|
||||
vars.getEntities().add(second);
|
||||
|
||||
final StringWriter sw = new StringWriter();
|
||||
final Serializer serializer = new Persister();
|
||||
serializer.write(vars, sw);
|
||||
|
||||
Assert.assertEquals(xml, sw.toString());
|
||||
|
||||
final Vars result = serializer.read(Vars.class, xml);
|
||||
final IConstant actualFirst = result.getEntities().get(0);
|
||||
final IConstant actualSecond = result.getEntities().get(1);
|
||||
|
||||
areEqual(first, actualFirst);
|
||||
areEqual(second, actualSecond);
|
||||
|
||||
}
|
||||
|
||||
private void areEqual(IConstant expected, IConstant actual) {
|
||||
Assert.assertEquals(expected.getName(), actual.getName());
|
||||
Assert.assertEquals(expected.getDescription(), actual.getDescription());
|
||||
Assert.assertEquals(expected.getValue(), actual.getValue());
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
* For more information, please, contact se.solovyev@gmail.com
|
||||
* or visit http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.plot;
|
||||
|
||||
import jscl.math.Expression;
|
||||
import jscl.math.function.Constant;
|
||||
import junit.framework.Assert;
|
||||
import org.achartengine.model.XYSeries;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 12/5/11
|
||||
* Time: 9:07 PM
|
||||
*/
|
||||
public class PlotUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testAddXY() throws Exception {
|
||||
MyXYSeries series = new MyXYSeries("test_01", 100);
|
||||
PlotUtils.addXY(-10, 10, Expression.valueOf("asin(t)"), new Constant("t"), series, new MyXYSeries("test_01_imag"), false, 200);
|
||||
testAscSeries(series);
|
||||
PlotUtils.addXY(-1, 1, Expression.valueOf("asin(t)"), new Constant("t"), series, new MyXYSeries("test_01_imag"), true, 200);
|
||||
testAscSeries(series);
|
||||
|
||||
series = new MyXYSeries("test_02", 1000);
|
||||
PlotUtils.addXY(-10, 10, Expression.valueOf("1/t"), new Constant("t"), series, new MyXYSeries("test_01_imag"), false, 1000);
|
||||
testAscSeries(series);
|
||||
PlotUtils.addXY(-1, 1, Expression.valueOf("1/t"), new Constant("t"), series, new MyXYSeries("test_01_imag"), true, 1000);
|
||||
testAscSeries(series);
|
||||
|
||||
}
|
||||
|
||||
public void testAscSeries(@NotNull XYSeries series) {
|
||||
for ( int i = 0; i < series.getItemCount(); i++ ) {
|
||||
if (i > 1) {
|
||||
Assert.assertTrue(series.getX(i - 1) + " > " +series.getX(i) + " at " + i + " of " + series.getItemCount(), series.getX(i - 1) <= series.getX(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user