Skip to content

Commit

Permalink
Unit Tests: To add Calculation Steps Register #260
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuszgromada committed May 20, 2022
1 parent 3fa6e9d commit 2d60bd7
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,33 +208,33 @@ public enum StepType {
* the calculation process will be done based on the calculation tree, where each group
* will be a marker of a particular (different) leaf of this tree.
*/
public int stepNumberGroup = 0;
public int numberGroup = 0;
/**
* Marker of a step in a process of calculation of a given expression pointed by
* a particular leaf in a calculation tree.
*/
public int stepNumberGroupWithin = 0;
public int numberGroupWithin = 0;
/**
* Calculation step type
* @see StepType
*/
public StepType stepType;
public StepType type;
/**
* Calculation step description - can be ana argument name, function name and or
* an expression string
*/
public String stepDescription;
public String description;
/**
* Calculation step content - so what is really happening in this step
*/
public String stepContent;
public String content;
/**
* True if this is a first step in a given group.
*/
public bool firstStepInGroup = false;
public bool firstInGroup = false;
/**
* True if this is a last step in a given group.
*/
public bool lastStepInGroup = false;
public bool lastInGroup = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ public class CalcStepsRegister {
* then function expression string is provided here.
*/
public String expressionStringStart = "";
/**
* Final result of calculation
*/
public double result = Double.NaN;
/**
* Duration of calculation process - in seconds
*/
public double computingTime = Double.NaN;
/**
* Information on error or lack of error
* while performing calculation process
*/
public String errorMessage = "";
/**
* Prints this step register to the console.
*/
Expand All @@ -222,28 +235,29 @@ public void consolePrint() {
mXparser.consolePrint("CalcStepsRegister for ");
bool toPrintEq = false;
if (argumentNameStart.Length > 0) {
mXparser.consolePrint("Argument ");
mXparser.consolePrint("Argument " + argumentNameStart);
toPrintEq = true;
}
if (functionNameStart.Length > 0) {
mXparser.consolePrint("Function ");
mXparser.consolePrint("Function " + functionNameStart);
toPrintEq = true;
}
if (toPrintEq)
mXparser.consolePrint(" = ");
else
mXparser.consolePrint("Expression ");
mXparser.consolePrintln(expressionStringStart);
mXparser.consolePrintln(expressionStringStart + ", result = " + result);
foreach (CalcStepRecord stepRecord in calcStepRecords)
mXparser.consolePrintln(
"gr = " + stepRecord.stepNumberGroup
+ ", nr = " + stepRecord.stepNumberGroupWithin
+ ", first = " + stepRecord.firstStepInGroup
+ ", last = " + stepRecord.lastStepInGroup
+ ", type = " + stepRecord.stepType
+ ", descr = " + stepRecord.stepDescription
+ ", step = " + stepRecord.stepContent
"gr = " + stepRecord.numberGroup
+ ", nr = " + stepRecord.numberGroupWithin
+ ", first = " + stepRecord.firstInGroup
+ ", last = " + stepRecord.lastInGroup
+ ", type = " + stepRecord.type
+ ", descr = " + stepRecord.description
+ ", step = " + stepRecord.content
);
mXparser.consolePrintln("Computing time = " + computingTime + " s.");
}
internal bool isStartSet = false;
internal int stepNumberGroup = 0;
Expand All @@ -267,22 +281,22 @@ internal static void setUserArgument(CalcStepsRegister calcStepsRegister, Argume
calcStepsRegister.stepTypeSetUserArgument();
if (calcStepsRegister.isStartSet) return;
calcStepsRegister.argumentNameStart = argument.getArgumentName();
calcStepsRegister.expressionStringStart = argument.getArgumentExpressionString();
calcStepsRegister.expressionStringStart = argument.getArgumentExpressionString().Trim();
calcStepsRegister.isStartSet = true;
}
internal static void setUserFunction(CalcStepsRegister calcStepsRegister, Function function) {
if (calcStepsRegister == null) return;
calcStepsRegister.stepTypeSetUserFunction();
if (calcStepsRegister.isStartSet) return;
calcStepsRegister.functionNameStart = function.getFunctionName();
calcStepsRegister.expressionStringStart = function.getFunctionExpressionString();
calcStepsRegister.expressionStringStart = function.getFunctionExpressionString().Trim();
calcStepsRegister.isStartSet = true;
}
internal static void stepNumberGroupIncrease(CalcStepsRegister calcStepsRegister, Expression expression) {
if (calcStepsRegister == null) return;
calcStepsRegister.stepNumberGroup++;
if (calcStepsRegister.isStartSet) return;
calcStepsRegister.expressionStringStart = expression.getExpressionString();
calcStepsRegister.expressionStringStart = expression.getExpressionString().Trim();
calcStepsRegister.isStartSet = true;
}
}
Expand Down
48 changes: 27 additions & 21 deletions CURRENT/c-sharp/src/org/mariuszgromada/math/mxparser/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5821,13 +5821,13 @@ public double calculate(CalcStepsRegister calcStepsRegister) {
loopCounter++;
if (calcStepsRegister != null) {
CalcStepRecord stepRecord = new CalcStepRecord();
stepRecord.stepNumberGroup = calcStepsRegister.stepNumberGroup;
stepRecord.stepNumberGroupWithin = loopCounter;
stepRecord.stepDescription = stepDescription;
stepRecord.stepContent = tokensListToString();
stepRecord.stepType = calcStepsRegister.stepType;
stepRecord.numberGroup = calcStepsRegister.stepNumberGroup;
stepRecord.numberGroupWithin = loopCounter;
stepRecord.description = stepDescription;
stepRecord.content = tokensListToString();
stepRecord.type = calcStepsRegister.stepType;
if (loopCounter == 1)
stepRecord.firstStepInGroup = true;
stepRecord.firstInGroup = true;
calcStepsRegister.calcStepRecords.Add(stepRecord);
}
if (mXparser.isCurrentCalculationCancelled()) {
Expand Down Expand Up @@ -6248,18 +6248,6 @@ public double calculate(CalcStepsRegister calcStepsRegister) {

} while (tokensList.Count > 1);

if (calcStepsRegister != null) {
CalcStepRecord stepRecord = new CalcStepRecord();
stepRecord.stepNumberGroup = calcStepsRegister.stepNumberGroup;
stepRecord.stepNumberGroupWithin = loopCounter;
stepRecord.stepDescription = stepDescription;
stepRecord.stepContent = tokensListToString();
stepRecord.stepType = calcStepsRegister.stepType;
stepRecord.lastStepInGroup = true;
calcStepsRegister.calcStepRecords.Add(stepRecord);
calcStepsRegister.stepNumberGroup--;
}

if (verboseMode == true) {
//printSystemInfo("\n", WITH_EXP_STR);
printSystemInfo("Calculated value: " + tokensList[0].tokenValue + "\n", WITH_EXP_STR);
Expand All @@ -6274,11 +6262,29 @@ public double calculate(CalcStepsRegister calcStepsRegister) {
if (mXparser.almostIntRounding) {
double resultint = Math.Round(result);
if ( Math.Abs(result-resultint) <= BinaryRelations.getEpsilon() )
return resultint;
result = resultint;
}
else if (mXparser.canonicalRounding)
result = MathFunctions.canonicalRound(result);
}

if (calcStepsRegister != null) {
CalcStepRecord stepRecord = new CalcStepRecord();
stepRecord.numberGroup = calcStepsRegister.stepNumberGroup;
stepRecord.numberGroupWithin = loopCounter;
stepRecord.description = stepDescription;
stepRecord.content = tokensListToString();
stepRecord.type = calcStepsRegister.stepType;
stepRecord.lastInGroup = true;
calcStepsRegister.calcStepRecords.Add(stepRecord);
calcStepsRegister.stepNumberGroup--;
if (calcStepsRegister.stepNumberGroup == 0) {
calcStepsRegister.result = result;
calcStepsRegister.computingTime = computingTime;
calcStepsRegister.errorMessage = errorMessage;
}
if (mXparser.canonicalRounding)
return MathFunctions.canonicalRound(result);
}

return result;
}
/**
Expand Down
72 changes: 71 additions & 1 deletion CURRENT/c-sharp/tests-and-release/4-Unit-Tests/ApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace org.mariuszgromada.math.mxparser.test {
* <a href="https://play.google.com/store/apps/details?id=org.mathparser.scalar.pro" target="_blank">Scalar Pro</a><br>
* <a href="https://mathspace.pl" target="_blank">MathSpace.pl</a><br>
*
* @version 5.0.0
* @version 5.0.3
*
*/
[TestClass]
Expand Down Expand Up @@ -3826,6 +3826,76 @@ public void testApi0147() {
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testApi0148() {
TestCommonTools.testApiSettingsInit();
bool testResult = true;
String testDescr = "Calc Steps Register - Expression 2*(3+4)+5";
TestCommonTools.consolePrintTestApiStart(148, testDescr);
Expression e = new Expression("2*(3+4)+5");
CalcStepsRegister calcStepsRegister = new CalcStepsRegister();
double result = e.calculate(calcStepsRegister);
if (result != calcStepsRegister.result) testResult = false;
if (!e.getExpressionString().Equals(calcStepsRegister.expressionStringStart)) testResult = false;
if (calcStepsRegister.argumentNameStart.Length > 0) testResult = false;
if (calcStepsRegister.functionNameStart.Length > 0) testResult = false;
if (calcStepsRegister.calcStepRecords.Count != 5) testResult = false;
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testApi0149() {
TestCommonTools.testApiSettingsInit();
bool testResult = true;
String testDescr = "Calc Steps Register - Argument x = 2*(3+4)+5";
TestCommonTools.consolePrintTestApiStart(149, testDescr);
Argument x = new Argument("x", "2*(3+4)+5");
CalcStepsRegister calcStepsRegister = new CalcStepsRegister();
double result = x.getArgumentValue(calcStepsRegister); if (result != calcStepsRegister.result) testResult = false;
if (!x.getArgumentName().Equals(calcStepsRegister.argumentNameStart)) testResult = false;
if (!x.getArgumentExpressionString().Equals(calcStepsRegister.expressionStringStart)) testResult = false;
if (calcStepsRegister.functionNameStart.Length > 0) testResult = false;
if (calcStepsRegister.calcStepRecords.Count != 5) testResult = false;
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testApi0150() {
TestCommonTools.testApiSettingsInit();
bool testResult = true;
String testDescr = "Calc Steps Register - Function f(x) = 2*(3+4)+5";
TestCommonTools.consolePrintTestApiStart(150, testDescr);
Function f = new Function("f(x) = 2*(3+4)+5");
CalcStepsRegister calcStepsRegister = new CalcStepsRegister();
double result = f.calculate(calcStepsRegister);
if (!f.getFunctionName().Equals(calcStepsRegister.functionNameStart)) testResult = false;
if (!f.getFunctionExpressionString().Trim().Equals(calcStepsRegister.expressionStringStart)) testResult = false;
if (calcStepsRegister.argumentNameStart.Length > 0) testResult = false;
if (calcStepsRegister.calcStepRecords.Count != 5) testResult = false;
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
[TestMethod]
public void testApi0151() {
TestCommonTools.testApiSettingsInit();
bool testResult = true;
String testDescr = "Calc Steps Register - Expression 2*(3+4)+5";
TestCommonTools.consolePrintTestApiStart(151, testDescr);
CalcStepsRegister calcStepsRegister = new CalcStepsRegister();
Argument x = new Argument("x", "2*rList(2,1,3,5,4)");
Argument y = new Argument("y = 2*x", x);
Argument z = new Argument("z = 3*y/x", x, y);
Function f = new Function("f(a,b) = a*b");
Expression e = new Expression("f(x,y)+z+(x*y)/z", x, y, z, f);
double result = e.calculate(calcStepsRegister);
if (result != calcStepsRegister.result) testResult = false;
if (!e.getExpressionString().Equals(calcStepsRegister.expressionStringStart)) testResult = false;
if (calcStepsRegister.argumentNameStart.Length > 0) testResult = false;
if (calcStepsRegister.functionNameStart.Length > 0) testResult = false;
if (calcStepsRegister.calcStepRecords.Count != 72) testResult = false;
TestCommonTools.consolePrintTestApiEnd(testResult);
Assert.IsTrue(testResult);
}
public static bool testCanonicalString(String expStr, String expResStr, params String[] elements) {
Expression e = new Expression(expStr);
if (elements != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,32 +205,32 @@ public enum StepType {
* the calculation process will be done based on the calculation tree, where each group
* will be a marker of a particular (different) leaf of this tree.
*/
public int stepNumberGroup = 0;
public int numberGroup = 0;
/**
* Marker of a step in a process of calculation of a given expression pointed by
* a particular leaf in a calculation tree.
*/
public int stepNumberGroupWithin = 0;
public int numberGroupWithin = 0;
/**
* Calculation step type
* @see StepType
*/
public StepType stepType;
public StepType type;
/**
* Calculation step description - can be ana argument name, function name and or
* an expression string
*/
public String stepDescription;
public String description;
/**
* Calculation step content - so what is really happening in this step
*/
public String stepContent;
public String content;
/**
* True if this is a first step in a given group.
*/
public boolean firstStepInGroup = false;
public boolean firstInGroup = false;
/**
* True if this is a last step in a given group.
*/
public boolean lastStepInGroup = false;
public boolean lastInGroup = false;
}
Loading

0 comments on commit 2d60bd7

Please sign in to comment.