static void TestGetBytes(Encoding xEncoding, string xName, string text, byte[] expectedResult, string desc) { byte[] result; result = xEncoding.GetBytes(text); Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), $"{xName} Encoding of {desc} text failed byte arrays different"); }
private static void TestGetBytes(Encoding xEncoding, string xName, string text, byte[] expectedResult, string desc) { byte[] result; result = xEncoding.GetBytes(text); if (result.Length != expectedResult.Length) { mDebugger.SendInternal($"The two byte arrays have a different length : {result.Length} vs {expectedResult.Length}"); } for (int i = 0; i < result.Length; i++) { if (result[i] != expectedResult[i]) { mDebugger.SendInternal($"Difference in byte {i} between {result[i]} and {expectedResult[i]}"); } } Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), $"{xName} Encoding of {desc} text failed byte arrays different"); }
public static void Execute() { //CosmosUTF8Encoding Encoder = new CosmosUTF8Encoding(); //Encoder Encoder = new UTF8Encoding().GetEncoder(); Encoding Encoder = new UTF8Encoding(); string text; byte[] result; byte[] expectedResult; text = "Cosmos is wonderful!"; result = Encoder.GetBytes(text); expectedResult = UTF8EnglishText; Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "UTF8 Encoding of English text failed byte arrays different"); text = "Cosmos è fantastico!"; result = Encoder.GetBytes(text); expectedResult = UTF8ItalianText; Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "UTF8 Encoding of Italian text failed byte arrays different"); text = "Cosmos es genial!"; result = Encoder.GetBytes(text); expectedResult = UTF8SpanishText; Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "UTF8 Encoding of Spanish text failed byte arrays different"); text = "Cosmos ist großartig!"; result = Encoder.GetBytes(text); expectedResult = UTF8GermanicText; Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "UTF8 Encoding of Germanic text failed byte arrays different"); text = "Cosmos είναι υπέροχος!"; result = Encoder.GetBytes(text); expectedResult = UTF8GreekText; Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "UTF8 Encoding of Greek text failed byte arrays different"); text = "Cosmos 素晴らしいです!"; result = Encoder.GetBytes(text); expectedResult = UTF8JapanaseText; Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "UTF8 Encoding of Japanese text failed byte arrays different"); /* This the only case on which UFT-16 must use a surrugate pairs... it is a Gothic letter go figure! */ text = "𐍈"; result = Encoder.GetBytes(text); expectedResult = UTF8GothicText; Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "UTF8 Encoding of Gothic text failed byte arrays different"); /* Now we do the other way: we have a UFT8 byte array and try to convert it in a UFT16 String */ string expectedText; text = Encoder.GetString(UTF8EnglishText); expectedText = "Cosmos is wonderful!"; Assert.IsTrue((text == expectedText), "UTF8 Decoding of English text failed strings different"); text = Encoder.GetString(UTF8ItalianText); expectedText = "Cosmos è fantastico!"; Assert.IsTrue((text == expectedText), "UTF8 Decoding of Italian text failed strings different"); text = Encoder.GetString(UTF8SpanishText); expectedText = "Cosmos es genial!"; Assert.IsTrue((text == expectedText), "UTF8 Decoding of Spanish text failed strings different"); text = Encoder.GetString(UTF8GermanicText); expectedText = "Cosmos ist großartig!"; Assert.IsTrue((text == expectedText), "UTF8 Decoding of Germanic text failed strings different"); text = Encoder.GetString(UTF8GreekText); expectedText = "Cosmos είναι υπέροχος!"; Assert.IsTrue((text == expectedText), "UTF8 Decoding of Greek text failed strings different"); text = Encoder.GetString(UTF8JapanaseText); expectedText = "Cosmos 素晴らしいです!"; Assert.IsTrue((text == expectedText), "UTF8 Decoding of Japanese text failed strings different"); text = Encoder.GetString(UTF8GothicText); expectedText = "𐍈"; Assert.IsTrue((text == expectedText), "UTF8 Decoding of Gothic text failed strings different"); /* But this not work is searching '437' in some native Windows tables, we need plugs for this sadly! */ //Encoder = Encoding.GetEncoding(437); //text = "àèìòù"; //result = Encoder.GetBytes(text); //expectedResult = new byte[] { 0x85, 0x8A, 0x8D, 0x95, 0x97 }; //Assert.IsTrue(EqualityHelper.ByteArrayAreEquals(result, expectedResult), "CP437 Encoding of accents text failed byte arrays different"); }