Ejemplo n.º 1
0
 public static Bucket GetFromBytes(byte[] data, ref int offset, Encoding textEncoding = null)
 {
     Bucket bucket = new Bucket();
     offset = bucket.FromBytes(data, offset, textEncoding);
     return bucket;
 }
Ejemplo n.º 2
0
        public void TestBucketToFromBytes()
        {
            // Arrange
            Bucket sourceBucket = new Bucket(54, "SourceBucket");
            Bucket otherBucket = new Bucket(4, "OtherBucket");
            Encoding textEncoding = Encoding.UTF8;
            Bucket utNewBucket = new Bucket();
            Bucket utFullBucket = new Bucket();
            Bucket utTransBucket = new Bucket();
            Bucket utEmptiedBucket = new Bucket();
            Bucket utNewBucketFromBuffer = new Bucket();
            Bucket utFullBucketFromBuffer = new Bucket();
            Bucket utTransBucketFromBuffer = new Bucket();
            Bucket utEmptiedBucketFromBuffer = new Bucket();
            Reservoire waterSrc = new Reservoire();

            // Act
            byte[] utNewBytes = sourceBucket.GetBytes(textEncoding);
            sourceBucket.Fill(waterSrc);
            byte[] utFullBytes = sourceBucket.GetBytes(textEncoding);
            sourceBucket.TransferTo(otherBucket);
            byte[] utTransBytes = sourceBucket.GetBytes(textEncoding);
            sourceBucket.Empty(waterSrc);
            byte[] utEmptiedBytes = sourceBucket.GetBytes(textEncoding);

            utNewBucket.FromBytes(utNewBytes, textEncoding: textEncoding);
            utFullBucket.FromBytes(utFullBytes, textEncoding: textEncoding);
            utTransBucket.FromBytes(utTransBytes, textEncoding: textEncoding);
            utEmptiedBucket.FromBytes(utEmptiedBytes, textEncoding: textEncoding);

            byte[] utBuffer = new byte[utNewBytes.Length + utFullBytes.Length + utTransBytes.Length + utEmptiedBytes.Length];
            int toBufferOffset = 0;
            toBufferOffset = utNewBucket.OntoBuffer(utBuffer, toBufferOffset, textEncoding);
            toBufferOffset = utFullBucket.OntoBuffer(utBuffer, toBufferOffset, textEncoding);
            toBufferOffset = utTransBucket.OntoBuffer(utBuffer, toBufferOffset, textEncoding);
            toBufferOffset = utEmptiedBucket.OntoBuffer(utBuffer, toBufferOffset, textEncoding);
            int fromBufferOffset = 0;
            fromBufferOffset = utNewBucketFromBuffer.FromBytes(utBuffer, fromBufferOffset, textEncoding);
            fromBufferOffset = utFullBucketFromBuffer.FromBytes(utBuffer, fromBufferOffset, textEncoding);
            fromBufferOffset = utTransBucketFromBuffer.FromBytes(utBuffer, fromBufferOffset, textEncoding);
            fromBufferOffset = utEmptiedBucketFromBuffer.FromBytes(utBuffer, fromBufferOffset, textEncoding);

            // Assert
            Assert.AreEqual(sourceBucket.Capacity, utNewBucket.Capacity, "Retrieved new bucket has different capacity");
            Assert.AreEqual(0, utNewBucket.CurrentFill, "Retrieved new bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utNewBucket.Name, "Retrieved new bucket has wrong name");
            Assert.IsTrue(utNewBucket.IsEmpty, "Retrieved new bucket is not empty");
            Assert.IsFalse(utNewBucket.IsFull, "Retrieved new bucket is full");

            Assert.AreEqual(sourceBucket.Capacity, utFullBucket.Capacity, "Retrieved full bucket has different capacity");
            Assert.AreEqual(sourceBucket.Capacity, utFullBucket.CurrentFill, "Retrieved full bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utFullBucket.Name, "Retrieved full bucket has wrong name");
            Assert.IsFalse(utFullBucket.IsEmpty, "Retrieved full bucket is empty");
            Assert.IsTrue(utFullBucket.IsFull, "Retrieved full bucket is not full");

            Assert.AreEqual(sourceBucket.Capacity, utTransBucket.Capacity, "Retrieved transferred bucket has different capacity");
            Assert.AreEqual(sourceBucket.Capacity - otherBucket.Capacity, utTransBucket.CurrentFill, "Retrieved transferred bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utTransBucket.Name, "Retrieved transferred bucket has wrong name");
            Assert.IsFalse(utTransBucket.IsEmpty, "Retrieved transferred bucket is empty");
            Assert.IsFalse(utTransBucket.IsFull, "Retrieved transferred bucket is full");

            Assert.AreEqual(sourceBucket.Capacity, utEmptiedBucket.Capacity, "Retrieved emptied bucket has different capacity");
            Assert.AreEqual(0, utEmptiedBucket.CurrentFill, "Retrieved emptied bucket has wrong current fill");
            Assert.AreEqual(sourceBucket.Name, utEmptiedBucket.Name, "Retrieved emptied bucket has wrong name");
            Assert.IsTrue(utEmptiedBucket.IsEmpty, "Retrieved emptied bucket is not empty");
            Assert.IsFalse(utEmptiedBucket.IsFull, "Retrieved emptied bucket is full");

            Assert.AreEqual(utNewBucket.Capacity, utNewBucketFromBuffer.Capacity, "Retrieved new bucket from buffer has different capacity");
            Assert.AreEqual(utNewBucket.CurrentFill, utNewBucketFromBuffer.CurrentFill, "Retrieved new bucket from buffer has wrong current fill");
            Assert.AreEqual(utNewBucket.Name, utNewBucketFromBuffer.Name, "Retrieved new bucket from buffer has wrong name");
            Assert.IsTrue(utNewBucketFromBuffer.IsEmpty, "Retrieved new bucket from buffer is not empty");
            Assert.IsFalse(utNewBucketFromBuffer.IsFull, "Retrieved new bucket from buffer is full");

            Assert.AreEqual(utFullBucket.Capacity, utFullBucketFromBuffer.Capacity, "Retrieved Full bucket from buffer has different capacity");
            Assert.AreEqual(utFullBucket.CurrentFill, utFullBucketFromBuffer.CurrentFill, "Retrieved Full bucket from buffer has wrong current fill");
            Assert.AreEqual(utFullBucket.Name, utFullBucketFromBuffer.Name, "Retrieved Full bucket from buffer has wrong name");
            Assert.IsFalse(utFullBucketFromBuffer.IsEmpty, "Retrieved Full bucket from buffer is empty");
            Assert.IsTrue(utFullBucketFromBuffer.IsFull, "Retrieved Full bucket from buffer is not full");

            Assert.AreEqual(utTransBucket.Capacity, utTransBucketFromBuffer.Capacity, "Retrieved Transferred bucket from buffer has different capacity");
            Assert.AreEqual(utTransBucket.CurrentFill, utTransBucketFromBuffer.CurrentFill, "Retrieved Transferred bucket from buffer has wrong current fill");
            Assert.AreEqual(utTransBucket.Name, utTransBucketFromBuffer.Name, "Retrieved Transferred bucket from buffer has wrong name");
            Assert.IsFalse(utTransBucketFromBuffer.IsEmpty, "Retrieved Transferred bucket from buffer is empty");
            Assert.IsFalse(utTransBucketFromBuffer.IsFull, "Retrieved Transferred bucket from buffer is full");

            Assert.AreEqual(utEmptiedBucket.Capacity, utEmptiedBucketFromBuffer.Capacity, "Retrieved Emptied bucket from buffer has different capacity");
            Assert.AreEqual(utEmptiedBucket.CurrentFill, utEmptiedBucketFromBuffer.CurrentFill, "Retrieved Emptied bucket from buffer has wrong current fill");
            Assert.AreEqual(utEmptiedBucket.Name, utEmptiedBucketFromBuffer.Name, "Retrieved Emptied bucket from buffer has wrong name");
            Assert.IsTrue(utEmptiedBucketFromBuffer.IsEmpty, "Retrieved Emptied bucket from buffer is not empty");
            Assert.IsFalse(utEmptiedBucketFromBuffer.IsFull, "Retrieved Emptied bucket from buffer is full");

            Assert.AreEqual(toBufferOffset, fromBufferOffset, "Buffer offsets differ");
        }