-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodingDetectionTests.cs
More file actions
221 lines (171 loc) · 7.8 KB
/
Copy pathEncodingDetectionTests.cs
File metadata and controls
221 lines (171 loc) · 7.8 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Text;
namespace ModerBox.Comtrade.Test {
/// <summary>
/// 编码自动检测测试
/// 测试 COMTRADE 库对不同文件编码的自动识别能力
/// </summary>
[TestClass]
public class EncodingDetectionTests {
private string _testDir;
[TestInitialize]
public void Setup() {
_testDir = Path.Combine(Path.GetTempPath(), "ComtradeEncodingTests_" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_testDir);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
[TestCleanup]
public void Cleanup() {
if (Directory.Exists(_testDir)) {
Directory.Delete(_testDir, true);
}
}
#region BOM 检测测试
[TestMethod]
public void DetectEncoding_Utf8WithBom_ReturnsUtf8() {
// Arrange
string testFile = Path.Combine(_testDir, "utf8_bom.cfg");
byte[] bom = new byte[] { 0xEF, 0xBB, 0xBF };
byte[] content = Encoding.UTF8.GetBytes("Test content");
byte[] fileContent = new byte[bom.Length + content.Length];
bom.CopyTo(fileContent, 0);
content.CopyTo(fileContent, bom.Length);
File.WriteAllBytes(testFile, fileContent);
// Act
Encoding detected = Comtrade.DetectEncoding(testFile);
// Assert
Assert.AreEqual(Encoding.UTF8.CodePage, detected.CodePage, "Should detect UTF-8 with BOM");
}
[TestMethod]
public void DetectEncoding_Utf16LeBom_ReturnsUtf16Le() {
// Arrange
string testFile = Path.Combine(_testDir, "utf16_le.cfg");
File.WriteAllText(testFile, "Test content", Encoding.Unicode);
// Act
Encoding detected = Comtrade.DetectEncoding(testFile);
// Assert
Assert.AreEqual(Encoding.Unicode.CodePage, detected.CodePage, "Should detect UTF-16 LE");
}
[TestMethod]
public void DetectEncoding_Utf16BeBom_ReturnsUtf16Be() {
// Arrange
string testFile = Path.Combine(_testDir, "utf16_be.cfg");
File.WriteAllText(testFile, "Test content", Encoding.BigEndianUnicode);
// Act
Encoding detected = Comtrade.DetectEncoding(testFile);
// Assert
Assert.AreEqual(Encoding.BigEndianUnicode.CodePage, detected.CodePage, "Should detect UTF-16 BE");
}
#endregion
#region 无 BOM 编码检测测试
[TestMethod]
public void DetectEncoding_AsciiContent_ReturnsUtf8Compatible() {
// Arrange - 纯 ASCII 内容,UTF-8 和 GBK 都兼容
string testFile = Path.Combine(_testDir, "ascii.cfg");
File.WriteAllBytes(testFile, Encoding.ASCII.GetBytes("Station1,Device1,2013\n2,1A,1D\n"));
// Act
Encoding detected = Comtrade.DetectEncoding(testFile);
// Assert - 纯 ASCII 应该返回 UTF-8 (向前兼容)
Assert.AreEqual(Encoding.UTF8.CodePage, detected.CodePage, "Pure ASCII should return UTF-8");
}
[TestMethod]
public void DetectEncoding_Utf8WithoutBom_ChineseContent_ReturnsUtf8() {
// Arrange - UTF-8 without BOM with Chinese characters
string testFile = Path.Combine(_testDir, "utf8_no_bom.cfg");
// 使用 UTF-8 编码写入中文字符 (不带 BOM)
using (var writer = new StreamWriter(testFile, false, new UTF8Encoding(false))) {
writer.WriteLine("北京变电站,录波器A,2013");
writer.WriteLine("2,1A,1D");
}
// Act
Encoding detected = Comtrade.DetectEncoding(testFile);
// Assert
Assert.AreEqual(Encoding.UTF8.CodePage, detected.CodePage, "UTF-8 without BOM should be detected");
}
[TestMethod]
public void DetectEncoding_GbkContent_ReturnsGbk() {
// Arrange - GBK encoded Chinese content
string testFile = Path.Combine(_testDir, "gbk.cfg");
Encoding gbk = Encoding.GetEncoding("GBK");
string chineseContent = "北京变电站,录波器A,2013\n2,1A,1D\n";
File.WriteAllBytes(testFile, gbk.GetBytes(chineseContent));
// Act
Encoding detected = Comtrade.DetectEncoding(testFile);
// Assert - 如果不是有效的 UTF-8,应该检测为 GBK
Assert.AreEqual(gbk.CodePage, detected.CodePage, "GBK content should be detected as GBK");
}
#endregion
#region 边界情况测试
[TestMethod]
public void DetectEncoding_EmptyFile_ReturnsUtf8() {
// Arrange
string testFile = Path.Combine(_testDir, "empty.cfg");
File.WriteAllBytes(testFile, Array.Empty<byte>());
// Act
Encoding detected = Comtrade.DetectEncoding(testFile);
// Assert
Assert.AreEqual(Encoding.UTF8.CodePage, detected.CodePage, "Empty file should return UTF-8 as default");
}
[TestMethod]
public void DetectEncoding_SingleByteFile_ReturnsUtf8() {
// Arrange
string testFile = Path.Combine(_testDir, "single_byte.cfg");
File.WriteAllBytes(testFile, new byte[] { 0x41 }); // 'A'
// Act
Encoding detected = Comtrade.DetectEncoding(testFile);
// Assert
Assert.AreEqual(Encoding.UTF8.CodePage, detected.CodePage, "Single ASCII byte should return UTF-8");
}
#endregion
#region DetectEncodingFromBytes 测试
[TestMethod]
public void DetectEncodingFromBytes_Utf8Bom_ReturnsUtf8() {
// Arrange
byte[] buffer = new byte[] { 0xEF, 0xBB, 0xBF, 0x41, 0x42, 0x43 };
// Act
Encoding detected = Comtrade.DetectEncodingFromBytes(buffer, buffer.Length);
// Assert
Assert.AreEqual(Encoding.UTF8.CodePage, detected.CodePage);
}
[TestMethod]
public void DetectEncodingFromBytes_Utf16LeBom_ReturnsUtf16Le() {
// Arrange
byte[] buffer = new byte[] { 0xFF, 0xFE, 0x41, 0x00, 0x42, 0x00 };
// Act
Encoding detected = Comtrade.DetectEncodingFromBytes(buffer, buffer.Length);
// Assert
Assert.AreEqual(Encoding.Unicode.CodePage, detected.CodePage);
}
[TestMethod]
public void DetectEncodingFromBytes_Utf16BeBom_ReturnsUtf16Be() {
// Arrange
byte[] buffer = new byte[] { 0xFE, 0xFF, 0x00, 0x41, 0x00, 0x42 };
// Act
Encoding detected = Comtrade.DetectEncodingFromBytes(buffer, buffer.Length);
// Assert
Assert.AreEqual(Encoding.BigEndianUnicode.CodePage, detected.CodePage);
}
[TestMethod]
public void DetectEncodingFromBytes_ValidUtf8Multibyte_ReturnsUtf8() {
// Arrange - "中" in UTF-8 is E4 B8 AD
byte[] buffer = new byte[] { 0xE4, 0xB8, 0xAD };
// Act
Encoding detected = Comtrade.DetectEncodingFromBytes(buffer, buffer.Length);
// Assert
Assert.AreEqual(Encoding.UTF8.CodePage, detected.CodePage);
}
[TestMethod]
public void DetectEncodingFromBytes_InvalidUtf8_ReturnsGbk() {
// Arrange - Invalid UTF-8 sequence that looks like GBK
// GBK "中" is D6 D0
byte[] buffer = new byte[] { 0xD6, 0xD0 };
// Act
Encoding detected = Comtrade.DetectEncodingFromBytes(buffer, buffer.Length);
// Assert
Assert.AreEqual(Encoding.GetEncoding("GBK").CodePage, detected.CodePage);
}
#endregion
}
}