Exemple #1
0
 public void Test()
 {
     System.Random r = new System.Random();
     for (int i = 0; i < 1024; i++)
     {
         //Test ClassOf and SetClass:
         int    c    = r.Next(160);
         byte[] buf0 = new byte[Address.MemSize];
         //Fill it with junk
         r.NextBytes(buf0);
         Address.SetClass(buf0, c);
         int c2 = Address.ClassOf(MemBlock.Reference(buf0, 0, Address.MemSize));
         Assert.AreEqual(c, c2, "Class Round Trip");
         //Test BigInteger stuff:
         int    size = r.Next(1, MemSize + 1);
         byte[] buf1 = new byte[size];
         r.NextBytes(buf1);
         BigInteger b1   = new BigInteger(buf1);
         byte[]     buf2 = Address.ConvertToAddressBuffer(b1);
         //Check to see if the bytes are equivalent:
         int  min_len = System.Math.Min(buf1.Length, buf2.Length);
         bool all_eq  = true;
         for (int j = 0; j < min_len; j++)
         {
             all_eq = all_eq &&
                      (buf2[buf2.Length - j - 1] == buf1[buf1.Length - j - 1]);
         }
         if (!all_eq)
         {
             System.Console.Error.WriteLine("Buf1: ");
             foreach (byte b in buf1)
             {
                 System.Console.Write("{0} ", b);
             }
             System.Console.Error.WriteLine();
             System.Console.Error.WriteLine("Buf2: ");
             foreach (byte b in buf2)
             {
                 System.Console.Write("{0} ", b);
             }
             System.Console.Error.WriteLine();
         }
         Assert.IsTrue(all_eq, "bytes are equivalent");
         BigInteger b2 = new BigInteger(buf2);
         Assert.AreEqual(b1, b2, "BigInteger round trip");
     }
 }