Beispiel #1
0
        public void TestRemove_End()
        {
            NcByteCollection instance = new NcByteCollection();

            byte[] data = NcTestUtils.RandomBytes(12 * 1024);
            instance.AddRange(data);
            instance.RemoveRange(7 * 1024, 5 * 1024);

            Assert.IsTrue(instance.SequenceEqual(data.Take(7 * 1024)));
        }
Beispiel #2
0
        public void TestRemove_Small()
        {
            NcByteCollection instance = new NcByteCollection();

            byte[] data = NcTestUtils.RandomBytes(100);
            instance.AddRange(data);
            instance.RemoveRange(10, 50);

            Assert.IsTrue(instance.SequenceEqual(data.Take(10).Concat(data.Skip(10 + 50))));
        }
Beispiel #3
0
        public void TestRemove_Medium()
        {
            NcByteCollection instance = new NcByteCollection();

            byte[] data = NcTestUtils.RandomBytes(5 * 1024);
            instance.AddRange(data);
            instance.RemoveRange(3 * 1024, 1536);

            Assert.IsTrue(instance.SequenceEqual(data.Take(3 * 1024).Concat(data.Skip(3 * 1024 + 1536))));
        }
Beispiel #4
0
        public void TestCompact()
        {
            NcByteCollection data = new NcByteCollection(NcTestUtils.RandomBytes(12 * 1024));

            data.RemoveRange(3 * 1024, 100);
            data.InsertRange(2 * 1024, NcTestUtils.RandomBytes(200));
            data.RemoveRange(5 * 1024, 5 * 1024);
            data.InsertRange(6 * 1024, NcTestUtils.RandomBytes(5 * 1024));
            data.RemoveRange(7 * 1024, 3 * 1024);
#if DEBUG
            long oldBytes = data.BlockLengthTotal;
            Console.WriteLine("Used bytes: {0}, Total bytes: {1}, Blocks: {2}", data.LongCount, data.BlockLengthTotal, data.BlockCount);
#endif
            byte[] snapshot = data.ToArray();
            data.Compact();
#if DEBUG
            Console.WriteLine("Used bytes: {0}, Total bytes: {1}, Blocks: {2}", data.LongCount, data.BlockLengthTotal, data.BlockCount);
#endif
            byte[] compacted = data.ToArray();
            Assert.IsTrue(snapshot.SequenceEqual(compacted));
#if DEBUG
            Assert.AreNotEqual(oldBytes, data.BlockLengthTotal);
#endif
        }
Beispiel #5
0
        /// <summary>
        /// Changes the length of the stream.
        /// </summary>
        /// <param name="value"></param>
        /// <remarks>
        /// If <paramref name="value"/> is less than the current length of the
        /// stream, the data is truncated; otherwise, additional (undefined)
        /// elements are added to the end of the collection.
        /// </remarks>
        public override void SetLength(long value)
        {
            lock (syncLock) {
                long diff = _data.LongCount - value;

                if (diff > 0)
                {
                    // truncate end
                    _data.RemoveRange(value, diff);
                }
                else if (diff < 0)
                {
                    // pad end
                    _data.Grow(-diff);
                }
            }
        }