summaryrefslogtreecommitdiff
path: root/trunk/qpid/dotnet/Qpid.Buffer.Tests/SimpleByteBufferTests.cs
blob: b028bdb1eec6d9745756fcab2bb4f54f45277e05 (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

using NUnit.Framework;
using Apache.Qpid.Buffer;

namespace Apache.Qpid.Buffer.Tests
{
   /// <summary>
   /// Tests for the SimpleByteBuffer class
   /// </summary>
   [TestFixture]
   public class SimpleByteBufferTests
   {
      [Test]
      public void CanCreateNewBuffer()
      {
         const int size = 10;
         ByteBuffer buffer = ByteBuffer.Allocate(size);
         Assert.AreEqual(size, buffer.Capacity);
         Assert.AreEqual(0, buffer.Position);
         Assert.AreEqual(size, buffer.Remaining);
         Assert.AreEqual(true, buffer.HasRemaining);
      }

      [Test]
      public void CanWrapArray()
      {
         byte[] array = new byte[10];
         for ( int i=0; i < array.Length; i++ )
         {
            array[i] = (byte) i;
         }
         ByteBuffer buffer = ByteBuffer.Wrap(array);
         // the buffer should be the same size, 
         // and positioned at the end
         Assert.AreEqual(array.Length, buffer.Capacity);
         Assert.AreEqual(array.Length, buffer.Position);
         Assert.AreEqual(array.Length, buffer.Limit);
      }

      #region Base Read/Write tests
      //
      // Base Read/Write tests
      //
      [Test]
      public void CanReadWriteBytes()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Put((byte)0x01).Put((byte)0x02).Put((byte)0x03);
         buffer.Rewind();
         Assert.AreEqual(0x01, buffer.GetByte());
         Assert.AreEqual(0x02, buffer.GetByte());
         Assert.AreEqual(0x03, buffer.GetByte());
      }

      [Test]
      [ExpectedException(typeof(BufferUnderflowException))]
      public void ThrowOnReadByteWithNoSpace()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(1);
         buffer.Put((byte)0x01);
         buffer.GetByte();
      }

      [Test]
      [ExpectedException(typeof(BufferOverflowException))]
      public void ThrowOnWriteByteWithNoSpace()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(1);
         buffer.Put((byte)0x01).Put((byte)0x02);
      }

      #endregion Base Read/Write tests

      #region Other Buffer Operations
      //
      // Other Buffer Operations
      //
      
      [Test]
      public void CanFlipBuffer()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Put((byte)0x01).Put((byte)0x02).Put((byte)0x03);
         buffer.Flip();
         Assert.AreEqual(10, buffer.Capacity);
         Assert.AreEqual(3, buffer.Limit);
         Assert.AreEqual(0, buffer.Position);
         Assert.AreEqual(3, buffer.Remaining);
      }

      [Test]
      public void CanCompactBuffer()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Put((byte)0x01).Put((byte)0x02).Put((byte)0x03);
         buffer.Flip();
         buffer.Position = 1;
         buffer.Compact();
         Assert.AreEqual(10, buffer.Capacity);
         Assert.AreEqual(10, buffer.Limit);
         Assert.AreEqual(2, buffer.Position);
         Assert.AreEqual(8, buffer.Remaining);
         buffer.Rewind();
         Assert.AreEqual((byte)0x02, buffer.GetByte());
         Assert.AreEqual((byte)0x03, buffer.GetByte());
      }

      [Test]
      public void CanClearBuffer()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Put((byte)0x01).Put((byte)0x02).Put((byte)0x03);
         buffer.Flip();
         buffer.Position = 2;
         buffer.Clear();
         Assert.AreEqual(10, buffer.Capacity);
         Assert.AreEqual(10, buffer.Limit);
         Assert.AreEqual(0, buffer.Position);
         Assert.AreEqual(10, buffer.Remaining);
      }

      [Test]
      public void CanExpandBuffer()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Put((byte)0x01).Put((byte)0x02).Put((byte)0x03);
         buffer.Flip();
         buffer.Position = 2;
         int pos = buffer.Position;
         buffer.Expand(20);

         Assert.AreEqual(pos, buffer.Position);
         Assert.IsTrue(buffer.Remaining >= 20);
         buffer.Rewind();
         Assert.AreEqual(0x01, buffer.GetByte());
      }

      [Test]
      public void CanAutoExpand()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(2);
         buffer.IsAutoExpand = true;
         // should cause autoexpand
         buffer.Put((byte)0x01).Put((byte)0x02).Put((byte)0x03);
         Assert.IsTrue(buffer.Capacity > 2);
      }

      [Test]
      public void CanGetArray()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Put((byte)0x01).Put((byte)0x02).Put((byte)0x03);
         buffer.Flip();

         byte[] array = buffer.Array;
         for ( int i=0; i < buffer.Limit; i++ )
         {
            Assert.AreEqual(buffer.GetByte(), array[i]);
         }
      }

      [Test]
      public void CanSkip()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Skip(4);
         Assert.AreEqual(4, buffer.Position);
      }

      #endregion // Base Read/Write tests

      #region Typed Accessors
      //
      // Typed Accessors
      //
      [Test]
      public void CanReadWriteSByte()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         sbyte value = -12;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetSByte());
      }
      [Test]
      public void CanReadWriteUInt16()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         ushort value = 41233;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetUInt16());
      }
      [Test]
      public void CanReadWriteInt16()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         short value = -21233;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetInt16());
      }
      [Test]
      public void CanReadWriteUInt32()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         uint value = 41233211;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetUInt32());
      }
      [Test]
      public void CanReadWriteInt32()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         int value = -22221233;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetInt32());
      }
      [Test]
      public void CanReadWriteUInt64()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         ulong value = 41233218871;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetUInt64());
      }
      [Test]
      public void CanReadWriteInt64()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         long value = -9887335411;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetInt64());
      }
      [Test]
      public void CanReadWriteFloat()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         float value = -1.2331f;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetFloat());
      }

      [Test]
      public void CanReadWriteDouble()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         double value = -1.2331E12;
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetDouble());
      }

      [Test]
      public void CanReadWriteChar()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         char value = 'H';
         buffer.Put(value);
         buffer.Flip();
         Assert.AreEqual(value, buffer.GetChar());
      }

      [Test]
      public void CanReadWriteByteArray()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Put(new byte[] { 0x01, 0x02, 0x03});
         buffer.Flip();
         byte[] data = new byte[3];
         buffer.GetBytes(data);
         Assert.AreEqual(0x01, data[0]);
         Assert.AreEqual(0x02, data[1]);
         Assert.AreEqual(0x03, data[2]);
      }

      [Test]
      public void CanReadWriteByteArrayWithOffset()
      {
         ByteBuffer buffer = ByteBuffer.Allocate(10);
         buffer.Put(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }, 1, 4);
         buffer.Flip();
         byte[] data = new byte[3];
         buffer.GetBytes(data, 2, 1);
         Assert.AreEqual(0x00, data[0]);
         Assert.AreEqual(0x00, data[1]);
         Assert.AreEqual(0x02, data[2]);
      }

      [Test]
      public void CanWriteByteBuffer()
      {
         ByteBuffer buffer1 = ByteBuffer.Allocate(10);
         buffer1.Put((byte)0x01).Put((byte)0x02).Put((byte)0x03);
         buffer1.Flip();

         ByteBuffer buffer2 = ByteBuffer.Allocate(10);
         buffer2.Put(buffer1);
         buffer2.Flip();
         Assert.AreEqual(buffer1.Limit, buffer2.Limit);
         Assert.AreEqual(0x01, buffer2.GetByte());
      }
      #endregion // Typed Accessors

   } // class SimpleByteBufferTests
}