Skip to content

Commit

Permalink
Minor tweaks to test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nyholku committed Jan 26, 2013
1 parent df98aab commit bbe1dbb
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 51 deletions.
19 changes: 13 additions & 6 deletions src/purejavacomm/testsuite/Test10.java
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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++) {
Expand All @@ -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)
Expand All @@ -97,23 +101,26 @@ 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;
}
});

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++) {
Expand Down
1 change: 0 additions & 1 deletion src/purejavacomm/testsuite/Test11.java
Expand Up @@ -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();

Expand Down
13 changes: 6 additions & 7 deletions src/purejavacomm/testsuite/Test15.java
Expand Up @@ -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");
Expand All @@ -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");
}


Expand Down
2 changes: 1 addition & 1 deletion src/purejavacomm/testsuite/Test5.java
Expand Up @@ -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) {
Expand Down
29 changes: 18 additions & 11 deletions src/purejavacomm/testsuite/Test6.java
Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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);
Expand Down
107 changes: 83 additions & 24 deletions src/purejavacomm/testsuite/Test8.java
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/purejavacomm/testsuite/Test9.java
Expand Up @@ -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");
Expand Down

0 comments on commit bbe1dbb

Please sign in to comment.