Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Strings.cs « Memory « tests « System.Memory « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b711179cd1aeb06a6ed4e5cfd81b36a890919dcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using System.Buffers;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace System.MemoryTests
{
    public static partial class MemoryTests
    {
        public static IEnumerable<object[]> StringInputs()
        {
            yield return new object[] { "" };
            yield return new object[] { "a" };
            yield return new object[] { "a\0bcdefghijklmnopqrstuvwxyz" };
        }

        [Theory]
        [MemberData(nameof(StringInputs))]
        public static void Memory_ToArray_Roundtrips(string input)
        {
            ReadOnlyMemory<char> readonlyMemory = input.AsMemory();
            Memory<char> m = MemoryMarshal.AsMemory(readonlyMemory);
            Assert.Equal(input, new string(m.ToArray()));
        }

        [Theory]
        [MemberData(nameof(StringInputs))]
        public static void Memory_Span_Roundtrips(string input)
        {
            ReadOnlyMemory<char> readonlyMemory = input.AsMemory();
            Memory<char> m = MemoryMarshal.AsMemory(readonlyMemory);
            ReadOnlySpan<char> s = m.Span;
            Assert.Equal(input, new string(s.ToArray()));
        }

        [Theory]
        [InlineData("", 0, 0)]
        [InlineData("0123456789", 0, 0)]
        [InlineData("0123456789", 10, 0)]
        [InlineData("0123456789", 0, 10)]
        [InlineData("0123456789", 1, 9)]
        [InlineData("0123456789", 2, 8)]
        [InlineData("0123456789", 9, 1)]
        [InlineData("0123456789", 1, 8)]
        [InlineData("0123456789", 5, 3)]
        public static void Memory_Slice_MatchesSubstring(string input, int offset, int count)
        {
            ReadOnlyMemory<char> readonlyMemory = input.AsMemory();
            Memory<char> m = MemoryMarshal.AsMemory(readonlyMemory);
            Assert.Equal(input.Substring(offset, count), new string(m.Slice(offset, count).ToArray()));
            Assert.Equal(input.Substring(offset, count), new string(m.Slice(offset, count).Span.ToArray()));
            Assert.Equal(input.Substring(offset), new string(m.Slice(offset).ToArray()));
        }

        [Fact]
        public static unsafe void Memory_Pin_ExpectedPointerValue()
        {
            string input = "0123456789";
            ReadOnlyMemory<char> readonlyMemory = input.AsMemory();
            Memory<char> m = MemoryMarshal.AsMemory(readonlyMemory);

            using (MemoryHandle h = m.Pin())
            {
                GC.Collect();
                fixed (char* ptr = input)
                {
                    Assert.Equal((IntPtr)ptr, (IntPtr)h.Pointer);
                }
            }
        }

        [Fact]
        public static void Memory_EqualsAndGetHashCode_ExpectedResults()
        {
            ReadOnlyMemory<char> readonlyMemory1 = new string('a', 4).AsMemory();
            ReadOnlyMemory<char> readonlyMemory2 = new string('a', 4).AsMemory();

            Memory<char> m1 = MemoryMarshal.AsMemory(readonlyMemory1);
            Memory<char> m2 = MemoryMarshal.AsMemory(readonlyMemory2);

            Assert.True(m1.Span.SequenceEqual(m2.Span));
            Assert.True(m1.Equals(m1));
            Assert.True(m1.Equals((object)m1));
            Assert.False(m1.Equals(m2));
            Assert.False(m1.Equals((object)m2));

            Assert.Equal(m1.GetHashCode(), m1.GetHashCode());
        }
    }
}