-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileMapTests.cs
More file actions
182 lines (151 loc) · 5.16 KB
/
Copy pathFileMapTests.cs
File metadata and controls
182 lines (151 loc) · 5.16 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
//
// FileMapTests.cs
//
// Author:
// Tomas Restrepo (tomasr@mvps.org)
//
using System;
using System.IO;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using Winterdom.IO.FileMap;
namespace Winterdom.IO.FileMap.Tests
{
[ TestFixture ]
public class FileMapTests
{
const int MAX_BYTES = 8*1024;
/// <summary>
/// Test simple writing and reading on non-backed MMF
/// </summary>
[ Test ]
public void TestNonBackedMMF()
{
try {
MemoryMappedFile map =
MemoryMappedFile.Create(MapProtection.PageReadWrite, MAX_BYTES);
using ( Stream view = map.MapView(MapAccess.FileMapWrite, 0, MAX_BYTES) )
{
WriteBytesToStream(view, MAX_BYTES);
}
using ( Stream view = map.MapView(MapAccess.FileMapRead, 0, MAX_BYTES/2) )
{
VerifyBytesInStream(view, MAX_BYTES/2);
}
map.Close();
} catch ( FileMapIOException e ) {
Assertion.Fail("Write error: " + e);
}
}
/// <summary>
/// Test named MMF
/// </summary>
[ Test ]
public void TestNamedMMF()
{
try {
const string NAME = "MyMappedFile";
MemoryMappedFile map =
MemoryMappedFile.Create(MapProtection.PageReadWrite, MAX_BYTES, NAME);
MemoryMappedFile map2 =
MemoryMappedFile.Open(MapAccess.FileMapRead, NAME);
map2.Close();
map.Close();
} catch ( FileMapIOException e ) {
Assertion.Fail("Failed Named MMF: " + e);
}
}
/// <summary>
/// Test simple writing and reading
/// </summary>
[ Test ]
public void TestMMFFileWrite()
{
try {
string filename = Path.GetTempFileName();
MemoryMappedFile map =
MemoryMappedFile.Create(filename, MapProtection.PageReadWrite, MAX_BYTES);
using ( Stream view = map.MapView(MapAccess.FileMapWrite, 0, MAX_BYTES) )
{
WriteBytesToStream(view, MAX_BYTES);
}
using ( Stream view = map.MapView(MapAccess.FileMapRead, 0, MAX_BYTES/2) )
{
VerifyBytesInStream(view, MAX_BYTES/2);
}
map.Close();
} catch ( FileMapIOException e ) {
Assertion.Fail("Write error: " + e);
}
}
/// <summary>
/// Test the position property
/// </summary>
[ Test ]
public void TestMMFViewSeeking()
{
string filename = Path.GetTempFileName();
MemoryMappedFile map =
MemoryMappedFile.Create(filename, MapProtection.PageReadWrite, MAX_BYTES);
using ( Stream view = map.MapView(MapAccess.FileMapWrite, 0, MAX_BYTES) )
{
view.WriteByte(0x12);
// seek from start of view
view.Seek(12, SeekOrigin.Begin);
Assertion.AssertEquals(12, (int)view.Position);
// Seek from current pos
view.Seek(1, SeekOrigin.Current);
Assertion.AssertEquals(13, (int)view.Position);
// seek from end
view.Seek(-1, SeekOrigin.End);
Assertion.AssertEquals(MAX_BYTES-1, (int)view.Position);
try {
// no seeking past end of stream
view.Seek(1, SeekOrigin.End);
Assertion.Fail("Seeked past end of stream");
} catch ( FileMapIOException ) {
}
}
map.Close();
}
/// <summary>
/// Verifies that you can't read or write beyond
/// a view size
/// </summary>
[ Test ]
public void TestMMFViewSize()
{
string filename = Path.GetTempFileName();
MemoryMappedFile map =
MemoryMappedFile.Create(filename, MapProtection.PageReadWrite, MAX_BYTES);
using ( Stream view = map.MapView(MapAccess.FileMapWrite, 0, MAX_BYTES) )
{
view.Seek(MAX_BYTES, SeekOrigin.Begin);
// no writing past end of view
view.WriteByte(0x01);
Assertion.AssertEquals(MAX_BYTES, (int)view.Position);
// no reading past end of stream
Assertion.AssertEquals(-1, view.ReadByte());
}
map.Close();
}
#region Private Methods
private void WriteBytesToStream(Stream stream, int count)
{
for ( int i=0; i < count; i++ ) {
stream.WriteByte((byte)(i/255));
}
}
private void VerifyBytesInStream(Stream stream, int count)
{
for ( int i=0; i < count; i++ ) {
int b = stream.ReadByte();
if ( b == -1 )
Assertion.Fail("Reached end of stream prematurely");
Assertion.AssertEquals((byte)(i/255), (byte)b);
}
}
#endregion // Private Methods
} // class FileMapTests
} // namespace Winterdom.IO.FileMap.Tests