// $Id$ // // 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 System; using System.IO; using NUnit.Framework; namespace Org.Apache.Etch.Bindings.Csharp.Util { [TestFixture] public class TestFlexBuffer { [TestFixtureSetUp] public void First() { Console.WriteLine(); Console.Write("TestFlexBuffer"); } private FlexBuffer buf = new FlexBuffer(); // General test function private void CheckBuf( int length, int index, int avail ) { Assert.AreEqual( length, buf.Length() ); Assert.AreEqual( index, buf.Index() ); Assert.AreEqual( avail, buf.Avail() ); } #region Basic Tests [Test] public void Initial1() { buf = new FlexBuffer(); CheckBuf( 0, 0, 0 ); } [Test] public void Initial2() { buf = new FlexBuffer( new byte[ 5 ] ); CheckBuf( 5, 0, 5 ); } [Test] public void Initial3() { buf = new FlexBuffer( new byte[ 5 ], 2 ); CheckBuf( 2, 0, 2 ); } [Test] public void Initial4() { buf = new FlexBuffer( new byte[ 5 ], 1, 3 ); CheckBuf( 4, 1, 3 ); } [Test] public void SetLength0() { buf.SetLength( 0 ); CheckBuf( 0, 0, 0 ); } [Test] public void SetLength1() { buf.SetLength( 1 ); CheckBuf( 1, 0, 1 ); } [Test] public void SetLength2() { buf.SetLength( 2 ); CheckBuf( 2, 0, 2 ); } [Test] public void SetLength3() { buf.SetLength( 2 ); buf.SetLength( 0 ); CheckBuf( 0, 0, 0 ); } [Test] [ExpectedException ( typeof ( ArgumentOutOfRangeException ) )] public void SetLength4() { buf.SetLength( -1 ); } [Test] public void SetIndex0() { buf.Reset(); buf.SetIndex( 0 ); CheckBuf( 0, 0, 0 ); } [Test] [ExpectedException ( typeof ( ArgumentOutOfRangeException ) )] public void SetIndex1() { buf.Reset(); buf.SetIndex( 1 ); } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void SetIndex2() { buf.SetIndex( -1 ); } [Test] public void SetIndex3() { buf.SetLength( 5 ); buf.SetIndex( 1 ); CheckBuf( 5, 1, 4 ); } [Test] public void SetIndex4() { buf.SetLength( 5 ); buf.SetIndex( 4 ); CheckBuf( 5, 4, 1 ); } [Test] public void SetIndex5() { buf.SetLength( 5 ); buf.SetIndex( 5 ); CheckBuf( 5, 5, 0 ); } [Test] public void SetIndex6() { buf.SetLength( 5 ); buf.SetIndex( 5 ); buf.SetLength( 0 ); CheckBuf( 0, 0, 0 ); } [Test] public void SetIndex7() { buf.SetLength( 5 ); buf.SetIndex( 5 ); buf.SetLength( 2 ); CheckBuf( 2, 2, 0 ); } [Test] public void Reset() { buf.SetLength( 2 ); buf.SetIndex( 1 ); buf.Reset(); CheckBuf( 0, 0, 0 ); } #endregion Basic Tests #region Cases for put ( int ) // put once or twice (the put 0 case has already been tested). [Test] public void Put1a() { buf.Reset(); buf.Put( 1 ); CheckBuf( 1, 1, 0 ); } [Test] public void Put1b() { buf.Reset(); buf.Put( 1 ); buf.Put( 2 ); CheckBuf( 2, 2, 0 ); } #endregion Cases for put ( int ) #region Cases for put ( byte[] buf ) // buf.length = null, 0, 1, 2 private void TestPutBytes( int bufLen ) { buf.Reset(); buf.Put( 1 ); buf.Put( bufLen >= 0 ? new byte[ bufLen ] : null ); CheckBuf( bufLen + 1, bufLen + 1, 0 ); } [Test] [ExpectedException( typeof ( NullReferenceException ) )] public void Put2a() { TestPutBytes( -1 ); } [Test] public void Put2b() { TestPutBytes( 0 ); } [Test] public void Put2c() { TestPutBytes( 1 ); } [Test] public void Put2d() { TestPutBytes( 2 ); } #endregion Cases for put ( byte[] buf ) #region Cases for put( byte[] buf, int off, int len ) // buf.length, off, len // failure reason // --- group 3: // null, 0, 0 // fail (buf is null) // null, 0, 1 // fail (buf is null) // null, 1, 0 // fail (buf is null) // --- group 4: // 0, 0, 0 // 0, 0, 1 // fail (off+len > buf.length) // 0, 1, 0 // fail (off > buf.length) // --- group 5: // 1, 0, 0 // 1, 0, 1 // 1, 0, 2 // fail (off+len > buf.length) // 1, 1, 0 // 1, 1, 1 // fail (off+len > buf.length) // 1, 1, 2 // fail (off+len > buf.length) // 1, 2, 0 // fail (off > buf.length) // 1, 2, 1 // fail (off > buf.length) // 1, 2, 2 // fail (off > buf.length) // --- group 6: // 2, 0, 0 // 2, 0, 1 // 2, 0, 2 // 2, 1, 0 // 2, 1, 1 // 2, 1, 2 // fail (off+len > buf.length) // 2, 2, 0 // 2, 2, 1 // fail (off+len > buf.length) // 2, 2, 2 // fail (off+len > buf.length) private void TestPutBytesOffLen( int bufLen, int offset, int length ) { buf.Reset(); buf.Put( 1 ); buf.Put( bufLen >= 0 ? new byte[ bufLen ] : null, offset, length ); CheckBuf( length + 1, length + 1, 0 ); } [Test] [ExpectedException( typeof( NullReferenceException ) )] public void Put3a() { TestPutBytesOffLen( -1, 0, 0 ); // fail (buf is null) } [Test] [ExpectedException( typeof( NullReferenceException ) )] public void Put3b() { TestPutBytesOffLen( -1, 0, 1 ); // fail (buf is null) } [Test] [ExpectedException( typeof( NullReferenceException ) )] public void Put3c() { TestPutBytesOffLen( -1, 1, 0 ); // fail (buf is null) } [Test] public void Put4a() { TestPutBytesOffLen( 0, 0, 0 ); } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Put4b() { TestPutBytesOffLen( 0, 0, 1 ); // fail (off+len > buf.length) } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void Put4c() { TestPutBytesOffLen( 0, 1, 0 ); // fail (off > buf.length) } [Test] public void Put5a() { TestPutBytesOffLen( 1, 0, 0 ); } [Test] public void Put5b() { TestPutBytesOffLen( 1, 0, 1 ); } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void Put5c() { TestPutBytesOffLen( 1, 0, 2 ); // fail (off+len > buf.length) } [Test] public void Put5d() { TestPutBytesOffLen( 1, 1, 0 ); } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void Put5e() { TestPutBytesOffLen( 1, 1, 1 ); // fail (off+len > buf.length) } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void Put5f() { TestPutBytesOffLen( 1, 1, 2 ); // fail (off+len > buf.length) } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void Put5g() { TestPutBytesOffLen( 1, 2, 0 ); // fail (off > buf.length) } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void Put5h() { TestPutBytesOffLen( 1, 2, 1 ); // fail (off > buf.length) } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void Put5i() { TestPutBytesOffLen( 1, 2, 2 ); // fail (off > buf.length) } [Test] public void Put6a() { TestPutBytesOffLen( 2, 2, 0 ); } [Test] public void Put6b() { TestPutBytesOffLen( 2, 0, 1 ); } [Test] public void Put6c() { TestPutBytesOffLen( 2, 0, 2 ); } [Test] public void Put6d() { TestPutBytesOffLen( 2, 1, 0 ); } [Test] public void Put6e() { TestPutBytesOffLen( 2, 1, 1 ); } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Put6f() { TestPutBytesOffLen( 2, 1, 2 ); // fail (off+len > buf.length) } [Test] public void Put6g() { TestPutBytesOffLen( 2, 2, 0 ); } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Put6h() { TestPutBytesOffLen( 2, 2, 1 ); // fail (off+len > buf.length) } [Test] [ExpectedException( typeof( ArgumentOutOfRangeException ) )] public void Put6i() { TestPutBytesOffLen( 2, 2, 2 ); // fail (off+len > buf.length) } #endregion Cases for put( byte[] buf, int off, int len ) #region Cases for Put( int ), Get() // put 0, get 0. // already tested above // put 0, get 1. // fails with EOFException // put 1, get 0. // put 1, get 1. // put 1, get 2. // fails with EOFException // put 2, get 0. // put 2, get 1. // put 2, get 2. // put 2, get 3. // fails with EOFException // put 10000, get 10000. // just like above, putting byte array instead // just like above, getting byte array instead. // just like above, putting and getting byte array. private void TestPutGet( int nPuts, int nGets ) { buf.Reset(); for ( int i = 0; i < nPuts; i++ ) buf.Put( i ); buf.SetIndex( 0 ); for ( int i =0; i < nGets; i++ ) Assert.AreEqual( ( byte ) i, ( byte ) buf.Get() ); CheckBuf( nPuts, nGets, nPuts - nGets ); } private void TestPutBytesGet( int nPuts, int nGets ) { buf.Reset(); byte[] buffer = new byte[ nPuts ]; for ( int i = 0; i < nPuts; i++ ) buffer[ i ] = ( byte ) i; buf.Put( buffer ); buf.SetIndex( 0 ); for ( int i =0; i buffer length, put = false. // fails with EOFException // skip length = 0, put = false. // skip length = buffer length, put = false. // skip length < buffer length, put = false. // skip length > buffer length, put = true. // skip length < buffer length, put = true. // skip length = max buffer length, put = true. // fails with IOException [Test] [ExpectedException( typeof( ArgumentException ) )] public void Skip1() { buf = new FlexBuffer( new byte[] { } ); buf.Skip( -1, false ); } [Test] [ExpectedException( typeof( EndOfStreamException ) )] public void Skip2() { buf = new FlexBuffer( new byte[] { 1, 2 } ); buf.Skip( 3, false ); } [Test] public void Skip3() { buf = new FlexBuffer( new byte[] { } ); buf.Skip( 0, false ); CheckBuf( 0, 0, 0 ); } [Test] public void Skip4() { buf = new FlexBuffer( new byte[] { 1, 2 } ); buf.Skip( 2, false ); CheckBuf( 2, 2, 0 ); } [Test] public void Skip5() { buf = new FlexBuffer( new byte[] { 1, 2 } ); buf.Skip( 1, false ); CheckBuf( 2, 1, 1 ); } [Test] public void Skip6() { buf = new FlexBuffer( new byte[] { 1, 2 } ); buf.Skip( 1, true ); CheckBuf( 2, 1, 1 ); } [Test] public void Skip7() { buf = new FlexBuffer( new byte[] { 1, 2 } ); buf.Skip( 5, true ); CheckBuf( 5, 5, 0 ); } [Test] [ExpectedException( typeof( IOException ) )] public void Skip8() { buf = new FlexBuffer( new byte[] { 1, 2 } ); int max = 4*1024*1024; buf.Skip( max+1, true ); CheckBuf( max+1, max+1, 0 ); } #endregion } }