package fi.celia.asciimath.test;
import fi.celia.asciimath.DtbookAsciiMathException;
import fi.celia.asciimath.JAsciiMath;
import fi.celia.asciimath.AsciiMathBlockValue;
import fi.celia.asciimath.xml.AsciiMathXml;
public class TestJAsciiMath {
/**
* @param args
*/
public static void main(String[] args)
{
try {
/*
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
*/
boolean bCorrectStrikeErrors = false;
boolean bCheckStrikeErrors = true;
JAsciiMath jAsciiMath = new JAsciiMath("dtbookasciimath.properties",
bCheckStrikeErrors, bCorrectStrikeErrors, false);
jAsciiMath.setConvertHtmlChars(false);
/*
if (strModifiedMathData == null || markValue == null | start < 0 || end < 0)
return -1; // = false
int len = strModifiedMathData.length();
if (len < 1)
return -1; // = false
if (start > len)
return -1; // = false
if (end > len)
return -1; // = false
int markLength = markValue.length();
int indStartMark = start - markLength;
if (indStartMark < 0)
indStartMark = 0;
int indEndMark = end + markLength;
if (indEndMark > len)
indEndMark = len;
String strPossibleMarkValue = strModifiedMathData.substring(indStartMark, indEndMark);
if (strPossibleMarkValue == null || strPossibleMarkValue.trim().length() == 0)
return -1;
int indFoundMarkValue = strPossibleMarkValue.indexOf(markValue);
if (indFoundMarkValue == -1)
return -1;
return (start +indFoundMarkValue);
"x_JAsciiMath_DontModifyconst__begin";
*/
/* String strModifiedMathData, String markValue, int start, int end */
String strToken = null;
String strModifiedMathData = "" +JAsciiMath.cnstDontModifyBegin;
String markValue = "__";
int start = strModifiedMathData.indexOf(JAsciiMath.cnstDontModifyBegin);
int end = start + JAsciiMath.cnstDontModifyBegin.length();
int ret = jAsciiMath.getMarkValueIndexNearOfFoundToken(strModifiedMathData,
markValue, start, end);
if (ret != strModifiedMathData.indexOf(markValue))
{
System.err.println("Testi ajo 7 ei ok!!");
}
else
System.out.println("Testi ajo ok 7.");
strModifiedMathData = "dffks kdfsdk_" +JAsciiMath.cnstDontModifyBegin;
markValue = JAsciiMath.cnstDontModifyBegin;
strToken = "_x_";
start = strModifiedMathData.indexOf(JAsciiMath.cnstDontModifyBegin);
end = start + JAsciiMath.cnstDontModifyBegin.length();
ret = jAsciiMath.getMarkValueIndexNearOfFoundToken(strModifiedMathData,
markValue, start, end);
if (ret != strModifiedMathData.indexOf(markValue))
{
System.err.println("Testi ajo 8 ei ok!!");
}
else
System.out.println("Testi ajo ok 8.");
/*
String markValue,
String strToken, String strModifiedMathData, int start, int end
*/
boolean retBoolean = jAsciiMath.getPossibleModificationOfMarkWord(markValue,
strToken, strModifiedMathData, start, end);
if (!retBoolean)
{
System.err.println("Testi ajo 9 ei ok!!");
}
else
System.out.println("Testi ajo ok 9.");
boolean success = true;
String strInputData = "<span class=\"asciimath2\">dataa</span>";
jAsciiMath.setInputData(strInputData);
boolean bConvertAsciiMathDataWithSpaceRules = true;
jAsciiMath.setConvertAsciiMathDataWithSpaceRules(bConvertAsciiMathDataWithSpaceRules);
jAsciiMath.replaceAsciiMathBlocks();
String srtOutPut = jAsciiMath.getOutputData();
if (srtOutPut != null && !jAsciiMath.getOutputData().equals(strInputData))
{
success = false;
System.err.println("Testi ajo 0 ei ok!!");
}
else
System.out.println("Testi ajo ok 0.");
strInputData = "<span class=\"asciimath\">`dataa �`</span>";
jAsciiMath.setInputData(strInputData);
jAsciiMath.setConvertAsciiMathDataWithSpaceRules(bConvertAsciiMathDataWithSpaceRules);
jAsciiMath.replaceAsciiMathBlocks();
if (!jAsciiMath.getOutputData().equals(strInputData))
{
success = false;
System.err.println("Testi ajo 1 ei ok!!");
System.err.println("strInputData='" +strInputData+"'");
System.err.println(" output='" +jAsciiMath.getOutputData()+"'");
}
else
System.out.println("Testi ajo ok 1.");
System.out.flush();
System.out.println("");
strInputData =
"<span class=\"asciimath\">`dataa �`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
jAsciiMath.setInputData(strInputData);
jAsciiMath.setConvertAsciiMathDataWithSpaceRules(bConvertAsciiMathDataWithSpaceRules);
jAsciiMath.replaceAsciiMathBlocks();
if (!jAsciiMath.getOutputData().equals(strInputData))
{
success = false;
System.err.println("Testi ajo 2 ei ok!!");
System.err.println("strInputData='" +strInputData+"'");
System.err.println(" output='" +jAsciiMath.getOutputData()+"'");
}
else
System.out.println("Testi ajo ok 2.");
System.out.flush();
System.out.println("");
String strInputDataAsciiMath = "|>|>|>|<|<|<=|<=|>=|>=|sin|cos|tan|cot|sec|int|int_|ln|test_substring()|test_substring|";
strInputData = "<span class=\"asciimath\">`"
+ JAsciiMath.getChangeNormalCharactersIntoLTAndGTHtmlCharacters(strInputDataAsciiMath)
+ "`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
String alkup_input = "<span class=\"asciimath\">"
+ JAsciiMath.getChangeLTAndGTHtmlCharactersIntoNormalCharacters(JAsciiMath.getChangeNormalCharactersIntoLTAndGTHtmlCharacters(strInputDataAsciiMath))
+ "</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
jAsciiMath.setInputData(strInputData);
jAsciiMath.setConvertAsciiMathDataWithSpaceRules(bConvertAsciiMathDataWithSpaceRules);
jAsciiMath.replaceAsciiMathBlocks();
String strCompare = "<span class=\"asciimath\">`| > | > | > | < | < | <= | <= | >= | >= | sin | cos | tan | cot | sec | int |int_| ln | test_substring() |test_substring|`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
if (!jAsciiMath.getOutputData().equals(strCompare))
{
success = false;
System.err.println("Testi ajo 3 ei ok!!");
System.err.println("strInputData='" +strInputData+"'");
System.err.println(" strCompare='" +strCompare+"'");
System.err.println(" output='" +jAsciiMath.getOutputData()+"'");
}
else
{
System.out.println("Testi ajo ok 3.");
System.out.println("strInputData ='" +strInputData+"'");
System.out.println("looginen syöttö='" +alkup_input+"'");
System.out.println(" strCompare ='" +strCompare+"'");
System.out.println(" output ='" +jAsciiMath.getOutputData()+"'");
}
System.out.flush();
System.out.println("");
strInputData =
"<span class=\"asciimath\">`|)|(|>|-|+|=|!=|~~|*|(|test_substring| # |`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
jAsciiMath.setInputData(strInputData);
jAsciiMath.setConvertAsciiMathDataWithSpaceRules(bConvertAsciiMathDataWithSpaceRules);
jAsciiMath.replaceAsciiMathBlocks();
strCompare = "<span class=\"asciimath\">`| )|( | > | -| +| =| !=| ~~| *|( |test_substring|#|`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
if (!jAsciiMath.getOutputData().equals(strCompare))
{
success = false;
System.err.println("Testi ajo 4 ei ok!!");
System.err.println("strInputData='" +strInputData+"'");
System.err.println(" strCompare='" +strCompare+"'");
System.err.println(" output='" +jAsciiMath.getOutputData()+"'");
}
else
{
System.out.println("Testi ajo ok 4.");
System.out.println("strInputData='" +strInputData+"'");
System.out.println(" strCompare='" +strCompare+"'");
System.out.println(" output='" +jAsciiMath.getOutputData()+"'");
}
System.out.flush();
System.out.println("");
strInputData =
"<span class=\"asciimath\">`test_substring| .202 |1.222/1..222 1999. 123.22 1000`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
jAsciiMath.setInputData(strInputData);
jAsciiMath.setConvertAsciiMathDataWithSpaceRules(false);
jAsciiMath.setConvertAsciiMathDataCommaRule(true);
jAsciiMath.replaceAsciiMathBlocks();
strCompare = "<span class=\"asciimath\">`test_substring| .202 |1,222/1..222 1999. 123,22 1000`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
if (!jAsciiMath.getOutputData().equals(strCompare))
{
success = false;
System.err.println("Testi ajo 5 ei ok!!");
System.err.println("strInputData='" +strInputData+"'");
System.err.println(" strCompare='" +strCompare+"'");
System.err.println(" output='" +jAsciiMath.getOutputData()+"'");
}
else
{
System.out.println("Testi ajo ok 5.");
System.out.println("strInputData='" +strInputData+"'");
System.out.println(" strCompare='" +strCompare+"'");
System.out.println(" output='" +jAsciiMath.getOutputData()+"'");
}
System.out.flush();
System.out.println("");
strInputData =
"<span class=\"asciimath\">`test_substring| Iraq 11 > Ira>q 12 ,2 0,22 1222,2 11111 1.222,2 333.1222,2 444.1222,2 111111 32.22.222.22.222222.2,0 > Irqa Qrai IraQ`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
jAsciiMath.setInputData(strInputData);
jAsciiMath.setConvertAsciiMathDataWithSpaceRules(false);
jAsciiMath.setConvertAsciiMathDataCommaRule(false);
jAsciiMath.setConvertAsciiMathDataThousndNumberRule(true);
jAsciiMath.replaceAsciiMathBlocks();
strCompare = "<span class=\"asciimath\">`test_substring| Iraq 11 > Ira>q 12 ,2 0,22 1.222,2 11.111 1.222,2 3.331.222,2 4.441.222,2 111.111 3.222.222.222.222.222,0 > Irqa Qrai IraQ`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
if (!jAsciiMath.getOutputData().equals(strCompare))
{
success = false;
System.err.println("Testi ajo 6 ei ok!!");
System.err.println("strInputData='" +strInputData+"'");
System.err.println(" strCompare='" +strCompare+"'");
System.err.println(" output='" +jAsciiMath.getOutputData()+"'");
}
else
{
System.out.println("Testi ajo ok 6.");
System.out.println("strInputData='" +strInputData+"'");
System.out.println(" strCompare='" +strCompare+"'");
System.out.println(" output='" +jAsciiMath.getOutputData()+"'");
}
System.out.flush();
System.out.println("");
strInputDataAsciiMath = "|\">\"|\">\"|\">\"|\"<\"|\"<\"|\"<=\"|\"<=\"|\">=\"|\">=\"|\"sin\"|\"cos\"|\"tan\"|\"cot\"|\"sec\"|\"int\"|\"int_\"|\"ln\"|\"test_substring()\"|\"test_substring\"|";
strInputData = "<span class=\"asciimath\">`"
+ JAsciiMath.getChangeNormalCharactersIntoLTAndGTHtmlCharacters(strInputDataAsciiMath)
+ "`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
alkup_input = "<span class=\"asciimath\">`"
+ JAsciiMath.getChangeLTAndGTHtmlCharactersIntoNormalCharacters(JAsciiMath.getChangeNormalCharactersIntoLTAndGTHtmlCharacters(strInputDataAsciiMath))
+ "`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
jAsciiMath.setInputData(strInputData);
jAsciiMath.setConvertAsciiMathDataWithSpaceRules(bConvertAsciiMathDataWithSpaceRules);
jAsciiMath.replaceAsciiMathBlocks();
strCompare = "<span class=\"asciimath\">`|\">\"|\">\"|\">\"|\"<\"|\"<\"|\"<=\"|\"<=\"|\">=\"|\">=\"|\"sin\"|\"cos\"|\"tan\"|\"cot\"|\"sec\"|\"int\"|\"int_\"|\"ln\"|\"test_substring()\"|\"test_substring\"|`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
if (!jAsciiMath.getOutputData().equals(strCompare))
{
success = false;
System.err.println("Testi ajo 7.1 ei ok!!");
System.err.println("strInputData='" +strInputData+"'");
System.err.println(" strCompare='" +strCompare+"'");
System.err.println(" output='" +jAsciiMath.getOutputData()+"'");
}
else
{
System.out.println("Testi ajo ok 7.1");
System.out.println("strInputData ='" +strInputData+"'");
System.out.println("looginen syöttö='" +alkup_input+"'");
System.out.println(" strCompare ='" +strCompare+"'");
System.out.println(" output ='" +jAsciiMath.getOutputData()+"'");
}
System.out.flush();
System.out.println("");
JAsciiMath jAsciiMath2 = new JAsciiMath("dtbookasciimath.properties",
bCheckStrikeErrors, bCorrectStrikeErrors, true);
jAsciiMath2.setConvertHtmlChars(false);
strInputData =
"<span class=\"asciimath\">`|)|(\"|)|(\"|)|(\"|)|(\"`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
jAsciiMath2.setInputData(strInputData);
jAsciiMath2.setConvertAsciiMathDataWithSpaceRules(true);
jAsciiMath2.setConvertAsciiMathDataCommaRule(true);
jAsciiMath2.replaceAsciiMathBlocks();
strCompare = "<span class=\"asciimath\">`| )|( \"|)|(\"| )|( \"|)|(\" )`</span> <span class=\"asciimath2\">`dataa �2`</span> <span class=\"asciimath\">`dataa �3`</span> <b/>";
if (!jAsciiMath2.getOutputData().equals(strCompare))
{
success = false;
System.err.println("Testi ajo 8 ei ok!!");
System.err.println("strInputData='" +strInputData+"'");
System.err.println(" strCompare='" +strCompare+"'");
System.err.println(" output='" +jAsciiMath2.getOutputData()+"'");
}
else
{
System.out.println("Testi ajo ok 8.");
System.out.println("strInputData='" +strInputData+"'");
System.out.println(" strCompare='" +strCompare+"'");
System.out.println(" output='" +jAsciiMath2.getOutputData()+"'");
}
boolean bAddBrowserStrikes = false;
System.out.println();
String p_strValue = "x";
strCompare = "`x`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "x``";
strCompare = "`x`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "``x";
strCompare = "`x`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "x`";
strCompare = "`x`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "`x";
strCompare = "`x`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "x`y";
strCompare = "`xy`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "x`y`";
strCompare = "`xy`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "`x`y";
strCompare = "`xy`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "`x``y";
strCompare = "`xy`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "`x````z``y`";
strCompare = "`xzy`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "`x`z`y`";
strCompare = "`xzy`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
bAddBrowserStrikes = true;
p_strValue = "`; &x& &z&y & &var";
strCompare = "`; &x& &z&y & &var";
testReplaceAmpCharacterIfnotReservedVariables(p_strValue, strCompare);
p_strValue = "`; &x&; ;&z&y & ;&var;";
strCompare = "`; &x&; ;&z&y & ;&var;";
testReplaceAmpCharacterIfnotReservedVariables(p_strValue, strCompare);
// test " ssss ssss " / " converting into "xxx xxxx" /
p_strValue = "` \" xxx xxxx \" / \"sss\"`";
strCompare = "` (\" xxx xxxx \") / (\"sss\")`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "` \"xxx xxxx \" / \"sss\"`";
strCompare = "` (\"xxx xxxx \") / (\"sss\")`";;
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "` \" xxx xxxx\" / \"sss\"`";
strCompare = "` (\" xxx xxxx\") / (\"sss\")`";;
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "` \"xxx xxxx\" / \"sss\"`";
strCompare = "` (\"xxx xxxx\") / (\"sss\")`";;
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "` \" xxx xxxx \"/\"sss\"`";
strCompare = "` (\" xxx xxxx \")/(\"sss\")`";;
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "` \"xxx xxxx\"/\"sss\"`";
strCompare = "` (\"xxx xxxx\")/(\"sss\")`";;
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "` (\" xxx xxxx \") = (\" sss \")`";
strCompare = "` (\" xxx xxxx \") = (\" sss \")`";;
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "`A_S = \"sektorin pinta-ala\"`";
strCompare = "`A_S = (\"sektorin pinta-ala\")`";
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
p_strValue = "` \"xxx xxxx\"/sss\"`";
strCompare = "` (\"xxx xxxx\") / (\"sss\")`";;
testWrongStrikeErrorCorrection(p_strValue, strCompare, bAddBrowserStrikes);
/*
p_strValue = "` \"xxx xxxx\"/\"sss\"`";
strCompare = "` \"xxx xxxx\" / \"sss\"`";;
testWrongStrikeErrorCorrection(p_strValue, strCompare);
*/
if (success)
System.exit(0);
else
System.exit(1);
}catch(Exception e){
e.printStackTrace();
}
}
private static void
testReplaceAmpCharacterIfnotReservedVariables(String value, String strCompare)
throws DtbookAsciiMathException, NullPointerException
{
String returned = AsciiMathXml.replaceAmpCharacterIfnotReservedVariables(value);
if (!strCompare.equals(returned))
{
System.err.println("Testi ei ok!!");
System.err.println(" strInputData='" +value+"'");
System.err.println(" strCompare='" + strCompare +"'");
System.err.println("after asciimath='" + returned +"'");
}
else
{
System.out.println("before value='" + value +"'");
System.out.println(" after value='" + returned +"'");
}
System.out.println();
}
private static void testWrongStrikeErrorCorrection(String value, String strCompare,
boolean bAddBrowserStrikes)
throws DtbookAsciiMathException, NullPointerException
{
boolean bCorrectStrikeErrors = true;
boolean bCheckStrikeErrors = true;
AsciiMathBlockValue test1 = new AsciiMathBlockValue(value,
0, 0, 0, 0, 0, 0, bCheckStrikeErrors, bCorrectStrikeErrors,
bAddBrowserStrikes);
if (test1 == null)
throw new NullPointerException("AsciiMathBlockValue is null!");
String test1_asciimathValue = test1.getAsciiMathValue();
if (test1_asciimathValue == null)
throw new NullPointerException("test1.getAsciiMathValue() is null!");
if (!strCompare.equals(test1_asciimathValue))
{
System.err.println("Testi ei ok!!");
System.err.println("strInputData='" +value+"'");
System.err.println(" strCompare='" + strCompare +"'");
System.err.println("after modification of asciimath='" + test1_asciimathValue +"'");
}
else
{
System.out.println("before value='" + value +"'");
System.out.println("after value='" + test1_asciimathValue +"'");
}
System.out.println();
}
}