Exemple #1
0
 public void ConstructWithInitialCapacity(uint capacity)
 {
     using (var buffer = new StringBuffer(capacity))
     {
         buffer.CharCapacity.Should().Be(capacity);
     }
 }
Exemple #2
0
 public void SetOverIndexThrowsArgumentOutOfRange()
 {
     using (var buffer = new StringBuffer())
     {
         Action action = () => { buffer[0] = 'Q'; };
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
Exemple #3
0
 public void OverByteCapacityThrowsArgumentOutOfRange()
 {
     using (var buffer = new StringBuffer())
     {
         Action action = () => buffer.EnsureCharCapacity(ulong.MaxValue / 2 + 1);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
Exemple #4
0
 public void ConstructFromEmptyString()
 {
     using (var buffer = new StringBuffer(""))
     {
         buffer.ByteCapacity.Should().Be(0);
         buffer.CharCapacity.Should().Be(0);
     }
 }
Exemple #5
0
 public void CanIndexChar()
 {
     using (var buffer = new StringBuffer())
     {
         buffer.Length = 1;
         buffer[0] = 'Q';
         buffer[0].Should().Be('Q');
     }
 }
Exemple #6
0
 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");
     }
 }
Exemple #7
0
        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");
                }
            }
        }
Exemple #8
0
        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);
            }
        }
Exemple #9
0
        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);
            }
        }
Exemple #10
0
 public unsafe void SetLengthToFirstNullEmptyBuffer()
 {
     using (var buffer = new StringBuffer())
     {
         buffer.SetLengthToFirstNull();
         buffer.Length.Should().Be(0);
     }
 }
Exemple #11
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);
     }
 }
Exemple #12
0
 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);
     }
 }
Exemple #13
0
 public void ToStringCountOverLengthThrows()
 {
     using (var buffer = new StringBuffer())
     {
         Action action = () => buffer.SubString(startIndex: 0, count: 1);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
Exemple #14
0
 public void AppendOverCountWithIndexThrows()
 {
     using (var buffer = new StringBuffer())
     {
         Action action = () => buffer.Append("A", startIndex: 1, count: 1);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
Exemple #15
0
        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>();
            }
        }
Exemple #16
0
        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");
            }
        }
Exemple #17
0
 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[] { "" });
     }
 }
Exemple #18
0
 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);
     }
 }
Exemple #19
0
 public void SubStringEqualsOverSizeCountWithIndexThrows()
 {
     using (var buffer = new StringBuffer("A"))
     {
         Action action = () => buffer.SubStringEquals("", startIndex: 1, count: 1);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
Exemple #20
0
 public void SubStringEqualsNegativeCountThrows()
 {
     using (var buffer = new StringBuffer())
     {
         Action action = () => buffer.SubStringEquals("", startIndex: 0, count: -2);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }
Exemple #21
0
 public void StartsWithNullThrows()
 {
     using (var buffer = new StringBuffer())
     {
         Action action = () => buffer.StartsWithOrdinal(null);
         action.ShouldThrow<ArgumentNullException>();
     }
 }
Exemple #22
0
 public void StartsWithOrdinal(string source, string value, bool expected)
 {
     using (var buffer = new StringBuffer(source))
     {
         buffer.StartsWithOrdinal(value).Should().Be(expected);
     }
 }
Exemple #23
0
 /// <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);
 }
Exemple #24
0
 /// <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);
 }
Exemple #25
0
        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);
            }
        }
Exemple #26
0
        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");
            }
        }
Exemple #27
0
 /// <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);
 }
Exemple #28
0
 public void AppendNullStringBufferThrows()
 {
     using (var buffer = new StringBuffer())
     {
         Action action = () => buffer.Append((StringBuffer)null);
         action.ShouldThrow<ArgumentNullException>();
     }
 }
Exemple #29
0
        /// <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)));
        }
Exemple #30
0
 public void AppendNegativeIndexThrows()
 {
     using (var buffer = new StringBuffer())
     {
         Action action = () => buffer.Append("a", startIndex: -1);
         action.ShouldThrow<ArgumentOutOfRangeException>();
     }
 }