This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
StreamUtilityTests.cs
165 lines (143 loc) · 5.03 KB
/
StreamUtilityTests.cs
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
using System;
using System.IO;
using System.Linq;
using System.Text;
using Logos.Utility.IO;
using NUnit.Framework;
namespace Logos.Utility.Tests
{
class StreamUtilityTests
{
[Test]
public void DetectEncodingUtf8()
{
DoDetectEncoding(Encoding.UTF8, c_strEnglish);
DoDetectEncoding(Encoding.UTF8, c_strKorean);
}
[Test]
public void DetectEncodingUtf16()
{
DoDetectEncoding(Encoding.Unicode, c_strEnglish);
DoDetectEncoding(Encoding.Unicode, c_strKorean);
}
[Test]
public void DetectEncoding949()
{
DoDetectEncoding(Encoding.GetEncoding(949), c_strKorean);
}
[Test]
public void DetectEncoding1252()
{
DoDetectEncoding(Encoding.GetEncoding(1252), c_strEnglish);
}
private static void DoDetectEncoding(Encoding encoding, string testPattern)
{
using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream, encoding))
{
writer.Write(testPattern);
writer.Flush();
// detect encoding
stream.Position = 0;
Encoding detected = StreamUtility.DetectBestEncoding(stream);
Assert.IsNotNull(detected);
Assert.AreEqual(encoding.CodePage, detected.CodePage);
// use encoding to read stream
using (WrappingStream wrappingStream = new WrappingStream(stream, Ownership.None))
using (StreamReader reader = new StreamReader(wrappingStream, detected, false))
{
string read = reader.ReadToEnd();
Assert.AreEqual(testPattern, read);
}
}
}
const string c_strEnglish = "‘This’ is “Unicode” text—it has €123.45 and symbols.™\r\nIn the beginning, God created the heavens and the earth. The earth was without form and void, and darkness was over the face of the deep. And the Spirit of God was hovering over the face of the waters.";
const string c_strKorean = "태초에 말씀이 계시니라 이 말씀이 하나님과 함께 계셨으니 이 말씀은 곧 하나님이시니라";
[Test]
public void ReadExactlyBadArguments()
{
using (Stream stream = new MemoryStream())
{
Assert.Throws<ArgumentNullException>(() => StreamUtility.ReadExactly(null, new byte[1], 0, 1));
Assert.Throws<ArgumentNullException>(() => stream.ReadExactly(null, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.ReadExactly(new byte[1], -1, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.ReadExactly(new byte[1], 1, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.ReadExactly(new byte[1], 0, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.ReadExactly(new byte[1], 0, 2));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.ReadExactly(new byte[1], 1, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.ReadExactly(-1));
}
}
[Test]
public void ReadExactlyZeroBytes()
{
using (Stream stream = new MemoryStream())
{
Assert.IsNotNull(stream.ReadExactly(0));
Assert.AreEqual(0, stream.ReadExactly(0).Length);
}
}
[Test]
public void ReadExactly()
{
byte[] abySource = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
using (Stream streamSource = new MemoryStream(abySource))
using (Stream stream = new SlowStream(streamSource))
{
byte[] read = stream.ReadExactly(5);
CollectionAssert.AreEqual(abySource.Take(5), read);
read = stream.ReadExactly(6);
CollectionAssert.AreEqual(abySource.Skip(5), read);
Assert.Throws<EndOfStreamException>(() => stream.ReadExactly(1));
}
using (Stream streamSource = new MemoryStream(abySource))
using (Stream stream = new SlowStream(streamSource))
{
byte[] read = stream.ReadExactly(11);
CollectionAssert.AreEqual(abySource, read);
}
using (Stream streamSource = new MemoryStream(abySource))
using (Stream stream = new SlowStream(streamSource))
{
Assert.Throws<EndOfStreamException>(() => stream.ReadExactly(12));
}
}
[Test]
public void ReadExactlyArray()
{
byte[] abySource = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
using (Stream streamSource = new MemoryStream(abySource))
using (Stream stream = new SlowStream(streamSource))
{
byte[] buffer = new byte[20];
stream.ReadExactly(buffer, 3, 8);
CollectionAssert.AreEqual(new byte[3].Concat(abySource.Take(8)).Concat(new byte[9]), buffer);
}
using (Stream streamSource = new MemoryStream(abySource))
using (Stream stream = new SlowStream(streamSource))
{
byte[] buffer = new byte[20];
stream.ReadExactly(buffer, 5, 11);
CollectionAssert.AreEqual(new byte[5].Concat(abySource).Concat(new byte[4]), buffer);
}
using (Stream streamSource = new MemoryStream(abySource))
using (Stream stream = new SlowStream(streamSource))
{
byte[] buffer = new byte[20];
Assert.Throws<EndOfStreamException>(() => stream.ReadExactly(buffer, 5, 12));
}
}
private class SlowStream : WrappingStream
{
public SlowStream(Stream stream)
: base(stream, Ownership.None)
{
}
public override int Read(byte[] buffer, int offset, int count)
{
return base.Read(buffer, offset, Math.Min(count, 2));
}
}
}
}