[Category("NotDotNet")] // A1/B1 return 24 on MS public void GetByteCount2() { string s = "za\u0306\u01FD\u03B2\uD8FF\uDCFF"; UTF32Encoding le = new UTF32Encoding(false, true); Assert.AreEqual(28, le.GetByteCount(s), "#A1"); Assert.AreEqual(0, le.GetByteCount(string.Empty), "#A2"); UTF32Encoding be = new UTF32Encoding(true, true); Assert.AreEqual(28, be.GetByteCount(s), "#B1"); Assert.AreEqual(0, be.GetByteCount(string.Empty), "#B2"); }
[Category("NotDotNet")] // A1/B1 return 24 on MS public void GetByteCount1() { char [] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' }; UTF32Encoding le = new UTF32Encoding(false, true); Assert.AreEqual(28, le.GetByteCount(chars), "#A1"); Assert.AreEqual(0, le.GetByteCount(new char [0]), "#A2"); UTF32Encoding be = new UTF32Encoding(true, true); Assert.AreEqual(28, be.GetByteCount(chars), "#B1"); Assert.AreEqual(0, be.GetByteCount(new char [0]), "#B2"); }
public static void Main() { // Create a UTF32Encoding object with error detection enabled. var encExc = new UTF32Encoding(!BitConverter.IsLittleEndian, true, true); // Create a UTF32Encoding object with error detection disabled. var encRepl = new UTF32Encoding(!BitConverter.IsLittleEndian, true, false); // Create a byte arrays from a string, and add an invalid surrogate pair, as follows. // Latin Small Letter Z (U+007A) // Latin Small Letter A (U+0061) // Combining Breve (U+0306) // Latin Small Letter AE With Acute (U+01FD) // Greek Small Letter Beta (U+03B2) // a high-surrogate value (U+D8FF) // an invalid low surrogate (U+01FF) String s = "za\u0306\u01FD\u03B2"; // Encode the string using little-endian byte order. int index = encExc.GetByteCount(s); Byte[] bytes = new Byte[index + 4]; encExc.GetBytes(s, 0, s.Length, bytes, 0); bytes[index] = 0xFF; bytes[index + 1] = 0xD8; bytes[index + 2] = 0xFF; bytes[index + 3] = 0x01; // Decode the byte array with error detection. Console.WriteLine("Decoding with error detection:"); PrintDecodedString(bytes, encExc); // Decode the byte array without error detection. Console.WriteLine("Decoding without error detection:"); PrintDecodedString(bytes, encRepl); }
[Test] // GetByteCount (Char [], Int32, Int32) public void GetByteCount4() { char [] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' }; UTF32Encoding le = new UTF32Encoding(false, true); Assert.AreEqual(12, le.GetByteCount(chars, 0, 3), "#A1"); Assert.AreEqual(16, le.GetByteCount(chars, 2, 4), "#A2"); Assert.AreEqual(4, le.GetByteCount(chars, 4, 1), "#A3"); Assert.AreEqual(4, le.GetByteCount(chars, 6, 1), "#A4"); Assert.AreEqual(0, le.GetByteCount(chars, 6, 0), "#A5"); Assert.AreEqual(24, le.GetByteCount(chars, 0, 6), "#A6"); //Assert.AreEqual (24, le.GetByteCount (chars, 0, 7), "#A7"); UTF32Encoding be = new UTF32Encoding(true, true); Assert.AreEqual(12, be.GetByteCount(chars, 0, 3), "#B1"); Assert.AreEqual(16, be.GetByteCount(chars, 2, 4), "#B2"); Assert.AreEqual(4, be.GetByteCount(chars, 4, 1), "#B3"); Assert.AreEqual(4, be.GetByteCount(chars, 6, 1), "#B4"); Assert.AreEqual(0, be.GetByteCount(chars, 6, 0), "#B5"); Assert.AreEqual(24, be.GetByteCount(chars, 0, 6), "#B6"); //Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B7"); }
public static void Main() { // Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order. UTF32Encoding u32LE = new UTF32Encoding(false, true, true); UTF32Encoding u32BE = new UTF32Encoding(true, true, true); // Create byte arrays from the same string containing the following characters: // Latin Small Letter Z (U+007A) // Latin Small Letter A (U+0061) // Combining Breve (U+0306) // Latin Small Letter AE With Acute (U+01FD) // Greek Small Letter Beta (U+03B2) // a high-surrogate value (U+D8FF) // a low-surrogate value (U+DCFF) String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF"; // barrBE uses the big-endian byte order. byte[] barrBE = new byte[u32BE.GetByteCount(myStr)]; u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0); // barrLE uses the little-endian byte order. byte[] barrLE = new byte[u32LE.GetByteCount(myStr)]; u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0); // Get the char counts and decode the byte arrays. Console.Write("BE array with BE encoding : "); PrintCountsAndChars(barrBE, u32BE); Console.Write("LE array with LE encoding : "); PrintCountsAndChars(barrLE, u32LE); // Decode the byte arrays using an encoding with a different byte order. Console.Write("BE array with LE encoding : "); try { PrintCountsAndChars(barrBE, u32LE); } catch (System.ArgumentException e) { Console.WriteLine(e.Message); } Console.Write("LE array with BE encoding : "); try { PrintCountsAndChars(barrLE, u32BE); } catch (System.ArgumentException e) { Console.WriteLine(e.Message); } }
[Test] // GetByteCount (String) public void GetByteCount2_S_Null() { UTF32Encoding enc = new UTF32Encoding(); try { enc.GetByteCount((string)null); Assert.Fail("#1"); } catch (ArgumentNullException ex) { Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#2"); Assert.IsNull(ex.InnerException, "#3"); Assert.IsNotNull(ex.Message, "#4"); Assert.AreEqual("s", ex.ParamName, "#5"); } }
[Test] // GetByteCount (Char *) public unsafe void GetByteCount3() { char [] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' }; fixed(char *cp = chars) { UTF32Encoding le = new UTF32Encoding(false, true); Assert.AreEqual(12, le.GetByteCount(cp, 3), "#A1"); Assert.AreEqual(4, le.GetByteCount(cp, 1), "#A2"); Assert.AreEqual(0, le.GetByteCount(cp, 0), "#A3"); Assert.AreEqual(24, le.GetByteCount(cp, 6), "#A4"); //Assert.AreEqual (24, le.GetByteCount (cp, 7), "#A5"); UTF32Encoding be = new UTF32Encoding(true, true); Assert.AreEqual(12, be.GetByteCount(cp, 3), "#B1"); Assert.AreEqual(4, be.GetByteCount(cp, 1), "#B2"); Assert.AreEqual(0, be.GetByteCount(cp, 0), "#B3"); Assert.AreEqual(24, be.GetByteCount(cp, 6), "#B4"); //Assert.AreEqual (24, be.GetByteCount (cp, 7), "#B5"); } }
public static void Main() { var utf32 = new UTF32Encoding(!BitConverter.IsLittleEndian, true); String s = "It was the best of times, it was the worst of times..."; // We need to dimension the array, since we'll populate it with 2 method calls. Byte[] bytes = new Byte[utf32.GetByteCount(s) + utf32.GetPreamble().Length]; // Encode the string. Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length); utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length); // Decode the byte array. String s2 = utf32.GetString(bytes, 0, bytes.Length); Console.WriteLine(s2); }
[Test] // GetByteCount (Char [], Int32, Int32) public void GetByteCount4_Index_Negative() { char [] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' }; UTF32Encoding enc = new UTF32Encoding(); try { enc.GetByteCount(chars, -1, 1); Assert.Fail("#1"); } catch (ArgumentOutOfRangeException ex) { // Non-negative number required Assert.AreEqual(typeof(ArgumentOutOfRangeException), ex.GetType(), "#2"); Assert.IsNull(ex.InnerException, "#3"); Assert.IsNotNull(ex.Message, "#4"); Assert.AreEqual("index", ex.ParamName, "#5"); } }
[Test] // GetByteCount (Char [], Int32, Int32) public void GetByteCount4_Index_Overflow() { char [] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' }; UTF32Encoding enc = new UTF32Encoding(); try { enc.GetByteCount(chars, 7, 1); Assert.Fail("#1"); } catch (ArgumentOutOfRangeException ex) { // Index and count must refer to a location // within the buffer Assert.AreEqual(typeof(ArgumentOutOfRangeException), ex.GetType(), "#2"); Assert.IsNull(ex.InnerException, "#3"); Assert.IsNotNull(ex.Message, "#4"); //Assert.AreEqual ("chars", ex.ParamName, "#5"); } }