back ported the BooleanStream class and it's associated test case

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@383051 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hiram R. Chirino 2006-03-04 06:23:01 +00:00
parent 477984fd4f
commit b858350da4
3 changed files with 328 additions and 192 deletions

View File

@ -55,10 +55,7 @@ namespace OpenWire.Client.Core
{ {
// re-grow the array. // re-grow the array.
byte[] d = new byte[data.Length * 2]; byte[] d = new byte[data.Length * 2];
for (int i = 0; i < data.Length; i++) Array.Copy(data, d, data.Length);
{
d[i] = data[i];
}
data = d; data = d;
} }
} }
@ -76,40 +73,31 @@ namespace OpenWire.Client.Core
public void Marshal(BinaryWriter dataOut) public void Marshal(BinaryWriter dataOut)
{ {
if (arrayLimit < 64) if( arrayLimit < 64 ) {
{
dataOut.Write((byte)arrayLimit); dataOut.Write((byte)arrayLimit);
} } else if( arrayLimit < 256 ) { // max value of unsigned byte
else if (arrayLimit < 256)
{ // max value of unsigned byte
dataOut.Write((byte)0xC0); dataOut.Write((byte)0xC0);
dataOut.Write((byte)arrayLimit); dataOut.Write((byte)arrayLimit);
} } else {
else dataOut.Write((byte)0x80);
{
dataOut.Write((byte) 0xE0);
BaseDataStreamMarshaller.WriteShort(arrayLimit, dataOut); BaseDataStreamMarshaller.WriteShort(arrayLimit, dataOut);
} }
dataOut.Write(data, 0, arrayLimit); dataOut.Write(data, 0, arrayLimit);
Clear(); Clear();
} }
public void Unmarshal(BinaryReader dataIn) public void Unmarshal(BinaryReader dataIn)
{ {
arrayLimit = BaseDataStreamMarshaller.ReadByte(dataIn); arrayLimit = (short)(BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF);
if ((arrayLimit & 0xE0) != 0) if ( arrayLimit == 0xC0 ) {
{ arrayLimit = (short)(BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF);
} else if( arrayLimit == 0x80 ) {
arrayLimit = BaseDataStreamMarshaller.ReadShort(dataIn); arrayLimit = BaseDataStreamMarshaller.ReadShort(dataIn);
} }
else if ((arrayLimit & 0xC0) != 0) if( data.Length < arrayLimit ) {
{
arrayLimit = (short) (BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF);
}
if (data.Length < arrayLimit)
{
data = new byte[arrayLimit]; data = new byte[arrayLimit];
} }
dataIn.Read(data, 0, arrayLimit); dataIn.Read(data, 0, arrayLimit);
Clear(); Clear();
} }
@ -122,14 +110,14 @@ namespace OpenWire.Client.Core
public int MarshalledSize() public int MarshalledSize()
{ {
if (arrayLimit < 64) if( arrayLimit < 64 ) {
{
return 1+arrayLimit; return 1+arrayLimit;
} } else if (arrayLimit < 256) {
else
{
return 2+arrayLimit; return 2+arrayLimit;
} } else {
return 3+arrayLimit;
}
} }
} }
} }

View File

@ -0,0 +1,147 @@
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed 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;
using OpenWire.Client;
using OpenWire.Client.Core;
namespace OpenWire.Client.Core
{
[TestFixture]
public class BooleanStreamTest : TestSupport
{
protected OpenWireFormat openWireformat;
protected int endOfStreamMarker = 0x12345678;
int numberOfBytes = 8 * 200;
public delegate bool GetBooleanValueDelegate(int index, int count);
[Test]
public void TestBooleanMarshallingUsingAllTrue()
{
TestBooleanStream(numberOfBytes, new GetBooleanValueDelegate(GetBooleanValueAllTrue));
}
public bool GetBooleanValueAllTrue(int index, int count)
{
return true;
}
[Test]
public void TestBooleanMarshallingUsingAllFalse()
{
TestBooleanStream(numberOfBytes, new GetBooleanValueDelegate(GetBooleanValueAllFalse));
}
public bool GetBooleanValueAllFalse(int index, int count)
{
return false;
}
[Test]
public void TestBooleanMarshallingUsingAlternateTrueFalse()
{
TestBooleanStream(numberOfBytes, new GetBooleanValueDelegate(GetBooleanValueAlternateTrueFalse));
}
public bool GetBooleanValueAlternateTrueFalse(int index, int count)
{
return (index & 1) == 0;
}
[Test]
public void TestBooleanMarshallingUsingAlternateFalseTrue()
{
TestBooleanStream(numberOfBytes, new GetBooleanValueDelegate(GetBooleanValueAlternateFalseTrue));
}
public bool GetBooleanValueAlternateFalseTrue(int index, int count)
{
return (index & 1) != 0;
}
protected void TestBooleanStream(int numberOfBytes, GetBooleanValueDelegate valueDelegate)
{
for (int i = 0; i < numberOfBytes; i++)
{
AssertMarshalBooleans(i, valueDelegate);
}
}
protected void AssertMarshalBooleans(int count, GetBooleanValueDelegate valueDelegate)
{
BooleanStream bs = new BooleanStream();
for (int i = 0; i < count; i++)
{
bs.WriteBoolean(valueDelegate(i, count));
}
MemoryStream buffer = new MemoryStream();
BinaryWriter ds = new BinaryWriter(buffer);
bs.Marshal(ds);
BaseDataStreamMarshaller.WriteInt(endOfStreamMarker, ds);
// now lets read from the stream
MemoryStream ins = new MemoryStream(buffer.ToArray());
BinaryReader dis = new BinaryReader(ins);
bs = new BooleanStream();
bs.Unmarshal(dis);
for (int i = 0; i < count; i++)
{
bool expected = valueDelegate(i, count);
try
{
bool actual = bs.ReadBoolean();
Assert.AreEqual(expected, actual);
}
catch (Exception e)
{
Assert.Fail("Failed to parse bool: " + i + " out of: " + count + " due to: " + e);
}
}
int marker = BaseDataStreamMarshaller.ReadInt(dis);
Assert.AreEqual(endOfStreamMarker, marker, "did not match: "+endOfStreamMarker+" and "+marker);
// lets try read and we should get an exception
try
{
dis.ReadByte();
Assert.Fail("Should have reached the end of the stream");
}
catch (IOException e)
{
}
}
[SetUp]
protected void SetUp()
{
openWireformat = createOpenWireFormat();
}
protected OpenWireFormat createOpenWireFormat()
{
OpenWireFormat wf = new OpenWireFormat();
// wf.setCacheEnabled(true);
// wf.setStackTraceEnabled(false);
// wf.setVersion(1);
return wf;
}
}
}

View File

@ -41,6 +41,7 @@
<Compile Include="OpenWire.Client\ClientTest.cs"/> <Compile Include="OpenWire.Client\ClientTest.cs"/>
<Compile Include="OpenWire.Client\CommandTest.cs"/> <Compile Include="OpenWire.Client\CommandTest.cs"/>
<Compile Include="OpenWire.Client\ConsumerTest.cs"/> <Compile Include="OpenWire.Client\ConsumerTest.cs"/>
<Compile Include="OpenWire.Client\Core\BooleanStreamTest.cs"/>
<Compile Include="OpenWire.Client\EndianTest.cs"/> <Compile Include="OpenWire.Client\EndianTest.cs"/>
<Compile Include="OpenWire.Client\JMSPropertyTest.cs"/> <Compile Include="OpenWire.Client\JMSPropertyTest.cs"/>
<Compile Include="OpenWire.Client\MapMessageTest.cs"/> <Compile Include="OpenWire.Client\MapMessageTest.cs"/>