Exemple #1
0
        public virtual void TestRandomBinaryRoundTrip()
        {
            sbyte[] binary  = new sbyte[MAX_RANDOM_BINARY_LENGTH];
            char[]  encoded = new char[MAX_RANDOM_BINARY_LENGTH * 10];
            sbyte[] decoded = new sbyte[MAX_RANDOM_BINARY_LENGTH];
            for (int testNum = 0; testNum < NUM_RANDOM_TESTS; ++testNum)
            {
                int numBytes = Random().Next(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1

                for (int byteNum = 0; byteNum < numBytes; ++byteNum)
                {
                    binary[byteNum] = (sbyte)Random().Next(0x100);
                }

                int encodedLen = IndexableBinaryStringTools.GetEncodedLength(binary, 0, numBytes);
                if (encoded.Length < encodedLen)
                {
                    encoded = new char[ArrayUtil.Oversize(encodedLen, RamUsageEstimator.NUM_BYTES_CHAR)];
                }
                IndexableBinaryStringTools.Encode(binary, 0, numBytes, encoded, 0, encodedLen);

                int decodedLen = IndexableBinaryStringTools.GetDecodedLength(encoded, 0, encodedLen);
                IndexableBinaryStringTools.Decode(encoded, 0, encodedLen, decoded, 0, decodedLen);

                Assert.AreEqual(BinaryDump(binary, numBytes), BinaryDump(decoded, decodedLen), "Test #" + (testNum + 1) + ": Round trip decode/decode returned different results:" + "\n  original: " + BinaryDump(binary, numBytes) + "\nencodedBuf: " + CharArrayDump(encoded, encodedLen) + "\ndecodedBuf: " + BinaryDump(decoded, decodedLen));
            }
        }
        public virtual void  TestEncodedSortability()
        {
            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);
            }
        }
        public virtual void  TestEmptyInput()
        {
            byte[]      binary  = new byte[0];
            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");
        }
        public virtual void  TestSingleBinaryRoundTrip()
        {
            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));
        }
Exemple #5
0
        public virtual void TestEncodedSortability()
        {
            sbyte[] originalArray1  = new sbyte[MAX_RANDOM_BINARY_LENGTH];
            char[]  originalString1 = new char[MAX_RANDOM_BINARY_LENGTH];
            char[]  encoded1        = new char[MAX_RANDOM_BINARY_LENGTH * 10];
            sbyte[] original2       = new sbyte[MAX_RANDOM_BINARY_LENGTH];
            char[]  originalString2 = new char[MAX_RANDOM_BINARY_LENGTH];
            char[]  encoded2        = new char[MAX_RANDOM_BINARY_LENGTH * 10];

            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]  = (sbyte)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]       = (sbyte)randomInt;
                    originalString2[byteNum] = (char)randomInt;
                }
                int originalComparison = String.CompareOrdinal(new string(originalString1, 0, numBytes1),
                                                               new string(originalString2, 0, numBytes2));

                originalComparison = originalComparison < 0 ? -1 : originalComparison > 0 ? 1 : 0;

                int encodedLen1 = IndexableBinaryStringTools.GetEncodedLength(originalArray1, 0, numBytes1);
                if (encodedLen1 > encoded1.Length)
                {
                    encoded1 = new char[ArrayUtil.Oversize(encodedLen1, RamUsageEstimator.NUM_BYTES_CHAR)];
                }
                IndexableBinaryStringTools.Encode(originalArray1, 0, numBytes1, encoded1, 0, encodedLen1);

                int encodedLen2 = IndexableBinaryStringTools.GetEncodedLength(original2, 0, numBytes2);
                if (encodedLen2 > encoded2.Length)
                {
                    encoded2 = new char[ArrayUtil.Oversize(encodedLen2, RamUsageEstimator.NUM_BYTES_CHAR)];
                }
                IndexableBinaryStringTools.Encode(original2, 0, numBytes2, encoded2, 0, encodedLen2);

                int encodedComparison = String.CompareOrdinal(new string(encoded1, 0, encodedLen1),
                                                              new string(encoded2, 0, encodedLen2));

                encodedComparison = encodedComparison < 0 ? -1 : encodedComparison > 0 ? 1 : 0;

                Assert.AreEqual(originalComparison, encodedComparison, "Test #" + (testNum + 1) + ": Original bytes and encoded chars compare differently:" + " \nbinary 1: " + BinaryDump(originalArray1, numBytes1) + " \nbinary 2: " + BinaryDump(original2, numBytes2) + "\nencoded 1: " + CharArrayDump(encoded1, encodedLen1) + "\nencoded 2: " + CharArrayDump(encoded2, encodedLen2));
            }
        }
        public virtual void  TestAllNullInput()
        {
            byte[]      binary    = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            List <byte> binaryBuf = new List <byte>(binary);
            List <char> encoded   = IndexableBinaryStringTools.Encode(binaryBuf);

            Assert.IsNotNull(encoded, "encode() returned null");
            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));
        }
        public virtual void TestSingleBinaryRoundTrip()
        {
            sbyte[] binary = new sbyte[] { (sbyte)0x23, unchecked((sbyte)0x98), (sbyte)0x13, unchecked((sbyte)0xE4), (sbyte)0x76, (sbyte)0x41, unchecked((sbyte)0xB2), unchecked((sbyte)0xC9), (sbyte)0x7F, (sbyte)0x0A, unchecked((sbyte)0xA6), unchecked((sbyte)0xD8) };

            int encodedLen = IndexableBinaryStringTools.GetEncodedLength(binary, 0, binary.Length);
            char[] encoded = new char[encodedLen];
            IndexableBinaryStringTools.Encode(binary, 0, binary.Length, encoded, 0, encoded.Length);

            int decodedLen = IndexableBinaryStringTools.GetDecodedLength(encoded, 0, encoded.Length);
            sbyte[] decoded = new sbyte[decodedLen];
            IndexableBinaryStringTools.Decode(encoded, 0, encoded.Length, decoded, 0, decoded.Length);

            Assert.AreEqual(BinaryDump(binary, binary.Length), BinaryDump(decoded, decoded.Length), "Round trip decode/decode returned different results:\noriginal: " + BinaryDump(binary, binary.Length) + "\n encoded: " + CharArrayDump(encoded, encoded.Length) + "\n decoded: " + BinaryDump(decoded, decoded.Length));
        }
        public virtual void TestAllNullInput()
        {
            sbyte[] binary = new sbyte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            int encodedLen = IndexableBinaryStringTools.GetEncodedLength(binary, 0, binary.Length);
            char[] encoded = new char[encodedLen];
            IndexableBinaryStringTools.Encode(binary, 0, binary.Length, encoded, 0, encoded.Length);

            int decodedLen = IndexableBinaryStringTools.GetDecodedLength(encoded, 0, encoded.Length);
            sbyte[] decoded = new sbyte[decodedLen];
            IndexableBinaryStringTools.Decode(encoded, 0, encoded.Length, decoded, 0, decoded.Length);

            Assert.AreEqual(BinaryDump(binary, binary.Length), BinaryDump(decoded, decoded.Length), "Round trip decode/decode returned different results:" + "\n  original: " + BinaryDump(binary, binary.Length) + "\ndecodedBuf: " + BinaryDump(decoded, decoded.Length));
        }
        public virtual void TestEmptyInput()
        {
            sbyte[] binary = new sbyte[0];

            int encodedLen = IndexableBinaryStringTools.GetEncodedLength(binary, 0, binary.Length);
            char[] encoded = new char[encodedLen];
            IndexableBinaryStringTools.Encode(binary, 0, binary.Length, encoded, 0, encoded.Length);

            int decodedLen = IndexableBinaryStringTools.GetDecodedLength(encoded, 0, encoded.Length);
            sbyte[] decoded = new sbyte[decodedLen];
            IndexableBinaryStringTools.Decode(encoded, 0, encoded.Length, decoded, 0, decoded.Length);

            Assert.AreEqual(decoded.Length, 0, "decoded empty input was not empty");
        }
        public virtual void  TestRandomBinaryRoundTrip()
        {
            System.Random random    = NewRandom();
            byte[]        binary    = new byte[MAX_RANDOM_BINARY_LENGTH];
            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];
            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
                for (int byteNum = 0; byteNum < numBytes; ++byteNum)
                {
                    binary[byteNum] = (byte)random.Next(0x100);
                }
                IndexableBinaryStringTools.Encode(binaryBuf, encodedBuf);
                IndexableBinaryStringTools.Decode(encodedBuf, decodedBuf);
                Assert.AreEqual(binaryBuf, decodedBuf, "Test #" + (testNum + 1) + ": Round trip decode/decode returned different results:" + System.Environment.NewLine + "  original: " + BinaryDump(binaryBuf) + System.Environment.NewLine + "encodedBuf: " + CharArrayDump(encodedBuf) + System.Environment.NewLine + "decodedBuf: " + BinaryDump(decodedBuf));
            }
        }