public void TestSequenceEquals_False() { NcByteCollection b1 = new NcByteCollection(NcTestUtils.RandomBytes(30 * 1000)); NcByteCollection b2 = new NcByteCollection(NcTestUtils.RandomBytes(30 * 1000)); Assert.IsFalse(b1.SequenceEqual(b2)); }
public void TestClone() { NcByteCollection original = new NcByteCollection(NcTestUtils.RandomBytes(12 * 1024)); NcByteCollection cloned = original.Clone(); Assert.IsTrue(original.SequenceEqual(cloned)); }
/// <summary> /// Initialises a new instance of the <see cref="NcByteStream"/> class /// initially populated with the specified sequence of bytes. /// </summary> /// <param name="data"></param> /// <param name="blockSize"></param> /// <remarks> /// An instance initialised with this constructor will not modify the /// original data. /// </remarks> public NcByteStream(IEnumerable <byte> data, int blockSize = NcByteCollection.DEFAULT_BLOCK_SIZE) { if (data == null) { throw new ArgumentNullException(nameof(data)); } _data = new NcByteCollection(data, blockSize); }
public void TestSequenceEquals_True() { byte[] data = NcTestUtils.RandomBytes(30 * 1000); NcByteCollection b1 = new NcByteCollection(data, 4096); NcByteCollection b2 = new NcByteCollection(data, 3000); Assert.IsTrue(b1.SequenceEqual(b2)); }
public void TestAdd_1() { NcByteCollection instance = new NcByteCollection(); instance.Add(127); Assert.IsTrue(instance[0] == 127); }
/// <summary> /// Creates a new file, writes the specified <see cref="NcByteCollection"/> /// to the file, and then closes the file. If the target file already exists, /// it is overwritten. /// </summary> /// <param name="filename"></param> /// <returns></returns> public static void WriteAllBytes(string filename, NcByteCollection data) { using (NcByteStream nbs = new NcByteStream(data)) { using (Stream s = File.Open(filename, FileMode.Create, FileAccess.Write)) { nbs.CopyTo(s); } } }
/// <summary> /// Initialises a new instance of the <see cref="NcByteStream"/> class /// using the specified <see cref="NcByteCollection"/> as a backing /// store. /// </summary> /// <param name="data"></param> /// <remarks> /// An instance initialised with this constructor will modify the /// original data. If this is not intended, use <see cref="NcByteCollection.Clone"/> /// to make a copy of the data first. /// </remarks> public NcByteStream(NcByteCollection data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } _data = data; }
public void TestCopyToStream() { NcByteCollection data = new NcByteCollection(NcTestUtils.RandomBytes(12 * 1024)); MemoryStream ms = new MemoryStream(); data.Copy(3 * 1024, ms, 5 * 1024); Assert.IsTrue(ms.ToArray().SequenceEqual(data.Skip(3 * 1024).Take(5 * 1024))); }
public void TestReserve() { NcByteCollection instance = new NcByteCollection(); long bytes = 11 * 1024; instance.Reserve(bytes); Assert.AreEqual(instance.BlockLengthTotal, bytes); }
public void TestAdd_IEnumerable() { NcByteCollection instance = new NcByteCollection(); byte[] data = NcTestUtils.RandomBytes(100); instance.AddRange((IEnumerable <byte>)data); Assert.IsTrue(instance.SequenceEqual(data)); }
public void TestAdd_Large() { NcByteCollection instance = new NcByteCollection(); byte[] data = NcTestUtils.RandomBytes(17 * 1024); instance.AddRange(data); Assert.IsTrue(instance.SequenceEqual(data)); }
public void TestCopyFromStream_Small() { NcByteCollection b1 = new NcByteCollection(NcTestUtils.RandomBytes(30 * 1000)); NcByteStream b2 = new NcByteStream(NcTestUtils.RandomBytes(1000)); b1.Copy(b2, 5000, 50); Assert.IsTrue(b1.Count == (30 * 1000)); Assert.IsTrue(b1.SequenceEqual(b1.Take(5000).Concat(b2.Data.Take(50)).Concat(b1.Skip(5050)))); }
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)))); }
public void TestCopyFromArray_Small() { NcByteCollection b1 = new NcByteCollection(NcTestUtils.RandomBytes(30 * 1000)); byte[] b2 = NcTestUtils.RandomBytes(1000); b1.Copy(b2, 0, 5000, 50); Assert.IsTrue(b1.Count == (30 * 1000)); Assert.IsTrue(b1.SequenceEqual(b1.Take(5000).Concat(b2.Take(50)).Concat(b1.Skip(5050)))); }
public void TestRemove_1() { NcByteCollection instance = new NcByteCollection(); byte[] data = NcTestUtils.RandomBytes(12 * 1024); instance.AddRange(data); instance.RemoveAt(3 * 1024); Assert.IsTrue(instance.SequenceEqual(data.Take(3 * 1024).Concat(data.Skip(3 * 1024 + 1)))); }
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))); }
public void TestInsert_Small() { NcByteCollection instance = new NcByteCollection(); byte[] initial = NcTestUtils.RandomBytes(100); byte[] inserted = NcTestUtils.RandomBytes(50); instance.AddRange(initial); instance.InsertRange(10, inserted); Assert.IsTrue(instance.SequenceEqual(initial.Take(10).Concat(inserted).Concat(initial.Skip(10)))); }
public void TestStreamWrite() { NcByteCollection original = new NcByteCollection(NcTestUtils.RandomBytes(12 * 1024)); NcByteStream stream = new NcByteStream(original); MemoryStream ms = new MemoryStream(NcTestUtils.RandomBytes(5 * 1024)); stream.Seek(5 * 1024, SeekOrigin.Begin); ms.CopyTo(stream); Assert.IsTrue(original.Take(5 * 1024).Concat(ms.ToArray()).Concat(original.Skip(5 * 1024 + 5 * 1024)).SequenceEqual(stream.Data)); }
public void TestInsert_End() { NcByteCollection instance = new NcByteCollection(); byte[] initial = NcTestUtils.RandomBytes(5 * 1024); byte[] inserted = NcTestUtils.RandomBytes(2 * 1024); instance.AddRange(initial); instance.InsertRange(initial.Length - 1, inserted); Assert.IsTrue(instance.SequenceEqual(initial.Take(initial.Length - 1).Concat(inserted).Concat(initial.Skip(initial.Length - 1)))); }
public void TestInsert_1() { NcByteCollection instance = new NcByteCollection(); byte[] initial = NcTestUtils.RandomBytes(5 * 1024); byte[] inserted = new byte[] { 127 }; instance.AddRange((IEnumerable <byte>)initial); instance.Insert(5120, inserted[0]); Assert.IsTrue(instance.SequenceEqual(initial.Take(5120).Concat(inserted).Concat(initial.Skip(5120)))); }
public void TestInsert_Start() { NcByteCollection instance = new NcByteCollection(); byte[] initial = NcTestUtils.RandomBytes(5 * 1024); byte[] inserted = NcTestUtils.RandomBytes(2 * 1024); instance.AddRange(initial); instance.InsertRange(0, inserted); Assert.IsTrue(instance.SequenceEqual(inserted.Concat(initial))); }
public void TestBinarySerialization() { NcByteCollection original = new NcByteCollection(NcTestUtils.RandomBytes(12 * 1024)); BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream()) { formatter.Serialize(ms, original); ms.Position = 0; NcByteCollection deserialized = (NcByteCollection)formatter.Deserialize(ms); Assert.IsTrue(original.SequenceEqual(deserialized)); } }
public void TestXmlSerialization() { NcByteCollection original = new NcByteCollection(NcTestUtils.RandomBytes(12 * 1024)); XmlSerializer xs = new XmlSerializer(typeof(NcByteCollection)); using (MemoryStream ms = new MemoryStream()) { xs.Serialize(ms, original); ms.Position = 0; NcByteCollection deserialized = (NcByteCollection)xs.Deserialize(ms); Assert.IsTrue(original.SequenceEqual(deserialized)); } }
public void TestMemoryNc() { LinkedList <NcByteCollection> instances = new LinkedList <NcByteCollection>(); int count = 0; while (true) { try { NcByteCollection data = new NcByteCollection(8 * 4096); data.Grow(10 * 1024 * 1024); instances.AddLast(data); count++; } catch (OutOfMemoryException) { Console.WriteLine("Collections allocated: {0}", count); break; } } instances.Clear(); instances = null; GC.Collect(); GC.WaitForPendingFinalizers(); }
/// <summary> /// Decodes the binary data in the specified <see cref="NcByteCollection"/> and returns a string. /// </summary> /// <param name="data"></param> /// <param name="encoding"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <returns></returns> public static string DecodeString(NcByteCollection data, Encoding encoding, int offset = 0, int?count = null) { using (NcByteStream nbs = new NcByteStream(data)) { nbs.Position = offset; using (StreamReader sr = new StreamReader(nbs, encoding)) { if (count.HasValue) { char[] buffer = new char[count.Value]; int charsRead; int bufPos = 0; while ((bufPos < buffer.Length) && ((charsRead = sr.Read(buffer, bufPos, buffer.Length)) > 0)) { bufPos += charsRead; } return(new string(buffer, 0, bufPos)); } else { return(sr.ReadToEnd()); } } } }
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 }
/// <summary> /// Initialises a new instance of the <see cref="NcByteStream"/> class /// that represents an empty, writable stream. /// </summary> public NcByteStream() { _data = new NcByteCollection(); }
/// <summary> /// Initialises a new instance of the <see cref="NcByteStream"/> class /// using the specified block size. /// </summary> public NcByteStream(int blockSize) { _data = new NcByteCollection(blockSize); }