-
Notifications
You must be signed in to change notification settings - Fork 3
/
DimensionParserSpecs.cs
194 lines (167 loc) · 6.14 KB
/
DimensionParserSpecs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.Collections.Generic;
using NUnit.Framework;
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
namespace OSPSuite.FuncParser.DimensionParserTests
{
public abstract class concern_for_DimensionParser : ContextSpecification<DimensionParser>
{
protected IList<QuantityDimensionInformation> _quantityDimensionInfos=new List<QuantityDimensionInformation>();
public override void GlobalContext()
{
base.GlobalContext();
var p1 = new QuantityDimensionInformation("p1", 1, 1, 1, 1, 1, 1, 1);
var p2 = new QuantityDimensionInformation("p2", 2, 2, 2, 2, 2, 2, 2);
var p3 = new QuantityDimensionInformation("p3", 0, 0, 0, 0, 0, 0, 0);
var p4 = new QuantityDimensionInformation("p4", -3, -2, -1, 0, 1, 2, 3);
var p5 = new QuantityDimensionInformation("p5", 1, 2, 3, 0, 3, 4, 5);
_quantityDimensionInfos.Add(p1);
_quantityDimensionInfos.Add(p2);
_quantityDimensionInfos.Add(p3);
_quantityDimensionInfos.Add(p4);
_quantityDimensionInfos.Add(p5);
}
protected override void Context()
{
sut = new DimensionParser();
}
}
public class when_getting_formula_dimension : concern_for_DimensionParser
{
[Observation]
[TestCaseSource(nameof(TestData))]
public void should_return_correct_parsed_dimension(string formula,
double lengthExponent, double massExponent, double timeExponent,
double electricCurrentExponent, double temperatureExponent, double amountExponent,
double luminousIntensityExponent)
{
var (dimension, parseSuccess, calculateDimensionSuccess, _) =
sut.GetDimensionInformationFor(formula, _quantityDimensionInfos);
parseSuccess.ShouldBeTrue();
calculateDimensionSuccess.ShouldBeTrue();
var returnedDimension=new DimensionInformation(lengthExponent, massExponent, timeExponent,
electricCurrentExponent, temperatureExponent, amountExponent, luminousIntensityExponent);
dimension.Equals(returnedDimension).ShouldBeTrue();
}
protected static IEnumerable<object[]> TestData()
{
//p1={1, 1, 1, 1, 1, 1, 1}
//p2={2, 2, 2, 2, 2, 2, 2}
//p3={0, 0, 0, 0, 0, 0, 0}
//p4={-3, -2, -1, 0, 1, 2, 3}
//p5={1, 2, 3, 0, 3, 4, 5}
yield return new object[]
{
"p1*p2*p3",
3,3,3,3,3,3,3
};
yield return new object[]
{
"p1*p4/p5",
-3,-3,-3,1,-1,-1,-1
};
yield return new object[]
{
"(p1+p2/p1)^17",
17,17,17,17,17,17,17
};
yield return new object[]
{
"2>1 ? min(p1-2*p1; p2/p1) : 99",
1,1,1,1,1,1,1
};
yield return new object[]
{
"p3*p1>p2/p1 and p3*p1>=p2/p1 or p3*p1<p2/p1 and not p3*p1<=p2/p1 or p3*p1<>p2/p1",
0,0,0,0,0,0,0
};
yield return new object[]
{
"2<1 ? 33 : max(p1-2*p1; p2/p1)",
1,1,1,1,1,1,1
};
yield return new object[]
{
"sqrt(p4)*exp(p3)*ln(p3)*log(p3)*log10(p3)",
-3/2.0,-2/2.0,-1/2.0, 0, 1/2.0,2/2.0,3/2.0
};
yield return new object[]
{
"acos(p3)+asin(p3)+atan(p3)+cos(p3)+cosh(p3)+sin(p3)+sinh(p3)+tan(p3)+tanh(p3)",
0,0,0,0,0,0,0
};
yield return new object[]
{
"(Rnd()*p1*p1-Srnd()*p2)/(p1*p1)+Rnd()+Srnd()+Rnd()*Srnd()",
0,0,0,0,0,0,0
};
yield return new object[]
{
"p1>p2 ? p1-2*p1 : p2/p1", //non-const if condition but same dimensions in THAN and ELSE parts
1,1,1,1,1,1,1
};
}
}
public class when_getting_dimension_for_invalid_formula : concern_for_DimensionParser
{
[Observation]
[TestCaseSource(nameof(TestData))]
public void should_return_parse_failure(string formula)
{
var (_, parseSuccess, calculateDimensionSuccess, errorMessage) =
sut.GetDimensionInformationFor(formula, _quantityDimensionInfos);
parseSuccess.ShouldBeFalse();
calculateDimensionSuccess.ShouldBeFalse();
string.IsNullOrEmpty(errorMessage).ShouldBeFalse();
}
protected static IEnumerable<string> TestData()
{
yield return "p1+p2+"; //invalid formula equation
yield return "p1+p2+p6"; //"p6" is not in the list of passed quantity dimension infos
}
}
public class when_cannot_calculate_dimension_for_valid_formula : concern_for_DimensionParser
{
[Observation]
[TestCaseSource(nameof(TestData))]
public void should_return_dimension_calculation_failure(string formula)
{
var (_, parseSuccess, calculateDimensionSuccess, errorMessage) =
sut.GetDimensionInformationFor(formula, _quantityDimensionInfos);
parseSuccess.ShouldBeTrue(); //formula is valid
calculateDimensionSuccess.ShouldBeFalse();
string.IsNullOrEmpty(errorMessage).ShouldBeFalse();
}
protected static IEnumerable<string> TestData()
{
yield return "acos(p5)";
yield return "asin(p5)";
yield return "atan(p5)";
yield return "cos(p5)";
yield return "cosh(p5)";
yield return "p5 and 2";
yield return "exp(p5)";
yield return "ln(p5)";
yield return "log(p5)";
yield return "log10(p5)";
yield return "2^p5";
yield return "sin(p5)";
yield return "sinh(p5)";
yield return "tan(p5)";
yield return "tanh(p5)";
yield return "p4>p5";
yield return "p4>=p5";
yield return "p4<p5";
yield return "p4<=p5";
yield return "p4<>p5";
yield return "max(p4;p5)";
yield return "min(p4;p5)";
yield return "p4-p5";
yield return "p4=p5";
yield return "NOT p4";
yield return "p4 OR p5";
yield return "p4+p5";
yield return "p1>p2 ? p1-2*p1 : p2";
}
}
}