Example #1
0
        private static void VerifyGetBytes(Encoding encoding, string source, int index, int count, byte[] bytes, int byteIndex, byte[] expectedBytes)
        {
            byte[] originalBytes = (byte[])bytes.Clone();

            if (index == 0 && count == source.Length)
            {
                // Use GetBytes(string)
                byte[] stringResultBasic = encoding.GetBytes(source);
                VerifyGetBytes(stringResultBasic, 0, stringResultBasic.Length, originalBytes, expectedBytes);

                // Use GetBytes(char[])
                byte[] charArrayResultBasic = encoding.GetBytes(source.ToCharArray());
                VerifyGetBytes(charArrayResultBasic, 0, charArrayResultBasic.Length, originalBytes, expectedBytes);
            }

            // Use GetBytes(char[], int, int)
            byte[] charArrayResultAdvanced = encoding.GetBytes(source.ToCharArray(), index, count);
            VerifyGetBytes(charArrayResultAdvanced, 0, charArrayResultAdvanced.Length, originalBytes, expectedBytes);

            // Use GetBytes(string, int, int, byte[], int)
            byte[] stringBytes     = (byte[])bytes.Clone();
            int    stringByteCount = encoding.GetBytes(source, index, count, stringBytes, byteIndex);

            VerifyGetBytes(stringBytes, byteIndex, stringByteCount, originalBytes, expectedBytes);
            EncodingHelpers.AreEqual(expectedBytes.Length, stringByteCount);

            // Use GetBytes(char[], int, int, byte[], int)
            byte[] charArrayBytes     = (byte[])bytes.Clone();
            int    charArrayByteCount = encoding.GetBytes(source.ToCharArray(), index, count, charArrayBytes, byteIndex);

            VerifyGetBytes(charArrayBytes, byteIndex, charArrayByteCount, originalBytes, expectedBytes);
            EncodingHelpers.AreEqual(expectedBytes.Length, charArrayByteCount);
        }
Example #2
0
 private static void GetString(Encoding encoding, byte[] bytes, int index, int count, string expected)
 {
     if (index == 0 && count == bytes.Length)
     {
         // Use GetString(byte[])
         EncodingHelpers.AreEqual(expected, encoding.GetString(bytes));
     }
     // Use GetString(byte[], int, int)
     EncodingHelpers.AreEqual(expected, encoding.GetString(bytes, index, count));
 }
Example #3
0
        private static void GetByteCount(Encoding encoding, string chars, int index, int count, int expected)
        {
            char[] charArray = chars.ToCharArray();
            if (index == 0 && count == chars.Length)
            {
                // Use GetByteCount(string) or GetByteCount(char[])
                EncodingHelpers.AreEqual(expected, encoding.GetByteCount(chars));
                EncodingHelpers.AreEqual(expected, encoding.GetByteCount(charArray));
            }

            // Use GetByteCount(char[], int, int)
            EncodingHelpers.AreEqual(expected, encoding.GetByteCount(charArray, index, count));
        }
Example #4
0
        private static void VerifyGetChars(Encoding encoding, byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, char[] expectedChars)
        {
            char[] originalChars = (char[])chars.Clone();

            // Use GetChars(byte[])
            if (byteIndex == 0 && byteCount == bytes.Length)
            {
                char[] resultBasic = encoding.GetChars(bytes);
                VerifyGetChars(resultBasic, 0, resultBasic.Length, originalChars, expectedChars);
            }

            // Use GetChars(byte[], int, int)
            char[] resultAdvanced = encoding.GetChars(bytes, byteIndex, byteCount);
            VerifyGetChars(resultAdvanced, 0, resultAdvanced.Length, originalChars, expectedChars);

            // Use GetChars(byte[], int, int, char[], int)
            char[] byteChars = (char[])chars.Clone();
            int    charCount = encoding.GetChars(bytes, byteIndex, byteCount, byteChars, charIndex);

            VerifyGetChars(byteChars, charIndex, charCount, originalChars, expectedChars);
            EncodingHelpers.AreEqual(expectedChars.Length, charCount);
        }