/* * Copyright (C) 2009 Manish Pandya, [manish at meetamanish dot com] * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * */ package org.hooliguns.ninja.telnet.teensyninja.test; import java.util.Formatter; import java.util.Locale; /** * A simple stand-alone Test Suite for teensyninja modded Ninja interface. It runs the test cases * caught by random movement generator and defined in this class. * * @author Manish Pandya (March 18 2009) * */ public class TestSuiteForTeensyNinja extends TestFrameworkForTeensyNinja { /** * Static collection of all tests */ static TestCase[] tests = null; /** * Default constructor, sets up the test cases. */ public TestSuiteForTeensyNinja() { tests = new TestCase[] { new TestCase( "Case that triggered introduction of +- 50 fuzz for center sensor", new Step[] { new Step(430, 137, 44), new Step(-28, -1128, 23) } ), new TestCase( "A random case that caused my teensyninja to get stuck, never reproduced.", new Step[] { new Step(278, -738, 10), new Step(1450, -91, 33), new Step(1461, 155, 45) } ) }; } /** * A class that runs tests on teensyninja * * @author Manish Pandya (March 18 2009) * */ public static class TestRunner implements Runnable { /** * the instance of test class */ TestSuiteForTeensyNinja tstn; /** * a typical constructor * * @param tstn * the instance of the test class */ public TestRunner(TestSuiteForTeensyNinja tstn) { this.tstn = tstn; } /** * Method that runs the tests. */ void runTests() { String retstr = new String(executeAndGetResponse("c", tstn.out, tstn.in)); System.out.println(retstr); while (isInMotion(tstn.out, tstn.in)) { try { Thread.sleep(1200); System.out.print('.'); } catch (InterruptedException e) { // wake up! } } for (int tccount = 0; tests != null && tccount < tests.length; tccount++) { TestCase testcase = tests[tccount]; System.out.println(); System.out.println("Running TestCase:"); System.out.println(testcase); for (int scount = 0; tstn.keepOn && testcase.steps != null && scount < testcase.steps.length; scount++) { Step step = testcase.steps[scount]; System.out.println(); System.out.println("Running Step:"); System.out.println(step); retstr = new String(executeAndGetResponse("x" + step.x, tstn.out, tstn.in)); System.out.println(retstr); retstr = new String(executeAndGetResponse("y" + step.y, tstn.out, tstn.in)); System.out.println(retstr); retstr = new String(executeAndGetResponse("v" + step.v, tstn.out, tstn.in)); System.out.println(retstr); retstr = new String(executeAndGetResponse("m", tstn.out, tstn.in)); System.out.println(retstr); while (isInMotion(tstn.out, tstn.in)) { try { Thread.sleep(1200); System.out.print('.'); } catch (InterruptedException e) { // wake up! } } } } System.out.println(); System.out.println("Done. All test passed successfully."); System.exit(0); } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ public void run() { runTests(); } } /** * A typical main method that starts up the test * * @param args * first string (arg[0]) is the name of the com port */ public static void main(String[] args) { try { TestSuiteForTeensyNinja tstn = new TestSuiteForTeensyNinja(); tstn.connect(args[0], new TestRunner(tstn)); Runtime.getRuntime().addShutdownHook(tstn.shook); } catch (Exception e) { e.printStackTrace(); } } /** * Java representation of a test case. * @author Manish Pandya * */ public class TestCase { /** * Description of the test case */ String desc = null; /** * Ordered list of steps to be taken to perform the test */ public Step[] steps = null; /** * Typical constructor * @param desc Description of the test case * @param steps Ordered list of steps to be taken to perform the test * */ public TestCase(String desc, Step[] steps) { this.desc = desc; this.steps = steps; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append(desc); sb.append("\r\n{ \r\n"); for (int i = 0; steps != null && i < steps.length; i++) { sb.append(" "); sb.append(steps[i]); sb.append("\r\n"); } sb.append("}\r\n"); return sb.toString(); } } /** * Java representation of a step (collection of instruction to get to defined position with given velocity) * @author Manish Pandya * */ public class Step { /** * azimuth (X) */ int x = 0; /** * tilt (Y) */ int y = 0; /** * velocity (V) */ int v = 0; /** * Typical constructor * @param x azimuth * @param y tilt * @param v velocity */ public Step(int x, int y, int v) { this.x = x; this.y = y; this.v = v; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { StringBuffer sb = new StringBuffer(); // Send all output to the Appendable object sb Formatter formatter = new Formatter(sb, Locale.US); formatter.format("{ x: %5d y: %5d v: %2d}", x, y, v); return sb.toString(); } } }