corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
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 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.Collections;
29 using System.Collections.Generic;
30 using OpenMetaverse;
31 using OpenMetaverse.Packets;
32 using OpenMetaverse.StructuredData;
33 using NUnit.Framework;
34  
35 namespace OpenMetaverse.Tests
36 {
37 [TestFixture]
38 public class TypeTests : Assert
39 {
40 [Test]
41 public void UUIDs()
42 {
43 // Creation
44 UUID a = new UUID();
45 byte[] bytes = a.GetBytes();
46 for (int i = 0; i < 16; i++)
47 Assert.IsTrue(bytes[i] == 0x00);
48  
49 // Comparison
50 a = new UUID(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
51 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF }, 0);
52 UUID b = new UUID(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
53 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }, 0);
54  
55 Assert.IsTrue(a == b, "UUID comparison operator failed, " + a.ToString() + " should equal " +
56 b.ToString());
57  
58 // From string
59 a = new UUID(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
60 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }, 0);
61 string zeroonetwo = "00010203-0405-0607-0809-0a0b0c0d0e0f";
62 b = new UUID(zeroonetwo);
63  
64 Assert.IsTrue(a == b, "UUID hyphenated string constructor failed, should have " + a.ToString() +
65 " but we got " + b.ToString());
66  
67 // ToString()
68 Assert.IsTrue(a == b);
69 Assert.IsTrue(a == (UUID)zeroonetwo);
70  
71 // TODO: CRC test
72 }
73  
74 [Test]
75 public void Vector3ApproxEquals()
76 {
77 Vector3 a = new Vector3(1f, 0f, 0f);
78 Vector3 b = new Vector3(0f, 0f, 0f);
79  
80 Assert.IsFalse(a.ApproxEquals(b, 0.9f), "ApproxEquals failed (1)");
81 Assert.IsTrue(a.ApproxEquals(b, 1.0f), "ApproxEquals failed (2)");
82  
83 a = new Vector3(-1f, 0f, 0f);
84 b = new Vector3(1f, 0f, 0f);
85  
86 Assert.IsFalse(a.ApproxEquals(b, 1.9f), "ApproxEquals failed (3)");
87 Assert.IsTrue(a.ApproxEquals(b, 2.0f), "ApproxEquals failed (4)");
88  
89 a = new Vector3(0f, -1f, 0f);
90 b = new Vector3(0f, -1.1f, 0f);
91  
92 Assert.IsFalse(a.ApproxEquals(b, 0.09f), "ApproxEquals failed (5)");
93 Assert.IsTrue(a.ApproxEquals(b, 0.11f), "ApproxEquals failed (6)");
94  
95 a = new Vector3(0f, 0f, 0.00001f);
96 b = new Vector3(0f, 0f, 0f);
97  
98 Assert.IsFalse(b.ApproxEquals(a, Single.Epsilon), "ApproxEquals failed (6)");
99 Assert.IsTrue(b.ApproxEquals(a, 0.0001f), "ApproxEquals failed (7)");
100 }
101  
102 [Test]
103 public void VectorCasting()
104 {
105 Dictionary<string, double> testNumbers;
106 testNumbers = new Dictionary<string, double>();
107 testNumbers["1.0"] = 1.0;
108 testNumbers["1.1"] = 1.1;
109 testNumbers["1.01"] = 1.01;
110 testNumbers["1.001"] = 1.001;
111 testNumbers["1.0001"] = 1.0001;
112 testNumbers["1.00001"] = 1.00001;
113 testNumbers["1.000001"] = 1.000001;
114 testNumbers["1.0000001"] = 1.0000001;
115 testNumbers["1.00000001"] = 1.00000001;
116  
117 foreach (KeyValuePair<string, double> kvp in testNumbers)
118 {
119 double testNumber = kvp.Value;
120 double testNumber2 = (double)((float)testNumber);
121 bool noPrecisionLoss = testNumber == testNumber2;
122  
123 Vector3 a = new Vector3(
124 (float)testNumber,
125 (float)testNumber, (float)testNumber);
126 Vector3d b = new Vector3d(testNumber, testNumber, testNumber);
127  
128 Vector3 c = (Vector3)b;
129 Vector3d d = a;
130  
131 if (noPrecisionLoss)
132 {
133 Console.Error.WriteLine("Unsuitable test value used-" +
134 " test number should have precision loss when" +
135 " cast to float ({0}).", kvp.Key);
136 }
137 else
138 {
139 Assert.IsFalse(a == b, string.Format(
140 "Vector casting failed, precision loss should" +
141 " have occurred. " +
142 "{0}: {1}, {2}", kvp.Key, a.X, b.X));
143 Assert.IsFalse(b == d, string.Format(
144 "Vector casting failed, explicit cast of double" +
145 " to float should result in precision loss" +
146 " whichwas should not magically disappear when" +
147 " Vector3 is implicitly cast to Vector3d." +
148 " {0}: {1}, {2}", kvp.Key, b.X, d.X));
149 }
150 Assert.IsTrue(a == c, string.Format(
151 "Vector casting failed, Vector3 compared to" +
152 " explicit cast of Vector3d to Vector3 should" +
153 " result in identical precision loss." +
154 " {0}: {1}, {2}", kvp.Key, a.X, c.X));
155 Assert.IsTrue(a == d, string.Format(
156 "Vector casting failed, implicit cast of Vector3" +
157 " to Vector3d should not result in precision loss." +
158 " {0}: {1}, {2}", kvp.Key, a.X, d.X));
159 }
160 }
161  
162 [Test]
163 public void Quaternions()
164 {
165 Quaternion a = new Quaternion(1, 0, 0, 0);
166 Quaternion b = new Quaternion(1, 0, 0, 0);
167  
168 Assert.IsTrue(a == b, "Quaternion comparison operator failed");
169  
170 Quaternion expected = new Quaternion(0, 0, 0, -1);
171 Quaternion result = a * b;
172  
173 Assert.IsTrue(result == expected, a.ToString() + " * " + b.ToString() + " produced " + result.ToString() +
174 " instead of " + expected.ToString());
175  
176 a = new Quaternion(1, 0, 0, 0);
177 b = new Quaternion(0, 1, 0, 0);
178 expected = new Quaternion(0, 0, 1, 0);
179 result = a * b;
180  
181 Assert.IsTrue(result == expected, a.ToString() + " * " + b.ToString() + " produced " + result.ToString() +
182 " instead of " + expected.ToString());
183  
184 a = new Quaternion(0, 0, 1, 0);
185 b = new Quaternion(0, 1, 0, 0);
186 expected = new Quaternion(-1, 0, 0, 0);
187 result = a * b;
188  
189 Assert.IsTrue(result == expected, a.ToString() + " * " + b.ToString() + " produced " + result.ToString() +
190 " instead of " + expected.ToString());
191 }
192  
193 //[Test]
194 //public void VectorQuaternionMath()
195 //{
196 // // Convert a vector to a quaternion and back
197 // Vector3 a = new Vector3(1f, 0.5f, 0.75f);
198 // Quaternion b = a.ToQuaternion();
199 // Vector3 c;
200 // b.GetEulerAngles(out c.X, out c.Y, out c.Z);
201  
202 // Assert.IsTrue(a == c, c.ToString() + " does not equal " + a.ToString());
203 //}
204  
205 [Test]
206 public void FloatsToTerseStrings()
207 {
208 float f = 1.20f;
209 string a = String.Empty;
210 string b = "1.2";
211  
212 a = Helpers.FloatToTerseString(f);
213 Assert.IsTrue(a == b, f.ToString() + " converted to " + a + ", expecting " + b);
214  
215 f = 24.00f;
216 b = "24";
217  
218 a = Helpers.FloatToTerseString(f);
219 Assert.IsTrue(a == b, f.ToString() + " converted to " + a + ", expecting " + b);
220  
221 f = -0.59f;
222 b = "-.59";
223  
224 a = Helpers.FloatToTerseString(f);
225 Assert.IsTrue(a == b, f.ToString() + " converted to " + a + ", expecting " + b);
226  
227 f = 0.59f;
228 b = ".59";
229  
230 a = Helpers.FloatToTerseString(f);
231 Assert.IsTrue(a == b, f.ToString() + " converted to " + a + ", expecting " + b);
232 }
233  
234 [Test]
235 public void BitUnpacking()
236 {
237 byte[] data = new byte[] { 0x80, 0x00, 0x0F, 0x50, 0x83, 0x7D };
238 BitPack bitpacker = new BitPack(data, 0);
239  
240 int b = bitpacker.UnpackBits(1);
241 Assert.IsTrue(b == 1, "Unpacked " + b + " instead of 1");
242  
243 b = bitpacker.UnpackBits(1);
244 Assert.IsTrue(b == 0, "Unpacked " + b + " instead of 0");
245  
246 bitpacker = new BitPack(data, 2);
247  
248 b = bitpacker.UnpackBits(4);
249 Assert.IsTrue(b == 0, "Unpacked " + b + " instead of 0");
250  
251 b = bitpacker.UnpackBits(8);
252 Assert.IsTrue(b == 0xF5, "Unpacked " + b + " instead of 0xF5");
253  
254 b = bitpacker.UnpackBits(4);
255 Assert.IsTrue(b == 0, "Unpacked " + b + " instead of 0");
256  
257 b = bitpacker.UnpackBits(10);
258 Assert.IsTrue(b == 0x0183, "Unpacked " + b + " instead of 0x0183");
259 }
260  
261 [Test]
262 public void BitPacking()
263 {
264 byte[] packedBytes = new byte[12];
265 BitPack bitpacker = new BitPack(packedBytes, 0);
266  
267 bitpacker.PackBits(0x0ABBCCDD, 32);
268 bitpacker.PackBits(25, 5);
269 bitpacker.PackFloat(123.321f);
270 bitpacker.PackBits(1000, 16);
271  
272 bitpacker = new BitPack(packedBytes, 0);
273  
274 int b = bitpacker.UnpackBits(32);
275 Assert.IsTrue(b == 0x0ABBCCDD, "Unpacked " + b + " instead of 2864434397");
276  
277 b = bitpacker.UnpackBits(5);
278 Assert.IsTrue(b == 25, "Unpacked " + b + " instead of 25");
279  
280 float f = bitpacker.UnpackFloat();
281 Assert.IsTrue(f == 123.321f, "Unpacked " + f + " instead of 123.321");
282  
283 b = bitpacker.UnpackBits(16);
284 Assert.IsTrue(b == 1000, "Unpacked " + b + " instead of 1000");
285  
286 packedBytes = new byte[1];
287 bitpacker = new BitPack(packedBytes, 0);
288 bitpacker.PackBit(true);
289  
290 bitpacker = new BitPack(packedBytes, 0);
291 b = bitpacker.UnpackBits(1);
292 Assert.IsTrue(b == 1, "Unpacked " + b + " instead of 1");
293  
294 packedBytes = new byte[1] { Byte.MaxValue };
295 bitpacker = new BitPack(packedBytes, 0);
296 bitpacker.PackBit(false);
297  
298 bitpacker = new BitPack(packedBytes, 0);
299 b = bitpacker.UnpackBits(1);
300 Assert.IsTrue(b == 0, "Unpacked " + b + " instead of 0");
301 }
302  
303 [Test]
304 public void LLSDTerseParsing()
305 {
306 string testOne = "[r0.99967899999999998428,r-0.025334599999999998787,r0]";
307 string testTwo = "[[r1,r1,r1],r0]";
308 string testThree = "{'region_handle':[r255232, r256512], 'position':[r33.6, r33.71, r43.13], 'look_at':[r34.6, r33.71, r43.13]}";
309  
310 OSD obj = OSDParser.DeserializeLLSDNotation(testOne);
311 Assert.IsInstanceOfType(typeof(OSDArray), obj, "Expected SDArray, got " + obj.GetType().ToString());
312 OSDArray array = (OSDArray)obj;
313 Assert.IsTrue(array.Count == 3, "Expected three contained objects, got " + array.Count);
314 Assert.IsTrue(array[0].AsReal() > 0.9d && array[0].AsReal() < 1.0d, "Unexpected value for first real " + array[0].AsReal());
315 Assert.IsTrue(array[1].AsReal() < 0.0d && array[1].AsReal() > -0.03d, "Unexpected value for second real " + array[1].AsReal());
316 Assert.IsTrue(array[2].AsReal() == 0.0d, "Unexpected value for third real " + array[2].AsReal());
317  
318 obj = OSDParser.DeserializeLLSDNotation(testTwo);
319 Assert.IsInstanceOfType(typeof(OSDArray), obj, "Expected SDArray, got " + obj.GetType().ToString());
320 array = (OSDArray)obj;
321 Assert.IsTrue(array.Count == 2, "Expected two contained objects, got " + array.Count);
322 Assert.IsTrue(array[1].AsReal() == 0.0d, "Unexpected value for real " + array[1].AsReal());
323 obj = array[0];
324 Assert.IsInstanceOfType(typeof(OSDArray), obj, "Expected ArrayList, got " + obj.GetType().ToString());
325 array = (OSDArray)obj;
326 Assert.IsTrue(array[0].AsReal() == 1.0d && array[1].AsReal() == 1.0d && array[2].AsReal() == 1.0d,
327 "Unexpected value(s) for nested array: " + array[0].AsReal() + ", " + array[1].AsReal() + ", " +
328 array[2].AsReal());
329  
330 obj = OSDParser.DeserializeLLSDNotation(testThree);
331 Assert.IsInstanceOfType(typeof(OSDMap), obj, "Expected LLSDMap, got " + obj.GetType().ToString());
332 OSDMap hashtable = (OSDMap)obj;
333 Assert.IsTrue(hashtable.Count == 3, "Expected three contained objects, got " + hashtable.Count);
334 Assert.IsInstanceOfType(typeof(OSDArray), hashtable["region_handle"]);
335 Assert.IsTrue(((OSDArray)hashtable["region_handle"]).Count == 2);
336 Assert.IsInstanceOfType(typeof(OSDArray), hashtable["position"]);
337 Assert.IsTrue(((OSDArray)hashtable["position"]).Count == 3);
338 Assert.IsInstanceOfType(typeof(OSDArray), hashtable["look_at"]);
339 Assert.IsTrue(((OSDArray)hashtable["look_at"]).Count == 3);
340 }
341 }
342 }