lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aro...@apache.org
Subject svn commit: r835484 - in /incubator/lucene.net/trunk/C#/src: Lucene.Net/Util/IndexableBinaryStringTools.cs Test/Util/TestIndexableBinaryStringTools.cs
Date Thu, 12 Nov 2009 18:25:29 GMT
Author: aroush
Date: Thu Nov 12 18:25:29 2009
New Revision: 835484

URL: http://svn.apache.org/viewvc?rev=835484&view=rev
Log:
Fixed NUnit test-cases for: TestIndexableBinaryStringTools

Modified:
    incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/IndexableBinaryStringTools.cs
    incubator/lucene.net/trunk/C#/src/Test/Util/TestIndexableBinaryStringTools.cs

Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/IndexableBinaryStringTools.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Util/IndexableBinaryStringTools.cs?rev=835484&r1=835483&r2=835484&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/IndexableBinaryStringTools.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/IndexableBinaryStringTools.cs Thu Nov
12 18:25:29 2009
@@ -68,19 +68,9 @@
 		/// <returns> The number of chars required to encode the given byte sequence
 		/// </returns>
 		/// <throws>  IllegalArgumentException If the given ByteBuffer is not backed by an
array </throws>
-		public static int GetEncodedLength(System.IO.MemoryStream original)
+		public static int GetEncodedLength(System.Collections.Generic.List<byte> original)
 		{
-			// if (original.hasArray()) // {{Aroush-2.9}}
-			{
-				// Use long for intermediaries to protect against overflow
-				// long length = (long) (original.limit() - original.arrayOffset()); // {{Aroush-2.9}}
-                long length = (long) (original.Capacity - original.Position);
-				return (int) ((length * 8L + 14L) / 15L) + 1;
-			}
-			// else // {{Aroush-2.9}}
-			// { // {{Aroush-2.9}}
-			// 	throw new System.ArgumentException("original argument must have a backing array");
// {{Aroush-2.9}}
-			// } // {{Aroush-2.9}}
+            return (original.Count == 0) ? 0 : ((original.Count * 8 + 14) / 15) + 1;
 		}
 		
 		/// <summary> Returns the number of bytes required to decode the given char sequence.
@@ -91,29 +81,19 @@
 		/// <returns> The number of bytes required to decode the given char sequence
 		/// </returns>
 		/// <throws>  IllegalArgumentException If the given CharBuffer is not backed by an
array </throws>
-        public static int GetDecodedLength(System.IO.MemoryStream encoded)
+        public static int GetDecodedLength(System.Collections.Generic.List<char> encoded)
 		{
-			// if (encoded.hasArray()) // {{Aroush-2.9}}
-			{
-				// int numChars = encoded.limit() - encoded.arrayOffset() - 1; // {{Aroush-2.9}}
-                long numChars = encoded.Capacity - encoded.Position - 1;
-				if (numChars <= 0)
-				{
-					return 0;
-				}
-				else
-				{
-                    // int numFullBytesInFinalChar = encoded.charAt(encoded.limit() - 1);
// {{Aroush-2.9}}
-                    byte[] buf = new byte[1];
-                    int numFullBytesInFinalChar = encoded.Read(buf, encoded.Capacity - 1,
1);
-					long numEncodedChars = numChars - 1;
-					return (int) ((numEncodedChars * 15 + 7) / 8 + numFullBytesInFinalChar);
-				}
-			}
-			// else // {{Aroush-2.9}}
-			// {
-			// 	throw new System.ArgumentException("encoded argument must have a backing array");
// {{Aroush-2.9}}
-			// } // {{Aroush-2.9}}
+            int numChars = encoded.Count - 1;
+            if (numChars <= 0)
+            {
+                return 0;
+            }
+            else
+            {
+                int numFullBytesInFinalChar = encoded[encoded.Count - 1];
+                int numEncodedChars = numChars - 1;
+                return ((numEncodedChars * 15 + 7) / 8 + numFullBytesInFinalChar);
+            }
 		}
 		
 		/// <summary> Encodes the input byte sequence into the output char sequence.  Before
@@ -129,77 +109,71 @@
 		/// <throws>  IllegalArgumentException If either the input or the output buffer </throws>
 		/// <summary>  is not backed by an array
 		/// </summary>
-		public static void  Encode(System.IO.MemoryStream input, System.IO.MemoryStream output)
+		public static void  Encode(System.Collections.Generic.List<byte> input, System.Collections.Generic.List<char>
output)
 		{
-			// if (input.hasArray() && output.hasArray()) // {{Aroush-2.9}}
-			{
-				// byte[] inputArray = input.array();   // {{Aroush-2.9}}
-                byte[] inputArray = input.GetBuffer();
-				// int inputOffset = input.arrayOffset(); // {{Aroush-2.9}}
-                long inputOffset = input.Position;
-				// int inputLength = input.limit() - inputOffset; // {{Aroush-2.9}}
-                long inputLength = input.Capacity - inputOffset;
-				// char[] outputArray = output.array(); // {{Aroush-2.9}}
-                byte[] outputArray = output.GetBuffer();
-				// int outputOffset = output.arrayOffset(); // {{Aroush-2.9}}
-                long outputOffset = output.Position;
-				int outputLength = GetEncodedLength(input);
-				// output.limit(outputOffset + outputLength); // Set output final pos + 1 // {{Aroush-2.9}}
-                output.Position = outputOffset + outputLength;
-				// output.position(0); // {{Aroush-2.9}}
-                output.Position = 0;
-				if (inputLength > 0)
-				{
-					long inputByteNum = inputOffset;
-					int caseNum = 0;
-					long outputCharNum = outputOffset;
-					CodingCase codingCase;
-					for (; inputByteNum + CODING_CASES[caseNum].numBytes <= inputLength; ++outputCharNum)
-					{
-						codingCase = CODING_CASES[caseNum];
-						if (2 == codingCase.numBytes)
-						{
-							outputArray[outputCharNum] = (byte) (((inputArray[inputByteNum] & 0xFF) <<
codingCase.initialShift) + ((SupportClass.Number.URShift((inputArray[inputByteNum + 1] &
0xFF), codingCase.finalShift)) & codingCase.finalMask) & (short) 0x7FFF);
-						}
-						else
-						{
-							// numBytes is 3
-							outputArray[outputCharNum] = (byte) (((inputArray[inputByteNum] & 0xFF) <<
codingCase.initialShift) + ((inputArray[inputByteNum + 1] & 0xFF) << codingCase.middleShift)
+ ((SupportClass.Number.URShift((inputArray[inputByteNum + 2] & 0xFF), codingCase.finalShift))
& codingCase.finalMask) & (short) 0x7FFF);
-						}
-						inputByteNum += codingCase.advanceBytes;
-						if (++caseNum == CODING_CASES.Length)
-						{
-							caseNum = 0;
-						}
-					}
-					// Produce final char (if any) and trailing count chars.
-					codingCase = CODING_CASES[caseNum];
-					
-					if (inputByteNum + 1 < inputLength)
-					{
-						// codingCase.numBytes must be 3
-						outputArray[outputCharNum++] = (byte) ((((inputArray[inputByteNum] & 0xFF) <<
codingCase.initialShift) + ((inputArray[inputByteNum + 1] & 0xFF) << codingCase.middleShift))
& (short) 0x7FFF);
-						// Add trailing char containing the number of full bytes in final char
-						outputArray[outputCharNum++] = (byte) 1;
-					}
-					else if (inputByteNum < inputLength)
-					{
-						outputArray[outputCharNum++] = (byte) (((inputArray[inputByteNum] & 0xFF) <<
codingCase.initialShift) & (short) 0x7FFF);
-						// Add trailing char containing the number of full bytes in final char
-						outputArray[outputCharNum++] = caseNum == 0?(byte) 1:(byte) 0;
-					}
-					else
-					{
-						// No left over bits - last char is completely filled.
-						// Add trailing char containing the number of full bytes in final char
-						outputArray[outputCharNum++] = (byte) 1;
-					}
-				}
-			}
-            // else // {{Aroush-2.9}}
-			// {
-			// 	throw new System.ArgumentException("Arguments must have backing arrays"); // {{Aroush-2.9}}
-			// }
+            int outputLength = GetEncodedLength(input);
+            // only adjust capacity if needed
+            if (output.Capacity < outputLength)
+            {
+                output.Capacity = outputLength;
+            }
+
+            // ensure the buffer we are writing into is occupied with nulls
+            if (output.Count < outputLength)
+            {
+                for (int i = output.Count; i < outputLength; i++)
+                {
+                    output.Add(Char.MinValue);
+                }
+            }
+
+            if (input.Count > 0)
+            {
+                int inputByteNum = 0;
+                int caseNum = 0;
+                int outputCharNum = 0;
+                CodingCase codingCase;
+                for (; inputByteNum + CODING_CASES[caseNum].numBytes <= input.Count; ++outputCharNum)
+                {
+                    codingCase = CODING_CASES[caseNum];
+                    if (2 == codingCase.numBytes)
+                    {
+                        output[outputCharNum] = (char)(((input[inputByteNum] & 0xFF)
<< codingCase.initialShift) + ((SupportClass.Number.URShift((input[inputByteNum + 1]
& 0xFF), codingCase.finalShift)) & codingCase.finalMask) & (short)0x7FFF);
+                    }
+                    else
+                    {
+                        // numBytes is 3
+                        output[outputCharNum] = (char)(((input[inputByteNum] & 0xFF)
<< codingCase.initialShift) + ((input[inputByteNum + 1] & 0xFF) << codingCase.middleShift)
+ ((SupportClass.Number.URShift((input[inputByteNum + 2] & 0xFF), codingCase.finalShift))
& codingCase.finalMask) & (short)0x7FFF);
+                    }
+                    inputByteNum += codingCase.advanceBytes;
+                    if (++caseNum == CODING_CASES.Length)
+                    {
+                        caseNum = 0;
+                    }
+                }
+                // Produce final char (if any) and trailing count chars.
+                codingCase = CODING_CASES[caseNum];
+                
+                if (inputByteNum + 1 < input.Count)
+                {
+                    // codingCase.numBytes must be 3
+                    output[outputCharNum++] = (char) ((((input[inputByteNum] & 0xFF)
<< codingCase.initialShift) + ((input[inputByteNum + 1] & 0xFF) << codingCase.middleShift))
& (short) 0x7FFF);
+                    // Add trailing char containing the number of full bytes in final char
+                    output[outputCharNum++] = (char) 1;
+                }
+                else if (inputByteNum < input.Count)
+                {
+                    output[outputCharNum++] = (char) (((input[inputByteNum] & 0xFF) <<
codingCase.initialShift) & (short) 0x7FFF);
+                    // Add trailing char containing the number of full bytes in final char
+                    output[outputCharNum++] = caseNum == 0?(char) 1:(char) 0;
+                }
+                else
+                {
+                    // No left over bits - last char is completely filled.
+                    // Add trailing char containing the number of full bytes in final char
+                    output[outputCharNum++] = (char) 1;
+                }
+            }
 		}
 		
 		/// <summary> Decodes the input char sequence into the output byte sequence.  Before
@@ -215,90 +189,85 @@
 		/// <throws>  IllegalArgumentException If either the input or the output buffer </throws>
 		/// <summary>  is not backed by an array
 		/// </summary>
-		public static void  Decode(System.IO.MemoryStream input, System.IO.MemoryStream output)
+		public static void Decode(System.Collections.Generic.List<char> input, System.Collections.Generic.List<byte>
output)
 		{
-			// if (input.hasArray() && output.hasArray()) // {{Aroush-2.9}}
-			{
-				// int numInputChars = input.limit() - input.arrayOffset() - 1; // {{Aroush-2.9}}
-                long numInputChars = input.Capacity - input.Position - 1;
-				int numOutputBytes = GetDecodedLength(input);
-				// output.limit(numOutputBytes + output.arrayOffset()); // Set output final pos + 1 //
{{Aroush-2.9}}
-                output.Capacity = (int) (numOutputBytes + output.Position);
-				// output.position(0); // {{Aroush-2.9}}
-                output.Position = 0;
-				// byte[] outputArray = output.array(); // {{Aroush-2.9}}
-                byte[] outputArray = output.GetBuffer();
-				// char[] inputArray = input.array(); // {{Aroush-2.9}}
-                byte[] inputArray = (byte[]) input.GetBuffer(); 
-				if (numOutputBytes > 0)
-				{
-					int caseNum = 0;
-					// int outputByteNum = output.arrayOffset(); // {{Aroush-2.9}}
-                    long outputByteNum = output.Position;
-					// int inputCharNum = input.arrayOffset(); // {{Aroush-2.9}}
-                    long inputCharNum = input.Position;
-					short inputChar;
-					CodingCase codingCase;
-					for (; inputCharNum < numInputChars - 1; ++inputCharNum)
-					{
-						codingCase = CODING_CASES[caseNum];
-						inputChar = (short) inputArray[inputCharNum];
-						if (2 == codingCase.numBytes)
-						{
-							if (0 == caseNum)
-							{
-								outputArray[outputByteNum] = (byte) (SupportClass.Number.URShift(inputChar, codingCase.initialShift));
-							}
-							else
-							{
-								outputArray[outputByteNum] = (byte) (outputArray[outputByteNum] + (byte) (SupportClass.Number.URShift(inputChar,
codingCase.initialShift)));
-							}
-							outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.finalMask) <<
codingCase.finalShift);
-						}
-						else
-						{
-							// numBytes is 3
-							outputArray[outputByteNum] = (byte) (outputArray[outputByteNum] + (byte) (SupportClass.Number.URShift(inputChar,
codingCase.initialShift)));
-							outputArray[outputByteNum + 1] = (byte) (SupportClass.Number.URShift((inputChar &
codingCase.middleMask), codingCase.middleShift));
-							outputArray[outputByteNum + 2] = (byte) ((inputChar & codingCase.finalMask) <<
codingCase.finalShift);
-						}
-						outputByteNum += codingCase.advanceBytes;
-						if (++caseNum == CODING_CASES.Length)
-						{
-							caseNum = 0;
-						}
-					}
-					// Handle final char
-					inputChar = (short) inputArray[inputCharNum];
-					codingCase = CODING_CASES[caseNum];
-					if (0 == caseNum)
-					{
-						outputArray[outputByteNum] = 0;
-					}
-					outputArray[outputByteNum] = (byte) (outputArray[outputByteNum] + (byte) (SupportClass.Number.URShift(inputChar,
codingCase.initialShift)));
-					long bytesLeft = numOutputBytes - outputByteNum;
-					if (bytesLeft > 1)
-					{
-						if (2 == codingCase.numBytes)
-						{
-							outputArray[outputByteNum + 1] = (byte) (SupportClass.Number.URShift((inputChar &
codingCase.finalMask), codingCase.finalShift));
-						}
-						else
-						{
-							// numBytes is 3
-							outputArray[outputByteNum + 1] = (byte) (SupportClass.Number.URShift((inputChar &
codingCase.middleMask), codingCase.middleShift));
-							if (bytesLeft > 2)
-							{
-								outputArray[outputByteNum + 2] = (byte) ((inputChar & codingCase.finalMask) <<
codingCase.finalShift);
-							}
-						}
-					}
-				}
-			}
-            // else // {{Aroush-2.9}}
-			// {
-			// 	throw new System.ArgumentException("Arguments must have backing arrays"); // {{Aroush-2.9}}
-			// }
+            int numOutputBytes = GetDecodedLength(input);
+            if (output.Capacity < numOutputBytes)
+            {
+                output.Capacity = numOutputBytes;
+            }
+
+            // ensure the buffer we are writing into is occupied with nulls
+            if (output.Count < numOutputBytes)
+            {
+                for (int i = output.Count; i < numOutputBytes; i++)
+                {
+                    output.Add(Byte.MinValue);
+                }
+            }
+
+            if (input.Count > 0)
+            {
+                int caseNum = 0;
+                int outputByteNum = 0;
+                int inputCharNum = 0;
+                short inputChar;
+                CodingCase codingCase;
+                for (; inputCharNum < input.Count - 2; ++inputCharNum)
+                {
+                    codingCase = CODING_CASES[caseNum];
+                    inputChar = (short) input[inputCharNum];
+                    if (2 == codingCase.numBytes)
+                    {
+                        if (0 == caseNum)
+                        {
+                            output[outputByteNum] = (byte) (SupportClass.Number.URShift(inputChar,
codingCase.initialShift));
+                        }
+                        else
+                        {
+                            output[outputByteNum] = (byte) (output[outputByteNum] + (byte)
(SupportClass.Number.URShift(inputChar, codingCase.initialShift)));
+                        }
+                        output[outputByteNum + 1] = (byte) ((inputChar & codingCase.finalMask)
<< codingCase.finalShift);
+                    }
+                    else
+                    {
+                        // numBytes is 3
+                        output[outputByteNum] = (byte) (output[outputByteNum] + (byte) (SupportClass.Number.URShift(inputChar,
codingCase.initialShift)));
+                        output[outputByteNum + 1] = (byte) (SupportClass.Number.URShift((inputChar
& codingCase.middleMask), codingCase.middleShift));
+                        output[outputByteNum + 2] = (byte) ((inputChar & codingCase.finalMask)
<< codingCase.finalShift);
+                    }
+                    outputByteNum += codingCase.advanceBytes;
+                    if (++caseNum == CODING_CASES.Length)
+                    {
+                        caseNum = 0;
+                    }
+                }
+                // Handle final char
+                inputChar = (short) input[inputCharNum];
+                codingCase = CODING_CASES[caseNum];
+                if (0 == caseNum)
+                {
+                    output[outputByteNum] = 0;
+                }
+                output[outputByteNum] = (byte) (output[outputByteNum] + (byte) (SupportClass.Number.URShift(inputChar,
codingCase.initialShift)));
+                long bytesLeft = numOutputBytes - outputByteNum;
+                if (bytesLeft > 1)
+                {
+                    if (2 == codingCase.numBytes)
+                    {
+                        output[outputByteNum + 1] = (byte) (SupportClass.Number.URShift((inputChar
& codingCase.finalMask), codingCase.finalShift));
+                    }
+                    else
+                    {
+                        // numBytes is 3
+                        output[outputByteNum + 1] = (byte) (SupportClass.Number.URShift((inputChar
& codingCase.middleMask), codingCase.middleShift));
+                        if (bytesLeft > 2)
+                        {
+                            output[outputByteNum + 2] = (byte) ((inputChar & codingCase.finalMask)
<< codingCase.finalShift);
+                        }
+                    }
+                }
+            }
 		}
 		
 		/// <summary> Decodes the given char sequence, which must have been encoded by
@@ -314,10 +283,10 @@
 		/// <throws>  IllegalArgumentException If the input buffer is not backed by an </throws>
 		/// <summary>  array
 		/// </summary>
-		public static System.IO.MemoryStream Decode(System.IO.MemoryStream input)
+        public static System.Collections.Generic.List<byte> Decode(System.Collections.Generic.List<char>
input)
 		{
-			byte[] outputArray = new byte[GetDecodedLength(input)];
-			System.IO.MemoryStream output = new System.IO.MemoryStream(outputArray);
+            System.Collections.Generic.List<byte> output = 
+                new System.Collections.Generic.List<byte>(new byte[GetDecodedLength(input)]);
 			Decode(input, output);
 			return output;
 		}
@@ -333,10 +302,10 @@
 		/// <throws>  IllegalArgumentException If the input buffer is not backed by an </throws>
 		/// <summary>  array
 		/// </summary>
-		public static System.IO.MemoryStream Encode(System.IO.MemoryStream input)
+		public static System.Collections.Generic.List<char> Encode(System.Collections.Generic.List<byte>
input)
 		{
-			byte[] outputArray = new byte[GetEncodedLength(input)];
-			System.IO.MemoryStream output = new System.IO.MemoryStream(outputArray);
+            System.Collections.Generic.List<char> output = 
+                new System.Collections.Generic.List<char>(new char[GetEncodedLength(input)]);
 			Encode(input, output);
 			return output;
 		}

Modified: incubator/lucene.net/trunk/C#/src/Test/Util/TestIndexableBinaryStringTools.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestIndexableBinaryStringTools.cs?rev=835484&r1=835483&r2=835484&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Util/TestIndexableBinaryStringTools.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Util/TestIndexableBinaryStringTools.cs Thu Nov
12 18:25:29 2009
@@ -16,6 +16,7 @@
  */
 
 using System;
+using System.Collections.Generic;
 
 using NUnit.Framework;
 
@@ -31,77 +32,76 @@
         [Test]
 		public virtual void  TestSingleBinaryRoundTrip()
 		{
-            System.Diagnostics.Debug.Fail("Port issue:", "byteBuf = ByteBuffer.wrap(newBuffer)");
// {{Aroush-2.9}}
+            byte[] binary = new byte[] {(byte)0x23, (byte)0x98, (byte)0x13, (byte)0xE4, (byte)0x76,
(byte)0x41, (byte)0xB2, (byte)0xC9, (byte)0x7F, (byte)0x0A, (byte)0xA6, (byte)0xD8 };
+
+            List<byte> binaryBuf = new List<byte>(binary);
+            List<char> encoded = IndexableBinaryStringTools.Encode(binaryBuf);
+            List<byte> decoded = IndexableBinaryStringTools.Decode(encoded);
+            Assert.AreEqual(binaryBuf, decoded, "Round trip decode/decode returned different
results:" + System.Environment.NewLine + "original: " + BinaryDump(binaryBuf) + System.Environment.NewLine
+ " encoded: " + CharArrayDump(encoded) + System.Environment.NewLine + " decoded: " + BinaryDump(decoded));
 
-			//byte[] binary = new byte[]{(byte) 0x23, (byte) SupportClass.Identity(0x98), (byte) 0x13,
(byte) SupportClass.Identity(0xE4), (byte) 0x76, (byte) 0x41, (byte) SupportClass.Identity(0xB2),
(byte) SupportClass.Identity(0xC9), (byte) 0x7F, (byte) 0x0A, (byte) SupportClass.Identity(0xA6),
(byte) SupportClass.Identity(0xD8)};
-			//
-			//System.IO.MemoryStream binaryBuf = ByteBuffer.wrap(binary);
-			//System.IO.MemoryStream encoded = IndexableBinaryStringTools.Encode(binaryBuf);
-			//ByteBuffer decoded = IndexableBinaryStringTools.Decode(encoded);
-            //Assert.AreEqual(binaryBuf, decoded, "Round trip decode/decode returned different
results:" + System.Environment.NewLine + "original: " + BinaryDump(binaryBuf) + System.Environment.NewLine
+ " encoded: " + CharArrayDump(encoded) + System.Environment.NewLine + " decoded: " + BinaryDump(decoded));
 		}
 		
         [Test]
 		public virtual void  TestEncodedSortability()
 		{
-			System.Random random = NewRandom();
-			byte[] originalArray1 = new byte[MAX_RANDOM_BINARY_LENGTH];
-            System.IO.MemoryStream originalBuf1 = new System.IO.MemoryStream(originalArray1);
-			byte[] originalString1 = new byte[MAX_RANDOM_BINARY_LENGTH];    // {{Aroush-2.9}} this
is char[] in Java
-			System.IO.MemoryStream originalStringBuf1 = new System.IO.MemoryStream(originalString1);
-			byte[] encoded1 = new byte[IndexableBinaryStringTools.GetEncodedLength(originalBuf1)];
 // {{Aroush-2.9}} this is char[] in Java
-			System.IO.MemoryStream encodedBuf1 = new System.IO.MemoryStream(encoded1);
-			byte[] original2 = new byte[MAX_RANDOM_BINARY_LENGTH];
-			System.IO.MemoryStream originalBuf2 = new System.IO.MemoryStream(original2);
-			byte[] originalString2 = new byte[MAX_RANDOM_BINARY_LENGTH];    // {{Aroush-2.9}} this
is char[] in Java
-			System.IO.MemoryStream originalStringBuf2 = new System.IO.MemoryStream(originalString2);
-			byte[] encoded2 = new byte[IndexableBinaryStringTools.GetEncodedLength(originalBuf2)];
 // {{Aroush-2.9}} this is char[] in Java
-			System.IO.MemoryStream encodedBuf2 = new System.IO.MemoryStream(encoded2);
-			for (int testNum = 0; testNum < NUM_RANDOM_TESTS; ++testNum)
-			{
-				int numBytes1 = random.Next(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1
-				originalBuf1.Capacity = numBytes1;
-				originalStringBuf1.Capacity = numBytes1;
-				
-				for (int byteNum = 0; byteNum < numBytes1; ++byteNum)
-				{
-					int randomInt = random.Next(0x100);
-					originalArray1[byteNum] = (byte) randomInt;
-					originalString1[byteNum] = (byte) randomInt;
-				}
-				
-				int numBytes2 = random.Next(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1
-				originalBuf2.Capacity = numBytes2;
-				originalStringBuf2.Capacity = numBytes2;
-				for (int byteNum = 0; byteNum < numBytes2; ++byteNum)
-				{
-					int randomInt = random.Next(0x100);
-					original2[byteNum] = (byte) randomInt;
-					originalString2[byteNum] = (byte) randomInt;
-				}
-                System.Diagnostics.Debug.Fail("Port issue:", "int originalComparison = originalStringBuf1.CompareTo(originalStringBuf2);");
// {{Aroush-2.9}}
-                int originalComparison = 0;
-				//int originalComparison = originalStringBuf1.CompareTo(originalStringBuf2);
-				//originalComparison = originalComparison < 0?- 1:(originalComparison > 0?1:0);
-				
-				IndexableBinaryStringTools.Encode(originalBuf1, encodedBuf1);
-				IndexableBinaryStringTools.Encode(originalBuf2, encodedBuf2);
-
-                System.Diagnostics.Debug.Fail("Port issue:", "int encodedComparison = encodedBuf1.CompareTo(encodedBuf2);");
// {{Aroush-2.9}}
-                int encodedComparison = 0;
-				//int encodedComparison = encodedBuf1.CompareTo(encodedBuf2);
-				//encodedComparison = encodedComparison < 0?- 1:(encodedComparison > 0?1:0);
-				
-				Assert.AreEqual(originalComparison, encodedComparison, "Test #" + (testNum + 1) + ":
Original bytes and encoded chars compare differently:" + System.Environment.NewLine + " binary
1: " + BinaryDump(originalBuf1) + System.Environment.NewLine + " binary 2: " + BinaryDump(originalBuf2)
+ System.Environment.NewLine + "encoded 1: " + CharArrayDump(encodedBuf1) + System.Environment.NewLine
+ "encoded 2: " + CharArrayDump(encodedBuf2) + System.Environment.NewLine);
-			}
+            System.Random random = NewRandom();
+            byte[] originalArray1 = new byte[MAX_RANDOM_BINARY_LENGTH];
+            List<byte> originalBuf1 = new List<byte>(originalArray1);
+            char[] originalString1 = new char[MAX_RANDOM_BINARY_LENGTH];
+            List<char> originalStringBuf1 = new List<char>(originalString1);
+            char[] encoded1 = new char[IndexableBinaryStringTools.GetEncodedLength(originalBuf1)];
+            List<char> encodedBuf1 = new List<char>(encoded1);
+            byte[] original2 = new byte[MAX_RANDOM_BINARY_LENGTH];
+            List<byte> originalBuf2 = new List<byte>(original2);
+            char[] originalString2 = new char[MAX_RANDOM_BINARY_LENGTH];
+            List<char> originalStringBuf2 = new List<char>(originalString2);
+            char[] encoded2 = new char[IndexableBinaryStringTools.GetEncodedLength(originalBuf2)];
+            List<char> encodedBuf2 = new List<char>(encoded2);
+            for (int testNum = 0; testNum < NUM_RANDOM_TESTS; ++testNum)
+            {
+                int numBytes1 = random.Next(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min ==
1
+                
+                for (int byteNum = 0; byteNum < numBytes1; ++byteNum)
+                {
+                    int randomInt = random.Next(0x100);
+                    originalArray1[byteNum] = (byte) randomInt;
+                    originalString1[byteNum] = (char) randomInt;
+                }
+                
+                int numBytes2 = random.Next(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min ==
1
+                for (int byteNum = 0; byteNum < numBytes2; ++byteNum)
+                {
+                    int randomInt = random.Next(0x100);
+                    original2[byteNum] = (byte) randomInt;
+                    originalString2[byteNum] = (char) randomInt;
+                }
+                // put in strings to compare ordinals
+                string orgStrBuf1 = new string(originalStringBuf1.ToArray());
+                string orgStrBuf2 = new string(originalStringBuf2.ToArray());
+
+                int originalComparison = string.CompareOrdinal(orgStrBuf1, orgStrBuf2);
+                originalComparison = originalComparison < 0 ? -1 : (originalComparison
> 0 ? 1 : 0);
+                
+                IndexableBinaryStringTools.Encode(originalBuf1, encodedBuf1);
+                IndexableBinaryStringTools.Encode(originalBuf2, encodedBuf2);
+
+                // put in strings to compare ordinals
+                string encBuf1 = new string(encodedBuf1.ToArray());
+                string encBuf2 = new string(encodedBuf2.ToArray());
+
+                int encodedComparison = string.CompareOrdinal(encBuf1, encBuf2);
+                encodedComparison = encodedComparison < 0?- 1:(encodedComparison >
0?1:0);
+                
+                Assert.AreEqual(originalComparison, encodedComparison, "Test #" + (testNum
+ 1) + ": Original bytes and encoded chars compare differently:" + System.Environment.NewLine
+ " binary 1: " + BinaryDump(originalBuf1) + System.Environment.NewLine + " binary 2: " +
BinaryDump(originalBuf2) + System.Environment.NewLine + "encoded 1: " + CharArrayDump(encodedBuf1)
+ System.Environment.NewLine + "encoded 2: " + CharArrayDump(encodedBuf2) + System.Environment.NewLine);
+            }
 		}
 		
-        [Test]
+		[Test]
 		public virtual void  TestEmptyInput()
 		{
 			byte[] binary = new byte[0];
-            System.IO.MemoryStream encoded = IndexableBinaryStringTools.Encode((new System.IO.MemoryStream(binary)));
-			System.IO.MemoryStream decoded = IndexableBinaryStringTools.Decode(encoded);
+            List<char> encoded = IndexableBinaryStringTools.Encode(new List<byte>(binary));
+			List<byte> decoded = IndexableBinaryStringTools.Decode(encoded);
 			Assert.IsNotNull(decoded, "decode() returned null");
 			Assert.AreEqual(decoded.Capacity, 0, "decoded empty input was not empty");
 		}
@@ -110,10 +110,10 @@
 		public virtual void  TestAllNullInput()
 		{
 			byte[] binary = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
-			System.IO.MemoryStream binaryBuf = new System.IO.MemoryStream(binary);
-			System.IO.MemoryStream encoded = IndexableBinaryStringTools.Encode(binaryBuf);
+            List<byte> binaryBuf = new List<byte>(binary);
+            List<char> encoded = IndexableBinaryStringTools.Encode(binaryBuf);
 			Assert.IsNotNull(encoded, "encode() returned null");
-			System.IO.MemoryStream decodedBuf = IndexableBinaryStringTools.Decode(encoded);
+            List<byte> decodedBuf = IndexableBinaryStringTools.Decode(encoded);
 			Assert.IsNotNull(decodedBuf, "decode() returned null");
 			Assert.AreEqual(binaryBuf, decodedBuf, "Round trip decode/decode returned different results:"
+ System.Environment.NewLine + "  original: " + BinaryDump(binaryBuf) + System.Environment.NewLine
+ "decodedBuf: " + BinaryDump(decodedBuf));
 		}
@@ -123,15 +123,14 @@
 		{
 			System.Random random = NewRandom();
 			byte[] binary = new byte[MAX_RANDOM_BINARY_LENGTH];
-			System.IO.MemoryStream binaryBuf = new System.IO.MemoryStream(binary);
-			byte[] encoded = new byte[IndexableBinaryStringTools.GetEncodedLength(binaryBuf)];  //
{{Aroush-2.9}} this is char[] in Java
-			System.IO.MemoryStream encodedBuf = new System.IO.MemoryStream(encoded);
+            List<byte> binaryBuf = new List<byte>(binary);
+			char[] encoded = new char[IndexableBinaryStringTools.GetEncodedLength(binaryBuf)];
+            List<char> encodedBuf = new List<char>(encoded);
 			byte[] decoded = new byte[MAX_RANDOM_BINARY_LENGTH];
-			System.IO.MemoryStream decodedBuf = new System.IO.MemoryStream(decoded);
+            List<byte> decodedBuf = new List<byte>(decoded);
 			for (int testNum = 0; testNum < NUM_RANDOM_TESTS; ++testNum)
 			{
 				int numBytes = random.Next(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1
-				binaryBuf.Capacity = numBytes;
 				for (int byteNum = 0; byteNum < numBytes; ++byteNum)
 				{
 					binary[byteNum] = (byte) random.Next(0x100);
@@ -142,20 +141,18 @@
 			}
 		}
 		
-		public virtual System.String BinaryDump(System.IO.MemoryStream binaryBuf)
+		public virtual System.String BinaryDump(List<byte> binaryBuf)
 		{
 			System.Text.StringBuilder buf = new System.Text.StringBuilder();
-			long numBytes = binaryBuf.Capacity - binaryBuf.Position;
-			byte[] binary = binaryBuf.ToArray();
-			for (int byteNum = 0; byteNum < numBytes; ++byteNum)
+			for (int byteNum = 0; byteNum < binaryBuf.Count; ++byteNum)
 			{
-				System.String hex = System.Convert.ToString((int) binary[byteNum] & 0xFF, 16);
+				System.String hex = System.Convert.ToString((int) binaryBuf[byteNum] & 0xFF, 16);
 				if (hex.Length == 1)
 				{
 					buf.Append('0');
 				}
 				buf.Append(hex.ToUpper());
-				if (byteNum < numBytes - 1)
+				if (byteNum < binaryBuf.Count - 1)
 				{
 					buf.Append(' ');
 				}
@@ -163,20 +160,18 @@
 			return buf.ToString();
 		}
 		
-		public virtual System.String CharArrayDump(System.IO.MemoryStream charBuf)
+		public virtual System.String CharArrayDump(List<char> charBuf)
 		{
 			System.Text.StringBuilder buf = new System.Text.StringBuilder();
-			long numBytes = charBuf.Capacity - charBuf.Position;
-			byte[] charArray = charBuf.GetBuffer(); // {{Aroush-2.9}} this is char[] in Java
-			for (int charNum = 0; charNum < numBytes; ++charNum)
+			for (int charNum = 0; charNum < charBuf.Count; ++charNum)
 			{
-				System.String hex = System.Convert.ToString((int) charArray[charNum], 16);
+				System.String hex = System.Convert.ToString((int) charBuf[charNum], 16);
 				for (int digit = 0; digit < 4 - hex.Length; ++digit)
 				{
 					buf.Append('0');
 				}
 				buf.Append(hex.ToUpper());
-				if (charNum < numBytes - 1)
+				if (charNum < charBuf.Count - 1)
 				{
 					buf.Append(' ');
 				}



Mime
View raw message