Skip to content

Commit ba98a3b

Browse files
committed
添加单元测试
1 parent ee65489 commit ba98a3b

File tree

10 files changed

+245
-13
lines changed

10 files changed

+245
-13
lines changed

csharp/ToolGood.Algorithm2/AlgorithmEngineEx.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ public string SearchRemark(string categoryName)
154154
throw new Exception($"CategoryName [{categoryName}] is missing.");
155155
}
156156

157+
/// <summary>
158+
/// 查找备注,如果异常,返回默认值
159+
/// </summary>
160+
/// <param name="categoryName"></param>
161+
/// <param name="def"></param>
162+
/// <returns></returns>
163+
public string TrySearchRemark(string categoryName, string def)
164+
{
165+
try {
166+
return SearchRemark(categoryName);
167+
} catch (Exception ex) {
168+
LastError = ex.Message;
169+
}
170+
return def;
171+
}
172+
157173
#region Parameter
158174
/// <summary>
159175
/// 清理参数

java/toolgood.algorithm/src/main/java/toolgood/algorithm/AlgorithmEngineEx.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public AlgorithmEngineEx(ConditionCache multiConditionCache) {
3232
MultiConditionCache = multiConditionCache;
3333
}
3434

35-
protected Operand GetParameter(final String parameter) throws Exception {
35+
protected Operand GetParameter(final String parameter) {
3636
if (_dict.containsKey(parameter)) {
3737
return _dict.get(parameter);
3838
}
@@ -117,8 +117,7 @@ public Operand Evaluate(String categoryName) {
117117
}
118118
return operand;
119119
}
120-
return Operand.Error("CategoryName [{categoryName}] is missing.");
121-
120+
return Operand.Error("CategoryName [" + categoryName + "] is missing.");
122121
}
123122

124123
/**
@@ -147,7 +146,19 @@ public String SearchRemark(String categoryName) throws Exception {
147146
}
148147
return conditionCache.Remark;
149148
}
150-
throw new Exception("CategoryName [{categoryName}] is missing.");
149+
throw new Exception("CategoryName [" + categoryName + "] is missing.");
150+
}
151+
152+
/**
153+
* 查找备注,如果异常,返回默认值
154+
*/
155+
public String TrySearchRemark(String categoryName, String def) {
156+
try {
157+
return SearchRemark(categoryName);
158+
} catch (Exception ex) {
159+
LastError = ex.getMessage();
160+
}
161+
return def;
151162
}
152163

153164
public void AddParameter(final String key, final Operand obj) {

java/toolgood.algorithm/src/main/java/toolgood/algorithm/ConditionCache.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ public class ConditionCache {
2626
*/
2727
public Boolean LazyLoad = false;
2828

29+
/**
30+
* 添加公式缓存
31+
* 有先后顺序的
32+
*
33+
* @param categoryName 分类名称
34+
* @param condition 条件 可空
35+
* @param formula 公式 必埴
36+
* @return
37+
*/
38+
public Boolean AddFormula(String categoryName, String condition, String formula) {
39+
return AddFormula(categoryName, condition, formula, null);
40+
}
41+
2942
/**
3043
* 添加公式缓存
3144
* 有先后顺序的
@@ -80,9 +93,10 @@ public Boolean AddFormula(String categoryName, String condition, String formula,
8093
/**
8194
* 添加 条件缓存,
8295
* 有先后顺序的
96+
*
8397
* @param categoryName 分类名称
84-
* @param condition 条件 可空
85-
* @param remark 备注 必埴
98+
* @param condition 条件 可空
99+
* @param remark 备注 必埴
86100
* @return
87101
*/
88102
public Boolean AddCondition(String categoryName, String condition, String remark) {
@@ -143,7 +157,7 @@ private ProgContext Parse(String exp) {
143157
LastError = antlrErrorListener.ErrorMsg;
144158
return null;
145159
}
146-
160+
return context;
147161
} catch (Exception ex) {
148162
LastError = ex.getMessage();
149163
}

java/toolgood.algorithm/src/main/java/toolgood/algorithm/internals/ConditionCacheInfo.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public class ConditionCacheInfo {
1717
private ProgContext _ConditionProg;
1818

1919
public ProgContext GetConditionProg() {
20-
if (_ConditionProg == null && ConditionString != null && ConditionString.length() > 0
21-
&& LastError != null && LastError.length() > 0) {
20+
if (_ConditionProg == null && ConditionString != null && ConditionString.length() > 0 && LastError == null) {
2221
_ConditionProg = Parse(ConditionString);
2322
}
2423
return _ConditionProg;
@@ -32,8 +31,7 @@ public void SetConditionProg(ProgContext value) {
3231
private ProgContext _FormulaProg;
3332

3433
public ProgContext GetFormulaProg() {
35-
if (_FormulaProg == null && FormulaString != null && FormulaString.length() > 0
36-
&& LastError != null && LastError.length() > 0) {
34+
if (_FormulaProg == null && FormulaString != null && FormulaString.length() > 0 && LastError == null) {
3735
_FormulaProg = Parse(FormulaString);
3836
}
3937
return _FormulaProg;
@@ -69,7 +67,7 @@ public ProgContext Parse(final String exp) {
6967
LastError = antlrErrorListener.ErrorMsg;
7068
return null;
7169
}
72-
70+
return context;
7371
} catch (Exception ex) {
7472
LastError = ex.getMessage();
7573
}

java/toolgood.algorithm/src/test/java/toolgood/algorithm/DemoApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package toolgood.algorithm;
22

3+
import static org.junit.Assert.assertEquals;
4+
5+
import toolgood.algorithm.Test2.Desk;
6+
import toolgood.algorithm.Test2.PriceAlgorithm;
37

48
// import org.springframework.core.io.ClassPathResource;
59
// import org.springframework.util.StopWatch;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package toolgood.algorithm.Test2;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import toolgood.algorithm.ConditionCache;
8+
9+
public class AlgorithmEngineExTest {
10+
@Test
11+
public void Test() {
12+
ConditionCache multiConditionCache = new ConditionCache();
13+
multiConditionCache.AddFormula("桌面积", "[圆桌]", "[半径]*[半径]*pi()");
14+
multiConditionCache.AddFormula("桌面积", "[方桌]", "[长]*[宽]");
15+
16+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<2.5", "[桌面积]*1.3");
17+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<5", "[桌面积]*1.5");
18+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<7", "[桌面积]*2");
19+
multiConditionCache.AddFormula("价格", "[圆桌]", "[桌面积]*2.5");
20+
21+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<1.3", "[桌面积]*1.3+[高]*1.1");
22+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<2", "[桌面积]*1.5+[高]*1.1");
23+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<5", "[桌面积]*2+[高]*1.1");
24+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<7", "[桌面积]*2.5");
25+
26+
multiConditionCache.AddFormula("出错了", "[方桌]&& [长]<11", "[出错]*2.5");
27+
28+
Desk desk = new Desk();
29+
desk.IsRoundTable = true;
30+
desk.Radius = 3;
31+
32+
PriceAlgorithm priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk);
33+
Double p1 = priceAlgorithm.TryEvaluate("价格", 0.0);
34+
assertEquals(3 * 3 * Math.PI * 1.5, p1, 0.0001);
35+
36+
Desk desk2 = new Desk();
37+
desk2.IsRoundTable = false;
38+
desk2.Length = 3;
39+
desk2.Width = 1.3;
40+
desk2.Heigth = 1;
41+
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk2);
42+
Double p2 = priceAlgorithm.TryEvaluate("价格", 0.0);
43+
assertEquals(3 * 1.3 * 2 + 1 * 1.1, p2, 0.0001);
44+
45+
Desk desk3 = new Desk();
46+
desk3.IsRoundTable = false;
47+
desk3.Length = 9;
48+
desk3.Width = 1.3;
49+
desk3.Heigth = 1;
50+
51+
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk3);
52+
Double p3 = priceAlgorithm.TryEvaluate("价格", 0.0);
53+
assertEquals(0, p3, 0.001);
54+
assertEquals("CategoryName [价格] is missing.", priceAlgorithm.LastError);
55+
56+
Double p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);
57+
}
58+
59+
@Test
60+
public void Test2() {
61+
ConditionCache multiConditionCache = new ConditionCache();
62+
multiConditionCache.LazyLoad = true;
63+
multiConditionCache.AddFormula("桌面积", "[圆桌]", "[半径]*[半径]*pi()");
64+
multiConditionCache.AddFormula("桌面积", "[方桌]", "[长]*[宽]");
65+
66+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<2.5", "[桌面积]*1.3");
67+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<5", "[桌面积]*1.5");
68+
multiConditionCache.AddFormula("价格", "[圆桌]&& [半径]<7", "[桌面积]*2");
69+
multiConditionCache.AddFormula("价格", "[圆桌]", "[桌面积]*2.5");
70+
71+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<1.3", "[桌面积]*1.3+[高]*1.1");
72+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<2", "[桌面积]*1.5+[高]*1.1");
73+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<5", "[桌面积]*2+[高]*1.1");
74+
multiConditionCache.AddFormula("价格", "[方桌]&& [长]<7", "[桌面积]*2.5");
75+
76+
multiConditionCache.AddFormula("出错了", "[方桌]&& [长]<11", "[出错]*2.5");
77+
78+
Desk desk = new Desk();
79+
desk.IsRoundTable = true;
80+
desk.Radius = 3;
81+
82+
PriceAlgorithm priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk);
83+
Double p1 = priceAlgorithm.TryEvaluate("价格", 0.0);
84+
assertEquals(3 * 3 * Math.PI * 1.5, p1, 0.0001);
85+
86+
Desk desk2 = new Desk();
87+
desk2.IsRoundTable = false;
88+
desk2.Length = 3;
89+
desk2.Width = 1.3;
90+
desk2.Heigth = 1;
91+
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk2);
92+
Double p2 = priceAlgorithm.TryEvaluate("价格", 0.0);
93+
assertEquals(3 * 1.3 * 2 + 1 * 1.1, p2, 0.0001);
94+
95+
Desk desk3 = new Desk();
96+
desk3.IsRoundTable = false;
97+
desk3.Length = 9;
98+
desk3.Width = 1.3;
99+
desk3.Heigth = 1;
100+
101+
priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk3);
102+
Double p3 = priceAlgorithm.TryEvaluate("价格", 0.0);
103+
assertEquals(0, p3, 0.001);
104+
assertEquals("CategoryName [价格] is missing.", priceAlgorithm.LastError);
105+
106+
Double p4 = priceAlgorithm.TryEvaluate("出错了", 0.0);
107+
108+
}
109+
110+
@Test
111+
public void Test3() throws Exception {
112+
ConditionCache multiConditionCache = new ConditionCache();
113+
multiConditionCache.AddCondition("类型", "[方桌]&& [长]<1.3", "1");
114+
multiConditionCache.AddCondition("类型", "[方桌]&& [长]<2", "2");
115+
multiConditionCache.AddCondition("类型", "[方桌]&& [长]<5", "3");
116+
multiConditionCache.AddCondition("类型", "[方桌]&& [长]<7", "4");
117+
118+
Desk desk = new Desk();
119+
desk.IsRoundTable = false;
120+
desk.Length = 3;
121+
desk.Width = 1.3;
122+
desk.Heigth = 1;
123+
124+
PriceAlgorithm priceAlgorithm = new PriceAlgorithm(multiConditionCache, desk);
125+
String p1 = priceAlgorithm.SearchRemark("类型");
126+
assertEquals("3", p1);
127+
}
128+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package toolgood.algorithm.Test2;
2+
3+
public class Desk {
4+
public boolean IsRoundTable;
5+
public double Heigth;
6+
public double Length;
7+
public double Width;
8+
public double Radius;
9+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package toolgood.algorithm.Test2;
2+
3+
import toolgood.algorithm.AlgorithmEngineEx;
4+
import toolgood.algorithm.ConditionCache;
5+
import toolgood.algorithm.Operand;
6+
7+
public class PriceAlgorithm extends AlgorithmEngineEx {
8+
private Desk _disk;
9+
10+
public PriceAlgorithm(ConditionCache multiConditionCache, Desk desk) {
11+
super(multiConditionCache);
12+
_disk = desk;
13+
}
14+
15+
@Override
16+
protected Operand GetParameter(String parameter) {
17+
if (parameter.equals("长")) {
18+
return Operand.Create(_disk.Length);
19+
}
20+
if (parameter.equals("宽")) {
21+
return Operand.Create(_disk.Width);
22+
}
23+
if (parameter.equals("高")) {
24+
return Operand.Create(_disk.Heigth);
25+
}
26+
if (parameter.equals("半径")) {
27+
return Operand.Create(_disk.Radius);
28+
}
29+
if (parameter.equals("方桌")) {
30+
return Operand.Create(_disk.IsRoundTable == false);
31+
}
32+
if (parameter.equals("圆桌")) {
33+
return Operand.Create(_disk.IsRoundTable);
34+
}
35+
return super.GetParameter(parameter);
36+
}
37+
}

java/toolgood.algorithm/src/test/java/toolgood/algorithm/Tests/AlgorithmEngineTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ public void Cylinder_Test() throws Exception
161161
{
162162
Cylinder c = new Cylinder(3, 10);
163163
double t = c.TryEvaluate("[半径]*[半径]*pi()", 0.0); //圆底面积
164+
165+
double t2 = c.TryEvaluate("半径*半径*pi()", 0.0); //圆底面积
166+
double t3 = c.TryEvaluate("{半径}*{半径}*pi()", 0.0); //圆底面积
167+
double t4 = c.TryEvaluate("@半径*@半径*pi()", 0.0); //圆底面积
168+
double t5 = c.TryEvaluate("#半径#*#半径#*pi()", 0.0); //圆底面积
169+
double t6 = c.TryEvaluate("【半径】*【半径】*pi()", 0.0); //圆底面积
170+
double t7 = c.TryEvaluate("【半径】*【半径】*pi()", 0.0); //圆底面积
171+
172+
assertEquals(t, t2,0.001);
173+
assertEquals(t, t3,0.001);
174+
assertEquals(t, t4,0.001);
175+
assertEquals(t, t5,0.001);
176+
assertEquals(t, t6,0.001);
177+
assertEquals(t, t7,0.001);
178+
164179
assertEquals(3 * 3 * Math.PI, t,0.001);
165180
t = c.TryEvaluate("[直径]*pi()", 0.0); //圆的长
166181
assertEquals(2 * 3 * Math.PI, t,0.001);

java/toolgood.algorithm/src/test/java/toolgood/algorithm/Tests/AlgorithmEngineTest_vlookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void vlookup_test()
2727
@Test
2828
public void lookup_test()
2929
{
30-
String json = "[{'灰色':'L','canBookCount':905,'saleCount':91,'specId':'43b0e72e98731aed69e1f0cc7d64bf4d'),{'灰色':'XL','canBookCount':929,'saleCount':70,'specId':'893746f5330dc3273d24aa1ac1a9a8b5'),{'灰色':'XXL','canBookCount':942,'saleCount':57,'specId':'42d994cba0210528142a743d4069700f'),{'白色':'L','canBookCount':862,'saleCount':136,'specId':'82114cbd2c10b5e97b01af1510807e2d'),{'白色':'XL','canBookCount':881,'saleCount':118,'specId':'c45d8408137e34adf8e695250c42a2e9'),{'白色':'XXL','canBookCount':917,'saleCount':82,'specId':'df78564262818d6eb0c428a37ab4a251'),{'蓝色':'L','canBookCount':962,'saleCount':35,'specId':'e959b6ab7c355e403a3312c75bd3d5b4','key':null),{'蓝色':'XL','canBookCount':973,'saleCount':26,'specId':'27402e07efd89afa50733afa94cd6976'),{'蓝色':'XXL','canBookCount':985,'saleCount':14,'specId':'358b6c3b52bf711ac8ecfe7513a4f3ad')]";
30+
String json = "[{'灰色':'L','canBookCount':905,'saleCount':91,'specId':'43b0e72e98731aed69e1f0cc7d64bf4d'},{'灰色':'XL','canBookCount':929,'saleCount':70,'specId':'893746f5330dc3273d24aa1ac1a9a8b5'},{'灰色':'XXL','canBookCount':942,'saleCount':57,'specId':'42d994cba0210528142a743d4069700f'},{'白色':'L','canBookCount':862,'saleCount':136,'specId':'82114cbd2c10b5e97b01af1510807e2d'},{'白色':'XL','canBookCount':881,'saleCount':118,'specId':'c45d8408137e34adf8e695250c42a2e9'},{'白色':'XXL','canBookCount':917,'saleCount':82,'specId':'df78564262818d6eb0c428a37ab4a251'},{'蓝色':'L','canBookCount':962,'saleCount':35,'specId':'e959b6ab7c355e403a3312c75bd3d5b4','key':null},{'蓝色':'XL','canBookCount':973,'saleCount':26,'specId':'27402e07efd89afa50733afa94cd6976'},{'蓝色':'XXL','canBookCount':985,'saleCount':14,'specId':'358b6c3b52bf711ac8ecfe7513a4f3ad'}]";
3131
AlgorithmEngine engine = new AlgorithmEngine();
3232
engine.AddParameter("jsonArray", json);
3333
// 第二种方法

0 commit comments

Comments
 (0)