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 * Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable. * applicable.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
using System; using System;
using System.IO; using System.IO;
using OpenWire.Client.Commands; using OpenWire.Client.Commands;
using OpenWire.Client.Core; using OpenWire.Client.Core;
using OpenWire.Client.IO; using OpenWire.Client.IO;
namespace OpenWire.Client.Core namespace OpenWire.Client.Core
{ {
/// <summary> /// <summary>
/// Represents a stream of boolean flags /// Represents a stream of boolean flags
/// </summary> /// </summary>
public class BooleanStream public class BooleanStream
{ {
byte[] data = new byte[48]; byte[] data = new byte[48];
short arrayLimit; short arrayLimit;
short arrayPos; short arrayPos;
byte bytePos; byte bytePos;
public bool ReadBoolean() public bool ReadBoolean()
{ {
byte b = data[arrayPos]; byte b = data[arrayPos];
bool rc = ((b >> bytePos) & 0x01) != 0; bool rc = ((b >> bytePos) & 0x01) != 0;
bytePos++; bytePos++;
if (bytePos >= 8) if (bytePos >= 8)
{ {
bytePos = 0; bytePos = 0;
arrayPos++; arrayPos++;
} }
return rc; return rc;
} }
public void WriteBoolean(bool value) public void WriteBoolean(bool value)
{ {
if (bytePos == 0) if (bytePos == 0)
{ {
arrayLimit++; arrayLimit++;
if (arrayLimit >= data.Length) if (arrayLimit >= data.Length)
{ {
// 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);
{ data = d;
d[i] = data[i]; }
} }
data = d; if (value)
} {
} data[arrayPos] |= (byte) (0x01 << bytePos);
if (value) }
{ bytePos++;
data[arrayPos] |= (byte) (0x01 << bytePos); if (bytePos >= 8)
} {
bytePos++; bytePos = 0;
if (bytePos >= 8) arrayPos++;
{ }
bytePos = 0; }
arrayPos++;
} public void Marshal(BinaryWriter dataOut)
} {
if( arrayLimit < 64 ) {
public void Marshal(BinaryWriter dataOut) dataOut.Write((byte)arrayLimit);
{ } else if( arrayLimit < 256 ) { // max value of unsigned byte
if (arrayLimit < 64) dataOut.Write((byte)0xC0);
{ dataOut.Write((byte)arrayLimit);
dataOut.Write((byte) arrayLimit); } else {
} dataOut.Write((byte)0x80);
else if (arrayLimit < 256) BaseDataStreamMarshaller.WriteShort(arrayLimit, dataOut);
{ // max value of unsigned byte }
dataOut.Write((byte) 0xC0); dataOut.Write(data, 0, arrayLimit);
dataOut.Write((byte) arrayLimit); Clear();
} }
else
{ public void Unmarshal(BinaryReader dataIn)
dataOut.Write((byte) 0xE0); {
BaseDataStreamMarshaller.WriteShort(arrayLimit, dataOut); arrayLimit = (short)(BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF);
} if ( arrayLimit == 0xC0 ) {
arrayLimit = (short)(BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF);
dataOut.Write(data, 0, arrayLimit); } else if( arrayLimit == 0x80 ) {
Clear(); arrayLimit = BaseDataStreamMarshaller.ReadShort(dataIn);
} }
if( data.Length < arrayLimit ) {
public void Unmarshal(BinaryReader dataIn) data = new byte[arrayLimit];
{ }
arrayLimit = BaseDataStreamMarshaller.ReadByte(dataIn);
if ((arrayLimit & 0xE0) != 0) dataIn.Read(data, 0, arrayLimit);
{ Clear();
arrayLimit = BaseDataStreamMarshaller.ReadShort(dataIn); }
}
else if ((arrayLimit & 0xC0) != 0) public void Clear()
{ {
arrayLimit = (short) (BaseDataStreamMarshaller.ReadByte(dataIn) & 0xFF); arrayPos = 0;
} bytePos = 0;
if (data.Length < arrayLimit) }
{
data = new byte[arrayLimit]; public int MarshalledSize()
} {
dataIn.Read(data, 0, arrayLimit); if( arrayLimit < 64 ) {
Clear(); return 1+arrayLimit;
} } else if (arrayLimit < 256) {
return 2+arrayLimit;
public void Clear() } else {
{ return 3+arrayLimit;
arrayPos = 0; }
bytePos = 0;
} }
}
public int MarshalledSize() }
{
if (arrayLimit < 64)
{
return 1 + arrayLimit;
}
else
{
return 2 + 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"> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion> <ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B409B247-A311-42A9-B68D-76054D63DD7D}</ProjectGuid> <ProjectGuid>{B409B247-A311-42A9-B68D-76054D63DD7D}</ProjectGuid>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<RootNamespace>tests</RootNamespace> <RootNamespace>tests</RootNamespace>
<AssemblyName>tests</AssemblyName> <AssemblyName>tests</AssemblyName>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<StartupObject/> <StartupObject/>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks> <AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>false</DebugSymbols> <DebugSymbols>false</DebugSymbols>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks> <AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="nunit.core"/> <Reference Include="nunit.core"/>
<Reference Include="nunit.framework"/> <Reference Include="nunit.framework"/>
<Reference Include="System"/> <Reference Include="System"/>
<Reference Include="System.Data"/> <Reference Include="System.Data"/>
<Reference Include="System.Xml"/> <Reference Include="System.Xml"/>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets"/> <Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets"/>
<ItemGroup> <ItemGroup>
<Compile Include="OpenWire.Client\AsyncConsumeTest.cs"/> <Compile Include="OpenWire.Client\AsyncConsumeTest.cs"/>
<Compile Include="OpenWire.Client\BadConsumeTest.cs"/> <Compile Include="OpenWire.Client\BadConsumeTest.cs"/>
<Compile Include="OpenWire.Client\BytesMessageTest.cs"/> <Compile Include="OpenWire.Client\BytesMessageTest.cs"/>
<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\EndianTest.cs"/> <Compile Include="OpenWire.Client\Core\BooleanStreamTest.cs"/>
<Compile Include="OpenWire.Client\JMSPropertyTest.cs"/> <Compile Include="OpenWire.Client\EndianTest.cs"/>
<Compile Include="OpenWire.Client\MapMessageTest.cs"/> <Compile Include="OpenWire.Client\JMSPropertyTest.cs"/>
<Compile Include="OpenWire.Client\TestMain.cs"/> <Compile Include="OpenWire.Client\MapMessageTest.cs"/>
<Compile Include="OpenWire.Client\TestSupport.cs"/> <Compile Include="OpenWire.Client\TestMain.cs"/>
<Compile Include="OpenWire.Client\TransactionTest.cs"/> <Compile Include="OpenWire.Client\TestSupport.cs"/>
</ItemGroup> <Compile Include="OpenWire.Client\TransactionTest.cs"/>
<ItemGroup> </ItemGroup>
<ProjectReference Include="../src/src.csproj"> <ItemGroup>
<Name>src</Name> <ProjectReference Include="../src/src.csproj">
<Project>{F7BA9EF7-ADF7-40EF-9A9E-206649DBB10C}</Project> <Name>src</Name>
</ProjectReference> <Project>{F7BA9EF7-ADF7-40EF-9A9E-206649DBB10C}</Project>
</ItemGroup> </ProjectReference>
</Project> </ItemGroup>
</Project>