Esempio n. 1
0
        public static ReadOnlySequence <byte> GetSequence(byte[] _dataUtf8, int segmentSize)
        {
            int numberOfSegments = _dataUtf8.Length / segmentSize + 1;

            byte[][] buffers = new byte[numberOfSegments][];

            for (int j = 0; j < numberOfSegments - 1; j++)
            {
                buffers[j] = new byte[segmentSize];
                Array.Copy(_dataUtf8, j * segmentSize, buffers[j], 0, segmentSize);
            }

            int remaining = _dataUtf8.Length % segmentSize;

            buffers[numberOfSegments - 1] = new byte[remaining];
            Array.Copy(_dataUtf8, _dataUtf8.Length - remaining, buffers[numberOfSegments - 1], 0, remaining);

            return(BufferFactory.Create(buffers));
        }
        public static void CheckOnlyOneOfValueSpanOrSequenceIsSet(int testCase, string jsonString)
        {
            byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);

            bool[] expectedHasValueSequence      = null;
            bool[] expectedHasValueSequenceSkip  = null;
            bool[] expectedHasValueSequenceAllow = null;

            ReadOnlySequence <byte> sequence;

            switch (testCase)
            {
            case 0:
                Debug.Assert(dataUtf8.Length == 95);
                byte[][] buffers = new byte[6][];
                buffers[0] = dataUtf8.AsSpan(0, 10).ToArray();
                buffers[1] = dataUtf8.AsSpan(10, 28).ToArray();
                buffers[2] = dataUtf8.AsSpan(38, 32).ToArray();
                buffers[3] = dataUtf8.AsSpan(70, 23).ToArray();
                buffers[4] = dataUtf8.AsSpan(93, 1).ToArray();
                buffers[5] = dataUtf8.AsSpan(94, 1).ToArray();
                sequence   = BufferFactory.Create(buffers);
                expectedHasValueSequence = new bool [] { false, true, false, false, true, false, true, false, false, false, false, false };
                break;

            case 1:
                Debug.Assert(dataUtf8.Length == 97);
                buffers    = new byte[7][];
                buffers[0] = dataUtf8.AsSpan(0, 39).ToArray();
                buffers[1] = dataUtf8.AsSpan(39, 22).ToArray();
                buffers[2] = dataUtf8.AsSpan(61, 18).ToArray();
                buffers[3] = dataUtf8.AsSpan(79, 7).ToArray();
                buffers[4] = dataUtf8.AsSpan(86, 7).ToArray();
                buffers[5] = dataUtf8.AsSpan(93, 3).ToArray();
                buffers[6] = dataUtf8.AsSpan(96, 1).ToArray();
                sequence   = BufferFactory.Create(buffers);
                expectedHasValueSequence = new bool[] { false, false, false, false, false, true, true, false, true, true, true, false, false, false };
                break;

            case 2:
                Debug.Assert(dataUtf8.Length == 90);
                buffers    = new byte[5][];
                buffers[0] = dataUtf8.AsSpan(0, 36).ToArray();
                buffers[1] = dataUtf8.AsSpan(36, 13).ToArray();
                buffers[2] = dataUtf8.AsSpan(49, 30).ToArray();
                buffers[3] = dataUtf8.AsSpan(79, 9).ToArray();
                buffers[4] = dataUtf8.AsSpan(88, 2).ToArray();
                sequence   = BufferFactory.Create(buffers);
                expectedHasValueSequenceSkip  = new bool[] { false, false, false, true, false, false };
                expectedHasValueSequenceAllow = new bool[] { false, false, false, true, true, true, false, false };
                break;

            default:
                return;
            }

            foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
            {
                if (commentHandling == JsonCommentHandling.Disallow && testCase == 2)
                {
                    continue;
                }
                var state = new JsonReaderState(options: new JsonReaderOptions {
                    CommentHandling = commentHandling
                });
                var json = new Utf8JsonReader(sequence, isFinalBlock: true, state);

                int index = 0;

                while (json.Read())
                {
                    if (testCase == 0 || testCase == 1)
                    {
                        Assert.True(expectedHasValueSequence[index] == json.HasValueSequence, $"{commentHandling}, {testCase}, {index}, {json.HasValueSequence}");
                    }
                    else
                    {
                        if (commentHandling == JsonCommentHandling.Skip)
                        {
                            Assert.True(expectedHasValueSequenceSkip[index] == json.HasValueSequence, $"{commentHandling}, {testCase}, {index}, {json.HasValueSequence}");
                        }
                        else
                        {
                            Assert.True(expectedHasValueSequenceAllow[index] == json.HasValueSequence, $"{commentHandling}, {testCase}, {index}, {json.HasValueSequence}");
                        }
                    }
                    if (json.HasValueSequence)
                    {
                        Assert.True(json.ValueSpan == default, $"Escaped ValueSpan to be empty when HasValueSequence is true. Test case: {testCase}");
                        Assert.False(json.ValueSequence.IsEmpty, $"Escaped ValueSequence to not be empty when HasValueSequence is true. Test case: {testCase}");
                    }
                    else
                    {
                        Assert.True(json.ValueSequence.IsEmpty, $"Escaped ValueSequence to be empty when HasValueSequence is false. Test case: {testCase}");
                        Assert.False(json.ValueSpan == default, $"Escaped ValueSpan to not be empty when HasValueSequence is false. Test case: {testCase}");
                    }

                    index++;
                }
            }
        }