-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTest_UncertainOrdinate.cs
More file actions
235 lines (200 loc) · 13.5 KB
/
Test_UncertainOrdinate.cs
File metadata and controls
235 lines (200 loc) · 13.5 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* NOTICE:
* The U.S. Army Corps of Engineers, Risk Management Center (USACE-RMC) makes no guarantees about
* the results, or appropriateness of outputs, obtained from Numerics.
*
* LIST OF CONDITIONS:
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* ● Redistributions of source code must retain the above notice, this list of conditions, and the
* following disclaimer.
* ● Redistributions in binary form must reproduce the above notice, this list of conditions, and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
* ● The names of the U.S. Government, the U.S. Army Corps of Engineers, the Institute for Water
* Resources, or the Risk Management Center may not be used to endorse or promote products derived
* from this software without specific prior written permission. Nor may the names of its contributors
* be used to endorse or promote products derived from this software without specific prior
* written permission.
*
* DISCLAIMER:
* THIS SOFTWARE IS PROVIDED BY THE U.S. ARMY CORPS OF ENGINEERS RISK MANAGEMENT CENTER
* (USACE-RMC) "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL USACE-RMC BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Numerics.Data;
using Numerics.Distributions;
using System.Xml.Linq;
using System.Globalization;
using System.Collections.Generic;
namespace Data.PairedData
{
/// <summary>
/// Unit tests for the UncertainOrdinate class
/// </summary>
/// <remarks>
/// <b> Authors: </b>
/// <list type="bullet">
/// <item><description>
/// Haden Smith, USACE Risk Management Center, cole.h.smith@usace.army.mil
/// </description></item>
/// <item><description>
/// Sadie Niblett, USACE Risk Management Center, sadie.s.niblett@usace.army.mil
/// </description></item>
/// </list>
/// </remarks>
[TestClass]
public class Test_UncertainOrdinate
{
/// <summary>
/// Test the construction of the uncertain ordinate class
/// </summary>
[TestMethod]
public void Test_Construction()
{
var distribution = new Normal();
var unordinate1 = new UncertainOrdinate(2, distribution);
var xElement = new XElement("xElement");
xElement.SetAttributeValue("X", 2);
xElement.SetAttributeValue("Mu", 0);
xElement.SetAttributeValue("Sigma", 1);
var unordinate2 = new UncertainOrdinate(xElement, UnivariateDistributionType.Normal);
var xElement2 = new XElement("xElement2");
xElement2.SetElementValue("Distribution", UnivariateDistributionType.Normal);
xElement2.SetAttributeValue("X", 2);
xElement2.Element("Distribution").SetAttributeValue("Type", UnivariateDistributionType.Normal);
xElement2.Element("Distribution").SetAttributeValue("Mu", 0);
xElement2.Element("Distribution").SetAttributeValue("Sigma", 1);
var unordinate3 = new UncertainOrdinate(xElement2);
var unordinate4 = new UncertainOrdinate(double.NegativeInfinity, distribution);
var unordinate5 = new UncertainOrdinate(3, null);
// Also testing overloaded equality operators
Assert.IsTrue(unordinate1 == unordinate3);
Assert.IsTrue(unordinate1 == unordinate2);
Assert.AreEqual(2,unordinate1.X);
Assert.AreEqual(distribution, unordinate1.Y);
Assert.AreEqual(2,unordinate3.X );
Assert.IsTrue(unordinate3.Y == distribution);
Assert.IsTrue(unordinate1.IsValid );
Assert.IsFalse(unordinate4.IsValid);
Assert.IsFalse(unordinate5.IsValid);
Assert.IsTrue(unordinate1 != unordinate4);
Assert.IsTrue(unordinate1 != unordinate5);
}
/// <summary>
/// Test the OrdinateValid() method that indicates if the ordinate being compare is valid given
/// the criteria
/// </summary>
[TestMethod]
public void Test_OrdinateValid()
{
var unordinate = new UncertainOrdinate(3, new Triangular(6, 8, 12));
var compareUnordinate1 = new UncertainOrdinate(0, new Triangular(1, 2, 3));
var compareUnordinate2 = new UncertainOrdinate(2, new Triangular(2, 4, 5));
var compareUnordinate3 = new UncertainOrdinate(5, new Triangular(13, 19, 20));
var compareUnordinate4 = new UncertainOrdinate(6, new Uniform(7,14));
var test1 = new bool[4];
test1[0] = unordinate.OrdinateValid(compareUnordinate1, false, true, SortOrder.Ascending, SortOrder.Ascending, true);
test1[1] = unordinate.OrdinateValid(compareUnordinate2, false, true, SortOrder.Ascending, SortOrder.Ascending, true);
test1[2] = unordinate.OrdinateValid(compareUnordinate3, false, true, SortOrder.Ascending, SortOrder.Ascending, true);
test1[3] = unordinate.OrdinateValid(compareUnordinate4, false, true, SortOrder.Ascending, SortOrder.Ascending, true);
var test1Expected = new bool[] { false, false, true, false };
CollectionAssert.AreEqual(test1Expected, test1);
var test2 = new bool[4];
test2[0] = unordinate.OrdinateValid(compareUnordinate1, false, true, SortOrder.Ascending, SortOrder.Ascending, false);
test2[1] = unordinate.OrdinateValid(compareUnordinate2, false, true, SortOrder.Ascending, SortOrder.Ascending, false);
test2[2] = unordinate.OrdinateValid(compareUnordinate3, false, true, SortOrder.Ascending, SortOrder.Ascending, false);
test2[3] = unordinate.OrdinateValid(compareUnordinate4, false, true, SortOrder.Ascending, SortOrder.Ascending, false);
var test2Expected = new bool[] { true, true, false, false };
CollectionAssert.AreEqual(test2Expected, test2);
var test3 = new bool[4];
test3[0] = unordinate.OrdinateValid(compareUnordinate1, true, true, SortOrder.Ascending, SortOrder.Ascending, true);
test3[1] = unordinate.OrdinateValid(compareUnordinate2, true, true, SortOrder.Ascending, SortOrder.Ascending, true);
test3[2] = unordinate.OrdinateValid(compareUnordinate3, true, true, SortOrder.Ascending, SortOrder.Ascending, true);
test3[3] = unordinate.OrdinateValid(compareUnordinate4, true, true, SortOrder.Ascending, SortOrder.Ascending, true);
var test3Expected = new bool[] { false, false, true, false };
CollectionAssert.AreEqual(test3Expected, test3);
var test4 = new bool[4];
test4[0] = unordinate.OrdinateValid(compareUnordinate1, false, false, SortOrder.Ascending, SortOrder.Ascending, true);
test4[1] = unordinate.OrdinateValid(compareUnordinate2, false, false, SortOrder.Ascending, SortOrder.Ascending, true);
test4[2] = unordinate.OrdinateValid(compareUnordinate3, false, false, SortOrder.Ascending, SortOrder.Ascending, true);
test4[3] = unordinate.OrdinateValid(compareUnordinate4, false, false, SortOrder.Ascending, SortOrder.Ascending, true);
var test4Expected = new bool[] { false, false, true, false };
CollectionAssert.AreEqual(test4Expected, test4);
var test5 = new bool[4];
test5[0] = unordinate.OrdinateValid(compareUnordinate1, false, true, SortOrder.Descending, SortOrder.Ascending, true);
test5[1] = unordinate.OrdinateValid(compareUnordinate2, false, true, SortOrder.Descending, SortOrder.Ascending, true);
test5[2] = unordinate.OrdinateValid(compareUnordinate3, false, true, SortOrder.Descending, SortOrder.Ascending, true);
test5[3] = unordinate.OrdinateValid(compareUnordinate4, false, true, SortOrder.Descending, SortOrder.Ascending, true);
var test5Expected = new bool[] { false, false, false, false };
CollectionAssert.AreEqual(test5Expected, test5);
var test6 = new bool[4];
test6[0] = unordinate.OrdinateValid(compareUnordinate1, false, true, SortOrder.Ascending, SortOrder.Descending, true);
test6[1] = unordinate.OrdinateValid(compareUnordinate2, false, true, SortOrder.Ascending, SortOrder.Descending, true);
test6[2] = unordinate.OrdinateValid(compareUnordinate3, false, true, SortOrder.Ascending, SortOrder.Descending, true);
test6[3] = unordinate.OrdinateValid(compareUnordinate4, false, true, SortOrder.Ascending, SortOrder.Descending, true);
var test6Expected = new bool[] { false, false, false, false };
CollectionAssert.AreEqual(test6Expected, test6);
var test7 = new bool[4];
test7[0] = unordinate.OrdinateValid(compareUnordinate1, false, true, SortOrder.Descending, SortOrder.Descending, true);
test7[1] = unordinate.OrdinateValid(compareUnordinate2, false, true, SortOrder.Descending, SortOrder.Descending, true);
test7[2] = unordinate.OrdinateValid(compareUnordinate3, false, true, SortOrder.Descending, SortOrder.Descending, true);
test7[3] = unordinate.OrdinateValid(compareUnordinate4, false, true, SortOrder.Descending, SortOrder.Descending, true);
var test7Expected = new bool[] { true, true, false, false };
CollectionAssert.AreEqual(test7Expected, test7);
var test8 = new bool[4];
test8[0] = unordinate.OrdinateValid(compareUnordinate1, false, true, SortOrder.Ascending, SortOrder.Ascending, true, true);
test8[1] = unordinate.OrdinateValid(compareUnordinate2, false, true, SortOrder.Ascending, SortOrder.Ascending, true, true);
test8[2] = unordinate.OrdinateValid(compareUnordinate3, false, true, SortOrder.Ascending, SortOrder.Ascending, true, true);
test8[3] = unordinate.OrdinateValid(compareUnordinate4, false, true, SortOrder.Ascending, SortOrder.Ascending, true, true);
var test8Expected = new bool[] { false, false, true, true };
CollectionAssert.AreEqual(test8Expected, test8);
}
/// <summary>
/// Test the OrdinateErrors() method that gives error messages about the ordinate being compared
/// given the criteria
/// </summary>
[TestMethod]
public void Test_OrdinateErrors()
{
var unordinate = new UncertainOrdinate(3, new Triangular(6, 8, 12));
var compareUnordinate1 = new UncertainOrdinate(2, new Triangular(2, 4, 5));
var compareUnordinate2= new UncertainOrdinate(5, new Triangular(13, 19, 20));
var compareUnordinate3 = new UncertainOrdinate(double.PositiveInfinity, new Uniform(7, 14));
var test1 = unordinate.OrdinateErrors( compareUnordinate3, true, true, SortOrder.Ascending, SortOrder.Ascending, true);
var test1Expected = new List<string>() { "Ordinate X value can not be infinity.", "Can't compare two ordinates with different distribution types." };
CollectionAssert.AreEqual (test1Expected, test1);
var test2 = unordinate.OrdinateErrors(compareUnordinate1, true, true, SortOrder.Ascending, SortOrder.Ascending, true);
// Each of the three tests (lower bound, central tendency, upper bound) will give the same error
var test2Expected = new List<string>() { "Y values must increase.", "X values must increase.", "Y values must increase.", "X values must increase.", "Y values must increase.", "X values must increase." };
CollectionAssert.AreEqual(test2Expected, test2);
var test3 = unordinate.OrdinateErrors(compareUnordinate2, true, true, SortOrder.Descending, SortOrder.Descending, true);
// Each of the three tests (lower bound, central tendency, upper bound) will give the same error
var test3Expected = new List<string>() { "Y values must decrease.", "X values must decrease.", "Y values must decrease.", "X values must decrease.", "Y values must decrease.", "X values must decrease." };
CollectionAssert.AreEqual(test3Expected, test3);
}
/// <summary>
/// Test the method that converts an UncertainOrdinate object to an XElement object
/// </summary>
[TestMethod]
public void Test_ToXElement()
{
double X = 2;
var distribution = new Normal();
var orginal = new UncertainOrdinate(X, distribution);
XElement xElement = orginal.ToXElement();
double x = 0;
UnivariateDistributionBase dist = null;
if (xElement.Attribute(nameof(X)) != null) double.TryParse(xElement.Attribute(nameof(X)).Value, NumberStyles.Any, CultureInfo.InvariantCulture, out x);
if (xElement.Element("Distribution") != null) { dist = UnivariateDistributionFactory.CreateDistribution(xElement.Element("Distribution")); }
Assert.AreEqual(X, x);
Assert.IsTrue(distribution == dist);
}
}
}