From b858350da4c4e7bc672e596d7d79089f1ffa166c Mon Sep 17 00:00:00 2001 From: "Hiram R. Chirino" Date: Sat, 4 Mar 2006 06:23:01 +0000 Subject: [PATCH] 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 --- .../src/OpenWire.Client/Core/BooleanStream.cs | 258 +++++++++--------- .../OpenWire.Client/Core/BooleanStreamTest.cs | 147 ++++++++++ openwire-dotnet/tests/tests.csproj | 115 ++++---- 3 files changed, 328 insertions(+), 192 deletions(-) create mode 100644 openwire-dotnet/tests/OpenWire.Client/Core/BooleanStreamTest.cs diff --git a/openwire-dotnet/src/OpenWire.Client/Core/BooleanStream.cs b/openwire-dotnet/src/OpenWire.Client/Core/BooleanStream.cs index 0f2f50c368..b441afa5b6 100755 --- a/openwire-dotnet/src/OpenWire.Client/Core/BooleanStream.cs +++ b/openwire-dotnet/src/OpenWire.Client/Core/BooleanStream.cs @@ -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 -{ - /// - /// Represents a stream of boolean flags - /// - 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 +{ + /// + /// Represents a stream of boolean flags + /// + 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; + } + + } + } +} diff --git a/openwire-dotnet/tests/OpenWire.Client/Core/BooleanStreamTest.cs b/openwire-dotnet/tests/OpenWire.Client/Core/BooleanStreamTest.cs new file mode 100644 index 0000000000..145c9df22a --- /dev/null +++ b/openwire-dotnet/tests/OpenWire.Client/Core/BooleanStreamTest.cs @@ -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; + } + } +} + diff --git a/openwire-dotnet/tests/tests.csproj b/openwire-dotnet/tests/tests.csproj index 57916ca56d..abdf55927c 100644 --- a/openwire-dotnet/tests/tests.csproj +++ b/openwire-dotnet/tests/tests.csproj @@ -1,57 +1,58 @@ - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {B409B247-A311-42A9-B68D-76054D63DD7D} - Library - tests - tests - 4 - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - false - - - false - true - bin\Release\ - TRACE - false - - - - - - - - - - - - - - - - - - - - - - - - - - src - {F7BA9EF7-ADF7-40EF-9A9E-206649DBB10C} - - - + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {B409B247-A311-42A9-B68D-76054D63DD7D} + Library + tests + tests + 4 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + false + + + false + true + bin\Release\ + TRACE + false + + + + + + + + + + + + + + + + + + + + + + + + + + + src + {F7BA9EF7-ADF7-40EF-9A9E-206649DBB10C} + + +