forked from dotnet/SqlClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodingTest.cs
More file actions
161 lines (140 loc) · 6.04 KB
/
EncodingTest.cs
File metadata and controls
161 lines (140 loc) · 6.04 KB
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
// 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;
namespace System.Text.UnitTests;
/// <summary>
/// Tests that the Encoding polyfills in netfx operate correctly and handle
/// invalid parameter values.
/// </summary>
/// <remarks>
/// In the netcore cases, we're testing the built-in GetBytes and GetByteCount
/// methods. The contract for our extension polyfills must match these implementations.
/// </remarks>
public class EncodingTest
{
private const string ExampleStringValue = "ABCDéFG1234567abcdefg";
/// <summary>
/// Represents a series of invalid [offset, count] pairs into the <see cref="ExampleStringValue"/>
/// constant.
/// </summary>
public static TheoryData<int, int> InvalidOffsetsAndCounts =>
new()
{
// Group 1: offset starts before the string.
// * Count extends beyond it.
{ -1, 999 },
// * Count is valid.
{ -1, 5 },
// Group 2: offset is valid.
// * Count extends beyond the end of it.
{ 0, 999 },
// * Count extends backwards to the start it.
{ 5, -5 },
// Group 3: offset starts beyond the end of the string.
// * Count extends beyond the end of it.
{ 999, 999 },
// * Count extends backwards into the string.
{ 999, -1005 }
};
#if NET
static EncodingTest()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
#endif
/// <summary>
/// Verifies that GetByteCount throws an ArgumentNullException when passed a null string.
/// </summary>
[Fact]
public void GetByteCount_ThrowsOnNullString()
{
string nullString = null!;
Action act = () => Encoding.Unicode.GetByteCount(nullString, 0, 0);
Assert.Throws<ArgumentNullException>(act);
}
/// <summary>
/// Verifies that GetBytes throws an ArgumentNullException when passed a null string.
/// </summary>
[Fact]
public void GetBytes_ThrowsOnNullString()
{
string nullString = null!;
Action act = () => Encoding.Unicode.GetBytes(nullString, 0, 0);
Assert.Throws<ArgumentNullException>(act);
}
/// <summary>
/// Verifies that GetByteCount throws an ArgumentOutOfRangeException when passes an offset
/// or count which is outside of the string.
/// </summary>
/// <param name="offset">offset parameter of GetByteCount.</param>
/// <param name="count">count parameter of GetByteCount.</param>
/// <seealso cref="InvalidOffsetsAndCounts"/>
[Theory]
[MemberData(nameof(InvalidOffsetsAndCounts))]
public void GetByteCount_ThrowsOnOutOfRangeOffsetOrCount(int offset, int count)
{
Action act = () => Encoding.Unicode.GetByteCount(ExampleStringValue, offset, count);
Assert.Throws<ArgumentOutOfRangeException>(act);
}
/// <summary>
/// Verifies that GetBytes throws an ArgumentOutOfRangeException when passes an offset
/// or count which is outside of the string.
/// </summary>
/// <param name="offset">offset parameter of GetBytes.</param>
/// <param name="count">count parameter of GetBytes.</param>
[Theory]
[MemberData(nameof(InvalidOffsetsAndCounts))]
public void GetBytes_ThrowsOnOutOfRangeOffsetOrCount(int offset, int count)
{
Action act = () => Encoding.Unicode.GetBytes(ExampleStringValue, offset, count);
Assert.Throws<ArgumentOutOfRangeException>(act);
}
/// <summary>
/// Verifies that when using the new GetByteCount and GetBytes polyfills to encode the entire string, the return
/// value is equal to passing the string as-is to GetByteCount(string) and GetBytes(string).
/// </summary>
[Fact]
public void GetBytesOfFullStringByLength_MatchesGetBytesOfFullString()
{
byte[] fullStringBytes = Encoding.Unicode.GetBytes(ExampleStringValue);
int fullStringByteCount = Encoding.Unicode.GetByteCount(ExampleStringValue);
byte[] partialStringBytes = Encoding.Unicode.GetBytes(ExampleStringValue, 0, ExampleStringValue.Length);
int partialStringByteCount = Encoding.Unicode.GetByteCount(ExampleStringValue, 0, ExampleStringValue.Length);
Assert.Equal(fullStringByteCount, partialStringByteCount);
Assert.Equal(fullStringByteCount, partialStringBytes.Length);
Assert.Equal(fullStringBytes, partialStringBytes);
}
/// <summary>
/// Verifies that encoding a specific substring returns a byte array which can be decoded into the same string, in
/// various code pages.
/// </summary>
/// <param name="codePage">The code page identifier to use for transcoding.</param>
[Theory]
// Unicode
[InlineData(1200)]
// UTF8
[InlineData(65001)]
public void GetBytes_Roundtrips(int codePage)
{
Encoding encoding = Encoding.GetEncoding(codePage);
byte[] partialStringBytes = encoding.GetBytes(ExampleStringValue, 4, 5);
string expectedRoundtrippedValue = ExampleStringValue.Substring(4, 5);
string roundtrip = encoding.GetString(partialStringBytes);
Assert.Equal(expectedRoundtrippedValue, roundtrip);
}
/// <summary>
/// Verifies that when a string contains a multibyte character, the byte array returns the correct number of
/// elements for the encoding.
/// </summary>
[Fact]
public void GetByteCount_ReturnsCorrectValueOnMultiCharacterRune()
{
// The character é is two bytes in UTF8.
Assert.Equal(6, Encoding.UTF8.GetByteCount(ExampleStringValue, 4, 5));
// All Unicode characters in our sample string are two bytes long.
Assert.Equal(10, Encoding.Unicode.GetByteCount(ExampleStringValue, 4, 5));
// Code page 1251 does not have the é character, so treats it as the single-byte character "e".
Assert.Equal(5, Encoding.GetEncoding(1251).GetByteCount(ExampleStringValue, 4, 5));
}
}