Skip to content

Commit eb509a5

Browse files
author
linzhijun
committed
添加 测试单元,修改一个bug
1 parent a1dce82 commit eb509a5

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

csharp/ToolGood.Algorithm2.Test/AlgorithmEngine/AlgorithmEngineTest_sum.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,34 @@ public void COUNTIF_test()
353353
AlgorithmEngine engine = new AlgorithmEngine();
354354
var t = engine.TryEvaluate("COUNTIF(array(1,2,3,4,2,2,1,4),'>1')", 0.0);
355355
Assert.AreEqual(t, 6.0);
356+
t = engine.TryEvaluate("COUNTIF(array(1,2,3,4,2,2,1,4),'>=1')", 0.0);
357+
Assert.AreEqual(t, 8.0);
358+
t = engine.TryEvaluate("COUNTIF(array(1,2,3,4,2,2,1,4),'!=1')", 0.0);
359+
Assert.AreEqual(t, 6.0);
356360
}
357-
358-
[Test]
361+
362+
[Test]
359363
public void SUMIF_test()
360364
{
361365
AlgorithmEngine engine = new AlgorithmEngine();
362366
var t = engine.TryEvaluate("SUMIF(array(1,2,3,4,2,2,1,4),'>1')", 0.0);
363367
Assert.AreEqual(t, 17);
364368
t = engine.TryEvaluate("SUMIF(array(1,2,3,4,2,2,1,4),'>1',array(1,1,1,1,1,1,1,1))", 0.0);
365369
Assert.AreEqual(t, 6);
370+
t = engine.TryEvaluate("SUMIF(array(1,2,3,4,2,2,1,4),'>=2',array(1,1,1,1,1,1,1,1))", 0.0);
371+
Assert.AreEqual(t, 6);
372+
t = engine.TryEvaluate("SUMIF(array(1,2,3,4,2,2,1,4),'<1',array(1,1,1,1,1,1,1,1))", 0.0);
373+
Assert.AreEqual(t, 0);
374+
t = engine.TryEvaluate("SUMIF(array(1,2,3,4,2,2,1,4),'==1',array(1,1,1,1,1,1,1,1))", 0.0);
375+
Assert.AreEqual(t, 2);
366376
}
367377
[Test]
368378
public void AVERAGEIF_test()
369379
{
370380
AlgorithmEngine engine = new AlgorithmEngine();
371381
var t = engine.TryEvaluate("AVERAGEIF(array(1,2,3,4,2,2,1,4),'>1')", 0.0);
372-
Assert.AreEqual(Math.Round(t, 6), Math.Round(2.833333333, 6));
382+
Assert.AreEqual(t, 2.833333333, 6);
383+
373384
t = engine.TryEvaluate("AVERAGEIF(array(1,2,3,4,2,2,1,4),'>1',array(1,1,1,1,1,1,1,1))", 0.0);
374385
Assert.AreEqual(t, 1);
375386
}

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public virtual Operand VisitMulDiv_fun(mathParser.MulDiv_funContext context)
4646
} else if (DateTime.TryParse(args1.TextValue, cultureInfo, DateTimeStyles.None, out DateTime dt)) {
4747
args1 = Operand.Create(new MyDate(dt));
4848
} else {
49-
return Operand.Error("两个类型无法乘除");
49+
return Operand.Error("Two types cannot be multiplied or divided.");
5050
}
5151
}
5252
if (args2.Type == OperandType.TEXT) {
@@ -59,7 +59,7 @@ public virtual Operand VisitMulDiv_fun(mathParser.MulDiv_funContext context)
5959
} else if (DateTime.TryParse(args2.TextValue, cultureInfo, DateTimeStyles.None, out DateTime dt)) {
6060
args2 = Operand.Create(new MyDate(dt));
6161
} else {
62-
return Operand.Error("两个类型无法乘除");
62+
return Operand.Error("Two types cannot be multiplied or divided.");
6363
}
6464
}
6565
var t = context.op.Text;
@@ -539,7 +539,7 @@ public virtual Operand VisitQUOTIENT_fun(mathParser.QUOTIENT_funContext context)
539539
if (secondValue.NumberValue == 0) {
540540
return Operand.Error("Function QUOTIENT div 0 error!");
541541
}
542-
return Operand.Create((double)(int)(firstValue.NumberValue / secondValue.NumberValue));
542+
return Operand.Create((int)(firstValue.NumberValue / secondValue.NumberValue));
543543
}
544544
public virtual Operand VisitMOD_fun(mathParser.MOD_funContext context)
545545
{
@@ -1039,10 +1039,10 @@ public virtual Operand VisitROUND_fun(mathParser.ROUND_funContext context)
10391039

10401040
var firstValue = args[0];
10411041
if (args.Count == 1) {
1042-
return Operand.Create((double)Math.Round((decimal)firstValue.NumberValue, 0, MidpointRounding.AwayFromZero));
1042+
return Operand.Create(Math.Round((decimal)firstValue.NumberValue, 0, MidpointRounding.AwayFromZero));
10431043
}
10441044
var secondValue = args[1];
1045-
return Operand.Create((double)Math.Round((decimal)firstValue.NumberValue, secondValue.IntValue, MidpointRounding.AwayFromZero));
1045+
return Operand.Create(Math.Round((decimal)firstValue.NumberValue, secondValue.IntValue, MidpointRounding.AwayFromZero));
10461046
}
10471047
public virtual Operand VisitROUNDDOWN_fun(mathParser.ROUNDDOWN_funContext context)
10481048
{
@@ -1137,7 +1137,6 @@ public virtual Operand VisitEVEN_fun(mathParser.EVEN_funContext context)
11371137
}
11381138
public virtual Operand VisitODD_fun(mathParser.ODD_funContext context)
11391139
{
1140-
11411140
var firstValue = context.expr().Accept(this).ToNumber("Function ODD parameter is error!");
11421141
if (firstValue.IsError) { return firstValue; }
11431142
var z = firstValue.NumberValue;
@@ -1331,9 +1330,7 @@ public virtual Operand VisitSUMSQ_fun(mathParser.SUMSQ_funContext context)
13311330
}
13321331
private static int F_base_Factorial(int a)
13331332
{
1334-
if (a == 0) {
1335-
return 1;
1336-
}
1333+
if (a == 0) { return 1; }
13371334
int r = 1;
13381335
for (int i = a; i > 0; i--) {
13391336
r *= i;
@@ -1387,7 +1384,7 @@ public virtual Operand VisitCODE_fun(mathParser.CODE_funContext context)
13871384
if (t.Length == 0) {
13881385
return Operand.Error("Function CODE parameter is error!");
13891386
}
1390-
return Operand.Create((double)(int)(char)t[0]);
1387+
return Operand.Create((int)t[0]);
13911388
}
13921389
public virtual Operand VisitCONCATENATE_fun(mathParser.CONCATENATE_funContext context)
13931390
{
@@ -2168,7 +2165,6 @@ public virtual Operand VisitMODE_fun(mathParser.MODE_funContext context)
21682165
var o = F_base_GetList(args, list);
21692166
if (o == false) { return Operand.Error("Function MODE parameter error!"); }
21702167

2171-
21722168
Dictionary<decimal, int> dict = new Dictionary<decimal, int>();
21732169
foreach (var item in list) {
21742170
if (dict.ContainsKey(item)) {
@@ -2686,16 +2682,16 @@ public virtual Operand VisitFISHER_fun(mathParser.FISHER_funContext context)
26862682
if (x >= 1 || x <= -1) {
26872683
return Operand.Error("Function FISHER parameter error!");
26882684
}
2689-
var n = 0.5 * Math.Log((double)(1 + x) / (double)(1 - x));
2685+
var n = 0.5 * Math.Log((double)((1 + x) / (1 - x)));
26902686
return Operand.Create(n);
26912687
}
26922688
public virtual Operand VisitFISHERINV_fun(mathParser.FISHERINV_funContext context)
26932689
{
26942690
var firstValue = context.expr().Accept(this).ToNumber("Function FISHERINV parameter error!");
26952691
if (firstValue.IsError) { return firstValue; }
26962692

2697-
var x = firstValue.NumberValue;
2698-
var n = (Math.Exp((double)(2 * x)) - 1) / (Math.Exp((double)(2 * x)) + 1);
2693+
var x = (double)firstValue.NumberValue;
2694+
var n = (Math.Exp((2 * x)) - 1) / (Math.Exp((2 * x)) + 1);
26992695
return Operand.Create(n);
27002696
}
27012697
public virtual Operand VisitGAMMADIST_fun(mathParser.GAMMADIST_funContext context)
@@ -2867,7 +2863,7 @@ private static int F_base_countif(List<decimal> dbs, decimal d)
28672863
int count = 0;
28682864
//d = Math.Round(d, 12, MidpointRounding.AwayFromZero);
28692865
foreach (var item in dbs) {
2870-
if (item==d) {
2866+
if (item == d) {
28712867
count++;
28722868
}
28732869
//if (Math.Round(item, 12, MidpointRounding.AwayFromZero) == d) {
@@ -2880,10 +2876,11 @@ private static int F_base_countif(List<decimal> dbs, string s)
28802876
{
28812877
var m = sumifRegex.Match(s);
28822878
var d = decimal.Parse(m.Groups[2].Value, NumberStyles.Any, cultureInfo);
2879+
var ss = m.Groups[1].Value;
28832880
int count = 0;
28842881

28852882
foreach (var item in dbs) {
2886-
if (MathVisitor.F_base_compare(item, d, s)) {
2883+
if (MathVisitor.F_base_compare(item, d, ss)) {
28872884
count++;
28882885
}
28892886
}
@@ -2894,11 +2891,11 @@ private static decimal F_base_sumif(List<decimal> dbs, string s, List<decimal> s
28942891
{
28952892
var m = sumifRegex.Match(s);
28962893
var d = decimal.Parse(m.Groups[2].Value, NumberStyles.Any, cultureInfo);
2897-
//var ss = m.Groups[1].Value;
2894+
var ss = m.Groups[1].Value;
28982895
decimal sum = 0;
28992896

29002897
for (int i = 0; i < dbs.Count; i++) {
2901-
if (MathVisitor.F_base_compare(dbs[i], d, s)) {
2898+
if (MathVisitor.F_base_compare(dbs[i], d, ss)) {
29022899
sum += sumdbs[i];
29032900
}
29042901
}
@@ -2919,7 +2916,7 @@ private static bool F_base_compare(decimal a, decimal b, string ss)
29192916
return (a >= b);
29202917
//return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) >= 0;
29212918
} else if (CharUtil.Equals(ss, "=", "==", "===")) {
2922-
return a==b;
2919+
return a == b;
29232920
//return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) == 0;
29242921
}
29252922
return a != b;
@@ -3822,13 +3819,6 @@ public virtual Operand VisitDiyFunction_fun(mathParser.DiyFunction_funContext co
38223819
return Operand.Error("DiyFunction is error!");
38233820
}
38243821

3825-
3826-
3827-
3828-
3829-
3830-
3831-
38323822
#endregion
38333823
}
38343824
}

csharp/ToolGood.Algorithm2/ToolGood.Algorithm2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<Product>ToolGood.Algorithm</Product>
2222
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2323
<SignAssembly>true</SignAssembly>
24-
<Version>3.3.0.0</Version>
24+
<Version>3.3.0.1</Version>
2525
<AssemblyOriginatorKeyFile>ToolGood.Algorithm.snk</AssemblyOriginatorKeyFile>
2626
<DelaySign>false</DelaySign>
2727
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\ToolGood.Algorithm.xml</DocumentationFile>

0 commit comments

Comments
 (0)