1 | using System;
|
---|
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
3 |
|
---|
4 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
5 | [TestClass]
|
---|
6 | public class IntervalTest {
|
---|
7 | private readonly Interval a = new Interval(-1, 1);
|
---|
8 | private readonly Interval b = new Interval(-2, 2);
|
---|
9 | private readonly Interval c = new Interval(0, 3);
|
---|
10 | private readonly Interval d = new Interval(1, 3);
|
---|
11 | private readonly Interval e = new Interval(4, 6);
|
---|
12 |
|
---|
13 | [TestMethod]
|
---|
14 | [TestCategory("Problems.DataAnalysis")]
|
---|
15 | [TestProperty("Time", "short")]
|
---|
16 | public void AddIntervalTest() {
|
---|
17 | //add [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
|
---|
18 |
|
---|
19 | // [-1,1] + [-2,2] = [-3,3]
|
---|
20 | Assert.AreEqual(Interval.Add(a, b), new Interval(-3, 3));
|
---|
21 | //([-1, 1] + [-2, 2]) + [0, 3] = [-3, 6]
|
---|
22 | Assert.AreEqual(Interval.Add(Interval.Add(a, b), c), new Interval(-3, 6));
|
---|
23 | //([-1, 1] + [0, 3]) + [-2, 2] = [-3, 6]
|
---|
24 | Assert.AreEqual(Interval.Add(Interval.Add(a, c), b), new Interval(-3, 6));
|
---|
25 | }
|
---|
26 |
|
---|
27 | [TestMethod]
|
---|
28 | [TestCategory("Problems.DataAnalysis")]
|
---|
29 | [TestProperty("Time", "short")]
|
---|
30 | public void SubtractIntervalTest() {
|
---|
31 | //subtract [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
|
---|
32 |
|
---|
33 | //[-1, 1] - [-2, 2] = [-3, 3]
|
---|
34 | Assert.AreEqual<Interval>(Interval.Subtract(a, b), new Interval(-3, 3));
|
---|
35 | //([-1, 1] - [-2, 2]) - [0, 3] = [-6, 3]
|
---|
36 | Assert.AreEqual<Interval>(Interval.Subtract(Interval.Subtract(a, b), c), new Interval(-6, 3));
|
---|
37 | //([-1, 1] - [0, 3]) - [-2, 2] = [-6, 3]
|
---|
38 | Assert.AreEqual<Interval>(Interval.Subtract(Interval.Subtract(a, c), b), new Interval(-6, 3));
|
---|
39 | }
|
---|
40 |
|
---|
41 | [TestMethod]
|
---|
42 | [TestCategory("Problems.DataAnalysis")]
|
---|
43 | [TestProperty("Time", "short")]
|
---|
44 | public void MultiplyIntervalTest() {
|
---|
45 | //multiply [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
|
---|
46 |
|
---|
47 | //[-1, 1] * [-2, 2] = [-2, 2]
|
---|
48 | Assert.AreEqual<Interval>(Interval.Multiply(a, b), new Interval(-2, 2));
|
---|
49 | //([-1, 1] * [-2, 2]) * [0, 3] = [-6, 6]
|
---|
50 | Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, b), c), new Interval(-6, 6));
|
---|
51 | //([-1, 1] * [0, 3]) * [-2, 2] = [-6, 6]
|
---|
52 | Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, c), b), new Interval(-6, 6));
|
---|
53 |
|
---|
54 | // [-2, 0] * [-2, 0] = [0, 4]
|
---|
55 | Assert.AreEqual<Interval>(new Interval(0, 4), Interval.Multiply(new Interval(-2, 0), new Interval(-2, 0)));
|
---|
56 | }
|
---|
57 |
|
---|
58 | [TestMethod]
|
---|
59 | [TestCategory("Problems.DataAnalysis")]
|
---|
60 | [TestProperty("Time", "short")]
|
---|
61 | public void DivideIntervalTest() {
|
---|
62 | //divide [x1, x2] / [y1, y2] = [x1, x2] * (1/[y1, y2]), where 1 / [y1,y2] = [1 / y2,1 / y1] if 0 not in [y_1, y_2].
|
---|
63 |
|
---|
64 | //[4, 6] / [1, 3] = [4/3, 6]
|
---|
65 | Assert.AreEqual<Interval>(Interval.Divide(e, d), new Interval(4.0 / 3.0, 6));
|
---|
66 | //([4, 6] / [1, 3]) / [1, 3] = [4/9, 6]
|
---|
67 | Assert.AreEqual<Interval>(Interval.Divide(Interval.Divide(e, d), d), new Interval(4.0 / 9.0, 6));
|
---|
68 | //[4, 6] / [0, 3] = [4/3, +Inf]
|
---|
69 | Assert.AreEqual<Interval>(Interval.Divide(e, c), new Interval(4.0 / 3.0, double.PositiveInfinity));
|
---|
70 | //[-1, 1] / [0, 3] = [+Inf, -Inf]
|
---|
71 | Assert.AreEqual<Interval>(Interval.Divide(a, c), new Interval(double.NegativeInfinity, double.PositiveInfinity));
|
---|
72 | //Devision by 0 ==> IsInfiniteOrUndefined == true
|
---|
73 | Assert.IsTrue(Interval.Divide(e, c).IsInfiniteOrUndefined);
|
---|
74 | //Devision by 0 ==> IsInfiniteOrUndefined == true
|
---|
75 | Assert.IsTrue(Interval.Divide(a, c).IsInfiniteOrUndefined);
|
---|
76 | Assert.AreEqual<Interval>(Interval.Divide(d, b), new Interval(double.NegativeInfinity, double.PositiveInfinity));
|
---|
77 | }
|
---|
78 |
|
---|
79 | [TestMethod]
|
---|
80 | [TestCategory("Problems.DataAnalysis")]
|
---|
81 | [TestProperty("Time", "short")]
|
---|
82 | public void SineIntervalTest() {
|
---|
83 | //sine depends on interval
|
---|
84 | //sin([0, 2*pi]) = [-1, 1]
|
---|
85 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
|
---|
86 | //sin([-pi/2, pi/2]) = [sin(-pi/2), sin(pi/2)]
|
---|
87 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(-1 * Math.PI / 2, Math.PI / 2)), new Interval(-1, 1));
|
---|
88 | //sin([0, pi/2]) = [sin(0), sin(pi/2)]
|
---|
89 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(0, Math.PI / 2)), new Interval(0, 1));
|
---|
90 | //sin([pi, 3*pi/2]) = [sin(pi), sin(3*pi/2)]
|
---|
91 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(Math.PI, 3 * Math.PI / 2)), new Interval(-1, 0));
|
---|
92 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(1, 2)), new Interval(Math.Min(Math.Sin(1), Math.Sin(2)), 1));
|
---|
93 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(1, 3)), new Interval(Math.Min(Math.Sin(1), Math.Sin(3)), 1));
|
---|
94 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(Math.PI, 5 * Math.PI / 2)), new Interval(-1, 1));
|
---|
95 | }
|
---|
96 |
|
---|
97 | [TestMethod]
|
---|
98 | [TestCategory("Problems.DataAnalysis")]
|
---|
99 | [TestProperty("Time", "short")]
|
---|
100 | public void CosineIntervalTest() {
|
---|
101 | //Cosine uses sine Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
|
---|
102 | Assert.AreEqual<Interval>(Interval.Cosine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
|
---|
103 | Assert.AreEqual<Interval>(new Interval(-1, 1), Interval.Cosine(new Interval(Math.PI, 4 * Math.PI / 2)));
|
---|
104 | }
|
---|
105 |
|
---|
106 | [TestMethod]
|
---|
107 | [TestCategory("Problems.DataAnalysis")]
|
---|
108 | [TestProperty("Time", "short")]
|
---|
109 | public void LogIntervalTest() {
|
---|
110 | //Log([3, 5]) = [log(3), log(5)]
|
---|
111 | Assert.AreEqual<Interval>(new Interval(Math.Log(3), Math.Log(5)), Interval.Logarithm(new Interval(3, 5)));
|
---|
112 | //Log([0.5, 1]) = [log(0.5), log(1)]
|
---|
113 | Assert.AreEqual<Interval>(new Interval(Math.Log(0.5), 0), Interval.Logarithm(new Interval(0.5, 1)));
|
---|
114 | //Log([-1, 5]) = [NaN, log(5)]
|
---|
115 | var result = Interval.Logarithm(new Interval(-1, 5));
|
---|
116 | Assert.AreEqual<Interval>(new Interval(double.NaN, Math.Log(5)),result);
|
---|
117 | Assert.IsTrue(result.IsInfiniteOrUndefined);
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | [TestMethod]
|
---|
122 | [TestCategory("Problems.DataAnalysis")]
|
---|
123 | [TestProperty("Time", "short")]
|
---|
124 | public void ExponentialIntervalTest() {
|
---|
125 | //Exp([0, 1]) = [exp(0), exp(1)]
|
---|
126 | Assert.AreEqual<Interval>(new Interval(1, Math.Exp(1)), Interval.Exponential(new Interval(0, 1)));
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | [TestMethod]
|
---|
131 | [TestCategory("Problems.DataAnalysis")]
|
---|
132 | [TestProperty("Time", "short")]
|
---|
133 | public void SquareIntervalTest() {
|
---|
134 | Assert.AreEqual<Interval>(new Interval(1, 4), Interval.Square(new Interval(1, 2)));
|
---|
135 | Assert.AreEqual<Interval>(new Interval(1, 4), Interval.Square(new Interval(-2, -1)));
|
---|
136 | Assert.AreEqual<Interval>(new Interval(0, 4), Interval.Square(new Interval(-2, 2)));
|
---|
137 | }
|
---|
138 |
|
---|
139 | [TestMethod]
|
---|
140 | [TestCategory("Problems.DataAnalysis")]
|
---|
141 | [TestProperty("Time", "short")]
|
---|
142 | public void SquarerootIntervalTest() {
|
---|
143 | Assert.AreEqual<Interval>(new Interval(1, 2), Interval.SquareRoot(new Interval(1, 4)));
|
---|
144 | Assert.AreEqual<Interval>(new Interval(double.NaN, double.NaN), Interval.SquareRoot(new Interval(-4, -1)));
|
---|
145 | }
|
---|
146 |
|
---|
147 | [TestMethod]
|
---|
148 | [TestCategory("Problems.DataAnalysis")]
|
---|
149 | [TestProperty("Time", "short")]
|
---|
150 | public void CubeIntervalTest() {
|
---|
151 | Assert.AreEqual<Interval>(new Interval(1, 8), Interval.Cube(new Interval(1, 2)));
|
---|
152 | Assert.AreEqual<Interval>(new Interval(-8, -1), Interval.Cube(new Interval(-2, -1)));
|
---|
153 | Assert.AreEqual<Interval>(new Interval(-8, 8), Interval.Cube(new Interval(-2, 2)));
|
---|
154 | }
|
---|
155 |
|
---|
156 | [TestMethod]
|
---|
157 | [TestCategory("Problems.DataAnalysis")]
|
---|
158 | [TestProperty("Time", "short")]
|
---|
159 | public void CubeRootIntervalTest() {
|
---|
160 | Assert.AreEqual<Interval>(new Interval(1, 2), Interval.CubicRoot(new Interval(1, 8)));
|
---|
161 | Assert.AreEqual<Interval>(new Interval(-2, -2), Interval.CubicRoot(new Interval(-8, -8)));
|
---|
162 | Assert.AreEqual<Interval>(new Interval(-2, 2), Interval.CubicRoot(new Interval(-8, 8)));
|
---|
163 | Assert.AreEqual(new Interval(2, 2), Interval.CubicRoot(new Interval(8, 8)));
|
---|
164 | Assert.AreEqual(new Interval(-Math.Pow(6, 1.0 / 3), 2), Interval.CubicRoot(new Interval(-6, 8)));
|
---|
165 | Assert.AreEqual(new Interval(2, 2), Interval.CubicRoot(new Interval(8, 8)));
|
---|
166 | Assert.AreEqual(new Interval(-2, 0), Interval.CubicRoot(new Interval(-8, 0)));
|
---|
167 | }
|
---|
168 |
|
---|
169 | [TestMethod]
|
---|
170 | [TestCategory("Problems.DataAnalysis")]
|
---|
171 | [TestProperty("Time", "short")]
|
---|
172 | public void AbsoluteIntervalTest() {
|
---|
173 | Assert.AreEqual(new Interval(2, 5), Interval.Absolute(new Interval(-5, -2)));
|
---|
174 | Assert.AreEqual(new Interval(2, 5), Interval.Absolute(new Interval(2, 5)));
|
---|
175 | Assert.AreEqual(new Interval(0, 3), Interval.Absolute(new Interval(-3, 0)));
|
---|
176 | Assert.AreEqual(new Interval(0, 5), Interval.Absolute(new Interval(0, 5)));
|
---|
177 | Assert.AreEqual(new Interval(0, 5), Interval.Absolute(new Interval(-2, 5)));
|
---|
178 | }
|
---|
179 |
|
---|
180 | [TestMethod]
|
---|
181 | [TestCategory("Problems.DataAnalysis")]
|
---|
182 | [TestProperty("Time", "short")]
|
---|
183 | public void AnalyticalQuotientIntervalTest() {
|
---|
184 | //Analytical Quotient ==> a / sqrt(b^2 + 1)
|
---|
185 | var aPos = new Interval(3, 5);
|
---|
186 | var aZero = new Interval(-3, 5);
|
---|
187 | var aNeg = new Interval(-5, -3);
|
---|
188 |
|
---|
189 | var bPos = new Interval(2, 4);
|
---|
190 | var bZero = new Interval(-2, 4);
|
---|
191 | var bNeg = new Interval(-4, -2);
|
---|
192 |
|
---|
193 | //Second interval goes over zero
|
---|
194 | Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound), Interval.AnalyticalQuotient(aPos, bZero));
|
---|
195 | Assert.AreEqual(new Interval(aZero.LowerBound, aZero.UpperBound), Interval.AnalyticalQuotient(aZero, bZero));
|
---|
196 | Assert.AreEqual(new Interval(aNeg.LowerBound, aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bZero));
|
---|
197 | //Second interval is positive
|
---|
198 | Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aPos, bPos));
|
---|
199 | Assert.AreEqual(new Interval(aZero.LowerBound/Math.Sqrt(5), aZero.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aZero, bPos));
|
---|
200 | Assert.AreEqual(new Interval(aNeg.LowerBound/Math.Sqrt(5), aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bPos));
|
---|
201 | //Second interval is negative
|
---|
202 | Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aPos, bNeg));
|
---|
203 | Assert.AreEqual(new Interval(aZero.LowerBound/Math.Sqrt(5), aZero.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aZero, bNeg));
|
---|
204 | Assert.AreEqual(new Interval(aNeg.LowerBound/Math.Sqrt(5), aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bNeg));
|
---|
205 | }
|
---|
206 |
|
---|
207 | [TestMethod]
|
---|
208 | [TestCategory("Problems.DataAnalysis")]
|
---|
209 | [TestProperty("Time", "short")]
|
---|
210 | public void IsNegativeIntervalTest() {
|
---|
211 | Assert.IsTrue(new Interval(-2, -1).IsNegative);
|
---|
212 | Assert.IsFalse(new Interval(-2, 0).IsNegative);
|
---|
213 | Assert.IsFalse(new Interval(-2, 2).IsNegative);
|
---|
214 | Assert.IsFalse(new Interval(2, 4).IsNegative);
|
---|
215 | }
|
---|
216 |
|
---|
217 | [TestMethod]
|
---|
218 | [TestCategory("Problems.DataAnalysis")]
|
---|
219 | [TestProperty("Time", "short")]
|
---|
220 | public void IsPositiveIntervalTest() {
|
---|
221 | Assert.IsTrue(new Interval(3, 5).IsPositive);
|
---|
222 | Assert.IsFalse(new Interval(0, 5).IsPositive);
|
---|
223 | Assert.IsFalse(new Interval(-1, 5).IsPositive);
|
---|
224 | Assert.IsFalse(new Interval(-5, -2).IsPositive);
|
---|
225 | }
|
---|
226 |
|
---|
227 | [TestMethod]
|
---|
228 | [TestCategory("Problems.DataAnalysis")]
|
---|
229 | [TestProperty("Time", "short")]
|
---|
230 | public void IsAlmostIntervalTest() {
|
---|
231 | var negativeLowerBound = -2E-13;
|
---|
232 | var negativeUpperBound = -1E-13;
|
---|
233 | var positiveLowerBound = 3E-13;
|
---|
234 | var positiveUpperBound = 5E-13;
|
---|
235 |
|
---|
236 | var negativeInterval = new Interval(negativeLowerBound, negativeUpperBound);
|
---|
237 | var positiveInterval = new Interval(positiveLowerBound, positiveUpperBound);
|
---|
238 | var zeroInterval = new Interval(negativeUpperBound, positiveLowerBound);
|
---|
239 |
|
---|
240 | //Check for right-shift of negative interval
|
---|
241 | Assert.AreEqual(negativeUpperBound, negativeInterval.LowerBound);
|
---|
242 | Assert.AreEqual(negativeUpperBound, negativeInterval.UpperBound);
|
---|
243 | //Check for left-shift of positive interval
|
---|
244 | Assert.AreEqual(positiveLowerBound, positiveInterval.LowerBound);
|
---|
245 | Assert.AreEqual(positiveLowerBound, positiveInterval.UpperBound);
|
---|
246 | //Check for setting interval to zero
|
---|
247 | Assert.AreEqual(0, zeroInterval.LowerBound);
|
---|
248 | Assert.AreEqual(0, zeroInterval.UpperBound);
|
---|
249 |
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|