public static void TestTextEqualsMismatchMultiSegment()
        {
            string jsonString = "\"Hi, \\\"Ahson\\\"!\"";

            byte[] lookup   = Encoding.UTF8.GetBytes("Hello, \"Ahson\"");
            byte[] utf8Data = Encoding.UTF8.GetBytes(jsonString);
            bool   found    = false;

            // Segment 1: "Hi, \"A
            // Segment 2: hson\"!"
            ReadOnlySequence <byte> sequence = JsonTestHelper.CreateSegments(utf8Data);

            var json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(lookup) ||
                        json.ValueTextEquals("Hello, \"Ahson\"".AsSpan()) ||
                        json.ValueTextEquals("Hello, \"Ahson\""))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);
        }
        public static void TestSingleStringsMultiSegment()
        {
            string jsonString     = "\"Hello, \\u0041hson!\"";
            string expectedString = "Hello, \\u0041hson!, ";

            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);

            ReadOnlySequence <byte> sequence = JsonTestHelper.CreateSegments(dataUtf8);

            for (int j = 0; j < dataUtf8.Length; j++)
            {
                var    utf8JsonReader    = new Utf8JsonReader(sequence.Slice(0, j), isFinalBlock: false, default);
                byte[] resultSequence    = JsonTestHelper.ReaderLoop(dataUtf8.Length, out int length, ref utf8JsonReader);
                string actualStrSequence = Encoding.UTF8.GetString(resultSequence, 0, length);

                long consumed = utf8JsonReader.BytesConsumed;
                Assert.Equal(consumed, utf8JsonReader.CurrentState.BytesConsumed);
                utf8JsonReader     = new Utf8JsonReader(sequence.Slice(consumed), isFinalBlock: true, utf8JsonReader.CurrentState);
                resultSequence     = JsonTestHelper.ReaderLoop(dataUtf8.Length, out length, ref utf8JsonReader);
                actualStrSequence += Encoding.UTF8.GetString(resultSequence, 0, length);
                string message = $"Expected consumed: {dataUtf8.Length - consumed}, Actual consumed: {utf8JsonReader.BytesConsumed}, Index: {j}";
                Assert.Equal(utf8JsonReader.BytesConsumed, utf8JsonReader.CurrentState.BytesConsumed);
                Assert.True(dataUtf8.Length - consumed == utf8JsonReader.BytesConsumed, message);
                Assert.Equal(expectedString, actualStrSequence);
            }
        }
        public static void TextEqualsEscapedCharAtTheLastSegment()
        {
            string jsonString   = "\"aaaaaa\\u0061\"";
            string lookupString = "aaaaaaa";

            byte[] utf8Data = Encoding.UTF8.GetBytes(jsonString);

            bool found = false;

            var json = new Utf8JsonReader(utf8Data, isFinalBlock: true, state: default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.True(found);

            ReadOnlySequence <byte> sequence = JsonTestHelper.CreateSegments(utf8Data);

            found = false;

            json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);
            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.True(found);
        }
        public static void TextMismatchSameLength(string jsonString, string lookupString)
        {
            byte[] utf8Data = Encoding.UTF8.GetBytes(jsonString);

            bool found = false;

            var json = new Utf8JsonReader(utf8Data, isFinalBlock: true, state: default);

            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);

            ReadOnlySequence <byte> sequence = JsonTestHelper.CreateSegments(utf8Data);

            found = false;

            json = new Utf8JsonReader(sequence, isFinalBlock: true, state: default);
            while (json.Read())
            {
                if (json.TokenType == JsonTokenType.String)
                {
                    if (json.ValueTextEquals(Encoding.UTF8.GetBytes(lookupString)) ||
                        json.ValueTextEquals(lookupString.AsSpan()) ||
                        json.ValueTextEquals(lookupString))
                    {
                        found = true;
                        break;
                    }
                }
            }

            Assert.False(found);
        }
        private static void SpanSequenceStatesAreEqual(byte[] dataUtf8)
        {
            ReadOnlySequence <byte> sequence = JsonTestHelper.CreateSegments(dataUtf8);

            var jsonSpan     = new Utf8JsonReader(dataUtf8, isFinalBlock: true, default);
            var jsonSequence = new Utf8JsonReader(sequence, isFinalBlock: true, default);

            while (true)
            {
                bool spanResult     = jsonSpan.Read();
                bool sequenceResult = jsonSequence.Read();

                Assert.Equal(spanResult, sequenceResult);
                Assert.Equal(jsonSpan.CurrentDepth, jsonSequence.CurrentDepth);
                Assert.Equal(jsonSpan.BytesConsumed, jsonSequence.BytesConsumed);
                Assert.Equal(jsonSpan.TokenType, jsonSequence.TokenType);

                if (!spanResult)
                {
                    break;
                }
            }
        }