From bbe1dbb1021f4a7fcae0e390101c9329de540e0f Mon Sep 17 00:00:00 2001 From: Kustaa Nyholm Date: Sat, 26 Jan 2013 11:02:00 +0200 Subject: [PATCH] Minor tweaks to test cases --- src/purejavacomm/testsuite/Test10.java | 19 +++-- src/purejavacomm/testsuite/Test11.java | 1 - src/purejavacomm/testsuite/Test15.java | 13 ++- src/purejavacomm/testsuite/Test5.java | 2 +- src/purejavacomm/testsuite/Test6.java | 29 ++++--- src/purejavacomm/testsuite/Test8.java | 107 +++++++++++++++++++------ src/purejavacomm/testsuite/Test9.java | 2 +- 7 files changed, 122 insertions(+), 51 deletions(-) diff --git a/src/purejavacomm/testsuite/Test10.java b/src/purejavacomm/testsuite/Test10.java index a2f5282..0b2d691 100644 --- a/src/purejavacomm/testsuite/Test10.java +++ b/src/purejavacomm/testsuite/Test10.java @@ -36,6 +36,7 @@ public class Test10 extends TestBase { static volatile boolean m_ReadThreadRunning; + static volatile boolean m_ThreadStarted; static volatile int m_ReadBytes = 0; static volatile long m_T0; static volatile long m_T1; @@ -59,6 +60,7 @@ static void run() throws Exception { Thread rxthread = new Thread(new Runnable() { public void run() { m_ReadThreadRunning = true; + m_ThreadStarted = true; try { m_ReadBytes = m_In.read(rxbuffer, 0, rxbuffer.length); m_T1 = System.currentTimeMillis(); @@ -70,8 +72,10 @@ public void run() { }); m_ReadThreadRunning = false; + m_ThreadStarted = false; + m_ReadBytes = -666; rxthread.start(); - while (!m_ReadThreadRunning) + while (!m_ThreadStarted) Thread.sleep(10); for (int i = 0; i < chunks; i++) { @@ -83,7 +87,7 @@ public void run() { } m_T0 = System.currentTimeMillis(); Thread.sleep(100); - int i = 200; + int i = 2000; while (--i > 0 && m_ReadThreadRunning) Thread.sleep(5); if (i <= 0) @@ -97,14 +101,15 @@ public void run() { fail("was expecting read to happen in " + timeMax + " but it took " + time + " msec"); } - { // Test a single big read with read length < threshold + { // Test a single big read with read length < threshold Thread rxthread = new Thread(new Runnable() { public void run() { m_ReadThreadRunning = true; + m_ThreadStarted = true; try { m_ReadBytes = m_In.read(rxbuffer, 0, threshold / 2); m_T1 = System.currentTimeMillis(); - } catch (IOException e) { + } catch (IOException e) { e.printStackTrace(); } m_ReadThreadRunning = false; @@ -112,8 +117,10 @@ public void run() { }); m_ReadThreadRunning = false; - rxthread.start(); - while (!m_ReadThreadRunning) + m_ThreadStarted = false; + m_ReadBytes = -666; + rxthread.start(); + while (!m_ThreadStarted) Thread.sleep(10); for (int i = 0; i < chunks / 2; i++) { diff --git a/src/purejavacomm/testsuite/Test11.java b/src/purejavacomm/testsuite/Test11.java index e22b125..8cd3d2d 100644 --- a/src/purejavacomm/testsuite/Test11.java +++ b/src/purejavacomm/testsuite/Test11.java @@ -40,7 +40,6 @@ public class Test11 extends TestBase { static void run() throws Exception { try { - System.setProperty("purejavacomm.rawreadmode", "false"); begin("Test11 - exit from blocking read "); openPort(); diff --git a/src/purejavacomm/testsuite/Test15.java b/src/purejavacomm/testsuite/Test15.java index c365a60..8982ce5 100644 --- a/src/purejavacomm/testsuite/Test15.java +++ b/src/purejavacomm/testsuite/Test15.java @@ -55,7 +55,7 @@ static void run() throws Exception { if (n != 0) fail("was expecting 0 bytes, but got " + n + " bytes"); int timeLo = timeout; - int timeHi = timeout * 105 / 100; + int timeHi = timeout * 110 / 100; int time = (int) (T1 - T0); if (time < timeLo) fail("timed out early, was expecting " + timeLo + " but got " + time + " msec"); @@ -69,13 +69,12 @@ static void run() throws Exception { long T0 = System.currentTimeMillis(); int n = m_In.read(rxbuffer, 0, 1000); long T1 = System.currentTimeMillis(); - if (n < 10) - fail("was expecting at least 10 bytes, but got " + n + " bytes"); - if (n > 100) - fail("was expecting at most 100 bytes, but got " + n + " bytes"); int time = (int) (T1 - T0); - if (time > 1) - fail("expected read to return ASAP but it took" + time + " msec"); + int etime= n * 150/100; // at 9600 + if (time > etime) + fail("expected read to return in " + etime+" but it took " + time + " msec and returned "+n +" bytes"); + if (n < 10) + fail("was expecting at least 900 bytes, but got " + n + " bytes"); } diff --git a/src/purejavacomm/testsuite/Test5.java b/src/purejavacomm/testsuite/Test5.java index 38d438d..f92ad8e 100644 --- a/src/purejavacomm/testsuite/Test5.java +++ b/src/purejavacomm/testsuite/Test5.java @@ -56,7 +56,7 @@ public void run() { fail("read did not time out as expected, read returned %d > 0", n); if (dT < 1000 - 10) fail("read timed out early, expected 1000 msec, got %d msec", dT); - if (dT > 1010) + if (dT > 1020) fail("read timed out with suspicious delay, expected 1000 msec, got %d msec", dT); } catch (InterruptedException e) { } catch (Exception e) { diff --git a/src/purejavacomm/testsuite/Test6.java b/src/purejavacomm/testsuite/Test6.java index 5c155d0..c558f5e 100644 --- a/src/purejavacomm/testsuite/Test6.java +++ b/src/purejavacomm/testsuite/Test6.java @@ -41,21 +41,26 @@ static void run() throws Exception { try { begin("Test6 - threshold + timeout"); openPort(); + m_Port.setSerialPortParams(230000, 8, 1, 0); + //m_In = purejavacomm.RawStream.getInputStream(m_Port); // receiving thread m_Receiver = new Thread(new Runnable() { public void run() { try { sync(2); m_Port.enableReceiveThreshold(4); - m_Port.enableReceiveTimeout(1000); - long T0 = System.currentTimeMillis(); - byte[] b = new byte[8]; - int n = m_In.read(b); - long dT = System.currentTimeMillis() - T0; - if (n != 4) - fail("read did not get 4 bytes as expected, got %d ", n); - if (dT >= 1000) - fail("read timed out though we got 4 bytes"); + m_Port.enableReceiveTimeout(10000); + //purejavacomm.RawStream.configureThresholdTimeout(m_Port, 4, 2000); + byte[] b = new byte[4]; + for (int i = 0; i < 1000; i++) { + long T0 = System.currentTimeMillis(); + int n = m_In.read(b); + long dT = System.currentTimeMillis() - T0; + if (n != 4) + fail("read did not get 4 bytes as expected, got %d ", n); + if (dT >= 1000) + fail("read timed out though we got 4 bytes "+dT); + } } catch (InterruptedException e) { } catch (Exception e) { @@ -72,7 +77,8 @@ public void run() { public void run() { try { sync(2); - m_Out.write(new byte[4]); + for (int i = 0; i < 1000; i++) + m_Out.write(new byte[4]); } catch (InterruptedException e) { } catch (Exception e) { e.printStackTrace(); @@ -84,8 +90,9 @@ public void run() { }; }); - m_Receiver.start(); m_Transmitter.start(); + sleep(100); + m_Receiver.start(); while (m_Receiver.isAlive() || m_Transmitter.isAlive()) { sleep(100); diff --git a/src/purejavacomm/testsuite/Test8.java b/src/purejavacomm/testsuite/Test8.java index c648833..862af90 100644 --- a/src/purejavacomm/testsuite/Test8.java +++ b/src/purejavacomm/testsuite/Test8.java @@ -36,45 +36,104 @@ public class Test8 extends TestBase { static void run() throws Exception { try { - begin("Test8 - parity"); + begin("Test8 - parity etc"); openPort(); int[] parity = { SerialPort.PARITY_NONE, SerialPort.PARITY_ODD, SerialPort.PARITY_EVEN }; - int[] stopbits = { SerialPort.STOPBITS_1, SerialPort.STOPBITS_2 };// , SerialPort.STOPBITS_1_5 }; - int[] databits = { SerialPort.DATABITS_5, SerialPort.DATABITS_6, SerialPort.DATABITS_7, SerialPort.DATABITS_8 }; + int[] stopbits = { SerialPort.STOPBITS_1, SerialPort.STOPBITS_1_5, SerialPort.STOPBITS_2 };// , + // SerialPort.STOPBITS_1_5 + // }; + int[] databits = { SerialPort.DATABITS_8, SerialPort.DATABITS_7, SerialPort.DATABITS_6, SerialPort.DATABITS_5 }; int[] datamask = { 0x1F, 0x3F, 0x7F, 0xFF }; - for (int pi = 0; pi < parity.length; pi++) { + System.out.println(); + int tn = 0; + for (int ppi = 0; ppi < parity.length; ppi++) { for (int sbi = 0; sbi < stopbits.length; sbi++) { for (int dbi = 0; dbi < databits.length; dbi++) { + m_Port.enableReceiveTimeout(10000); m_Port.enableReceiveThreshold(256); - m_Port.setSerialPortParams(115200, databits[dbi], stopbits[sbi], parity[pi]); - byte[] sent = new byte[256]; - byte[] rcvd = new byte[256]; - for (int i = 0; i < 256; i++) - sent[i] = (byte) (i & datamask[dbi]); - m_Out = m_Port.getOutputStream(); - m_In = m_Port.getInputStream(); - long t0 = System.currentTimeMillis(); - m_Out.write(sent); + try { + String db = "?"; + switch (databits[dbi]) { + case SerialPort.DATABITS_5: + db = "5"; + break; + case SerialPort.DATABITS_6: + db = "6"; + break; + case SerialPort.DATABITS_7: + db = "7"; + break; + case SerialPort.DATABITS_8: + db = "8"; + break; + } + + String sb = "?"; + switch (stopbits[sbi]) { + case SerialPort.STOPBITS_1: + sb = "1"; + break; + case SerialPort.STOPBITS_1_5: + sb = "1.5"; + break; + case SerialPort.STOPBITS_2: + sb = "2"; + break; + } - //sleep(500); + String pb = "?"; + switch (parity[ppi]) { + case SerialPort.PARITY_EVEN: + pb = "E"; + break; + case SerialPort.PARITY_ODD: + pb = "O"; + break; + case SerialPort.PARITY_MARK: + pb = "M"; + break; + case SerialPort.PARITY_SPACE: + pb = "S"; + break; + case SerialPort.PARITY_NONE: + pb = "N"; + break; + } + tn++; + begin("Test8." + tn + " databits=" + db + " stopbits=" + sb + " parity=" + pb); + m_Port.setSerialPortParams(19200, databits[dbi], stopbits[sbi], parity[ppi]); + sleep(100); + byte[] sent = new byte[256]; + byte[] rcvd = new byte[256]; + for (int i = 0; i < 256; i++) + sent[i] = (byte) (i & datamask[dbi]); + m_Out = m_Port.getOutputStream(); + m_In = m_Port.getInputStream(); + long t0 = System.currentTimeMillis(); + m_Out.write(sent); - int n = m_In.read(rcvd); + int n = 0; + while ((n += m_In.read(rcvd, n, 256 - n)) < 256) + ; - if (n != sent.length) - fail("was expecting %d characters got %d", sent.length, n); - for (int i = 0; i < 256; ++i) { - if (sent[i] != rcvd[i]) - fail("failed: transmit '0x%02X' != receive'0x%02X' with databits %d stopbit %d parity %d", sent[i], rcvd[i], databits[dbi], stopbits[sbi], parity[pi]); + if (n != sent.length) + fail("was expecting %d characters got %d", sent.length, n); + for (int i = 0; i < 256; ++i) { + if (sent[i] != rcvd[i]) + fail("failed: transmit '0x%02X' != receive'0x%02X'", sent[i], rcvd[i]); + } + if (n < 256) + fail("did not receive all 256 chars, got %d", n); + finishedOK(); + } catch (UnsupportedCommOperationException e) { + finishedOK(" NOT SUPPORTED"); } - if (n < 256) - fail("did not receive all 256 chars, got %d", n); - //sleep(1); + // sleep(1); } } } - finishedOK(); } finally { closePort(); } diff --git a/src/purejavacomm/testsuite/Test9.java b/src/purejavacomm/testsuite/Test9.java index 52a0627..677ef02 100644 --- a/src/purejavacomm/testsuite/Test9.java +++ b/src/purejavacomm/testsuite/Test9.java @@ -105,7 +105,7 @@ static void run() throws Exception { fail("read did not complete in time"); int time = (int) (m_T1 - m_T0); - int timeMax = (threshold/txn+1)*txt; + int timeMax = (threshold/txn+2)*txt; if (time>timeMax) fail("was expecting read to happen in " + timeMax + " but it took " + time + " msec");