Beispiel #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  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));
        }
        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));
            }
        }