1
0

UtilTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using NUnit.Framework;
  29. using NUnit.Framework.SyntaxHelpers;
  30. using OpenMetaverse;
  31. using OpenSim.Tests.Common;
  32. namespace OpenSim.Framework.Tests
  33. {
  34. [TestFixture]
  35. public class UtilTests
  36. {
  37. [Test]
  38. public void VectorOperationTests()
  39. {
  40. Vector3 v1, v2;
  41. double expectedDistance;
  42. double expectedMagnitude;
  43. double lowPrecisionTolerance = 0.001;
  44. //Lets test a simple case of <0,0,0> and <5,5,5>
  45. {
  46. v1 = new Vector3(0, 0, 0);
  47. v2 = new Vector3(5, 5, 5);
  48. expectedDistance = 8.66;
  49. Assert.That(Util.GetDistanceTo(v1, v2),
  50. new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
  51. "Calculated distance between two vectors was not within tolerances.");
  52. expectedMagnitude = 0;
  53. Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
  54. expectedMagnitude = 8.66;
  55. Assert.That(Util.GetMagnitude(v2),
  56. new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
  57. "Magnitude of vector was incorrect.");
  58. TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
  59. bool causesArgumentException = TestHelper.AssertThisDelegateCausesArgumentException(d);
  60. Assert.That(causesArgumentException, Is.True,
  61. "Getting magnitude of null vector did not cause argument exception.");
  62. Vector3 expectedNormalizedVector = new Vector3(.577f, .577f, .577f);
  63. double expectedNormalizedMagnitude = 1;
  64. Vector3 normalizedVector = Util.GetNormalizedVector(v2);
  65. Assert.That(normalizedVector,
  66. new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance),
  67. "Normalized vector generated from vector was not what was expected.");
  68. Assert.That(Util.GetMagnitude(normalizedVector),
  69. new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance),
  70. "Normalized vector generated from vector does not have magnitude of 1.");
  71. }
  72. //Lets test a simple case of <0,0,0> and <0,0,0>
  73. {
  74. v1 = new Vector3(0, 0, 0);
  75. v2 = new Vector3(0, 0, 0);
  76. expectedDistance = 0;
  77. Assert.That(Util.GetDistanceTo(v1, v2),
  78. new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
  79. "Calculated distance between two vectors was not within tolerances.");
  80. expectedMagnitude = 0;
  81. Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
  82. expectedMagnitude = 0;
  83. Assert.That(Util.GetMagnitude(v2),
  84. new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
  85. "Magnitude of vector was incorrect.");
  86. TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
  87. bool causesArgumentException = TestHelper.AssertThisDelegateCausesArgumentException(d);
  88. Assert.That(causesArgumentException, Is.True,
  89. "Getting magnitude of null vector did not cause argument exception.");
  90. d = delegate() { Util.GetNormalizedVector(v2); };
  91. causesArgumentException = TestHelper.AssertThisDelegateCausesArgumentException(d);
  92. Assert.That(causesArgumentException, Is.True,
  93. "Getting magnitude of null vector did not cause argument exception.");
  94. }
  95. //Lets test a simple case of <0,0,0> and <-5,-5,-5>
  96. {
  97. v1 = new Vector3(0, 0, 0);
  98. v2 = new Vector3(-5, -5, -5);
  99. expectedDistance = 8.66;
  100. Assert.That(Util.GetDistanceTo(v1, v2),
  101. new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
  102. "Calculated distance between two vectors was not within tolerances.");
  103. expectedMagnitude = 0;
  104. Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
  105. expectedMagnitude = 8.66;
  106. Assert.That(Util.GetMagnitude(v2),
  107. new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
  108. "Magnitude of vector was incorrect.");
  109. TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
  110. bool causesArgumentException = TestHelper.AssertThisDelegateCausesArgumentException(d);
  111. Assert.That(causesArgumentException, Is.True,
  112. "Getting magnitude of null vector did not cause argument exception.");
  113. Vector3 expectedNormalizedVector = new Vector3(-.577f, -.577f, -.577f);
  114. double expectedNormalizedMagnitude = 1;
  115. Vector3 normalizedVector = Util.GetNormalizedVector(v2);
  116. Assert.That(normalizedVector,
  117. new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance),
  118. "Normalized vector generated from vector was not what was expected.");
  119. Assert.That(Util.GetMagnitude(normalizedVector),
  120. new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance),
  121. "Normalized vector generated from vector does not have magnitude of 1.");
  122. }
  123. }
  124. [Test]
  125. public void UUIDTests()
  126. {
  127. Assert.IsTrue(Util.isUUID("01234567-89ab-Cdef-0123-456789AbCdEf"),
  128. "A correct UUID wasn't recognized.");
  129. Assert.IsFalse(Util.isUUID("FOOBAR67-89ab-Cdef-0123-456789AbCdEf"),
  130. "UUIDs with non-hex characters are recognized as correct UUIDs.");
  131. Assert.IsFalse(Util.isUUID("01234567"),
  132. "Too short UUIDs are regognized as correct UUIDs.");
  133. Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123-456789AbCdEf0"),
  134. "Too long UUIDs are regognized as correct UUIDs.");
  135. Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123+456789AbCdEf"),
  136. "UUIDs with wrong format are recognized as correct UUIDs.");
  137. }
  138. [Test]
  139. public void GetHashGuidTests()
  140. {
  141. string string1 = "This is one string";
  142. string string2 = "This is another";
  143. // Two consecutive runs should equal the same
  144. Assert.AreEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret1"));
  145. Assert.AreEqual(Util.GetHashGuid(string2, "secret1"), Util.GetHashGuid(string2, "secret1"));
  146. // Varying data should not eqal the same
  147. Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string2, "secret1"));
  148. // Varying secrets should not eqal the same
  149. Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret2"));
  150. }
  151. }
  152. }