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(); //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"); }