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

@ -1,135 +1,123 @@
/*
* 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 OpenWire.Client.Commands;
using OpenWire.Client.Core;
using OpenWire.Client.IO;
namespace OpenWire.Client.Core
{
/// <summary>
/// Represents a stream of boolean flags
/// </summary>
public class BooleanStream
{
byte[] data = new byte[48];
short arrayLimit;
short arrayPos;
byte bytePos;
public bool ReadBoolean()
{
byte b = data[arrayPos];
bool rc = ((b >> bytePos) & 0x01) != 0;
bytePos++;
if (bytePos >= 8)
{
bytePos = 0;
arrayPos++;
}
return rc;
}
public void WriteBoolean(bool value)
{
if (bytePos == 0)
{
arrayLimit++;
if (arrayLimit >= data.Length)
{
// re-grow the array.
byte[] d = new byte[data.Length * 2];
for (int i = 0; i < data.Length; i++)
{
d[i] = data[i];
}
data = d;
}
}
if (value)
{
data[arrayPos] |= (byte) (0x01 << bytePos);
}
bytePos++;
if (bytePos >= 8)
{
bytePos = 0;
arrayPos++;
}
}
public void Marshal(BinaryWriter dataOut)
{
if (arrayLimit < 64)
{
dataOut.Write((byte) arrayLimit);
}
else if (arrayLimit < 256)
{ // max value of unsigned byte
dataOut.Write((byte) 0xC0);
dataOut.Write((byte) arrayLimit);
}
else
{
dataOut.Write((byte) 0xE0);
BaseDataStreamMarshaller.WriteShort(arrayLimit, dataOut);
}
dataOut.Write(data, 0, arrayLimit);
Clear();
}
public void Unmarshal(BinaryReader dataIn)
{
arrayLimit = BaseDataStreamMarshaller.ReadByte(dataIn);
if ((arrayLimit & 0xE0) != 0)
{
arrayLimit = BaseDataStreamMarshaller.ReadShort(dataIn);
}
else if ((arrayLimit & 0xC0) != 0)
{
arrayLimit = (short) (BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF);
}
if (data.Length < arrayLimit)
{
data = new byte[arrayLimit];
}
dataIn.Read(data, 0, arrayLimit);
Clear();
}
public void Clear()
{
arrayPos = 0;
bytePos = 0;
}
public int MarshalledSize()
{
if (arrayLimit < 64)
{
return 1 + arrayLimit;
}
else
{
return 2 + arrayLimit;
}
}
}
}
/*
* 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 OpenWire.Client.Commands;
using OpenWire.Client.Core;
using OpenWire.Client.IO;
namespace OpenWire.Client.Core
{
/// <summary>
/// Represents a stream of boolean flags
/// </summary>
public class BooleanStream
{
byte[] data = new byte[48];
short arrayLimit;
short arrayPos;
byte bytePos;
public bool ReadBoolean()
{
byte b = data[arrayPos];
bool rc = ((b >> bytePos) & 0x01) != 0;
bytePos++;
if (bytePos >= 8)
{
bytePos = 0;
arrayPos++;
}
return rc;
}
public void WriteBoolean(bool value)
{
if (bytePos == 0)
{
arrayLimit++;
if (arrayLimit >= data.Length)
{
// re-grow the array.
byte[] d = new byte[data.Length * 2];
Array.Copy(data, d, data.Length);
data = d;
}
}
if (value)
{
data[arrayPos] |= (byte) (0x01 << bytePos);
}
bytePos++;
if (bytePos >= 8)
{
bytePos = 0;
arrayPos++;
}
}
public void Marshal(BinaryWriter dataOut)
{
if( arrayLimit < 64 ) {
dataOut.Write((byte)arrayLimit);
} else if( arrayLimit < 256 ) { // max value of unsigned byte
dataOut.Write((byte)0xC0);
dataOut.Write((byte)arrayLimit);
} else {
dataOut.Write((byte)0x80);
BaseDataStreamMarshaller.WriteShort(arrayLimit, dataOut);
}
dataOut.Write(data, 0, arrayLimit);
Clear();
}
public void Unmarshal(BinaryReader dataIn)
{
arrayLimit = (short)(BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF);
if ( arrayLimit == 0xC0 ) {
arrayLimit = (short)(BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF);
} else if( arrayLimit == 0x80 ) {
arrayLimit = BaseDataStreamMarshaller.ReadShort(dataIn);
}
if( data.Length < arrayLimit ) {
data = new byte[arrayLimit];
}
dataIn.Read(data, 0, arrayLimit);
Clear();
}
public void Clear()
{
arrayPos = 0;
bytePos = 0;
}
public int MarshalledSize()
{
if( arrayLimit < 64 ) {
return 1+arrayLimit;
} else if (arrayLimit < 256) {
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

@ -1,57 +1,58 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B409B247-A311-42A9-B68D-76054D63DD7D}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>tests</RootNamespace>
<AssemblyName>tests</AssemblyName>
<WarningLevel>4</WarningLevel>
<StartupObject/>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>false</DebugSymbols>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.core"/>
<Reference Include="nunit.framework"/>
<Reference Include="System"/>
<Reference Include="System.Data"/>
<Reference Include="System.Xml"/>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets"/>
<ItemGroup>
<Compile Include="OpenWire.Client\AsyncConsumeTest.cs"/>
<Compile Include="OpenWire.Client\BadConsumeTest.cs"/>
<Compile Include="OpenWire.Client\BytesMessageTest.cs"/>
<Compile Include="OpenWire.Client\ClientTest.cs"/>
<Compile Include="OpenWire.Client\CommandTest.cs"/>
<Compile Include="OpenWire.Client\ConsumerTest.cs"/>
<Compile Include="OpenWire.Client\EndianTest.cs"/>
<Compile Include="OpenWire.Client\JMSPropertyTest.cs"/>
<Compile Include="OpenWire.Client\MapMessageTest.cs"/>
<Compile Include="OpenWire.Client\TestMain.cs"/>
<Compile Include="OpenWire.Client\TestSupport.cs"/>
<Compile Include="OpenWire.Client\TransactionTest.cs"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../src/src.csproj">
<Name>src</Name>
<Project>{F7BA9EF7-ADF7-40EF-9A9E-206649DBB10C}</Project>
</ProjectReference>
</ItemGroup>
</Project>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B409B247-A311-42A9-B68D-76054D63DD7D}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>tests</RootNamespace>
<AssemblyName>tests</AssemblyName>
<WarningLevel>4</WarningLevel>
<StartupObject/>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>false</DebugSymbols>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.core"/>
<Reference Include="nunit.framework"/>
<Reference Include="System"/>
<Reference Include="System.Data"/>
<Reference Include="System.Xml"/>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets"/>
<ItemGroup>
<Compile Include="OpenWire.Client\AsyncConsumeTest.cs"/>
<Compile Include="OpenWire.Client\BadConsumeTest.cs"/>
<Compile Include="OpenWire.Client\BytesMessageTest.cs"/>
<Compile Include="OpenWire.Client\ClientTest.cs"/>
<Compile Include="OpenWire.Client\CommandTest.cs"/>
<Compile Include="OpenWire.Client\ConsumerTest.cs"/>
<Compile Include="OpenWire.Client\Core\BooleanStreamTest.cs"/>
<Compile Include="OpenWire.Client\EndianTest.cs"/>
<Compile Include="OpenWire.Client\JMSPropertyTest.cs"/>
<Compile Include="OpenWire.Client\MapMessageTest.cs"/>
<Compile Include="OpenWire.Client\TestMain.cs"/>
<Compile Include="OpenWire.Client\TestSupport.cs"/>
<Compile Include="OpenWire.Client\TransactionTest.cs"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../src/src.csproj">
<Name>src</Name>
<Project>{F7BA9EF7-ADF7-40EF-9A9E-206649DBB10C}</Project>
</ProjectReference>
</ItemGroup>
</Project>