diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Util.cs | 38 | ||||
-rw-r--r-- | OpenSim/Tests/Common/ANumericalToleranceConstraint.cs | 30 | ||||
-rw-r--r-- | OpenSim/Tests/Common/DoubleToleranceConstraint.cs | 52 | ||||
-rw-r--r-- | OpenSim/Tests/Common/TestHelper.cs | 25 | ||||
-rw-r--r-- | OpenSim/Tests/Common/VectorToleranceConstraint.cs | 58 | ||||
-rw-r--r-- | OpenSim/Tests/OpenSim/Framework/UtilTest.cs | 115 |
6 files changed, 316 insertions, 2 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 5bfd8e1..856cac3 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -47,7 +47,12 @@ namespace OpenSim.Framework | |||
47 | private static Dictionary<LLUUID, string> capsURLS = new Dictionary<LLUUID, string>(); | 47 | private static Dictionary<LLUUID, string> capsURLS = new Dictionary<LLUUID, string>(); |
48 | 48 | ||
49 | #region Vector Equasions | 49 | #region Vector Equasions |
50 | 50 | /// <summary> | |
51 | /// Get the distance between two 3d vectors | ||
52 | /// </summary> | ||
53 | /// <param name="a">A 3d vector</param> | ||
54 | /// <param name="b">A 3d vector</param> | ||
55 | /// <returns>The distance between the two vectors</returns> | ||
51 | public static double GetDistanceTo(LLVector3 a, LLVector3 b) | 56 | public static double GetDistanceTo(LLVector3 a, LLVector3 b) |
52 | { | 57 | { |
53 | float dx = a.X - b.X; | 58 | float dx = a.X - b.X; |
@@ -55,14 +60,43 @@ namespace OpenSim.Framework | |||
55 | float dz = a.Z - b.Z; | 60 | float dz = a.Z - b.Z; |
56 | return Math.Sqrt(dx*dx + dy*dy + dz*dz); | 61 | return Math.Sqrt(dx*dx + dy*dy + dz*dz); |
57 | } | 62 | } |
63 | |||
64 | /// <summary> | ||
65 | /// Get the magnitude of a 3d vector | ||
66 | /// </summary> | ||
67 | /// <param name="a">A 3d vector</param> | ||
68 | /// <returns>The magnitude of the vector</returns> | ||
58 | public static double GetMagnitude(LLVector3 a) { | 69 | public static double GetMagnitude(LLVector3 a) { |
59 | return Math.Sqrt((a.X * a.X) + (a.Y * a.Y) + (a.Z * a.Z)); | 70 | return Math.Sqrt((a.X * a.X) + (a.Y * a.Y) + (a.Z * a.Z)); |
60 | } | 71 | } |
61 | public static LLVector3 GetNormal(LLVector3 a) | 72 | |
73 | /// <summary> | ||
74 | /// Get a normalized form of a 3d vector | ||
75 | /// </summary> | ||
76 | /// <param name="a">A 3d vector</param> | ||
77 | /// <returns>A new vector which is normalized form of the vector</returns> | ||
78 | /// <remarks>The vector paramater cannot be <0,0,0></remarks> | ||
79 | public static LLVector3 GetNormalizedVector(LLVector3 a) | ||
62 | { | 80 | { |
81 | if (IsZeroVector(a)) | ||
82 | throw new ArgumentException("Vector paramater cannot be a zero vector."); | ||
83 | |||
63 | float Mag = (float)GetMagnitude(a); | 84 | float Mag = (float)GetMagnitude(a); |
64 | return new LLVector3(a.X / Mag, a.Y / Mag, a.Z / Mag); | 85 | return new LLVector3(a.X / Mag, a.Y / Mag, a.Z / Mag); |
86 | } | ||
65 | 87 | ||
88 | /// <summary> | ||
89 | /// Returns if a vector is a zero vector (has all zero components) | ||
90 | /// </summary> | ||
91 | /// <returns></returns> | ||
92 | public static bool IsZeroVector( LLVector3 v ) | ||
93 | { | ||
94 | if( v.X == 0 && v.Y == 0 && v.Z == 0) | ||
95 | { | ||
96 | return true; | ||
97 | } | ||
98 | |||
99 | return false; | ||
66 | } | 100 | } |
67 | # endregion | 101 | # endregion |
68 | 102 | ||
diff --git a/OpenSim/Tests/Common/ANumericalToleranceConstraint.cs b/OpenSim/Tests/Common/ANumericalToleranceConstraint.cs new file mode 100644 index 0000000..289e4bb --- /dev/null +++ b/OpenSim/Tests/Common/ANumericalToleranceConstraint.cs | |||
@@ -0,0 +1,30 @@ | |||
1 | using System; | ||
2 | using NUnit.Framework.Constraints; | ||
3 | |||
4 | namespace OpenSim.Tests.Common | ||
5 | { | ||
6 | public abstract class ANumericalToleranceConstraint : Constraint | ||
7 | { | ||
8 | protected double _tolerance; | ||
9 | |||
10 | public ANumericalToleranceConstraint(double tolerance) | ||
11 | { | ||
12 | if (tolerance < 0) | ||
13 | { | ||
14 | throw new ArgumentException("Tolerance cannot be negative."); | ||
15 | } | ||
16 | _tolerance = tolerance; | ||
17 | } | ||
18 | |||
19 | |||
20 | protected bool IsWithinDoubleConstraint(double doubleValue, double baseValue) | ||
21 | { | ||
22 | if (doubleValue >= baseValue - _tolerance && doubleValue <= baseValue + _tolerance) | ||
23 | { | ||
24 | return true; | ||
25 | } | ||
26 | |||
27 | return false; | ||
28 | } | ||
29 | } | ||
30 | } | ||
diff --git a/OpenSim/Tests/Common/DoubleToleranceConstraint.cs b/OpenSim/Tests/Common/DoubleToleranceConstraint.cs new file mode 100644 index 0000000..f3da236 --- /dev/null +++ b/OpenSim/Tests/Common/DoubleToleranceConstraint.cs | |||
@@ -0,0 +1,52 @@ | |||
1 | using System; | ||
2 | using NUnit.Framework; | ||
3 | using NUnit.Framework.Constraints; | ||
4 | using OpenSim.Tests.Common; | ||
5 | |||
6 | namespace OpenSim.Tests.Common | ||
7 | { | ||
8 | public class DoubleToleranceConstraint : ANumericalToleranceConstraint | ||
9 | { | ||
10 | |||
11 | private double _baseValue; | ||
12 | private double _valueToBeTested; | ||
13 | |||
14 | public DoubleToleranceConstraint(double baseValue, double tolerance) : base(tolerance) | ||
15 | { | ||
16 | _baseValue = baseValue; | ||
17 | } | ||
18 | |||
19 | ///<summary> | ||
20 | ///Test whether the constraint is satisfied by a given value | ||
21 | ///</summary> | ||
22 | ///<param name="valueToBeTested">The value to be tested</param> | ||
23 | ///<returns> | ||
24 | ///True for success, false for failure | ||
25 | ///</returns> | ||
26 | public override bool Matches(object valueToBeTested) | ||
27 | { | ||
28 | if (valueToBeTested == null) | ||
29 | { | ||
30 | throw new ArgumentException("Constraint cannot be used upon null values."); | ||
31 | } | ||
32 | if( valueToBeTested.GetType() != typeof(double)) | ||
33 | { | ||
34 | throw new ArgumentException("Constraint cannot be used upon non double-values."); | ||
35 | } | ||
36 | |||
37 | _valueToBeTested = (double)valueToBeTested; | ||
38 | |||
39 | return IsWithinDoubleConstraint(_valueToBeTested, _baseValue ); | ||
40 | } | ||
41 | |||
42 | public override void WriteDescriptionTo(MessageWriter writer) | ||
43 | { | ||
44 | writer.WriteExpectedValue(string.Format("A value {0} within tolerance of plus or minus {1}",_baseValue,_tolerance)); | ||
45 | } | ||
46 | |||
47 | public override void WriteActualValueTo(MessageWriter writer) | ||
48 | { | ||
49 | writer.WriteActualValue(_valueToBeTested); | ||
50 | } | ||
51 | } | ||
52 | } | ||
diff --git a/OpenSim/Tests/Common/TestHelper.cs b/OpenSim/Tests/Common/TestHelper.cs new file mode 100644 index 0000000..f59c157 --- /dev/null +++ b/OpenSim/Tests/Common/TestHelper.cs | |||
@@ -0,0 +1,25 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | |||
5 | namespace OpenSim.Tests.Common | ||
6 | { | ||
7 | public delegate void TestDelegate(); | ||
8 | |||
9 | public class TestHelper | ||
10 | { | ||
11 | public static bool AssertThisDelegateCausesArgumentException(TestDelegate d) | ||
12 | { | ||
13 | try | ||
14 | { | ||
15 | d(); | ||
16 | } | ||
17 | catch(ArgumentException e) | ||
18 | { | ||
19 | return true; | ||
20 | } | ||
21 | |||
22 | return false; | ||
23 | } | ||
24 | } | ||
25 | } | ||
diff --git a/OpenSim/Tests/Common/VectorToleranceConstraint.cs b/OpenSim/Tests/Common/VectorToleranceConstraint.cs new file mode 100644 index 0000000..1faf7dd --- /dev/null +++ b/OpenSim/Tests/Common/VectorToleranceConstraint.cs | |||
@@ -0,0 +1,58 @@ | |||
1 | using System; | ||
2 | using libsecondlife; | ||
3 | using NUnit.Framework; | ||
4 | |||
5 | namespace OpenSim.Tests.Common | ||
6 | { | ||
7 | public class VectorToleranceConstraint : ANumericalToleranceConstraint | ||
8 | { | ||
9 | private LLVector3 _baseValue; | ||
10 | private LLVector3 _valueToBeTested; | ||
11 | |||
12 | public VectorToleranceConstraint(LLVector3 baseValue, double tolerance) : base(tolerance) | ||
13 | { | ||
14 | _baseValue = baseValue; | ||
15 | } | ||
16 | |||
17 | ///<summary> | ||
18 | ///Test whether the constraint is satisfied by a given value | ||
19 | ///</summary> | ||
20 | ///<param name="valueToBeTested">The value to be tested</param> | ||
21 | ///<returns> | ||
22 | ///True for success, false for failure | ||
23 | ///</returns> | ||
24 | public override bool Matches(object valueToBeTested) | ||
25 | { | ||
26 | if (valueToBeTested == null) | ||
27 | { | ||
28 | throw new ArgumentException("Constraint cannot be used upon null values."); | ||
29 | } | ||
30 | if (valueToBeTested.GetType() != typeof (LLVector3)) | ||
31 | { | ||
32 | throw new ArgumentException("Constraint cannot be used upon non vector values."); | ||
33 | } | ||
34 | |||
35 | _valueToBeTested = (LLVector3) valueToBeTested; | ||
36 | |||
37 | if ( IsWithinDoubleConstraint(_valueToBeTested.X,_baseValue.X) && | ||
38 | IsWithinDoubleConstraint(_valueToBeTested.Y,_baseValue.Y) && | ||
39 | IsWithinDoubleConstraint(_valueToBeTested.Z,_baseValue.Z) ) | ||
40 | { | ||
41 | return true; | ||
42 | } | ||
43 | |||
44 | return false; | ||
45 | } | ||
46 | |||
47 | public override void WriteDescriptionTo(MessageWriter writer) | ||
48 | { | ||
49 | writer.WriteExpectedValue( | ||
50 | string.Format("A value {0} within tolerance of plus or minus {1}", _baseValue, _tolerance)); | ||
51 | } | ||
52 | |||
53 | public override void WriteActualValueTo(MessageWriter writer) | ||
54 | { | ||
55 | writer.WriteActualValue(_valueToBeTested); | ||
56 | } | ||
57 | } | ||
58 | } | ||
diff --git a/OpenSim/Tests/OpenSim/Framework/UtilTest.cs b/OpenSim/Tests/OpenSim/Framework/UtilTest.cs new file mode 100644 index 0000000..ef42eee --- /dev/null +++ b/OpenSim/Tests/OpenSim/Framework/UtilTest.cs | |||
@@ -0,0 +1,115 @@ | |||
1 | using libsecondlife; | ||
2 | using NUnit.Framework; | ||
3 | using NUnit.Framework.SyntaxHelpers; | ||
4 | |||
5 | using OpenSim.Tests.Common; | ||
6 | |||
7 | namespace OpenSim.Framework.Tests | ||
8 | { | ||
9 | [TestFixture] | ||
10 | public class UtilTests | ||
11 | { | ||
12 | [Test] | ||
13 | public void VectorOperationTests() | ||
14 | { | ||
15 | LLVector3 v1, v2; | ||
16 | double expectedDistance; | ||
17 | double expectedMagnitude; | ||
18 | double lowPrecisionTolerance = 0.001; | ||
19 | |||
20 | //Lets test a simple case of <0,0,0> and <5,5,5> | ||
21 | { | ||
22 | v1 = new LLVector3(0, 0, 0); | ||
23 | v2 = new LLVector3(5, 5, 5); | ||
24 | expectedDistance = 8.66; | ||
25 | Assert.That(Util.GetDistanceTo(v1, v2), | ||
26 | new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance), | ||
27 | "Calculated distance between two vectors was not within tolerances."); | ||
28 | |||
29 | expectedMagnitude = 0; | ||
30 | Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero."); | ||
31 | |||
32 | expectedMagnitude = 8.66; | ||
33 | Assert.That(Util.GetMagnitude(v2), | ||
34 | new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance), | ||
35 | "Magnitude of vector was incorrect."); | ||
36 | |||
37 | TestDelegate d = delegate() { Util.GetNormalizedVector(v1); }; | ||
38 | bool causesArgumentException = TestHelper.AssertThisDelegateCausesArgumentException(d); | ||
39 | Assert.That(causesArgumentException, Is.True, | ||
40 | "Getting magnitude of null vector did not cause argument exception."); | ||
41 | |||
42 | LLVector3 expectedNormalizedVector = new LLVector3(.577f, .577f, .577f); | ||
43 | double expectedNormalizedMagnitude = 1; | ||
44 | LLVector3 normalizedVector = Util.GetNormalizedVector(v2); | ||
45 | Assert.That(normalizedVector, | ||
46 | new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance), | ||
47 | "Normalized vector generated from vector was not what was expected."); | ||
48 | Assert.That(Util.GetMagnitude(normalizedVector), | ||
49 | new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance), | ||
50 | "Normalized vector generated from vector does not have magnitude of 1."); | ||
51 | } | ||
52 | |||
53 | //Lets test a simple case of <0,0,0> and <0,0,0> | ||
54 | { | ||
55 | v1 = new LLVector3(0, 0, 0); | ||
56 | v2 = new LLVector3(0, 0, 0); | ||
57 | expectedDistance = 0; | ||
58 | Assert.That(Util.GetDistanceTo(v1, v2), | ||
59 | new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance), | ||
60 | "Calculated distance between two vectors was not within tolerances."); | ||
61 | |||
62 | expectedMagnitude = 0; | ||
63 | Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero."); | ||
64 | |||
65 | expectedMagnitude = 0; | ||
66 | Assert.That(Util.GetMagnitude(v2), | ||
67 | new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance), | ||
68 | "Magnitude of vector was incorrect."); | ||
69 | |||
70 | TestDelegate d = delegate() { Util.GetNormalizedVector(v1); }; | ||
71 | bool causesArgumentException = TestHelper.AssertThisDelegateCausesArgumentException(d); | ||
72 | Assert.That(causesArgumentException, Is.True, | ||
73 | "Getting magnitude of null vector did not cause argument exception."); | ||
74 | |||
75 | d = delegate() { Util.GetNormalizedVector(v2); }; | ||
76 | causesArgumentException = TestHelper.AssertThisDelegateCausesArgumentException(d); | ||
77 | Assert.That(causesArgumentException, Is.True, | ||
78 | "Getting magnitude of null vector did not cause argument exception."); | ||
79 | } | ||
80 | |||
81 | //Lets test a simple case of <0,0,0> and <-5,-5,-5> | ||
82 | { | ||
83 | v1 = new LLVector3(0, 0, 0); | ||
84 | v2 = new LLVector3(-5, -5, -5); | ||
85 | expectedDistance = 8.66; | ||
86 | Assert.That(Util.GetDistanceTo(v1, v2), | ||
87 | new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance), | ||
88 | "Calculated distance between two vectors was not within tolerances."); | ||
89 | |||
90 | expectedMagnitude = 0; | ||
91 | Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero."); | ||
92 | |||
93 | expectedMagnitude = 8.66; | ||
94 | Assert.That(Util.GetMagnitude(v2), | ||
95 | new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance), | ||
96 | "Magnitude of vector was incorrect."); | ||
97 | |||
98 | TestDelegate d = delegate() { Util.GetNormalizedVector(v1); }; | ||
99 | bool causesArgumentException = TestHelper.AssertThisDelegateCausesArgumentException(d); | ||
100 | Assert.That(causesArgumentException, Is.True, | ||
101 | "Getting magnitude of null vector did not cause argument exception."); | ||
102 | |||
103 | LLVector3 expectedNormalizedVector = new LLVector3(-.577f, -.577f, -.577f); | ||
104 | double expectedNormalizedMagnitude = 1; | ||
105 | LLVector3 normalizedVector = Util.GetNormalizedVector(v2); | ||
106 | Assert.That(normalizedVector, | ||
107 | new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance), | ||
108 | "Normalized vector generated from vector was not what was expected."); | ||
109 | Assert.That(Util.GetMagnitude(normalizedVector), | ||
110 | new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance), | ||
111 | "Normalized vector generated from vector does not have magnitude of 1."); | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | } | ||