Skip to content

Commit 7d93e10

Browse files
committed
添加 单元测试
1 parent 74ea82a commit 7d93e10

File tree

4 files changed

+132
-7
lines changed

4 files changed

+132
-7
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public void Test()
108108
var t1 = engine.TryEvaluate("-7 < -2 ?1 : 2", 0);
109109
Assert.AreEqual(t1, 1);
110110
t1 = engine.TryEvaluate("-7 < -2 ?1 : 2", 0);
111+
Assert.AreEqual(t1, 1);
112+
111113
t1 = engine.TryEvaluate("-7 < -2 ?1 :2", 0);
112114
Assert.AreEqual(t1, 1);
113115
t1 = engine.TryEvaluate("-7 < -2 ? 1 : 2", 0);
@@ -175,7 +177,6 @@ public void Cylinder_Test()
175177
Cylinder c = new Cylinder(3, 10);
176178
var t = c.TryEvaluate("[半径]*[半径]*pi()", 0.0); //圆底面积
177179
var t2 = c.TryEvaluate("半径*半径*pi()", 0.0); //圆底面积
178-
179180
var t3 = c.TryEvaluate("{半径}*{半径}*pi()", 0.0); //圆底面积
180181
var t4 = c.TryEvaluate("@半径*@半径*pi()", 0.0); //圆底面积
181182
var t5 = c.TryEvaluate("#半径#*#半径#*pi()", 0.0); //圆底面积
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using PetaTest;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using ToolGood.Algorithm;
8+
9+
namespace ToolGood.Algorithm2.Test.AlgorithmEngineEx
10+
{
11+
[TestFixture]
12+
public class AlgorithmEngineExTest
13+
{
14+
[Test]
15+
public void Test()
16+
{
17+
MultiConditionCache multiConditionCache = new MultiConditionCache();
18+
multiConditionCache.AddFormula("桌面积", "[圆桌]", "[半径]*[半径]*pi()");
19+
multiConditionCache.AddFormula("桌面积", "[方桌]", "[长]*[宽]");
20+
21+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<2.5", "[桌面积]*1.3");
22+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<5", "[桌面积]*1.5");
23+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<7", "[桌面积]*2");
24+
multiConditionCache.AddFormula("价格", "", "[桌面积]*2.5");
25+
26+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<1.3", "[桌面积]*1.3+[高]*1.1");
27+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<2", "[桌面积]*1.5+[高]*1.1");
28+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<5", "[桌面积]*2+[高]*1.1");
29+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<7", "[桌面积]*2.5");
30+
31+
multiConditionCache.AddFormula("出错了", "[方桌]&& [长]<11", "[出错]*2.5");
32+
33+
34+
Desk desk = new Desk() {
35+
IsRoundTable = true,
36+
Radius = 3
37+
};
38+
PriceAlgorithm priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk);
39+
var p1 = priceAlgorithm.TryEvaluate("价格", 0.0);
40+
Assert.AreEqual(3 * 3 * Math.PI * 1.5, p1, 0.0001);
41+
42+
Desk desk2 = new Desk() {
43+
IsRoundTable = false,
44+
Length = 3,
45+
Width = 1.3,
46+
Heigth = 1
47+
};
48+
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk2);
49+
var p2 = priceAlgorithm.TryEvaluate("价格", 0.0);
50+
Assert.AreEqual(3 * 1.3 * 2 + 1 * 1.1, p2, 0.0001);
51+
52+
53+
Desk desk3 = new Desk() {
54+
IsRoundTable = false,
55+
Length = 9,
56+
Width = 1.3,
57+
Heigth = 1
58+
};
59+
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk3);
60+
var p3 = priceAlgorithm.TryEvaluate("价格", 0.0);
61+
Assert.AreEqual(0, p3, 0.001);
62+
Assert.AreEqual("CategoryName [价格] is missing.", priceAlgorithm.LastError);
63+
64+
65+
var p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);
66+
67+
68+
}
69+
70+
71+
72+
}
73+
74+
public class Desk
75+
{
76+
public bool IsRoundTable { get; set; }
77+
public double Heigth { get; set; }
78+
public double Length { get; set; }
79+
public double Width { get; set; }
80+
public double Radius { get; set; }
81+
}
82+
83+
public class PriceAlgorithm : ToolGood.Algorithm.AlgorithmEngineEx
84+
{
85+
private Desk _disk;
86+
public PriceAlgorithm(MultiConditionCache multiConditionCache, Desk desk) : base(multiConditionCache)
87+
{
88+
_disk = desk;
89+
}
90+
91+
protected override Operand GetParameter(string parameter)
92+
{
93+
if (parameter == "长") {
94+
return Operand.Create(_disk.Length);
95+
}
96+
if (parameter == "宽") {
97+
return Operand.Create(_disk.Width);
98+
}
99+
if (parameter == "高") {
100+
return Operand.Create(_disk.Heigth);
101+
}
102+
if (parameter == "半径") {
103+
return Operand.Create(_disk.Radius);
104+
}
105+
if (parameter == "方桌") {
106+
return Operand.Create(_disk.IsRoundTable == false);
107+
}
108+
if (parameter == "圆桌") {
109+
return Operand.Create(_disk.IsRoundTable);
110+
}
111+
return base.GetParameter(parameter);
112+
}
113+
114+
}
115+
116+
}

csharp/ToolGood.Algorithm2/AlgorithmEngineEx.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class AlgorithmEngineEx
2626
/// 自定义 函数
2727
/// </summary>
2828
public event Func<string, List<Operand>, Operand> DiyFunction;
29+
/// <summary>
30+
/// 多条件缓存
31+
/// </summary>
2932
public MultiConditionCache MultiConditionCache { get; private set; }
3033

3134
public AlgorithmEngineEx(MultiConditionCache multiConditionCache)
@@ -51,13 +54,13 @@ protected virtual Operand GetParameter(string parameter)
5154
if (conditionCache.Condition != null) {
5255
var b = Evaluate(conditionCache.Condition);
5356
if (b.IsError) {
54-
return Operand.Error($"Parameter [{parameter}] is error.\r\n Condition `{conditionCache.ConditionString}` is error:{b.ErrorMsg}");
57+
return Operand.Error($"Parameter [{parameter}] condition `{conditionCache.ConditionString}` is error.\r\n{b.ErrorMsg}");
5558
}
5659
if (b.BooleanValue == false) continue;
5760
}
5861
operand = Evaluate(conditionCache.Formula);
5962
if (operand.IsError) {
60-
operand = Operand.Error($"Parameter [{parameter}] is error.\r\n Formula `{conditionCache.FormulaString}` is error:{operand.ErrorMsg}");
63+
operand = Operand.Error($"Parameter [{parameter}] formula `{conditionCache.FormulaString}` is error.\r\n{operand.ErrorMsg}");
6164
}
6265
_dict[parameter] = operand;
6366
return operand;
@@ -91,6 +94,11 @@ public void ClearDiyFunctions()
9194

9295
#endregion
9396

97+
/// <summary>
98+
/// 执行
99+
/// </summary>
100+
/// <param name="categoryName"></param>
101+
/// <returns></returns>
94102
public Operand Evaluate(string categoryName)
95103
{
96104
Operand operand;
@@ -100,13 +108,13 @@ public Operand Evaluate(string categoryName)
100108
if (conditionCache.Condition != null) {
101109
var b = Evaluate(conditionCache.Condition);
102110
if (b.IsError) {
103-
return Operand.Error($"CategoryName [{categoryName}] is error.\r\nCondition `{conditionCache.ConditionString}` is error:{b.ErrorMsg}");
111+
return Operand.Error($"CategoryName [{categoryName}] condition `{conditionCache.ConditionString}` is error.\r\n{b.ErrorMsg}");
104112
}
105113
if (b.BooleanValue == false) continue;
106114
}
107115
operand = Evaluate(conditionCache.Formula);
108116
if (operand.IsError) {
109-
operand = Operand.Error($"CategoryName [{categoryName}] is error.\r\nFormula `{conditionCache.FormulaString}` is error:{operand.ErrorMsg}");
117+
operand = Operand.Error($"CategoryName [{categoryName}] formula `{conditionCache.FormulaString}` is error.\r\n{operand.ErrorMsg}");
110118
}
111119
return operand;
112120
}

csharp/ToolGood.Algorithm2/MultiConditionalCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class MultiConditionCache
2525
/// <param name="condition">条件</param>
2626
/// <param name="remark">备注</param>
2727
/// <returns></returns>
28-
public bool AddFormulaCache(string categoryName, string formula, string condition = null, string remark = null)
28+
public bool AddFormula(string categoryName, string condition, string formula, string remark = null)
2929
{
3030
ConditionCache conditionCache = new ConditionCache() {
3131
CategoryName = categoryName,
@@ -64,7 +64,7 @@ public bool AddFormulaCache(string categoryName, string formula, string conditio
6464
/// <param name="condition"></param>
6565
/// <param name="remark"></param>
6666
/// <returns></returns>
67-
public bool AddConditionCache(string categoryName, string condition, string remark)
67+
public bool AddCondition(string categoryName, string condition, string remark)
6868
{
6969
ConditionCache conditionCache = new ConditionCache() {
7070
CategoryName = categoryName,

0 commit comments

Comments
 (0)