diff options
Diffstat (limited to 'OpenSim/Framework/Tests/UtilTest.cs')
-rw-r--r-- | OpenSim/Framework/Tests/UtilTest.cs | 359 |
1 files changed, 359 insertions, 0 deletions
diff --git a/OpenSim/Framework/Tests/UtilTest.cs b/OpenSim/Framework/Tests/UtilTest.cs new file mode 100644 index 0000000..cfe3139 --- /dev/null +++ b/OpenSim/Framework/Tests/UtilTest.cs | |||
@@ -0,0 +1,359 @@ | |||
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 | |||
28 | using System; | ||
29 | using NUnit.Framework; | ||
30 | using OpenMetaverse; | ||
31 | using OpenSim.Tests.Common; | ||
32 | |||
33 | namespace OpenSim.Framework.Tests | ||
34 | { | ||
35 | [TestFixture] | ||
36 | public class UtilTests : OpenSimTestCase | ||
37 | { | ||
38 | [Test] | ||
39 | public void VectorOperationTests() | ||
40 | { | ||
41 | Vector3 v1, v2; | ||
42 | double expectedDistance; | ||
43 | double expectedMagnitude; | ||
44 | double lowPrecisionTolerance = 0.001; | ||
45 | |||
46 | //Lets test a simple case of <0,0,0> and <5,5,5> | ||
47 | { | ||
48 | v1 = new Vector3(0, 0, 0); | ||
49 | v2 = new Vector3(5, 5, 5); | ||
50 | expectedDistance = 8.66; | ||
51 | Assert.That(Util.GetDistanceTo(v1, v2), | ||
52 | new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance), | ||
53 | "Calculated distance between two vectors was not within tolerances."); | ||
54 | |||
55 | expectedMagnitude = 0; | ||
56 | Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero."); | ||
57 | |||
58 | expectedMagnitude = 8.66; | ||
59 | Assert.That(Util.GetMagnitude(v2), | ||
60 | new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance), | ||
61 | "Magnitude of vector was incorrect."); | ||
62 | |||
63 | TestDelegate d = delegate() { Util.GetNormalizedVector(v1); }; | ||
64 | bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d); | ||
65 | Assert.That(causesArgumentException, Is.True, | ||
66 | "Getting magnitude of null vector did not cause argument exception."); | ||
67 | |||
68 | Vector3 expectedNormalizedVector = new Vector3(.577f, .577f, .577f); | ||
69 | double expectedNormalizedMagnitude = 1; | ||
70 | Vector3 normalizedVector = Util.GetNormalizedVector(v2); | ||
71 | Assert.That(normalizedVector, | ||
72 | new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance), | ||
73 | "Normalized vector generated from vector was not what was expected."); | ||
74 | Assert.That(Util.GetMagnitude(normalizedVector), | ||
75 | new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance), | ||
76 | "Normalized vector generated from vector does not have magnitude of 1."); | ||
77 | } | ||
78 | |||
79 | //Lets test a simple case of <0,0,0> and <0,0,0> | ||
80 | { | ||
81 | v1 = new Vector3(0, 0, 0); | ||
82 | v2 = new Vector3(0, 0, 0); | ||
83 | expectedDistance = 0; | ||
84 | Assert.That(Util.GetDistanceTo(v1, v2), | ||
85 | new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance), | ||
86 | "Calculated distance between two vectors was not within tolerances."); | ||
87 | |||
88 | expectedMagnitude = 0; | ||
89 | Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero."); | ||
90 | |||
91 | expectedMagnitude = 0; | ||
92 | Assert.That(Util.GetMagnitude(v2), | ||
93 | new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance), | ||
94 | "Magnitude of vector was incorrect."); | ||
95 | |||
96 | TestDelegate d = delegate() { Util.GetNormalizedVector(v1); }; | ||
97 | bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d); | ||
98 | Assert.That(causesArgumentException, Is.True, | ||
99 | "Getting magnitude of null vector did not cause argument exception."); | ||
100 | |||
101 | d = delegate() { Util.GetNormalizedVector(v2); }; | ||
102 | causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d); | ||
103 | Assert.That(causesArgumentException, Is.True, | ||
104 | "Getting magnitude of null vector did not cause argument exception."); | ||
105 | } | ||
106 | |||
107 | //Lets test a simple case of <0,0,0> and <-5,-5,-5> | ||
108 | { | ||
109 | v1 = new Vector3(0, 0, 0); | ||
110 | v2 = new Vector3(-5, -5, -5); | ||
111 | expectedDistance = 8.66; | ||
112 | Assert.That(Util.GetDistanceTo(v1, v2), | ||
113 | new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance), | ||
114 | "Calculated distance between two vectors was not within tolerances."); | ||
115 | |||
116 | expectedMagnitude = 0; | ||
117 | Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero."); | ||
118 | |||
119 | expectedMagnitude = 8.66; | ||
120 | Assert.That(Util.GetMagnitude(v2), | ||
121 | new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance), | ||
122 | "Magnitude of vector was incorrect."); | ||
123 | |||
124 | TestDelegate d = delegate() { Util.GetNormalizedVector(v1); }; | ||
125 | bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d); | ||
126 | Assert.That(causesArgumentException, Is.True, | ||
127 | "Getting magnitude of null vector did not cause argument exception."); | ||
128 | |||
129 | Vector3 expectedNormalizedVector = new Vector3(-.577f, -.577f, -.577f); | ||
130 | double expectedNormalizedMagnitude = 1; | ||
131 | Vector3 normalizedVector = Util.GetNormalizedVector(v2); | ||
132 | Assert.That(normalizedVector, | ||
133 | new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance), | ||
134 | "Normalized vector generated from vector was not what was expected."); | ||
135 | Assert.That(Util.GetMagnitude(normalizedVector), | ||
136 | new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance), | ||
137 | "Normalized vector generated from vector does not have magnitude of 1."); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | [Test] | ||
142 | public void UUIDTests() | ||
143 | { | ||
144 | Assert.IsTrue(Util.isUUID("01234567-89ab-Cdef-0123-456789AbCdEf"), | ||
145 | "A correct UUID wasn't recognized."); | ||
146 | Assert.IsFalse(Util.isUUID("FOOBAR67-89ab-Cdef-0123-456789AbCdEf"), | ||
147 | "UUIDs with non-hex characters are recognized as correct UUIDs."); | ||
148 | Assert.IsFalse(Util.isUUID("01234567"), | ||
149 | "Too short UUIDs are recognized as correct UUIDs."); | ||
150 | Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123-456789AbCdEf0"), | ||
151 | "Too long UUIDs are recognized as correct UUIDs."); | ||
152 | Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123+456789AbCdEf"), | ||
153 | "UUIDs with wrong format are recognized as correct UUIDs."); | ||
154 | } | ||
155 | |||
156 | [Test] | ||
157 | public void GetHashGuidTests() | ||
158 | { | ||
159 | string string1 = "This is one string"; | ||
160 | string string2 = "This is another"; | ||
161 | |||
162 | // Two consecutive runs should equal the same | ||
163 | Assert.AreEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret1")); | ||
164 | Assert.AreEqual(Util.GetHashGuid(string2, "secret1"), Util.GetHashGuid(string2, "secret1")); | ||
165 | |||
166 | // Varying data should not eqal the same | ||
167 | Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string2, "secret1")); | ||
168 | |||
169 | // Varying secrets should not eqal the same | ||
170 | Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret2")); | ||
171 | } | ||
172 | |||
173 | [Test] | ||
174 | public void SLUtilTypeConvertTests() | ||
175 | { | ||
176 | int[] assettypes = new int[]{-1,0,1,2,3,5,6,7,8,10,11,12,13,17,18,19,20,21,22 | ||
177 | ,24,25}; | ||
178 | string[] contenttypes = new string[] | ||
179 | { | ||
180 | "application/octet-stream", | ||
181 | "image/x-j2c", | ||
182 | "audio/ogg", | ||
183 | "application/vnd.ll.callingcard", | ||
184 | "application/vnd.ll.landmark", | ||
185 | "application/vnd.ll.clothing", | ||
186 | "application/vnd.ll.primitive", | ||
187 | "application/vnd.ll.notecard", | ||
188 | "application/vnd.ll.folder", | ||
189 | "application/vnd.ll.lsltext", | ||
190 | "application/vnd.ll.lslbyte", | ||
191 | "image/tga", | ||
192 | "application/vnd.ll.bodypart", | ||
193 | "audio/x-wav", | ||
194 | "image/tga", | ||
195 | "image/jpeg", | ||
196 | "application/vnd.ll.animation", | ||
197 | "application/vnd.ll.gesture", | ||
198 | "application/x-metaverse-simstate", | ||
199 | "application/vnd.ll.link", | ||
200 | "application/vnd.ll.linkfolder", | ||
201 | }; | ||
202 | for (int i=0;i<assettypes.Length;i++) | ||
203 | { | ||
204 | Assert.That(SLUtil.SLAssetTypeToContentType(assettypes[i]) == contenttypes[i], "Expecting {0} but got {1}", contenttypes[i], SLUtil.SLAssetTypeToContentType(assettypes[i])); | ||
205 | } | ||
206 | |||
207 | for (int i = 0; i < contenttypes.Length; i++) | ||
208 | { | ||
209 | int expected; | ||
210 | if (contenttypes[i] == "image/tga") | ||
211 | expected = 12; // if we know only the content-type "image/tga", then we assume the asset type is TextureTGA; not ImageTGA | ||
212 | else | ||
213 | expected = assettypes[i]; | ||
214 | Assert.AreEqual(expected, SLUtil.ContentTypeToSLAssetType(contenttypes[i]), | ||
215 | String.Format("Incorrect AssetType mapped from Content-Type {0}", contenttypes[i])); | ||
216 | } | ||
217 | |||
218 | int[] inventorytypes = new int[] {-1,0,1,2,3,6,7,8,10,15,17,18,20}; | ||
219 | string[] invcontenttypes = new string[] | ||
220 | { | ||
221 | "application/octet-stream", | ||
222 | "image/x-j2c", | ||
223 | "audio/ogg", | ||
224 | "application/vnd.ll.callingcard", | ||
225 | "application/vnd.ll.landmark", | ||
226 | "application/vnd.ll.primitive", | ||
227 | "application/vnd.ll.notecard", | ||
228 | "application/vnd.ll.rootfolder", | ||
229 | "application/vnd.ll.lsltext", | ||
230 | "image/x-j2c", | ||
231 | "application/vnd.ll.primitive", | ||
232 | "application/vnd.ll.clothing", | ||
233 | "application/vnd.ll.gesture" | ||
234 | }; | ||
235 | |||
236 | for (int i=0;i<inventorytypes.Length;i++) | ||
237 | { | ||
238 | Assert.AreEqual(invcontenttypes[i], SLUtil.SLInvTypeToContentType(inventorytypes[i]), | ||
239 | String.Format("Incorrect Content-Type mapped from InventoryType {0}", inventorytypes[i])); | ||
240 | } | ||
241 | |||
242 | invcontenttypes = new string[] | ||
243 | { | ||
244 | "image/x-j2c","image/jp2","image/tga", | ||
245 | "image/jpeg","application/ogg","audio/ogg", | ||
246 | "audio/x-wav","application/vnd.ll.callingcard", | ||
247 | "application/x-metaverse-callingcard", | ||
248 | "application/vnd.ll.landmark", | ||
249 | "application/x-metaverse-landmark", | ||
250 | "application/vnd.ll.clothing", | ||
251 | "application/x-metaverse-clothing","application/vnd.ll.bodypart", | ||
252 | "application/x-metaverse-bodypart","application/vnd.ll.primitive", | ||
253 | "application/x-metaverse-primitive","application/vnd.ll.notecard", | ||
254 | "application/x-metaverse-notecard","application/vnd.ll.folder", | ||
255 | "application/vnd.ll.rootfolder","application/vnd.ll.lsltext", | ||
256 | "application/x-metaverse-lsl","application/vnd.ll.lslbyte", | ||
257 | "application/x-metaverse-lso","application/vnd.ll.trashfolder", | ||
258 | "application/vnd.ll.snapshotfolder", | ||
259 | "application/vnd.ll.lostandfoundfolder","application/vnd.ll.animation", | ||
260 | "application/x-metaverse-animation","application/vnd.ll.gesture", | ||
261 | "application/x-metaverse-gesture","application/x-metaverse-simstate", | ||
262 | "application/octet-stream" | ||
263 | }; | ||
264 | sbyte[] invtypes = new sbyte[] | ||
265 | { | ||
266 | 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 18, 18, 18, 18, 6, 6, 7, 7, -1, 8, 10, 10, 10, 10 | ||
267 | , 14, 15, 16, 19, 19, 20, 20, 15, -1 | ||
268 | }; | ||
269 | |||
270 | for (int i = 0; i < invtypes.Length; i++) | ||
271 | { | ||
272 | Assert.AreEqual(invtypes[i], SLUtil.ContentTypeToSLInvType(invcontenttypes[i]), | ||
273 | String.Format("Incorrect InventoryType mapped from Content-Type {0}", invcontenttypes[i])); | ||
274 | } | ||
275 | } | ||
276 | |||
277 | [Test] | ||
278 | public void FakeParcelIDTests() | ||
279 | { | ||
280 | byte[] hexBytes8 = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; | ||
281 | byte[] hexBytes16 = { | ||
282 | 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, | ||
283 | 0x77, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f }; | ||
284 | UInt64 var64Bit = (UInt64)0xfedcba9876543210; | ||
285 | |||
286 | //Region handle is for location 255000,256000. | ||
287 | ulong regionHandle1 = 1095216660736000; | ||
288 | uint x1 = 100; | ||
289 | uint y1 = 200; | ||
290 | uint z1 = 22; | ||
291 | ulong regionHandle2; | ||
292 | uint x2, y2, z2; | ||
293 | UUID fakeParcelID1, uuid; | ||
294 | |||
295 | ulong bigInt64 = Util.BytesToUInt64Big(hexBytes8); | ||
296 | Assert.AreEqual(var64Bit, bigInt64, | ||
297 | "BytesToUint64Bit conversion of 8 bytes to UInt64 failed."); | ||
298 | |||
299 | //Test building and decoding using some typical input values | ||
300 | fakeParcelID1 = Util.BuildFakeParcelID(regionHandle1, x1, y1); | ||
301 | Util.ParseFakeParcelID(fakeParcelID1, out regionHandle2, out x2, out y2); | ||
302 | Assert.AreEqual(regionHandle1, regionHandle2, | ||
303 | "region handle decoded from FakeParcelID wth X/Y failed."); | ||
304 | Assert.AreEqual(x1, x2, | ||
305 | "X coordinate decoded from FakeParcelID wth X/Y failed."); | ||
306 | Assert.AreEqual(y1, y2, | ||
307 | "Y coordinate decoded from FakeParcelID wth X/Y failed."); | ||
308 | |||
309 | fakeParcelID1 = Util.BuildFakeParcelID(regionHandle1, x1, y1, z1); | ||
310 | Util.ParseFakeParcelID(fakeParcelID1, out regionHandle2, out x2, out y2, out z2); | ||
311 | Assert.AreEqual(regionHandle1, regionHandle2, | ||
312 | "region handle decoded from FakeParcelID with X/Y/Z failed."); | ||
313 | Assert.AreEqual(x1, x2, | ||
314 | "X coordinate decoded from FakeParcelID with X/Y/Z failed."); | ||
315 | Assert.AreEqual(y1, y2, | ||
316 | "Y coordinate decoded from FakeParcelID with X/Y/Z failed."); | ||
317 | Assert.AreEqual(z1, z2, | ||
318 | "Z coordinate decoded from FakeParcelID with X/Y/Z failed."); | ||
319 | |||
320 | //Do some more extreme tests to check the encoding and decoding | ||
321 | x1 = 0x55aa; | ||
322 | y1 = 0x9966; | ||
323 | z1 = 0x5a96; | ||
324 | |||
325 | fakeParcelID1 = Util.BuildFakeParcelID(var64Bit, x1, y1); | ||
326 | Util.ParseFakeParcelID(fakeParcelID1, out regionHandle2, out x2, out y2); | ||
327 | Assert.AreEqual(var64Bit, regionHandle2, | ||
328 | "region handle decoded from FakeParcelID with X/Y/Z failed."); | ||
329 | Assert.AreEqual(x1, x2, | ||
330 | "X coordinate decoded from FakeParcelID with X/Y/Z failed."); | ||
331 | Assert.AreEqual(y1, y2, | ||
332 | "Y coordinate decoded from FakeParcelID with X/Y/Z failed."); | ||
333 | |||
334 | fakeParcelID1 = Util.BuildFakeParcelID(var64Bit, x1, y1, z1); | ||
335 | Util.ParseFakeParcelID(fakeParcelID1, out regionHandle2, out x2, out y2, out z2); | ||
336 | Assert.AreEqual(var64Bit, regionHandle2, | ||
337 | "region handle decoded from FakeParcelID with X/Y/Z failed."); | ||
338 | Assert.AreEqual(x1, x2, | ||
339 | "X coordinate decoded from FakeParcelID with X/Y/Z failed."); | ||
340 | Assert.AreEqual(y1, y2, | ||
341 | "Y coordinate decoded from FakeParcelID with X/Y/Z failed."); | ||
342 | Assert.AreEqual(z1, z2, | ||
343 | "Z coordinate decoded from FakeParcelID with X/Y/Z failed."); | ||
344 | |||
345 | |||
346 | x1 = 64; | ||
347 | y1 = 192; | ||
348 | fakeParcelID1 = Util.BuildFakeParcelID(regionHandle1, x1, y1); | ||
349 | Util.FakeParcelIDToGlobalPosition(fakeParcelID1, out x2, out y2); | ||
350 | Assert.AreEqual(255000+x1, x2, | ||
351 | "Global X coordinate decoded from regionHandle failed."); | ||
352 | Assert.AreEqual(256000+y1, y2, | ||
353 | "Global Y coordinate decoded from regionHandle failed."); | ||
354 | |||
355 | uuid = new UUID("00dd0700-00d1-0700-3800-000032000000"); | ||
356 | Util.FakeParcelIDToGlobalPosition(uuid, out x2, out y2); | ||
357 | } | ||
358 | } | ||
359 | } | ||