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

Strings.cs « ReadOnlyMemory « tests « System.Memory « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6a775ad87ad7f89f59291de1957d6912d12790ea (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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 ReadOnlyMemoryTests
    {
        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 AsReadOnlyMemory_ToArray_Roundtrips(string input)
        {
            ReadOnlyMemory<char> m = input.AsMemory();
            Assert.Equal(input, new string(m.ToArray()));
        }

        [Theory]
        [MemberData(nameof(StringInputs))]
        public static void AsReadOnlyMemory_Span_Roundtrips(string input)
        {
            ReadOnlyMemory<char> m = input.AsMemory();
            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 AsReadOnlyMemory_Slice_MatchesSubstring(string input, int offset, int count)
        {
            ReadOnlyMemory<char> m = input.AsMemory();
            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 void AsReadOnlyMemory_NullString_Default()
        {
            ReadOnlyMemory<char> m = ((string)null).AsMemory();
            m.Validate();
            Assert.Equal(default, m);

            m = ((string)null).AsMemory(0);
            m.Validate();
            Assert.Equal(default, m);

            m = ((string)null).AsMemory(0, 0);
            m.Validate();
            Assert.Equal(default, m);
        }

        [Fact]
        public static void NullAsReadOnlyMemoryNonZeroStartAndLength()
        {
            string str = null;

            Assert.Throws<ArgumentOutOfRangeException>(() => str.AsMemory(1));
            Assert.Throws<ArgumentOutOfRangeException>(() => str.AsMemory(-1));

            Assert.Throws<ArgumentOutOfRangeException>(() => str.AsMemory(0, 1));
            Assert.Throws<ArgumentOutOfRangeException>(() => str.AsMemory(1, 0));
            Assert.Throws<ArgumentOutOfRangeException>(() => str.AsMemory(1, 1));
            Assert.Throws<ArgumentOutOfRangeException>(() => str.AsMemory(-1, -1));
        }

        [Fact]
        public static void AsReadOnlyMemory_TryGetArray_ReturnsFalse()
        {
            ReadOnlyMemory<char> m = "0123456789".AsMemory();
            Assert.False(MemoryMarshal.TryGetArray(m, out ArraySegment<char> array));
            Assert.Null(array.Array);
            Assert.Equal(0, array.Offset);
            Assert.Equal(0, array.Count);
        }

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

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

        [Theory]
        [MemberData(nameof(TestHelpers.StringSliceTestData), MemberType = typeof(TestHelpers))]
        public static unsafe void AsReadOnlyMemory_PointerAndLength(string text, int start, int length)
        {
            ReadOnlyMemory<char> m;
            if (start == -1)
            {
                start = 0;
                length = text.Length;
                m = text.AsMemory();
            }
            else if (length == -1)
            {
                length = text.Length - start;
                m = text.AsMemory(start);
            }
            else
            {
                m = text.AsMemory(start, length);
            }

            Assert.Equal(length, m.Length);

            using (MemoryHandle h = m.Pin())
            {
                fixed (char* pText = text)
                {
                    char* expected = pText + start;
                    void* actual = h.Pointer;
                    Assert.Equal((IntPtr)expected, (IntPtr)actual);
                }
            }
        }

        [Theory]
        [MemberData(nameof(TestHelpers.StringSlice2ArgTestOutOfRangeData), MemberType = typeof(TestHelpers))]
        public static unsafe void AsReadOnlyMemory_2Arg_OutOfRange(string text, int start)
        {
            AssertExtensions.Throws<ArgumentOutOfRangeException>("start", () => text.AsMemory(start));
        }

        [Theory]
        [MemberData(nameof(TestHelpers.StringSlice3ArgTestOutOfRangeData), MemberType = typeof(TestHelpers))]
        public static unsafe void AsReadOnlyMemory_3Arg_OutOfRange(string text, int start, int length)
        {
            AssertExtensions.Throws<ArgumentOutOfRangeException>("start", () => text.AsMemory(start, length));
        }

        [Fact]
        public static void AsReadOnlyMemory_EqualsAndGetHashCode_ExpectedResults()
        {
            ReadOnlyMemory<char> m1 = new string('a', 4).AsMemory();
            ReadOnlyMemory<char> m2 = new string('a', 4).AsMemory();

            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());
        }
    }
}