public void Test() { System.Random r = new System.Random(); //Test numeric type codes: for (int i = 1; i < 32; i++) { PType p = new PType(i); MemBlock b = p.ToMemBlock(); byte[] buf = new byte[100]; r.NextBytes(buf); //Get some junk: MemBlock junk = MemBlock.Reference(buf); MemBlock b1 = MemBlock.Concat(b, junk); MemBlock rest = null; PType pp = PType.Parse(b1, out rest); byte[] buf2 = new byte[1]; buf2[0] = (byte)i; MemBlock b2 = MemBlock.Reference(buf2); Assert.AreEqual(p, pp, System.String.Format("Round trip int: {0}", i)); Assert.AreEqual(b, b2, System.String.Format("Convert to MemBlock int: {0}", i)); Assert.AreEqual(i, pp.TypeNumber, "Typenumber equality"); Assert.AreEqual(rest, junk, "rest in int PType"); } //Test string types: for (int i = 0; i < 1000; i++) { //Make a random string: // byte[] buf = new byte[r.Next(1, 100)]; r.NextBytes(buf); string s = Base32.Encode(buf); PType p1 = new PType(s); r.NextBytes(buf); //Get some junk: MemBlock b = MemBlock.Copy(buf); MemBlock combine = MemBlock.Concat(p1.ToMemBlock(), b); MemBlock b2 = null; PType p2 = PType.Parse(combine, out b2); Assert.AreEqual(p1, p2, "Round trip string: " + s); Assert.AreEqual(b, b2, "Round trip rest"); Assert.AreEqual(s, p2.ToString(), "Round trip to string"); Assert.AreEqual(s, p1.ToString(), "Round trip to string"); Assert.AreEqual(p1.TypeNumber, p2.TypeNumber, "RT: TypeNumber test"); } //Test all one byte ascii strings: for (byte b = 32; b < ASCII_UPPER_BOUND; b++) { MemBlock raw = MemBlock.Reference(new byte[] { b, 0 }); MemBlock rest; PType p1 = PType.Parse(raw, out rest); Assert.AreEqual(rest, MemBlock.Null, "Rest is null"); PType p2 = PType.Parse(raw, out rest); Assert.AreEqual(rest, MemBlock.Null, "Rest is null"); Assert.IsTrue(p1 == p2, "reference equality of single byte type"); Assert.AreEqual(p1, p2, "equality of single byte type"); Assert.AreEqual(p1, new PType(p1.ToString()), "Round trip string"); } //Test TypeNumber of string types: for (int i = 0; i < 100; i++) { byte[] buf = new byte[20]; r.NextBytes(buf); for (int j = 1; j < 4; j++) { string s = Base32.Encode(buf).Substring(0, j); PType p1 = new PType(s); byte[] buf2 = System.Text.Encoding.UTF8.GetBytes(s); int t = 0; for (int k = 0; k < buf2.Length; k++) { t = t | buf2[k]; t <<= 8; } Assert.AreEqual(t, p1.TypeNumber, System.String.Format("String type number: {0}, s={1}", t, s)); } } //Console.Error.WriteLine("Tested PType"); }
public void Test() { System.Random r = new System.Random(); byte[] data; for (int i = 0; i < 100; i++) { data = new byte[r.Next(1024)]; r.NextBytes(data); int offset = r.Next(data.Length); MemBlock mb1 = new MemBlock(data, 0, data.Length); MemBlock mb1a = MemBlock.Copy(data, 0, data.Length); Assert.IsTrue(mb1.Equals(mb1a), "MemBlock.Copy"); Assert.IsTrue(mb1.Equals(data), "MemBlock == byte[]"); MemBlock mb2 = new MemBlock(data, offset, data.Length - offset); MemBlock mb2a = mb1.Slice(offset); MemBlock mb3 = new MemBlock(data, 0, offset); MemBlock mb3a = mb1.Slice(0, offset); Assert.IsTrue(mb3.Equals(mb3a), "mb3.Equals(mb3a)"); Assert.IsTrue(mb3a.Equals(mb3), "mb3a.Equals(mb3)"); Assert.IsTrue(mb3.CompareTo(mb2) + mb2.CompareTo(mb3) == 0, "CompareTo"); Assert.IsTrue(mb2.Equals(mb2a), "mb2.Equals(mb2a)"); Assert.IsTrue(mb2a.Equals(mb2), "mb2a.Equals(mb2)"); MemBlock cat = MemBlock.Concat(mb3, mb2); MemBlock cata = MemBlock.Concat(mb3a, mb2a); Assert.IsTrue(cat.Equals(cata), "Concat Equals"); Assert.IsTrue(cata.Equals(cat), "Concat a Equals"); Assert.IsTrue(mb1.Equals(cat), "Concat Equals Original"); if (offset != 0) { //These should not be equal Assert.IsFalse(mb2.Equals(mb1), "mb2 != mb1"); } int mb2a_l = mb2a.Length; byte[] tmp_data = new byte[mb2a_l]; mb2a.CopyTo(tmp_data, 0); MemBlock mb2b = new MemBlock(tmp_data, 0, tmp_data.Length); Assert.IsTrue(mb2a.Equals(mb2b), "mb2a.Equals(mb2b)"); Assert.IsTrue(mb2b.Equals(mb2a), "mb2b.Equals(mb2a)"); //Check the Hash: Assert.IsTrue(mb2b.GetHashCode() == mb2a.GetHashCode(), "GetHashCode"); //Here are some manual equality testing using the indexer bool all_equals = true; int j = 0; while (all_equals && (j < mb1.Length)) { all_equals = (mb1[j] == cat[j]); j++; } Assert.IsTrue(all_equals, "Manual equality test mb1"); all_equals = true; j = 0; while (all_equals && (j < mb2.Length)) { all_equals = (mb2[j] == mb2a[j]); j++; } Assert.IsTrue(all_equals, "Manual equality test mb2"); all_equals = true; j = 0; while (all_equals && (j < mb2.Length)) { all_equals = (mb2[j] == mb2b[j]); j++; } Assert.IsTrue(all_equals, "Manual equality test mb2b"); } }