public static IEnumerable <object[]> Encode_Advanced_TestData() { string optionalChars1 = "!\"#$%&*;<=>@[]^_`{|}"; byte[] optionalFalseBytes = new byte[] { 43, 65, 67, 69, 65, 73, 103, 65, 106, 65, 67, 81, 65, 74, 81, 65, 109, 65, 67, 111, 65, 79, 119, 65, 56, 65, 68, 48, 65, 80, 103, 66, 65, 65, 70, 115, 65, 88, 81, 66, 101, 65, 70, 56, 65, 89, 65, 66, 55, 65, 72, 119, 65, 102, 81, 45 }; byte[] optionalTrueBytes = new byte[] { 33, 34, 35, 36, 37, 38, 42, 59, 60, 61, 62, 64, 91, 93, 94, 95, 96, 123, 124, 125 }; yield return(new object[] { false, optionalChars1, 0, optionalChars1.Length, optionalFalseBytes }); yield return(new object[] { true, optionalChars1, 0, optionalChars1.Length, optionalTrueBytes }); yield return(new object[] { false, EncodingHelpers.ToString(new int[] { 0x0023, 0x0025, 0x03A0, 0x03A3 }), 1, 2, new byte[] { 43, 65, 67, 85, 68, 111, 65, 45 } }); yield return(new object[] { true, EncodingHelpers.ToString(new int[] { 0x0023, 0x0025, 0x03A0, 0x03A3 }), 1, 2, new byte[] { 37, 43, 65, 54, 65, 45 } }); yield return(new object[] { false, "!}", 0, 2, new byte[] { 0x2B, 0x41, 0x43, 0x45, 0x41, 0x66, 0x51, 0x2D } }); yield return(new object[] { false, "!}", 1, 1, new byte[] { 0x2B, 0x41, 0x48, 0x30, 0x2D } }); yield return(new object[] { false, EncodingHelpers.ToString(new int[] { 0x0041, 0x0021, 0x007D, 0x0009, 0x0E59, 0x05D1 }), 0, 6, new byte[] { 0x41, 0x2B, 0x41, 0x43, 0x45, 0x41, 0x66, 0x51, 0x2D, 0x09, 0x2B, 0x44, 0x6C, 0x6B, 0x46, 0x30, 0x51, 0x2D } }); }
public static IEnumerable <object[]> Encode_TestData() { // All ASCII chars for (int i = 0; i <= byte.MaxValue; i++) { char c = (char)i; yield return(new object[] { "a" + c + "b", 0, 3, new byte[] { 97, 0, (byte)c, 0, 98, 0 } }); yield return(new object[] { "a" + c + "b", 1, 1, new byte[] { (byte)c, 0 } }); yield return(new object[] { "a" + c + "b", 2, 1, new byte[] { 98, 0 } }); } // Unicode yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0061, 0x1234, 0x0062 }), 0, 3, new byte[] { 97, 0, 52, 18, 98, 0 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0061, 0x1234, 0x0062 }), 1, 1, new byte[] { 52, 18 } }); // Surrogate pairs yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 0, 2, new byte[] { 0, 216, 0, 220 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0061, 0xD800, 0xDC00, 0x0062 }), 0, 4, new byte[] { 97, 0, 0, 216, 0, 220, 98, 0 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0xFFFD, 0xFEB7 }), 0, 4, new byte[] { 0x00, 0xD8, 0x00, 0xDC, 0xFD, 0xFF, 0xB7, 0xFE } }); // Mixture of ASCII and Unicode yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0046, 0x006F, 0x006F, 0x0042, 0x0041, 0x0400, 0x0052 }), 0, 7, new byte[] { 70, 0, 111, 0, 111, 0, 66, 0, 65, 0, 0, 4, 82, 0 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00C0, 0x006E, 0x0069, 0x006D, 0x0061, 0x0300, 0x006C }), 0, 7, new byte[] { 192, 0, 110, 0, 105, 0, 109, 0, 97, 0, 0, 3, 108, 0 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0xD803, 0xDD75, 0x0054, 0x0065, 0x0073, 0x0074 }), 0, 10, new byte[] { 84, 0, 101, 0, 115, 0, 116, 0, 3, 216, 117, 221, 84, 0, 101, 0, 115, 0, 116, 0 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD803, 0xDD75, 0xD803, 0xDD75, 0xD803, 0xDD75 }), 0, 6, new byte[] { 3, 216, 117, 221, 3, 216, 117, 221, 3, 216, 117, 221 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0130 }), 0, 1, new byte[] { 48, 1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x007A, 0x0061, 0x0306, 0x01FD, 0x03B2 }), 0, 5, new byte[] { 122, 0, 97, 0, 6, 3, 253, 1, 178, 3 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x007A, 0x0061, 0x0306, 0x01FD, 0x03B2, 0xD8FF, 0xDCFF }), 0, 7, new byte[] { 122, 0, 97, 0, 6, 3, 253, 1, 178, 3, 255, 216, 255, 220 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x007A, 0x0061, 0x0306, 0x01FD, 0x03B2, 0xD8FF, 0xDCFF }), 4, 3, new byte[] { 178, 3, 255, 216, 255, 220 } }); // High BMP non-chars yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFD }), 0, 1, new byte[] { 253, 255 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFE }), 0, 1, new byte[] { 254, 255 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFF }), 0, 1, new byte[] { 255, 255 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFF, 0xFFFE }), 0, 2, new byte[] { 0xFF, 0xFF, 0xFE, 0xFF } }); // Empty strings yield return(new object[] { string.Empty, 0, 0, new byte[0] }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0061, 0x1234, 0x0062 }), 3, 0, new byte[0] }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0061, 0x1234, 0x0062 }), 0, 0, new byte[0] }); }
public static void GetBytes_InvalidASCIIUnicode() { Encoding encoding = Encoding.UTF8; // Bytes does not have enough capacity to accomodate result string s = EncodingHelpers.ToString(new int[] { 0x0054, 0xD83D, 0xDE01, 0x0065, 0x0073, 0x0074 }); Assert.Throws <ArgumentException>(() => encoding.GetBytes(s, 0, 2, new byte[3], 0)); Assert.Throws <ArgumentException>(() => encoding.GetBytes(s, 0, 3, new byte[4], 0)); Assert.Throws <ArgumentException>(() => encoding.GetBytes(s, 0, 4, new byte[5], 0)); Assert.Throws <ArgumentException>(() => encoding.GetBytes(s, 0, 5, new byte[6], 0)); char[] c = s.ToCharArray(); Assert.Throws <ArgumentException>(() => encoding.GetBytes(c, 0, 2, new byte[3], 0)); Assert.Throws <ArgumentException>(() => encoding.GetBytes(c, 0, 3, new byte[4], 0)); Assert.Throws <ArgumentException>(() => encoding.GetBytes(c, 0, 4, new byte[5], 0)); Assert.Throws <ArgumentException>(() => encoding.GetBytes(c, 0, 5, new byte[6], 0)); }
public static void GetBytes_ValidASCIIUnicode() { Encoding encoding = Encoding.UTF8; // Bytes has enough capacity to accomodate result string s = EncodingHelpers.ToString(new int[] { 0x0054, 0xD83D, 0xDE01, 0x0065, 0x0073, 0x0074 }); Assert.AreEqual(4, encoding.GetBytes(s, 0, 2, new byte[4], 0)); Assert.AreEqual(5, encoding.GetBytes(s, 0, 3, new byte[5], 0)); Assert.AreEqual(6, encoding.GetBytes(s, 0, 4, new byte[6], 0)); Assert.AreEqual(7, encoding.GetBytes(s, 0, 5, new byte[7], 0)); char[] c = s.ToCharArray(); Assert.AreEqual(4, encoding.GetBytes(c, 0, 2, new byte[4], 0)); Assert.AreEqual(5, encoding.GetBytes(c, 0, 3, new byte[5], 0)); Assert.AreEqual(6, encoding.GetBytes(c, 0, 4, new byte[6], 0)); Assert.AreEqual(7, encoding.GetBytes(c, 0, 5, new byte[7], 0)); }
public static IEnumerable <object[]> Encode_InvalidChars_TestData() { byte[] unicodeReplacementBytes1 = new byte[] { 239, 191, 189 }; // Lone high surrogate yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800 }), 0, 1, unicodeReplacementBytes1 }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDD75 }), 0, 1, unicodeReplacementBytes1 }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDBFF }), 0, 1, unicodeReplacementBytes1 }); // Lone low surrogate yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00 }), 0, 1, unicodeReplacementBytes1 }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00 }), 0, 1, unicodeReplacementBytes1 }); // Surrogate pair out of range yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 0, 1, unicodeReplacementBytes1 }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 1, 1, unicodeReplacementBytes1 }); // Invalid surrogate pair yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0041, 0xD800, 0xE000 }), 0, 3, new byte[] { 0x41, 0xEF, 0xBF, 0xBD, 0xEE, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0x0041, 0xDC00 }), 0, 3, new byte[] { 0xEF, 0xBF, 0xBD, 0x41, 0xEF, 0xBF, 0xBD } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0x0041, 0x0042, 0x07FF, 0x0043, 0xDC00 }), 0, 6, new byte[] { 0xEF, 0xBF, 0xBD, 0x41, 0x42, 0xDF, 0xBF, 0x43, 0xEF, 0xBF, 0xBD } }); // Mixture of ASCII, valid Unicode and invalid unicode yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDD75, 0xDD75, 0xD803, 0xDD75, 0xDD75, 0xDD75, 0xDD75, 0xD803, 0xD803, 0xD803, 0xDD75, 0xDD75, 0xDD75, 0xDD75 }), 0, 14, new byte[] { 239, 191, 189, 239, 191, 189, 240, 144, 181, 181, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191, 189, 240, 144, 181, 181, 239, 191, 189, 239, 191, 189, 239, 191, 189 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0xD803, 0x0054, 0x0065, 0x0073, 0x0074 }), 0, 9, new byte[] { 84, 101, 115, 116, 239, 191, 189, 84, 101, 115, 116 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0xDD75, 0x0054, 0x0065, 0x0073, 0x0074 }), 0, 9, new byte[] { 84, 101, 115, 116, 239, 191, 189, 84, 101, 115, 116 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0x0054, 0x0065, 0x0073, 0x0074, 0xDD75 }), 0, 9, new byte[] { 84, 101, 115, 116, 84, 101, 115, 116, 239, 191, 189 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0x0054, 0x0065, 0x0073, 0x0074, 0xD803 }), 0, 9, new byte[] { 84, 101, 115, 116, 84, 101, 115, 116, 239, 191, 189 } }); byte[] unicodeReplacementBytes2 = new byte[] { 239, 191, 189, 239, 191, 189 }; yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xD800 }), 0, 2, unicodeReplacementBytes2 }); // High, high yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00, 0xD800 }), 0, 2, unicodeReplacementBytes2 }); // Low, high yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00, 0xDC00 }), 0, 2, unicodeReplacementBytes2 }); // Low, low }
public static IEnumerable <object[]> Encode_InvalidChars_TestData() { var list = new List <object[]>(); byte[] unicodeReplacementBytes1 = new byte[] { 253, 255, 0, 0 }; list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xD800 }), 0, 1, unicodeReplacementBytes1 }); // Lone high surrogate list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xDD75 }), 0, 1, unicodeReplacementBytes1 }); // Lone high surrogate list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xDC00 }), 0, 1, unicodeReplacementBytes1 }); // Lone low surrogate list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xD800, 0xDC00 }), 0, 1, unicodeReplacementBytes1 }); // Surrogate pair out of range list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xD800, 0xDC00 }), 1, 1, unicodeReplacementBytes1 }); // Surrogate pair out of range byte[] unicodeReplacementBytes2 = new byte[] { 253, 255, 0, 0, 253, 255, 0, 0 }; list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xD800, 0xD800 }), 0, 2, unicodeReplacementBytes2 }); // High, high list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xDC00, 0xD800 }), 0, 2, unicodeReplacementBytes2 }); // Low, high list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xDC00, 0xDC00 }), 0, 2, unicodeReplacementBytes2 }); // Low, low // Invalid first/second in surrogate pair list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xD800, 0x0041 }), 0, 2, new byte[] { 0xFD, 0xFF, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00 } }); list.Add(new object[] { EncodingHelpers.ToString(new[] { 0x0065, 0xDC00 }), 0, 2, new byte[] { 0x65, 0x00, 0x00, 0x00, 0xFD, 0xFF, 0x00, 0x00 } }); return(list); }
public static IEnumerable <object[]> Decode_InvalidBytes_TestData() { var list = new List <object[]>(); list.Add(new object[] { new byte[] { 123 }, 0, 1, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 123, 123 }, 0, 2, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 123, 123, 123 }, 0, 3, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 123, 123, 123, 123 }, 1, 3, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 97, 0, 0, 0, 0 }, 0, 5, "a" + EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 0xFF, 0xDB, 0x00, 0x00, 0xFF, 0xDF, 0x00, 0x00 }, 0, 8, EncodingHelpers.ToString(new[] { 0xFFFD, 0xFFFD }) }); list.Add(new object[] { new byte[] { 0xFF, 0xDB, 0x00, 0x00, 0xFF, 0xDF, 0x00, 0x00 }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 0xFF, 0xDB, 0x00, 0x00, 0xFF, 0xDF, 0x00, 0x00 }, 4, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 0x00, 0xD8, 0x00, 0x00, 0x00, 0xDC, 0x00, 0x00 }, 0, 8, EncodingHelpers.ToString(new[] { 0xFFFD, 0xFFFD }) }); list.Add(new object[] { new byte[] { 0xFF, 0xDB, 0x00, 0x00, 0xFD, 0xFF, 0x00, 0x00 }, 0, 8, EncodingHelpers.ToString(new[] { 0xFFFD, 0xFFFD }) }); list.Add(new object[] { new byte[] { 0x00, 0x80, 0x00, 0x00, 0xFF, 0xDF, 0x00, 0x00 }, 0, 8, EncodingHelpers.ToString(new[] { 0x8000, 0xFFFD }) }); list.Add(new object[] { new byte[] { 0xFF, 0xFF, 0x11, 0x00 }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 0x00, 0x00, 0x11, 0x00 }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 0x00, 0x00, 0x00, 0x01 }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 0xFF, 0xFF, 0x10, 0x01 }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 0x00, 0x00, 0x00, 0xFF }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); return(list); }
public static IEnumerable <object[]> Encode_TestData() { var list = new List <object[]>(); // All ASCII chars for (char c = char.MinValue; c <= 0xFF; c++) { list.Add(new object[] { c.ToString(), 0, 1, new byte[] { (byte)c, 0, 0, 0 } }); list.Add(new object[] { "a" + c.ToString() + "b", 1, 1, new byte[] { (byte)c, 0, 0, 0 } }); list.Add(new object[] { "a" + c.ToString() + "b", 2, 1, new byte[] { 98, 0, 0, 0 } }); list.Add(new object[] { "a" + c.ToString() + "b", 0, 3, new byte[] { 97, 0, 0, 0, (byte)c, 0, 0, 0, 98, 0, 0, 0 } }); } // Surrogate pairs list.Add(new object[] { EncodingHelpers.ToString(new [] { 0xD800, 0xDC00 }), 0, 2, new byte[] { 0, 0, 1, 0 } }); list.Add(new object[] { "a" + EncodingHelpers.ToString(new[] { 0xD800, 0xDC00 }) + "b", 0, 4, new byte[] { 97, 0, 0, 0, 0, 0, 1, 0, 98, 0, 0, 0 } }); list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xD800, 0xDFFF }), 0, 2, new byte[] { 0xFF, 0x03, 0x01, 0x00 } }); list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xDBFF, 0xDC00 }), 0, 2, new byte[] { 0x00, 0xFC, 0x10, 0x00 } }); list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xDBFF, 0xDFFF }), 0, 2, new byte[] { 0xFF, 0xFF, 0x10, 0x00 } }); // Mixture of ASCII and Unciode list.Add(new object[] { "FooBA" + EncodingHelpers.ToString(new[] { 0x0400 }) + "R", 0, 7, new byte[] { 70, 0, 0, 0, 111, 0, 0, 0, 111, 0, 0, 0, 66, 0, 0, 0, 65, 0, 0, 0, 0, 4, 0, 0, 82, 0, 0, 0 } }); // High BMP non-chars: U+FFFF, U+FFFE, U+FFFD list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xFFFD }), 0, 1, new byte[] { 253, 255, 0, 0 } }); list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xFFFE }), 0, 1, new byte[] { 254, 255, 0, 0 } }); list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xFFFF }), 0, 1, new byte[] { 255, 255, 0, 0 } }); list.Add(new object[] { EncodingHelpers.ToString(new[] { 0xFFFF, 0xFFFE, 0xFFFD }), 0, 3, new byte[] { 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0xFD, 0xFF, 0x00, 0x00 } }); // Empty strings list.Add(new object[] { "abc", 3, 0, new byte[0] }); list.Add(new object[] { "abc", 0, 0, new byte[0] }); list.Add(new object[] { string.Empty, 0, 0, new byte[0] }); return(list); }
public static IEnumerable <object[]> Decode_TestData() { var list = new List <object[]>(); // All ASCII chars for (char c = char.MinValue; c <= 0xFF; c++) { list.Add(new object[] { new byte[] { (byte)c, 0, 0, 0 }, 0, 4, c.ToString() }); list.Add(new object[] { new byte[] { 97, 0, 0, 0, (byte)c, 0, 0, 0, 98, 0, 0, 0 }, 4, 4, c.ToString() }); list.Add(new object[] { new byte[] { 97, 0, 0, 0, (byte)c, 0, 0, 0, 98, 0, 0, 0 }, 0, 12, "a" + c.ToString() + "b" }); } // Surrogate pairs list.Add(new object[] { new byte[] { 0, 0, 1, 0 }, 0, 4, EncodingHelpers.ToString(new[] { 0xD800, 0xDC00 }) }); list.Add(new object[] { new byte[] { 97, 0, 0, 0, 0, 0, 1, 0, 98, 0, 0, 0 }, 0, 12, "a" + EncodingHelpers.ToString(new[] { 0xD800, 0xDC00 }) + "b" }); list.Add(new object[] { new byte[] { 0x00, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0x10, 0x00 }, 0, 8, EncodingHelpers.ToString(new[] { 0xD800, 0xDC00, 0xDBFF, 0xDFFF }) }); // Mixture of ASCII and Unciode list.Add(new object[] { new byte[] { 70, 0, 0, 0, 111, 0, 0, 0, 111, 0, 0, 0, 66, 0, 0, 0, 65, 0, 0, 0, 0, 4, 0, 0, 82, 0, 0, 0 }, 0, 28, "FooBA" + EncodingHelpers.ToString(new[] { 0x0400 }) + "R" }); // U+FDD0 - U+FDEF list.Add(new object[] { new byte[] { 0xD0, 0xFD, 0x00, 0x00, 0xEF, 0xFD, 0x00, 0x00 }, 0, 8, EncodingHelpers.ToString(new[] { 0xFDD0, 0xFDEF }) }); list.Add(new object[] { new byte[] { 0xD0, 0xFD, 0x00, 0x00, 0xEF, 0xFD, 0x00, 0x00 }, 0, 8, EncodingHelpers.ToString(new[] { 0xFDD0, 0xFDEF }) }); // High BMP non-chars: U+FFFF, U+FFFE, U+FFFD list.Add(new object[] { new byte[] { 253, 255, 0, 0 }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFD }) }); list.Add(new object[] { new byte[] { 254, 255, 0, 0 }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFE }) }); list.Add(new object[] { new byte[] { 255, 255, 0, 0 }, 0, 4, EncodingHelpers.ToString(new[] { 0xFFFF }) }); list.Add(new object[] { new byte[] { 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0xFD, 0xFF, 0x00, 0x00 }, 0, 12, EncodingHelpers.ToString(new[] { 0xFFFF, 0xFFFE, 0xFFFD }) }); // Empty strings list.Add(new object[] { new byte[0], 0, 0, string.Empty }); list.Add(new object[] { new byte[10], 10, 0, string.Empty }); return(list); }
public static IEnumerable <object[]> Encode_InvalidChars_TestData() { byte[] unicodeReplacementBytes1 = new byte[] { 253, 255 }; yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800 }), 0, 1, unicodeReplacementBytes1 }); // Lone high surrogate yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00 }), 0, 1, unicodeReplacementBytes1 }); // Lone low surrogate // Surrogate pair out of range yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 0, 1, unicodeReplacementBytes1 }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 1, 1, unicodeReplacementBytes1 }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDBFF, 0xDFFF }), 0, 1, unicodeReplacementBytes1 }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDBFF, 0xDFFF }), 1, 1, unicodeReplacementBytes1 }); byte[] unicodeReplacementBytes2 = new byte[] { 253, 255, 253, 255 }; yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xD800 }), 0, 2, unicodeReplacementBytes2 }); // High, high yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00, 0xD800 }), 0, 2, unicodeReplacementBytes2 }); // Low, high yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00, 0xDC00 }), 0, 2, unicodeReplacementBytes2 }); // Low, low // Mixture of ASCII, valid Unicode and invalid Unicode yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0xD803, 0x0054, 0x0065, 0x0073, 0x0074 }), 0, 9, new byte[] { 84, 0, 101, 0, 115, 0, 116, 0, 253, 255, 84, 0, 101, 0, 115, 0, 116, 0 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0xDD75, 0x0054, 0x0065, 0x0073, 0x0074 }), 0, 9, new byte[] { 84, 0, 101, 0, 115, 0, 116, 0, 253, 255, 84, 0, 101, 0, 115, 0, 116, 0 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0x0054, 0x0065, 0x0073, 0x0074, 0xDD75 }), 0, 9, new byte[] { 84, 0, 101, 0, 115, 0, 116, 0, 84, 0, 101, 0, 115, 0, 116, 0, 253, 255 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0054, 0x0065, 0x0073, 0x0074, 0x0054, 0x0065, 0x0073, 0x0074, 0xD803 }), 0, 9, new byte[] { 84, 0, 101, 0, 115, 0, 116, 0, 84, 0, 101, 0, 115, 0, 116, 0, 253, 255 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDD75 }), 0, 1, new byte[] { 253, 255 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDD75, 0xDD75, 0xD803, 0xDD75, 0xDD75, 0xDD75, 0xDD75, 0xD803, 0xD803, 0xD803, 0xDD75, 0xDD75, 0xDD75, 0xDD75 }), 0, 14, new byte[] { 253, 255, 253, 255, 3, 216, 117, 221, 253, 255, 253, 255, 253, 255, 253, 255, 253, 255, 3, 216, 117, 221, 253, 255, 253, 255, 253, 255 } }); }
public static IEnumerable <object[]> Encode_TestData() { // All ASCII chars for (char c = char.MinValue; c <= 0x7F; c++) { yield return(new object[] { c.ToString(), 0, 1, new byte[] { (byte)c } }); yield return(new object[] { "a" + c.ToString() + "b", 1, 1, new byte[] { (byte)c } }); yield return(new object[] { "a" + c.ToString() + "b", 2, 1, new byte[] { 98 } }); yield return(new object[] { "a" + c.ToString() + "b", 0, 3, new byte[] { 97, (byte)c, 98 } }); } // Misc ASCII and Unicode strings yield return(new object[] { "FooBA" + EncodingHelpers.ToString(new int[] { 0x0400 }) + "R", 0, 7, new byte[] { 70, 111, 111, 66, 65, 208, 128, 82 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00C0 }) + "nima" + EncodingHelpers.ToString(new int[] { 0x0300 }) + "l", 0, 7, new byte[] { 195, 128, 110, 105, 109, 97, 204, 128, 108 } }); yield return(new object[] { "Test" + EncodingHelpers.ToString(new int[] { 0xD803, 0xDD75 }) + "Test", 0, 10, new byte[] { 84, 101, 115, 116, 240, 144, 181, 181, 84, 101, 115, 116 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0130 }), 0, 1, new byte[] { 196, 176 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD803, 0xDD75, 0xD803, 0xDD75, 0xD803, 0xDD75 }), 0, 6, new byte[] { 240, 144, 181, 181, 240, 144, 181, 181, 240, 144, 181, 181 } }); yield return(new object[] { "za" + EncodingHelpers.ToString(new int[] { 0x0306, 0x01FD, 0x03B2, 0xD8FF, 0xDCFF }), 0, 7, new byte[] { 122, 97, 204, 134, 199, 189, 206, 178, 241, 143, 179, 191 } }); yield return(new object[] { "za" + EncodingHelpers.ToString(new int[] { 0x0306, 0x01FD, 0x03B2, 0xD8FF, 0xDCFF }), 4, 3, new byte[] { 206, 178, 241, 143, 179, 191 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0023, 0x0025, 0x03A0, 0x03A3 }), 1, 2, new byte[] { 37, 206, 160 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00C5 }), 0, 1, new byte[] { 0xC3, 0x85 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0065, 0x0065, 0x00E1, 0x0065, 0x0065, 0x8000, 0x00E1, 0x0065, 0xD800, 0xDC00, 0x8000, 0x00E1, 0x0065, 0x0065, 0x0065 }), 0, 15, new byte[] { 0x65, 0x65, 0xC3, 0xA1, 0x65, 0x65, 0xE8, 0x80, 0x80, 0xC3, 0xA1, 0x65, 0xF0, 0x90, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xC3, 0xA1, 0x65, 0x65, 0x65 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00A4, 0x00D0 }) + "aR|{AnGe" + EncodingHelpers.ToString(new int[] { 0x00A3, 0x00A4 }), 0, 12, new byte[] { 0xC2, 0xA4, 0xC3, 0x90, 0x61, 0x52, 0x7C, 0x7B, 0x41, 0x6E, 0x47, 0x65, 0xC2, 0xA3, 0xC2, 0xA4 } }); // Control codes yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x001F, 0x0010, 0x0000, 0x0009 }), 0, 4, new byte[] { 0x1F, 0x10, 0x00, 0x09 } }); // Long ASCII strings yield return(new object[] { "eeeee", 0, 5, new byte[] { 0x65, 0x65, 0x65, 0x65, 0x65 } }); yield return(new object[] { "e" + EncodingHelpers.ToString(new int[] { 0x00E1 }) + "eee", 0, 5, new byte[] { 0x65, 0xC3, 0xA1, 0x65, 0x65, 0x65 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0065, 0x8000, 0x0065, 0x0065, 0x0065 }), 0, 5, new byte[] { 0x65, 0xE8, 0x80, 0x80, 0x65, 0x65, 0x65 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0065, 0xD800, 0xDC00, 0x0065, 0x0065, 0x0065 }), 0, 6, new byte[] { 0x65, 0xF0, 0x90, 0x80, 0x80, 0x65, 0x65, 0x65 } }); yield return(new object[] { "eeeeeeeeeeeeeee", 0, 15, new byte[] { 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65 } }); yield return(new object[] { "eeeeee" + EncodingHelpers.ToString(new int[] { 0x00E1 }) + "eeeeeeee", 0, 15, new byte[] { 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0xC3, 0xA1, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x8000, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065 }), 0, 15, new byte[] { 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0xE8, 0x80, 0x80, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0xD800, 0xDC00, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065, 0x0065 }), 0, 16, new byte[] { 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0xF0, 0x90, 0x80, 0x80, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65, 0x65 } }); // 2 bytes yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1 }), 0, 1, new byte[] { 0xC3, 0xA1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1 }), 0, 5, new byte[] { 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1, 0x0065, 0x00E1, 0x00E1, 0x00E1 }), 0, 5, new byte[] { 0xC3, 0xA1, 0x65, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1, 0x8000, 0x00E1, 0x00E1, 0x00E1 }), 0, 5, new byte[] { 0xC3, 0xA1, 0xE8, 0x80, 0x80, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1, 0xD800, 0xDC00, 0x00E1, 0x00E1, 0x00E1 }), 0, 6, new byte[] { 0xC3, 0xA1, 0xF0, 0x90, 0x80, 0x80, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1 }), 0, 15, new byte[] { 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1, 0x00E1, 0x0065, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1 }), 0, 15, new byte[] { 0xC3, 0xA1, 0xC3, 0xA1, 0x65, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1, 0x00E1, 0x8000, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1 }), 0, 15, new byte[] { 0xC3, 0xA1, 0xC3, 0xA1, 0xE8, 0x80, 0x80, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x00E1, 0x00E1, 0xD800, 0xDC00, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1, 0x00E1 }), 0, 16, new byte[] { 0xC3, 0xA1, 0xC3, 0xA1, 0xF0, 0x90, 0x80, 0x80, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1, 0xC3, 0xA1 } }); // 3 bytes yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000 }), 0, 1, new byte[] { 0xE8, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000, 0x8000, 0x8000, 0x8000 }), 0, 4, new byte[] { 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000, 0x0065, 0x8000, 0x8000 }), 0, 4, new byte[] { 0xE8, 0x80, 0x80, 0x65, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000, 0x00E1, 0x8000, 0x8000 }), 0, 4, new byte[] { 0xE8, 0x80, 0x80, 0xC3, 0xA1, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000, 0xD800, 0xDC00, 0x8000, 0x8000 }), 0, 5, new byte[] { 0xE8, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000 }), 0, 15, new byte[] { 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000, 0x8000, 0x8000, 0x0065, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000 }), 0, 15, new byte[] { 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0x65, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000, 0x8000, 0x8000, 0x00E1, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000 }), 0, 15, new byte[] { 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xC3, 0xA1, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x8000, 0x8000, 0x8000, 0xD800, 0xDC00, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000 }), 0, 16, new byte[] { 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xE8, 0x80, 0x80 } }); // Surrogate pairs yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 0, 2, new byte[] { 240, 144, 128, 128 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0061, 0xD800, 0xDC00, 0x0062 }), 0, 4, new byte[] { 97, 240, 144, 128, 128, 98 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDB80, 0xDC00 }), 0, 2, new byte[] { 0xF3, 0xB0, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDFFF }), 0, 2, new byte[] { 0xF0, 0x90, 0x8F, 0xBF } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDBFF, 0xDC00 }), 0, 2, new byte[] { 0xF4, 0x8F, 0xB0, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDBFF, 0xDFFF }), 0, 2, new byte[] { 0xF4, 0x8F, 0xBF, 0xBF } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00 }), 0, 6, new byte[] { 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0x0065, 0xD800, 0xDC00 }), 0, 5, new byte[] { 0xF0, 0x90, 0x80, 0x80, 0x65, 0xF0, 0x90, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0x00E1, 0xD800, 0xDC00 }), 0, 5, new byte[] { 0xF0, 0x90, 0x80, 0x80, 0xC3, 0xA1, 0xF0, 0x90, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0x8000, 0xD800, 0xDC00 }), 0, 5, new byte[] { 0xF0, 0x90, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00 }), 0, 16, new byte[] { 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0x0065, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00 }), 0, 15, new byte[] { 0xF0, 0x90, 0x80, 0x80, 0x65, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0x00E1, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00 }), 0, 15, new byte[] { 0xF0, 0x90, 0x80, 0x80, 0xC3, 0xA1, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00, 0x8000, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00, 0xD800, 0xDC00 }), 0, 15, new byte[] { 0xF0, 0x90, 0x80, 0x80, 0xE8, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80, 0xF0, 0x90, 0x80, 0x80 } }); // U+FDD0 - U+FDEF yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFDD0, 0xFDEF }), 0, 2, new byte[] { 0xEF, 0xB7, 0x90, 0xEF, 0xB7, 0xAF } }); // BOM yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFEFF, 0x0041 }), 0, 2, new byte[] { 0xEF, 0xBB, 0xBF, 0x41 } }); // High BMP non-chars yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFD }), 0, 1, new byte[] { 239, 191, 189 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFE }), 0, 1, new byte[] { 239, 191, 190 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFF }), 0, 1, new byte[] { 239, 191, 191 } }); // Empty strings yield return(new object[] { string.Empty, 0, 0, new byte[0] }); yield return(new object[] { "abc", 3, 0, new byte[0] }); yield return(new object[] { "abc", 0, 0, new byte[0] }); }
public static IEnumerable <object[]> Encode_Basic_TestData() { // ASCII yield return(new object[] { "\t\n\rXYZabc123", 0, 12, new byte[] { 9, 10, 13, 88, 89, 90, 97, 98, 99, 49, 50, 51 } }); yield return(new object[] { "A\t\r\n /z", 0, 7, new byte[] { 0x41, 0x09, 0x0D, 0x0A, 0x20, 0x2F, 0x7A } }); yield return(new object[] { "", 0, 1, new byte[] { 0x2B, 0x41, 0x41, 0x77, 0x2D } }); string chars2 = "UTF7 Encoding Example"; yield return(new object[] { chars2, 1, 2, new byte[] { 84, 70 } }); // Unicode yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x0E59, 0x05D1 }), 0, 2, new byte[] { 0x2B, 0x44, 0x6C, 0x6B, 0x46, 0x30, 0x51, 0x2D } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x212B }), 0, 1, new byte[] { 0x2B, 0x49, 0x53, 0x73, 0x2D } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0x03A0, 0x03A3 }), 0, 2, new byte[] { 43, 65, 54, 65, 68, 111, 119, 45 } }); // Surrogate pairs yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 0, 2, new byte[] { 43, 50, 65, 68, 99, 65, 65, 45 } }); yield return(new object[] { "a" + EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }) + "b", 0, 4, new byte[] { 97, 43, 50, 65, 68, 99, 65, 65, 45, 98 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDFFF }), 0, 2, new byte[] { 0x2B, 0x32, 0x41, 0x44, 0x66, 0x2F, 0x77, 0x2D } }); // Plus and minus yield return(new object[] { "+", 0, 1, new byte[] { 43, 45 } }); yield return(new object[] { "-", 0, 1, new byte[] { 0x2D } }); yield return(new object[] { "+-", 0, 2, new byte[] { 0x2B, 0x2D, 0x2D } }); // Empty strings yield return(new object[] { string.Empty, 0, 0, new byte[0] }); yield return(new object[] { "abc", 3, 0, new byte[0] }); yield return(new object[] { "abc", 0, 0, new byte[0] }); // Invalid Unicode yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800 }), 0, 1, new byte[] { 43, 50, 65, 65, 45 } }); // Lone high surrogate yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00 }), 0, 1, new byte[] { 43, 51, 65, 65, 45 } }); // Lone low surrogate yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDFFF }), 0, 1, new byte[] { 0x2B, 0x33, 0x2F, 0x38, 0x2D } }); // Lone low surrogate yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 0, 1, new byte[] { 43, 50, 65, 65, 45 } }); // Surrogate pair out of range yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xDC00 }), 1, 1, new byte[] { 43, 51, 65, 65, 45 } }); // Surrogate pair out of range yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xD800, 0xD800 }), 0, 2, new byte[] { 43, 50, 65, 68, 89, 65, 65, 45 } }); // High, high yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00, 0xD800 }), 0, 2, new byte[] { 43, 51, 65, 68, 89, 65, 65, 45 } }); // Low, high yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xDC00, 0xDC00 }), 0, 2, new byte[] { 43, 51, 65, 68, 99, 65, 65, 45 } }); // Low, low // High BMP non-chars yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFD }), 0, 1, new byte[] { 43, 47, 47, 48, 45 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFE }), 0, 1, new byte[] { 43, 47, 47, 52, 45 } }); yield return(new object[] { EncodingHelpers.ToString(new int[] { 0xFFFF }), 0, 1, new byte[] { 43, 47, 47, 56, 45 } }); }
public static IEnumerable <object[]> Decode_TestData() { // All ASCII chars for (int i = 0; i <= byte.MaxValue; i++) { char c = (char)i; if (c == 43) { continue; } yield return(new object[] { new byte[] { (byte)c }, 0, 1, c.ToString() }); yield return(new object[] { new byte[] { 97, (byte)c, 98 }, 1, 1, c.ToString() }); yield return(new object[] { new byte[] { 97, (byte)c, 98 }, 2, 1, "b" }); yield return(new object[] { new byte[] { 97, (byte)c, 98 }, 0, 3, "a" + c.ToString() + "b" }); } // Plus yield return(new object[] { new byte[] { (byte)'+' }, 0, 1, string.Empty }); yield return(new object[] { new byte[] { 43, 45 }, 0, 2, "+" }); yield return(new object[] { new byte[] { 43, 45, 65 }, 0, 3, "+A" }); yield return(new object[] { new byte[] { 0x2B, 0x2D, 0x2D }, 0, 3, "+-" }); // UTF7 code points can be represented in different sequences of bytes yield return(new object[] { new byte[] { 0x41, 0x09, 0x0D, 0x0A, 0x20, 0x2F, 0x7A }, 0, 7, "A\t\r\n /z" }); yield return(new object[] { new byte[] { 0x2B, 0x41, 0x45, 0x45, 0x41, 0x43, 0x51 }, 0, 7, "A\t" }); yield return(new object[] { new byte[] { 0x2B, 0x09 }, 0, 2, "\t" }); yield return(new object[] { new byte[] { 0x2B, 0x09, 0x2D }, 0, 3, "\t-" }); yield return(new object[] { new byte[] { 0x2B, 0x1E, 0x2D }, 0, 3, EncodingHelpers.ToString(new int[] { 0x001E }) + "-" }); yield return(new object[] { new byte[] { 0x2B, 0x7F, 0x1E, 0x2D }, 0, 4, EncodingHelpers.ToString(new int[] { 0x007F, 0x001E }) + "-" }); yield return(new object[] { new byte[] { 0x1E }, 0, 1, EncodingHelpers.ToString(new int[] { 0x001E }) }); yield return(new object[] { new byte[] { 0x21 }, 0, 1, "!" }); yield return(new object[] { new byte[] { 0x2B, 0x21, 0x2D }, 0, 3, "!-" }); yield return(new object[] { new byte[] { 0x2B, 0x21, 0x41, 0x41, 0x2D }, 0, 5, "!AA-" }); yield return(new object[] { new byte[] { 0x2B, 0x80, 0x81, 0x82, 0x2D }, 0, 5, EncodingHelpers.ToString(new int[] { 0x0080, 0x0081, 0x0082 }) + "-" }); yield return(new object[] { new byte[] { 0x2B, 0x80, 0x81, 0x82, 0x2D }, 0, 4, EncodingHelpers.ToString(new int[] { 0x0080, 0x0081, 0x0082 }) }); yield return(new object[] { new byte[] { 0x80, 0x81 }, 0, 2, EncodingHelpers.ToString(new int[] { 0x0080, 0x0081 }) }); yield return(new object[] { new byte[] { 0x2B, 0x80, 0x21, 0x80, 0x21, 0x1E, 0x2D }, 0, 7, EncodingHelpers.ToString(new int[] { 0x0080 }) + "!" + EncodingHelpers.ToString(new int[] { 0x0080 }) + "!-" }); // Exclamation mark yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x45, 0x41, 0x66, 0x51 }, 0, 7, "!}" }); yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x45, 0x41, 0x66, 0x51, 0x2D }, 0, 8, "!}" }); yield return(new object[] { new byte[] { 0x21, 0x7D }, 0, 2, "!}" }); yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x45, 0x41, 0x66, 0x51, 0x2D }, 1, 2, "AC" }); yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x45, 0x2D }, 0, 5, "!" }); yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x45, 0x2D }, 0, 2, string.Empty }); yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x45, 0x2D }, 0, 3, string.Empty }); yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x48, 0x2D }, 0, 5, "!" }); // Unicode yield return(new object[] { new byte[] { 0x2B, 0x44, 0x6C, 0x6B, 0x46, 0x30, 0x51, 0x2D }, 0, 8, EncodingHelpers.ToString(new int[] { 0x0E59, 0x05D1 }) }); yield return(new object[] { new byte[] { 0x2B, 0x44, 0x6C, 0x6B, 0x46, 0x30, 0x51 }, 0, 7, EncodingHelpers.ToString(new int[] { 0x0E59, 0x05D1 }) }); yield return(new object[] { new byte[] { 0x41, 0x2B, 0x41, 0x43, 0x45, 0x41, 0x66, 0x51, 0x2D, 0x09, 0x2B, 0x44, 0x6C, 0x6B, 0x46, 0x30, 0x51 }, 0, 17, EncodingHelpers.ToString(new int[] { 0x0041, 0x0021, 0x007D, 0x009, 0x0E59, 0x05D1 }) }); yield return(new object[] { new byte[] { 0x41, 0x2B, 0x41, 0x43, 0x45, 0x41, 0x66, 0x51, 0x2D, 0x09, 0x2B, 0x44, 0x6C, 0x6B, 0x46, 0x30, 0x51, 0x2D }, 0, 18, EncodingHelpers.ToString(new int[] { 0x0041, 0x0021, 0x007D, 0x0009, 0x0E59, 0x05D1 }) }); yield return(new object[] { new byte[] { 0x41, 0x21, 0x7D, 0x09, 0x2B, 0x44, 0x6C, 0x6B, 0x46, 0x30, 0x51, 0x2D }, 0, 12, EncodingHelpers.ToString(new int[] { 0x0041, 0x0021, 0x007D, 0x0009, 0x0E59, 0x05D1 }) }); yield return(new object[] { new byte[] { 0x41, 0x21, 0x7D, 0x09, 0x2B, 0x44, 0x6C, 0x6B, 0x46, 0x30, 0x51 }, 0, 11, EncodingHelpers.ToString(new int[] { 0x0041, 0x0021, 0x007D, 0x0009, 0x0E59, 0x05D1 }) }); //TODO: this test case is failed, need to fix it //yield return new object[] { new byte[] { 0x2B, 0x2B, 0x41, 0x41, 0x2D }, 0, 5, EncodingHelpers.ToString(new int[] { 0xF800 }) }; yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x48, 0x35, 0x41, 0x41, 0x2D }, 0, 8, EncodingHelpers.ToString(new int[] { 0x0021, 0xF900 }) }); yield return(new object[] { new byte[] { 0x2B, 0x41, 0x43, 0x48, 0x35, 0x41, 0x41, 0x2D }, 0, 4, "!" }); // Surrogate pairs yield return(new object[] { new byte[] { 0x2B, 0x32, 0x41, 0x44, 0x66, 0x2F, 0x77, 0x2D }, 0, 8, EncodingHelpers.ToString(new int[] { 0xD800, 0xDFFF }) }); // Invalid Unicode yield return(new object[] { new byte[] { 43, 50, 65, 65, 45 }, 0, 5, EncodingHelpers.ToString(new int[] { 0xD800 }) }); // Lone high surrogate yield return(new object[] { new byte[] { 43, 51, 65, 65, 45 }, 0, 5, EncodingHelpers.ToString(new int[] { 0xDC00 }) }); // Lone low surrogate yield return(new object[] { new byte[] { 0x2B, 0x33, 0x2F, 0x38, 0x2D }, 0, 5, EncodingHelpers.ToString(new int[] { 0xDFFF }) }); // Lone low surrogate yield return(new object[] { new byte[] { 43, 50, 65, 68, 89, 65, 65, 45 }, 0, 8, EncodingHelpers.ToString(new int[] { 0xD800, 0xD800 }) }); // High, high yield return(new object[] { new byte[] { 43, 51, 65, 68, 89, 65, 65, 45 }, 0, 8, EncodingHelpers.ToString(new int[] { 0xDC00, 0xD800 }) }); // Low, high yield return(new object[] { new byte[] { 43, 51, 65, 68, 99, 65, 65, 45 }, 0, 8, EncodingHelpers.ToString(new int[] { 0xDC00, 0xDC00 }) }); // Low, low // High BMP non-chars yield return(new object[] { new byte[] { 43, 47, 47, 48, 45 }, 0, 5, EncodingHelpers.ToString(new int[] { 0xFFFD }) }); yield return(new object[] { new byte[] { 43, 47, 47, 52, 45 }, 0, 5, EncodingHelpers.ToString(new int[] { 0xFFFE }) }); yield return(new object[] { new byte[] { 43, 47, 47, 56, 45 }, 0, 5, EncodingHelpers.ToString(new int[] { 0xFFFF }) }); // Empty strings yield return(new object[] { new byte[0], 0, 0, string.Empty }); yield return(new object[] { new byte[10], 0, 0, string.Empty }); yield return(new object[] { new byte[10], 10, 0, string.Empty }); }