Exemple #1
0
        public void TestBoolean(bool value, string expected)
        {
            RespValue resp = value;
            int       len  = checked ((int)resp.GetByteCount());

            using var lease = Lifetime.RentMemory <byte>(len);
            Assert.Equal(len, resp.CopyTo(lease.Value.Span));
            var result = Encoding.UTF8.GetString(lease.Value.Span);

            Assert.Equal(expected, result);
        }
Exemple #2
0
        public void TestBytes()
        {
            const int MAX_LENGTH = 800;
            var       sourceData = ArrayPool <byte> .Shared.Rent(MAX_LENGTH);

            var target = ArrayPool <byte> .Shared.Rent(MAX_LENGTH + 2);

            try
            {
                var rand = new Random(12345);
                rand.NextBytes(sourceData);
                for (int i = 0; i < MAX_LENGTH; i++)
                {
                    int       offset = MAX_LENGTH - i;
                    var       seq    = new ReadOnlySequence <byte>(sourceData, offset, i);
                    RespValue resp   = RespValue.Create(RespType.BlobString, seq);

                    Assert.Equal(i, resp.GetByteCount());
                    var dest = new Span <byte>(target, 1, i);
                    Array.Clear(target, 0, i + 2);
                    Assert.Equal(i, resp.CopyTo(dest));
                    Assert.Equal(0, target[0]);
                    Assert.Equal(0, target[i + 1]);
                    for (int j = 0; j < i; j++)
                    {
                        Assert.Equal(sourceData[j + offset], target[j + 1]);
                    }
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(sourceData);

                ArrayPool <byte> .Shared.Return(target);
            }
        }