1
0

UtilTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 OpenMetaverse;
  30. using OpenSim.Tests.Common;
  31. namespace OpenSim.Framework.Tests
  32. {
  33. [TestFixture]
  34. public class UtilTests
  35. {
  36. [Test]
  37. public void VectorOperationTests()
  38. {
  39. Vector3 v1, v2;
  40. double expectedDistance;
  41. double expectedMagnitude;
  42. double lowPrecisionTolerance = 0.001;
  43. //Lets test a simple case of <0,0,0> and <5,5,5>
  44. {
  45. v1 = new Vector3(0, 0, 0);
  46. v2 = new Vector3(5, 5, 5);
  47. expectedDistance = 8.66;
  48. Assert.That(Util.GetDistanceTo(v1, v2),
  49. new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
  50. "Calculated distance between two vectors was not within tolerances.");
  51. expectedMagnitude = 0;
  52. Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
  53. expectedMagnitude = 8.66;
  54. Assert.That(Util.GetMagnitude(v2),
  55. new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
  56. "Magnitude of vector was incorrect.");
  57. TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
  58. bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d);
  59. Assert.That(causesArgumentException, Is.True,
  60. "Getting magnitude of null vector did not cause argument exception.");
  61. Vector3 expectedNormalizedVector = new Vector3(.577f, .577f, .577f);
  62. double expectedNormalizedMagnitude = 1;
  63. Vector3 normalizedVector = Util.GetNormalizedVector(v2);
  64. Assert.That(normalizedVector,
  65. new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance),
  66. "Normalized vector generated from vector was not what was expected.");
  67. Assert.That(Util.GetMagnitude(normalizedVector),
  68. new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance),
  69. "Normalized vector generated from vector does not have magnitude of 1.");
  70. }
  71. //Lets test a simple case of <0,0,0> and <0,0,0>
  72. {
  73. v1 = new Vector3(0, 0, 0);
  74. v2 = new Vector3(0, 0, 0);
  75. expectedDistance = 0;
  76. Assert.That(Util.GetDistanceTo(v1, v2),
  77. new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
  78. "Calculated distance between two vectors was not within tolerances.");
  79. expectedMagnitude = 0;
  80. Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
  81. expectedMagnitude = 0;
  82. Assert.That(Util.GetMagnitude(v2),
  83. new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
  84. "Magnitude of vector was incorrect.");
  85. TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
  86. bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d);
  87. Assert.That(causesArgumentException, Is.True,
  88. "Getting magnitude of null vector did not cause argument exception.");
  89. d = delegate() { Util.GetNormalizedVector(v2); };
  90. causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d);
  91. Assert.That(causesArgumentException, Is.True,
  92. "Getting magnitude of null vector did not cause argument exception.");
  93. }
  94. //Lets test a simple case of <0,0,0> and <-5,-5,-5>
  95. {
  96. v1 = new Vector3(0, 0, 0);
  97. v2 = new Vector3(-5, -5, -5);
  98. expectedDistance = 8.66;
  99. Assert.That(Util.GetDistanceTo(v1, v2),
  100. new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
  101. "Calculated distance between two vectors was not within tolerances.");
  102. expectedMagnitude = 0;
  103. Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
  104. expectedMagnitude = 8.66;
  105. Assert.That(Util.GetMagnitude(v2),
  106. new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
  107. "Magnitude of vector was incorrect.");
  108. TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
  109. bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d);
  110. Assert.That(causesArgumentException, Is.True,
  111. "Getting magnitude of null vector did not cause argument exception.");
  112. Vector3 expectedNormalizedVector = new Vector3(-.577f, -.577f, -.577f);
  113. double expectedNormalizedMagnitude = 1;
  114. Vector3 normalizedVector = Util.GetNormalizedVector(v2);
  115. Assert.That(normalizedVector,
  116. new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance),
  117. "Normalized vector generated from vector was not what was expected.");
  118. Assert.That(Util.GetMagnitude(normalizedVector),
  119. new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance),
  120. "Normalized vector generated from vector does not have magnitude of 1.");
  121. }
  122. }
  123. [Test]
  124. public void UUIDTests()
  125. {
  126. Assert.IsTrue(Util.isUUID("01234567-89ab-Cdef-0123-456789AbCdEf"),
  127. "A correct UUID wasn't recognized.");
  128. Assert.IsFalse(Util.isUUID("FOOBAR67-89ab-Cdef-0123-456789AbCdEf"),
  129. "UUIDs with non-hex characters are recognized as correct UUIDs.");
  130. Assert.IsFalse(Util.isUUID("01234567"),
  131. "Too short UUIDs are regognized as correct UUIDs.");
  132. Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123-456789AbCdEf0"),
  133. "Too long UUIDs are regognized as correct UUIDs.");
  134. Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123+456789AbCdEf"),
  135. "UUIDs with wrong format are recognized as correct UUIDs.");
  136. }
  137. [Test]
  138. public void GetHashGuidTests()
  139. {
  140. string string1 = "This is one string";
  141. string string2 = "This is another";
  142. // Two consecutive runs should equal the same
  143. Assert.AreEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret1"));
  144. Assert.AreEqual(Util.GetHashGuid(string2, "secret1"), Util.GetHashGuid(string2, "secret1"));
  145. // Varying data should not eqal the same
  146. Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string2, "secret1"));
  147. // Varying secrets should not eqal the same
  148. Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret2"));
  149. }
  150. [Test]
  151. public void SLUtilTypeConvertTests()
  152. {
  153. int[] assettypes = new int[]{-1,0,1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
  154. ,23,24,25,46,47,48};
  155. string[] contenttypes = new string[]
  156. {
  157. "application/octet-stream",
  158. "image/x-j2c",
  159. "audio/ogg",
  160. "application/vnd.ll.callingcard",
  161. "application/vnd.ll.landmark",
  162. "application/vnd.ll.clothing",
  163. "application/vnd.ll.primitive",
  164. "application/vnd.ll.notecard",
  165. "application/vnd.ll.folder",
  166. "application/vnd.ll.rootfolder",
  167. "application/vnd.ll.lsltext",
  168. "application/vnd.ll.lslbyte",
  169. "image/tga",
  170. "application/vnd.ll.bodypart",
  171. "application/vnd.ll.trashfolder",
  172. "application/vnd.ll.snapshotfolder",
  173. "application/vnd.ll.lostandfoundfolder",
  174. "audio/x-wav",
  175. "image/tga",
  176. "image/jpeg",
  177. "application/vnd.ll.animation",
  178. "application/vnd.ll.gesture",
  179. "application/x-metaverse-simstate",
  180. "application/vnd.ll.favoritefolder",
  181. "application/vnd.ll.link",
  182. "application/vnd.ll.linkfolder",
  183. "application/vnd.ll.currentoutfitfolder",
  184. "application/vnd.ll.outfitfolder",
  185. "application/vnd.ll.myoutfitsfolder"
  186. };
  187. for (int i=0;i<assettypes.Length;i++)
  188. {
  189. Assert.That(SLUtil.SLAssetTypeToContentType(assettypes[i]) == contenttypes[i], "Expecting {0} but got {1}", contenttypes[i], SLUtil.SLAssetTypeToContentType(assettypes[i]));
  190. }
  191. for (int i = 0; i < contenttypes.Length; i++)
  192. {
  193. if (SLUtil.ContentTypeToSLAssetType(contenttypes[i]) == 18)
  194. {
  195. Assert.That(contenttypes[i] == "image/tga");
  196. }
  197. else
  198. {
  199. Assert.That(SLUtil.ContentTypeToSLAssetType(contenttypes[i]) == assettypes[i],
  200. "Expecting {0} but got {1}", assettypes[i],
  201. SLUtil.ContentTypeToSLAssetType(contenttypes[i]));
  202. }
  203. }
  204. int[] inventorytypes = new int[] {-1,0,1,2,3,6,7,8,9,10,15,17,18,20};
  205. string[] invcontenttypes = new string[]
  206. {
  207. "application/octet-stream",
  208. "image/x-j2c",
  209. "audio/ogg",
  210. "application/vnd.ll.callingcard",
  211. "application/vnd.ll.landmark",
  212. "application/vnd.ll.primitive",
  213. "application/vnd.ll.notecard",
  214. "application/vnd.ll.folder",
  215. "application/octet-stream",
  216. "application/vnd.ll.lsltext",
  217. "image/x-j2c",
  218. "application/vnd.ll.primitive",
  219. "application/vnd.ll.clothing",
  220. "application/vnd.ll.gesture"
  221. };
  222. for (int i=0;i<inventorytypes.Length;i++)
  223. {
  224. Assert.That(SLUtil.SLInvTypeToContentType(inventorytypes[i]) == invcontenttypes[i], "Expected {0}, Got {1}", invcontenttypes[i], SLUtil.SLInvTypeToContentType(inventorytypes[i]));
  225. }
  226. invcontenttypes = new string[]
  227. {
  228. "image/x-j2c","image/jp2","image/tga",
  229. "image/jpeg","application/ogg","audio/ogg",
  230. "audio/x-wav","application/vnd.ll.callingcard",
  231. "application/x-metaverse-callingcard",
  232. "application/vnd.ll.landmark",
  233. "application/x-metaverse-landmark",
  234. "application/vnd.ll.clothing",
  235. "application/x-metaverse-clothing","application/vnd.ll.bodypart",
  236. "application/x-metaverse-bodypart","application/vnd.ll.primitive",
  237. "application/x-metaverse-primitive","application/vnd.ll.notecard",
  238. "application/x-metaverse-notecard","application/vnd.ll.folder",
  239. "application/vnd.ll.rootfolder","application/vnd.ll.lsltext",
  240. "application/x-metaverse-lsl","application/vnd.ll.lslbyte",
  241. "application/x-metaverse-lso","application/vnd.ll.trashfolder",
  242. "application/vnd.ll.snapshotfolder",
  243. "application/vnd.ll.lostandfoundfolder","application/vnd.ll.animation",
  244. "application/x-metaverse-animation","application/vnd.ll.gesture",
  245. "application/x-metaverse-gesture","application/x-metaverse-simstate",
  246. "application/octet-stream"
  247. };
  248. sbyte[] invtypes = new sbyte[]
  249. {
  250. 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 18, 18, 18, 18, 6, 6, 7, 7, 8, 9, 10, 10, 10, 10
  251. , 8, 8, 8, 19, 19, 20, 20, 15, -1
  252. };
  253. for (int i = 0; i < invtypes.Length; i++)
  254. {
  255. Assert.That(SLUtil.ContentTypeToSLInvType(invcontenttypes[i]) == invtypes[i], "Expected {0}, Got {1}", invtypes[i], SLUtil.ContentTypeToSLInvType(invcontenttypes[i]));
  256. }
  257. }
  258. }
  259. }