This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
/
WindowsRuntimeBufferExtensionsTests.cs
211 lines (184 loc) · 7.03 KB
/
WindowsRuntimeBufferExtensionsTests.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
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
//
// WindowsRuntimeBufferExtensionsTests.cs
//
// Author:
// Eric Maupin <me@ermau.com>
//
// Copyright (c) 2011 Eric Maupin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using NUnit.Framework;
using Windows.Storage.Streams;
namespace WinRTNET.Tests.Streams
{
[TestFixture]
public class WindowsRuntimeBufferExtensionsTests
{
[Test]
public void AsBuffer_Null()
{
Assert.Throws<ArgumentNullException> (() => WindowsRuntimeBufferExtensions.AsBuffer (null));
Assert.Throws<ArgumentNullException>(() => WindowsRuntimeBufferExtensions.AsBuffer (null, 0, 0));
}
[Test]
public void AsBuffer()
{
byte[] array = new byte[5];
IBuffer buffer = array.AsBuffer();
Assert.IsNotNull (buffer);
Assert.AreEqual (array.Length, buffer.Length);
Assert.AreEqual (array.Length, buffer.Capacity);
}
[Test]
public void AsBuffer_OffsetLength()
{
byte[] array = new byte[5];
IBuffer buffer = array.AsBuffer (1, 2);
Assert.IsNotNull (buffer);
Assert.AreEqual ((uint)2, buffer.Length);
Assert.AreEqual ((uint)2, buffer.Capacity);
}
[TestCase (6, 0)]
[TestCase (6, 1)]
[TestCase (6, 6)]
[TestCase (3, 3)]
public void AsBuffer_OffsetLength_Invalid (int offset, int length)
{
byte[] array = new byte[5];
Assert.Throws<ArgumentOutOfRangeException>(() => array.AsBuffer (offset, length));
}
[Test]
public void AsBuffer_OffsetLengthCapacity()
{
byte[] array = new byte[5];
IBuffer buffer = array.AsBuffer (1, 2, 3);
Assert.IsNotNull (buffer);
Assert.AreEqual ((uint)2, buffer.Length);
Assert.AreEqual ((uint)3, buffer.Capacity);
}
[TestCase (6, 0, 0)]
[TestCase (6, 0, 1)]
[TestCase (6, 1, 1)]
[TestCase (6, 6, 6)]
[TestCase (3, 3, 3)]
[TestCase (0, 3, 2)]
[TestCase (4, 1, 1)]
public void AsBuffer_OffsetLengthCapacity_Invalid (int offset, int length, int capacity)
{
byte[] array = new byte[5];
Assert.Throws<ArgumentOutOfRangeException>(() => array.AsBuffer (offset, length, capacity));
}
[Test]
public void AsStream_Invalid()
{
Assert.Throws<ArgumentNullException> (() => WindowsRuntimeBufferExtensions.AsStream (null));
Assert.Throws<InvalidCastException> (() => new FakeBuffer().AsStream());
}
[Test]
public void AsStream()
{
byte[] buffer = new byte[5];
Stream s = buffer.AsBuffer(1, 2, 3).AsStream();
Assert.IsTrue (s.CanRead);
Assert.IsTrue (s.CanWrite);
Assert.AreEqual (0, s.Position);
Assert.AreEqual (3, s.Length);
}
[Test]
public void CopyTo_ByteToBuffer_Invalid()
{
Assert.Throws<ArgumentNullException> (() => WindowsRuntimeBufferExtensions.CopyTo ((byte[])null, new WindowsRuntimeBuffer(new byte[1])));
Assert.Throws<ArgumentNullException> (() => WindowsRuntimeBufferExtensions.CopyTo (new byte[1], null));
Assert.Throws<InvalidCastException> (() => WindowsRuntimeBufferExtensions.CopyTo (new byte[1], new FakeBuffer()));
Assert.Throws<ArgumentException> (() => WindowsRuntimeBufferExtensions.CopyTo (new byte[6], new byte[5].AsBuffer()));
}
[Test]
public void CopyTo_ByteToBuffer()
{
byte[] source = new byte[] { 1, 2, 3 };
byte[] destination = new byte[4];
WindowsRuntimeBufferExtensions.CopyTo (source, destination.AsBuffer());
Assert.AreEqual (1, destination[0]);
Assert.AreEqual (2, destination[1]);
Assert.AreEqual (3, destination[2]);
Assert.AreEqual (0, destination[3]);
}
[Test]
public void CopyTo_BufferToByte_Invalid()
{
Assert.Throws<ArgumentNullException> (() => WindowsRuntimeBufferExtensions.CopyTo (null, new byte[1]));
Assert.Throws<ArgumentNullException> (() => WindowsRuntimeBufferExtensions.CopyTo (new byte[1].AsBuffer(), (byte[])null));
Assert.Throws<InvalidCastException> (() => WindowsRuntimeBufferExtensions.CopyTo (new FakeBuffer(), new byte[1]));
Assert.Throws<ArgumentException>(() => WindowsRuntimeBufferExtensions.CopyTo (new byte[6].AsBuffer(), new byte[5]));
}
[Test]
public void CopyTo_BufferToByte()
{
byte[] source = new byte[] { 1, 2, 3 };
byte[] destination = new byte[4];
WindowsRuntimeBufferExtensions.CopyTo(source.AsBuffer(), destination);
Assert.AreEqual(1, destination[0]);
Assert.AreEqual(2, destination[1]);
Assert.AreEqual(3, destination[2]);
Assert.AreEqual(0, destination[3]);
}
[Test]
public void CopyTo_BufferToBuffer_Invalid()
{
Assert.Throws<ArgumentNullException>(() => WindowsRuntimeBufferExtensions.CopyTo ((IBuffer)null, new byte[1].AsBuffer()));
Assert.Throws<ArgumentNullException>(() => WindowsRuntimeBufferExtensions.CopyTo (new byte[1].AsBuffer(), (IBuffer)null));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo (new FakeBuffer(), new byte[1].AsBuffer()));
Assert.Throws<InvalidCastException>(() => WindowsRuntimeBufferExtensions.CopyTo (new byte[1].AsBuffer(), new FakeBuffer()));
Assert.Throws<ArgumentException>(() => WindowsRuntimeBufferExtensions.CopyTo (new byte[6].AsBuffer(), new byte[5].AsBuffer()));
}
[Test]
public void CopyTo_BufferToBuffer()
{
byte[] source = new byte[] { 1, 2, 3 };
byte[] destination = new byte[4];
WindowsRuntimeBufferExtensions.CopyTo(source.AsBuffer(), destination.AsBuffer());
Assert.AreEqual(1, destination[0]);
Assert.AreEqual(2, destination[1]);
Assert.AreEqual(3, destination[2]);
Assert.AreEqual(0, destination[3]);
}
[Test]
public void TryGetUnderlyingData_Invalid()
{
byte[] data; int offset;
Assert.Throws<ArgumentNullException> (() => WindowsRuntimeBufferExtensions.TryGetUnderlyingData (null, out data, out offset));
Assert.IsFalse (WindowsRuntimeBufferExtensions.TryGetUnderlyingData (new FakeBuffer(), out data, out offset));
Assert.IsNull (data);
Assert.AreEqual (-1, offset);
}
[Test]
public void TryGetUnderlyingData()
{
byte[] odata = new byte[] { 1, 2, 3, 4 };
IBuffer b = odata.AsBuffer (1, 3);
byte[] data; int offset;
Assert.IsTrue (b.TryGetUnderlyingData (out data, out offset));
Assert.AreSame (odata, data);
Assert.AreEqual (1, offset);
}
}
}