public void ConstructWithInitialCapacity(uint capacity) { using (var buffer = new StringBuffer(capacity)) { buffer.CharCapacity.Should().Be(capacity); } }
public void SetOverIndexThrowsArgumentOutOfRange() { using (var buffer = new StringBuffer()) { Action action = () => { buffer[0] = 'Q'; }; action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void OverByteCapacityThrowsArgumentOutOfRange() { using (var buffer = new StringBuffer()) { Action action = () => buffer.EnsureCharCapacity(ulong.MaxValue / 2 + 1); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void ConstructFromEmptyString() { using (var buffer = new StringBuffer("")) { buffer.ByteCapacity.Should().Be(0); buffer.CharCapacity.Should().Be(0); } }
public void CanIndexChar() { using (var buffer = new StringBuffer()) { buffer.Length = 1; buffer[0] = 'Q'; buffer[0].Should().Be('Q'); } }
public void ReduceLength() { using (var buffer = new StringBuffer("Food")) { buffer.CharCapacity.Should().Be(5); buffer.Length = 3; buffer.ToString().Should().Be("Foo"); buffer.CharCapacity.Should().Be(5, "shouldn't reduce capacity when dropping length"); } }
public void ReduceLength(string testString) { using (var buffer = new StringBuffer(testString)) { buffer.CharCapacity.Should().Be((uint)testString.Length + 1); for (int i = 1; i <= testString.Length; i++) { buffer.Length = (uint)(testString.Length - i); buffer.ToString().Should().Be(testString.Substring(0, testString.Length - i)); buffer.CharCapacity.Should().Be((uint)testString.Length + 1, "shouldn't reduce capacity when dropping length"); } } }
public unsafe void ConstructFromString(string testString) { using (var buffer = new StringBuffer(testString)) { buffer.Length.Should().Be((uint)testString.Length); buffer.CharCapacity.Should().Be((uint)testString.Length + 1); for (int i = 0; i < testString.Length; i++) { buffer[(uint)i].Should().Be(testString[i]); } buffer.CharPointer[testString.Length].Should().Be('\0', "should be null terminated"); buffer.ToString().Should().Be(testString); } }
public unsafe void SetLengthToFirstNullTests(string content, ulong startLength, ulong endLength) { using (var buffer = new StringBuffer(content)) { // With existing content buffer.Length.Should().Be(startLength); buffer.SetLengthToFirstNull(); buffer.Length.Should().Be(endLength); // Clear the buffer & manually copy in buffer.Length = 0; fixed (char* contentPointer = content) { Buffer.MemoryCopy(contentPointer, buffer.CharPointer, (long)buffer.CharCapacity * 2, content.Length * sizeof(char)); } buffer.Length.Should().Be(0); buffer.SetLengthToFirstNull(); buffer.Length.Should().Be(endLength); } }
public unsafe void SetLengthToFirstNullEmptyBuffer() { using (var buffer = new StringBuffer()) { buffer.SetLengthToFirstNull(); buffer.Length.Should().Be(0); } }
public unsafe void SetLengthToFirstNullNoNull() { using (var buffer = new StringBuffer("A")) { // Wipe out the last null buffer.CharPointer[buffer.Length] = 'B'; buffer.SetLengthToFirstNull(); buffer.Length.Should().Be(1); } }
public void ToStringTest(string source, int startIndex, int count, string expected) { using (var buffer = new StringBuffer(source)) { buffer.SubString(startIndex: (ulong)startIndex, count: count).Should().Be(expected); } }
public void ToStringCountOverLengthThrows() { using (var buffer = new StringBuffer()) { Action action = () => buffer.SubString(startIndex: 0, count: 1); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void AppendOverCountWithIndexThrows() { using (var buffer = new StringBuffer()) { Action action = () => buffer.Append("A", startIndex: 1, count: 1); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void ToStringOverSizeThrowsOverflow() { using (var buffer = new StringBuffer()) { var length = buffer.GetType().GetField("_length", BindingFlags.NonPublic | BindingFlags.Instance); length.SetValue(buffer, (uint)int.MaxValue + 1); Action action = () => buffer.ToString(); action.ShouldThrow<OverflowException>(); } }
public void SubStringIndexPlusCountCombinedOutOfRangeThrows() { using (var buffer = new StringBuffer("a")) { Action action = () => buffer.SubString(startIndex: 1, count: 1); action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("startIndex"); buffer.Append("b"); action.ShouldNotThrow<ArgumentOutOfRangeException>(); action = () => buffer.SubString(startIndex: 1, count: 2); action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count"); } }
public void Split(string content, char splitChar) { // We want equivalence with built-in string behavior using (var buffer = new StringBuffer(content)) { buffer.Split(splitChar).ShouldAllBeEquivalentTo(content?.Split(splitChar) ?? new string[] { "" }); } }
public void SubStringEquals(string source, string value, int startIndex, int count, bool expected) { using (var buffer = new StringBuffer(source)) { buffer.SubStringEquals(value, startIndex: (ulong)startIndex, count: count).Should().Be(expected); } }
public void SubStringEqualsOverSizeCountWithIndexThrows() { using (var buffer = new StringBuffer("A")) { Action action = () => buffer.SubStringEquals("", startIndex: 1, count: 1); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void SubStringEqualsNegativeCountThrows() { using (var buffer = new StringBuffer()) { Action action = () => buffer.SubStringEquals("", startIndex: 0, count: -2); action.ShouldThrow<ArgumentOutOfRangeException>(); } }
public void StartsWithNullThrows() { using (var buffer = new StringBuffer()) { Action action = () => buffer.StartsWithOrdinal(null); action.ShouldThrow<ArgumentNullException>(); } }
public void StartsWithOrdinal(string source, string value, bool expected) { using (var buffer = new StringBuffer(source)) { buffer.StartsWithOrdinal(value).Should().Be(expected); } }
/// <summary> /// Append the given buffer. /// </summary> /// <param name="nameof(value)">The buffer to append.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="nameof(value)"/> is null.</exception> public unsafe void Append(StringBuffer value) { if (value == null) throw new ArgumentNullException(nameof(value)); this.Append(value, 0, value.Length); }
/// <summary> /// Append the specified count of characters from the given buffer at the given start index. /// </summary> /// <param name="nameof(value)">The buffer to append.</param> /// <param name="nameof(startIndex)">The index in the input buffer to start appending from.</param> /// <param name="nameof(count)">The count of characters to copy from the buffer.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="nameof(value)"/> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown if <paramref name="startIndex"/> or <paramref name="nameof(count)"/> are outside the range /// of <paramref name="value"/> characters. /// </exception> public unsafe void Append(StringBuffer value, ulong startIndex, ulong count) { if (count == 0) return; if (value == null) throw new ArgumentNullException(nameof(value)); value.CopyTo( bufferIndex: startIndex, destination: this, destinationIndex: this.Length, count: count); }
public void AppendTests(string source, string value, int startIndex, int count, string expected) { // From string using (var buffer = new StringBuffer(source)) { buffer.Append(value, startIndex, count); buffer.ToString().Should().Be(expected); } // From buffer using (var buffer = new StringBuffer(source)) using (var valueBuffer = new StringBuffer(value)) { if (count == -1) buffer.Append(valueBuffer, (ulong)startIndex); else buffer.Append(valueBuffer, (ulong)startIndex, (ulong)count); buffer.ToString().Should().Be(expected); } }
public void SubStringImplicitCountOverMaxStringThrows() { using (var buffer = new StringBuffer()) { var length = buffer.GetType().GetField("_length", BindingFlags.NonPublic | BindingFlags.Instance); length.SetValue(buffer, (uint)int.MaxValue + 1); Action action = () => buffer.SubString(startIndex: 0, count: -1); action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count"); } }
/// <summary> /// Append the given buffer starting at the given buffer index. /// </summary> /// <param name="nameof(value)">The buffer to append.</param> /// <param name="nameof(startIndex)">The index in the input buffer to start appending from.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="nameof(value)"/> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// Thrown if <paramref name="startIndex"/> is outside the range of <paramref name="value"/> characters. /// </exception> public unsafe void Append(StringBuffer value, ulong startIndex) { if (value == null) throw new ArgumentNullException(nameof(value)); this.Append(value, startIndex, value.Length - startIndex); }
public void AppendNullStringBufferThrows() { using (var buffer = new StringBuffer()) { Action action = () => buffer.Append((StringBuffer)null); action.ShouldThrow<ArgumentNullException>(); } }
/// <summary> /// Copy contents to the specified buffer. Will grow the destination buffer if needed. /// </summary> /// <exception cref="ArgumentNullException">Thrown if <paramref name="nameof(destination)"/> is null</exception> public unsafe void CopyTo(ulong bufferIndex, StringBuffer destination, ulong destinationIndex, ulong count) { if (destination == null) throw new ArgumentNullException(nameof(destination)); if (destinationIndex > destination.length) throw new ArgumentOutOfRangeException(nameof(destinationIndex)); if (this.Length < bufferIndex + count) throw new ArgumentOutOfRangeException(nameof(count)); if (count == 0) return; ulong lastIndex = destinationIndex + (ulong)count; if (destination.Length < lastIndex) destination.Length = lastIndex; Buffer.MemoryCopy( source: this.CharPointer + bufferIndex, destination: destination.CharPointer + destinationIndex, destinationSizeInBytes: checked((long)(destination.ByteCapacity + destinationIndex * sizeof(char))), sourceBytesToCopy: checked((long)count * sizeof(char))); }
public void AppendNegativeIndexThrows() { using (var buffer = new StringBuffer()) { Action action = () => buffer.Append("a", startIndex: -1); action.ShouldThrow<ArgumentOutOfRangeException>(); } }