public void Read_Timeout() { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None); byte[] byteXmitBuffer = new UTF32Encoding().GetBytes(charXmitBuffer); char[] charRcvBuffer = new char[charXmitBuffer.Length]; int result; Debug.WriteLine( "Verifying that Read(char[], int, int) works appropriately after TimeoutException has been thrown"); com1.Encoding = new UTF32Encoding(); com2.Encoding = new UTF32Encoding(); com1.ReadTimeout = 500; // 20 seconds com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } //Write the first 3 bytes of a character com2.Write(byteXmitBuffer, 0, 3); try { com1.Read(charXmitBuffer, 0, charXmitBuffer.Length); Fail("Err_29299aize Expected ReadTo to throw TimeoutException"); } catch (TimeoutException) { } //Expected Assert.Equal(3, com1.BytesToRead); com2.Write(byteXmitBuffer, 3, byteXmitBuffer.Length - 3); // retValue &= TCSupport.WaitForPredicate(delegate() {return com1.BytesToRead == byteXmitBuffer.Length; }, // 5000, "Err_91818aheid Expected BytesToRead={0} actual={1}", byteXmitBuffer.Length, com1.BytesToRead); TCSupport.WaitForExpected(() => com1.BytesToRead, byteXmitBuffer.Length, 5000, "Err_91818aheid BytesToRead"); result = com1.Read(charRcvBuffer, 0, charRcvBuffer.Length); Assert.Equal(charXmitBuffer.Length, result); Assert.Equal(charXmitBuffer, charRcvBuffer); VerifyBytesReadOnCom1FromCom2(com1, com2, byteXmitBuffer, charXmitBuffer, charRcvBuffer, 0, charRcvBuffer.Length); } }
public void Read_Surrogate() { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { char[] surrogateChars = { (char)0xDB26, (char)0xDC49 }; char[] additionalChars = TCSupport.GetRandomChars(32, TCSupport.CharacterOptions.None); char[] charRcvBuffer = new char[2]; Debug.WriteLine("Verifying that ReadChar works correctly when trying to read surrogate characters"); com1.Encoding = new UTF32Encoding(); com2.Encoding = new UTF32Encoding(); com1.ReadTimeout = 500; // 20 seconds com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } int numBytes = com2.Encoding.GetByteCount(surrogateChars); numBytes += com2.Encoding.GetByteCount(additionalChars); com2.Write(surrogateChars, 0, 2); com2.Write(additionalChars, 0, additionalChars.Length); TCSupport.WaitForExpected(() => com1.BytesToRead, numBytes, 5000, "Err_91818aheid BytesToRead"); // We expect this to fail, because it can't read a surrogate // parameter name can be different on different platforms Assert.Throws <ArgumentException>(() => com1.ReadChar()); int result = com1.Read(charRcvBuffer, 0, 2); Assert.Equal(2, result); if (charRcvBuffer[0] != surrogateChars[0]) { Fail("Err_12929anied Expected first char read={0}({1:X}) actually read={2}({3:X})", surrogateChars[0], (int)surrogateChars[0], charRcvBuffer[0], (int)charRcvBuffer[0]); } else if (charRcvBuffer[1] != surrogateChars[1]) { Fail("Err_12929anied Expected second char read={0}({1:X}) actually read={2}({3:X})", surrogateChars[1], (int)surrogateChars[1], charRcvBuffer[1], (int)charRcvBuffer[1]); } PerformReadOnCom1FromCom2(com1, com2, additionalChars); } }
public void Read_Partial() { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { char utf32Char = (char)0x254b; //Box drawing char byte[] utf32CharBytes = Encoding.UTF32.GetBytes(new[] { utf32Char }); char[] charRcvBuffer = new char[3]; int result; Debug.WriteLine("Verifying that Read(char[], int, int) works when reading partial characters"); com1.Encoding = new UTF32Encoding(); com2.Encoding = new UTF32Encoding(); com1.ReadTimeout = 500; com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } //Write the first 3 bytes of a character com2.Write(utf32CharBytes, 0, 4); com2.Write(utf32CharBytes, 0, 3); TCSupport.WaitForExpected(() => com1.BytesToRead, 7, 5000, "Err_018158ajid BytesToRead"); result = com1.Read(charRcvBuffer, 0, charRcvBuffer.Length); Assert.Equal(1, result); com2.Write(utf32CharBytes, 3, 1); result = com1.Read(charRcvBuffer, 1, charRcvBuffer.Length - 1); Assert.Equal(1, result); Assert.Equal(utf32Char, charRcvBuffer[0]); Assert.Equal(utf32Char, charRcvBuffer[1]); VerifyBytesReadOnCom1FromCom2(com1, com2, utf32CharBytes, new[] { utf32Char }, charRcvBuffer, 0, charRcvBuffer.Length); } }
public void Read_SurrogateBoundary() { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { char[] charXmitBuffer = new char[32]; int result; Debug.WriteLine("Verifying that Read(char[], int, int) works with reading surrogate characters"); TCSupport.GetRandomChars(charXmitBuffer, 0, charXmitBuffer.Length - 2, TCSupport.CharacterOptions.Surrogates); charXmitBuffer[charXmitBuffer.Length - 2] = TCSupport.GenerateRandomHighSurrogate(); charXmitBuffer[charXmitBuffer.Length - 1] = TCSupport.GenerateRandomLowSurrogate(); com1.Encoding = Encoding.Unicode; com2.Encoding = Encoding.Unicode; com1.ReadTimeout = 500; com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } //[] First lets try with buffer size that is larger then what we are asking for com2.Write(charXmitBuffer, 0, charXmitBuffer.Length); TCSupport.WaitForExpected(() => com1.BytesToRead, charXmitBuffer.Length * 2, 5000, "Err_018158ajid BytesToRead"); var charRcvBuffer = new char[charXmitBuffer.Length]; result = com1.Read(charRcvBuffer, 0, charXmitBuffer.Length - 1); Assert.Equal(charXmitBuffer.Length - 2, result); char[] actualChars = new char[charXmitBuffer.Length]; Array.Copy(charRcvBuffer, actualChars, result); result = com1.Read(actualChars, actualChars.Length - 2, 2); Assert.Equal(2, result); Assert.Equal(charXmitBuffer, actualChars); //[] Next lets try with buffer size that is the same size as what we are asking for com2.Write(charXmitBuffer, 0, charXmitBuffer.Length); TCSupport.WaitForExpected(() => com1.BytesToRead, charXmitBuffer.Length * 2, 5000, "Err_018158ajid BytesToRead"); charRcvBuffer = new char[charXmitBuffer.Length - 1]; result = com1.Read(charRcvBuffer, 0, charXmitBuffer.Length - 1); Assert.Equal(charXmitBuffer.Length - 2, result); actualChars = new char[charXmitBuffer.Length]; Array.Copy(charRcvBuffer, actualChars, result); result = com1.Read(actualChars, actualChars.Length - 2, 2); Assert.Equal(2, result); Assert.Equal(charXmitBuffer, actualChars); } }
public void Read_Timeout() { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None); byte[] byteXmitBuffer = new System.Text.UTF32Encoding().GetBytes(charXmitBuffer); char[] charRcvBuffer = new char[charXmitBuffer.Length]; int result; Debug.WriteLine( "Verifying that Read(char[], int, int) works appropriately after TimeoutException has been thrown"); com1.Encoding = new System.Text.UTF32Encoding(); com2.Encoding = new System.Text.UTF32Encoding(); com1.ReadTimeout = 500; // 20 seconds com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } //Write the first 3 bytes of a character com2.Write(byteXmitBuffer, 0, 3); Assert.Throws <TimeoutException>(() => com1.ReadChar()); Assert.Equal(3, com1.BytesToRead); com2.Write(byteXmitBuffer, 3, byteXmitBuffer.Length - 3); TCSupport.WaitForExpected(() => com1.BytesToRead, byteXmitBuffer.Length, 5000, "Err_91818aheid BytesToRead"); result = com1.ReadChar(); if (result != charXmitBuffer[0]) { Fail("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", charXmitBuffer[0], result); } else { charRcvBuffer[0] = (char)result; int readResult = com1.Read(charRcvBuffer, 1, charRcvBuffer.Length - 1); if (readResult + 1 != charXmitBuffer.Length) { Fail("Err_051884ajoedo Expected Read to read {0} characters actually read {1}", charXmitBuffer.Length - 1, readResult); } else { for (int i = 0; i < charXmitBuffer.Length; ++i) { if (charRcvBuffer[i] != charXmitBuffer[i]) { Fail("Err_05188ahed Characters differ at {0} expected:{1}({1:X}) actual:{2}({2:X})", i, charXmitBuffer[i], charRcvBuffer[i]); } } } } VerifyBytesReadOnCom1FromCom2(com1, com2, byteXmitBuffer, charXmitBuffer); } }
public bool Read_Surrogate() { SerialPort com1 = TCSupport.InitFirstSerialPort(); SerialPort com2 = TCSupport.InitSecondSerialPort(com1); char[] surrogateChars = new char[] { (char)0xDB26, (char)0xDC49 }; char[] additionalChars = TCSupport.GetRandomChars(32, TCSupport.CharacterOptions.None); char[] charRcvBuffer = new char[2]; int numBytes; bool retValue = true; int result; Console.WriteLine("Verifying that ReadChar works correctly when trying to read surrogate characters"); com1.Encoding = new System.Text.UTF32Encoding(); com2.Encoding = new System.Text.UTF32Encoding(); com1.ReadTimeout = 500; // 20 seconds com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } numBytes = com2.Encoding.GetByteCount(surrogateChars); numBytes += com2.Encoding.GetByteCount(additionalChars); com2.Write(surrogateChars, 0, 2); com2.Write(additionalChars, 0, additionalChars.Length); retValue &= TCSupport.WaitForExpected(delegate() { return(com1.BytesToRead); }, numBytes, 5000, "Err_91818aheid BytesToRead"); try { com1.ReadChar(); retValue = false; Console.WriteLine("Err_11919aueic Expected ReadChar() to throw when reading a surrogate"); } catch (ArgumentException) { } //Expected result = com1.Read(charRcvBuffer, 0, 2); if (2 != result) { retValue = false; Console.WriteLine("Err_2829aheid Expected to Read 2 chars instead read={0}", result); } else if (charRcvBuffer[0] != surrogateChars[0]) { retValue = false; Console.WriteLine("Err_12929anied Expected first char read={0}({1:X}) actually read={2}({3:X})", surrogateChars[0], (int)surrogateChars[0], charRcvBuffer[0], (int)charRcvBuffer[0]); } else if (charRcvBuffer[1] != surrogateChars[1]) { Console.WriteLine("Err_12929anied Expected second char read={0}({1:X}) actually read={2}({3:X})", surrogateChars[1], (int)surrogateChars[1], charRcvBuffer[1], (int)charRcvBuffer[1]); } if (!PerformReadOnCom1FromCom2(com1, com2, additionalChars)) { Console.WriteLine("Err_01588akeid Verify ReadChar after read failed"); retValue = false; } if (!retValue) { Console.WriteLine("Err_0154688aieide Verifying that ReadChar works correctly when trying to read surrogate characters failed"); } com1.Close(); com2.Close(); return(retValue); }
public bool Read_Timeout() { SerialPort com1 = TCSupport.InitFirstSerialPort(); SerialPort com2 = TCSupport.InitSecondSerialPort(com1); char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None); byte[] byteXmitBuffer = new System.Text.UTF32Encoding().GetBytes(charXmitBuffer); char[] charRcvBuffer = new char[charXmitBuffer.Length]; bool retValue = true; int result; Console.WriteLine("Verifying that Read(char[], int, int) works appropriately after TimeoutException has been thrown"); com1.Encoding = new System.Text.UTF32Encoding(); com2.Encoding = new System.Text.UTF32Encoding(); com1.ReadTimeout = 500; // 20 seconds com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } //Write the first 3 bytes of a character com2.Write(byteXmitBuffer, 0, 3); try { com1.ReadChar(); Console.WriteLine("Err_29299aize Expected ReadChar to throw TimeoutException"); retValue = false; } catch (TimeoutException) { }//Expected if (3 != com1.BytesToRead) { Console.WriteLine("Err_689855ozpbea Expected BytesToRead: {0} actual: {1}", 3, com1.BytesToRead); retValue = false; } com2.Write(byteXmitBuffer, 3, byteXmitBuffer.Length - 3); retValue &= TCSupport.WaitForExpected(delegate() { return(com1.BytesToRead); }, byteXmitBuffer.Length, 5000, "Err_91818aheid BytesToRead"); result = com1.ReadChar(); if (result != charXmitBuffer[0]) { retValue = false; Console.WriteLine("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", charXmitBuffer[0], result); } else { charRcvBuffer[0] = (char)result; int readResult = com1.Read(charRcvBuffer, 1, charRcvBuffer.Length - 1); if (readResult + 1 != charXmitBuffer.Length) { retValue = false; Console.WriteLine("Err_051884ajoedo Expected Read to read {0} cahracters actually read {1}", charXmitBuffer.Length - 1, readResult); } else { for (int i = 0; i < charXmitBuffer.Length; ++i) { if (charRcvBuffer[i] != charXmitBuffer[i]) { retValue = false; Console.WriteLine("Err_05188ahed Characters differ at {0} expected:{1}({1:X}) actual:{2}({2:X})", i, charXmitBuffer[i], charRcvBuffer[i]); } } } } if (!VerifyBytesReadOnCom1FromCom2(com1, com2, byteXmitBuffer, charXmitBuffer)) { Console.WriteLine("Err_05188ajied Verify ReadLine after read failed"); retValue = false; } if (!retValue) { Console.WriteLine("Err_9858wiapxzg Verifying that ReadChar() works appropriately after TimeoutException has been thrown failed"); } com1.Close(); com2.Close(); return(retValue); }