예제 #1
0
        private unsafe void TestComparisons(string heapValue, int offset, string value, bool unicode = false)
        {
            byte[] heap;

            fixed(byte *heapPtr = (heap = Encoding.UTF8.GetBytes(heapValue)))
            {
                var    block      = new MemoryBlock(heapPtr, heap.Length);
                string heapSubstr = GetStringHeapValue(heapValue, offset);

                // compare:
                if (!unicode)
                {
                    int actualCmp   = block.CompareUtf8NullTerminatedStringWithAsciiString(offset, value);
                    int expectedCmp = StringComparer.Ordinal.Compare(heapSubstr, value);
                    Assert.Equal(Math.Sign(expectedCmp), Math.Sign(actualCmp));
                }

                // equals:
                bool actualEq   = block.Utf8NullTerminatedEquals(offset, value);
                bool expectedEq = StringComparer.Ordinal.Equals(heapSubstr, value);

                Assert.Equal(expectedEq, actualEq);

                // starts with:
                bool actualSW   = block.Utf8NullTerminatedStartsWith(offset, value);
                bool expectedSW = heapSubstr.StartsWith(value, StringComparison.Ordinal);

                Assert.Equal(actualSW, expectedSW);
            }
        }
예제 #2
0
        private unsafe void TestComparisons(string heapValue, int offset, string value, bool unicode = false, bool ignoreCase = false)
        {
            byte[] heap;
            MetadataStringDecoder decoder = MetadataStringDecoder.DefaultUTF8;

            fixed(byte *heapPtr = (heap = Encoding.UTF8.GetBytes(heapValue)))
            {
                var    block      = new MemoryBlock(heapPtr, heap.Length);
                string heapSubstr = GetStringHeapValue(heapValue, offset);

                // compare:
                if (!unicode)
                {
                    int actualCmp   = block.CompareUtf8NullTerminatedStringWithAsciiString(offset, value);
                    int expectedCmp = StringComparer.Ordinal.Compare(heapSubstr, value);
                    Assert.Equal(Math.Sign(expectedCmp), Math.Sign(actualCmp));
                }

                if (!ignoreCase)
                {
                    TestComparison(block, offset, value, heapSubstr, decoder, ignoreCase: true);
                }

                TestComparison(block, offset, value, heapSubstr, decoder, ignoreCase);
            }
        }