Repository navigation
Expand file tree
/
Copy pathLatin1EncodingExtraTests.cs
More file actions
112 lines (98 loc) · 4.57 KB
/
Copy pathLatin1EncodingExtraTests.cs
File metadata and controls
112 lines (98 loc) · 4.57 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
using System.Text;
namespace XISOSharp.Tests;
/// <summary>
/// Unit tests for the internal <see cref="Latin1Encoding"/>: full byte-range
/// round-trips, counting consistency, out-of-range rejection, and buffer guards.
/// </summary>
public class Latin1EncodingExtraTests
{
private static readonly Encoding Encoding = Latin1Encoding.Instance;
/// <summary>Verifies every byte value 0x00-0xFF round-trips through chars and back.</summary>
[Fact]
public void RoundTrip_AllByteValues()
{
byte[] bytes = new byte[256];
for (int i = 0; i < bytes.Length; i++) bytes[i] = (byte)i;
char[] chars = new char[256];
Assert.Equal(256, Encoding.GetChars(bytes, 0, 256, chars, 0));
for (int i = 0; i < chars.Length; i++) Assert.Equal((char)i, chars[i]);
byte[] back = new byte[256];
Assert.Equal(256, Encoding.GetBytes(chars, 0, 256, back, 0));
Assert.Equal(bytes, back);
}
/// <summary>Verifies byte counting equals character count for every overload.</summary>
[Fact]
public void GetByteCount_MatchesLength()
{
const string text = "caf\u00E9 \u00FF\u00FE";
Assert.Equal(text.Length, Encoding.GetByteCount(text));
Assert.Equal(text.Length, Encoding.GetByteCount(text.AsSpan()));
Assert.Equal(text.Length, Encoding.GetByteCount(text.ToCharArray(), 0, text.Length));
}
/// <summary>Verifies characters outside Latin-1 are rejected while counting.</summary>
[Theory]
[InlineData("\u0100")]
[InlineData("\u65E5")]
public void GetByteCount_NonLatin1_Throws(string text)
{
Assert.Throws<ArgumentException>(() => Encoding.GetByteCount(text));
Assert.Throws<ArgumentException>(() => Encoding.GetByteCount(text.AsSpan()));
}
/// <summary>Verifies characters outside Latin-1 are rejected while encoding.</summary>
[Fact]
public void GetBytes_NonLatin1_Throws()
{
char[] chars = ['a', '\u0100'];
byte[] buffer = new byte[2];
Assert.Throws<ArgumentException>(() => Encoding.GetBytes(chars, 0, 2, buffer, 0));
Assert.Throws<ArgumentException>(() => Encoding.GetBytes(new string(chars), 0, 2, buffer, 0));
Assert.Throws<ArgumentException>(() => Encoding.GetBytes(chars.AsSpan(), buffer.AsSpan()));
}
/// <summary>Verifies an undersized byte destination is rejected.</summary>
[Fact]
public void GetBytes_TooSmallDestination_Throws()
{
Assert.Throws<ArgumentException>(() => Encoding.GetBytes("abc".AsSpan(), new byte[2]));
}
/// <summary>Verifies an undersized char destination is rejected.</summary>
[Fact]
public void GetChars_TooSmallDestination_Throws()
{
Assert.Throws<ArgumentException>(() => Encoding.GetChars([1, 2, 3], new char[2]));
}
/// <summary>Verifies max-count estimates are the identity for Latin-1.</summary>
[Fact]
public void MaxCounts_AreIdentity()
{
Assert.Equal(5, Encoding.GetMaxByteCount(5));
Assert.Equal(7, Encoding.GetMaxCharCount(7));
}
/// <summary>Verifies null inputs are rejected.</summary>
[Fact]
public void NullInputs_Throw()
{
Assert.Throws<ArgumentNullException>(() => Encoding.GetByteCount((string)null!));
Assert.Throws<ArgumentNullException>(() => Encoding.GetBytes((string)null!, 0, 0, [], 0));
Assert.Throws<ArgumentNullException>(() => Encoding.GetChars(null!, 0, 0, [], 0));
}
/// <summary>
/// Verifies the array overloads validate index/count per the Encoding
/// contract instead of throwing IndexOutOfRangeException (Todo #62).
/// </summary>
[Fact]
public void ArrayOverloads_InvalidRanges_ThrowArgumentOutOfRange()
{
char[] chars = ['a', 'b'];
byte[] bytes = new byte[4];
Assert.Throws<ArgumentOutOfRangeException>(() => Encoding.GetBytes(chars, -1, 1, bytes, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => Encoding.GetBytes(chars, 0, 3, bytes, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => Encoding.GetBytes("ab", 0, 3, bytes, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => Encoding.GetCharCount(bytes, 0, 5));
byte[] source = [1, 2];
char[] dest = new char[4];
Assert.Throws<ArgumentOutOfRangeException>(() => Encoding.GetChars(source, 0, 3, dest, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => Encoding.GetChars(source, -1, 1, dest, 0));
Assert.Throws<ArgumentException>(() => Encoding.GetBytes(chars, 0, 2, bytes, 3));
Assert.Throws<ArgumentException>(() => Encoding.GetChars(source, 0, 2, dest, 3));
}
}