Exemplo n.º 1
0
        public void AddTest()
        {
            ByteString bytes = new ByteString();

              byte[] arr = new byte[10];
              for (int i = 0;i < 10;i++) {
            arr[i] = (byte)i;
              }
              bytes.Add(arr);
              Assert.AreEqual(bytes.Length,10);

              byte[] arr2 = new byte[15];
              for (int i = 0;i < 15;i++) {
            arr2[i] = (byte)(i + 10);
              }
              bytes.Add(arr2);
              Assert.AreEqual(bytes.Length,25);

              Assert.AreEqual(bytes.GetValue(5),(byte)5);
              Assert.AreEqual(bytes.GetValue(12),(byte)12);

              byte[] r = bytes.ToArray();
              Assert.AreEqual(r.Length,25);
              for (int i = 0;i < 25;i++) {
            Assert.AreEqual(r[i],(byte)i);
              }
        }
Exemplo n.º 2
0
        public void CompareTest()
        {
            var a = new ByteString(new byte[4]{1,2,3,4});
              var b = new ByteString(new byte[4]{1,2,3,4});

              Assert.IsTrue(a.Equals(b));
              Assert.IsTrue(a == b);
        }
Exemplo n.º 3
0
 public void AddOther()
 {
     var a = new ByteString(new byte[4]{1,2,3,4});
       var b = new ByteString(new byte[4]{1,2,3,4});
       Assert.AreEqual(a.Length,4);
       Assert.AreEqual(b.Length,4);
       a.Add(b);
       Assert.AreEqual(a.Length,8);
       Assert.AreEqual(b.Length,4);
 }
 public void ReadByteTest()
 {
     var arr = new byte[10];
       for (int i = 0;i < 10;i++) {
     arr[i] = (byte)i;
       }
       var bytes = new ByteString(arr);
       var reader = new ByteStringReader(bytes);
       for (int i = 0;i < 10;i++) {
     var b = reader.ReadByte();
     Assert.AreEqual(b,(byte)i);
       }
 }
Exemplo n.º 5
0
        public void ConcatTest(int a,int b)
        {
            byte[] arr_a = new byte[a];
              for (int i = 0;i < a;i++) {
            arr_a[i] = (byte)i;
              }
              ByteString ba = new ByteString(arr_a);

              byte[] arr_b = new byte[b];
              for (int i = 0;i <b;i++) {
            arr_b[i] = (byte)(i + a);
              }
              ByteString bb = new ByteString(arr_b);

              ByteString bc = ba + bb;
              Assert.AreEqual(bc.Length,a + b);
              byte [] arr_c = bc.ToArray();
              for (int i = 0;i < a + b;i++) {
            Assert.AreEqual(arr_c[i],(byte)i);
              }
        }
Exemplo n.º 6
0
        public void GetBytesTest(int begin,int end)
        {
            ByteString bytes = new ByteString();

              byte[] arr = new byte[10];
              for (int i = 0;i < 10;i++) {
            arr[i] = (byte)i;
              }
              bytes.Add(arr);
              Assert.AreEqual(bytes.Length,10);

              byte[] arr2 = new byte[15];
              for (int i = 0;i < 15;i++) {
            arr2[i] = (byte)(i + 10);
              }
              bytes.Add(arr2);
              Assert.AreEqual(bytes.Length,25);

              byte[] part = bytes.GetBytes(begin,end);

              int l = end - begin;
              Assert.AreEqual(part.Length,l);
              for (int i = 0;i < l;i++) {
            Assert.AreEqual(part[i],(byte)(i + begin));
              }
        }
Exemplo n.º 7
0
 public ByteString Drop(int n)
 {
     var newBytes = new ByteString();
       foreach (PartBytes bytes in _bytesList) {
     if (n == 0) {
       newBytes.Add(bytes);
     } else {
       var l = bytes.Length;
       if (l <= n) {
     // skip
     n -= l;
       } else {
     //byte[] left = new byte[l - n];
     //Array.Copy(bytes,n,left,0,l - n);
     var left = new PartBytes(bytes,n,l - n);
     newBytes.Add(left);
     n = 0;
       }
     }
       }
       return newBytes;
 }
Exemplo n.º 8
0
 public ByteString Add(ByteString other)
 {
     foreach(PartBytes b in other._bytesList) {
     Add(b);
       }
       return this;
 }
Exemplo n.º 9
0
 public static short CalcCrc(ByteString bytes)
 {
     int c = 0;
       for (int i = 0;i < bytes.Length;i++) {
     c += (int)((sbyte)bytes[i]);
       }
       return (short)(c ^ 0xffff);
 }
Exemplo n.º 10
0
 public Packet(ByteString bytes,bool encrypted = false)
 {
     _bytes = bytes;
       _encrypted = encrypted;
       _crc = CalcCrc(_bytes);
 }