00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package sensix.test;
00036
00037 import junit.framework.*;
00038 import sensix.*;
00039 import sensix.discovery.*;
00040 import sensix.sensing.*;
00041
00042
00043 public class ParserTest extends TestCase
00044 {
00045 protected void setUp() {
00046 }
00047
00048 protected void tearDown() {
00049 }
00050
00051
00052 public void testInvalidCommand() {
00053 String s = "gobbledygook(foo[bar;baz)";
00054 Sensory sens = null;
00055 try {
00056 sens = SenseParser.senseParser(s);
00057 fail("Should raise an exception on invalid input");
00058 } catch (sensix.sensing.ParseException e) {
00059 } catch (sensix.sensing.TokenMgrError e) {
00060 } catch (java.io.IOException e) {
00061 } catch (NumberFormatException e) {
00062 }
00063 }
00064
00065 public void testFunctorString() {
00066 String s_orig = "peakSENSE(sEnsOr=temperature,HIGH threshold=5.8)";
00067 PeakSense psi = new PeakSense(Sensix.TEMP, 5.8, true);
00068 String s_constr = psi.asString();
00069 assertTrue("\nOriginal: " + s_orig + "\nConstructed: " + s_constr,
00070 s_constr.equalsIgnoreCase(s_orig));
00071 }
00072
00073 public void testCompoundFunctorString() {
00074 String s_orig = "recite(spatialseries(" +
00075 "sense(sensor=light,rate=5.0))&&" +
00076 "spatialseries(" +
00077 "peaksense(sensor=accelerometer,rate=5.0,high threshold=3.0)))";
00078 try {
00079 Sensory si = (Sensory)(new Sense(Sensix.LIGHT, 5.0));
00080 Sensory psi = (Sensory)(new PeakSense(Sensix.ACCEL, 5.0, 3.0, true));
00081 Collection sl = (Collection)(new SpatialSeries(si));
00082 Collection sa = (Collection)(new SpatialSeries(psi));
00083 Collection[] c = {sl, sa};
00084 Recite r = (Recite)(new Recite(c));
00085 String s_constr = r.asString();
00086 assertTrue("\nOriginal: " + s_orig +
00087 "\nConstructed: " + s_constr,
00088 s_constr.equalsIgnoreCase(s_orig));
00089 } catch (Exception e) {
00090 fail("Exception: " + e);
00091 }
00092 }
00093
00094 public void testShortCommand() {
00095 String s_orig = "mean(spatialseries(sense(sensor=temperature)))";
00096 String s_constr = null;
00097 Sensory sens;
00098 try {
00099 sens = SenseParser.senseParser(s_orig);
00100 s_constr = sens.asString();
00101 assertTrue(s_orig.equalsIgnoreCase(s_constr));
00102 } catch (Error e) {
00103 e.printStackTrace(System.err);
00104 fail("\nError on valid command: " + s_orig +
00105 "\nConstructed: " + s_constr);
00106 } catch (Exception e) {
00107 e.printStackTrace(System.err);
00108 fail("\nException on valid command: " + s_orig +
00109 "\nConstructed: " + s_constr);
00110 }
00111 }
00112
00113 public void testMedCommand() {
00114 String s_orig = "sum(spatialseries(sense(level=1,sensor=light)))";
00115 String s_constr = null;
00116 Sensory sens;
00117 try {
00118 sens = SenseParser.senseParser(s_orig);
00119 s_constr = sens.asString();
00120 assertTrue(s_orig.equalsIgnoreCase(s_constr));
00121 } catch (Error e) {
00122 e.printStackTrace(System.err);
00123 fail("\nError on valid command: " + s_orig +
00124 "\nConstructed: " + s_constr);
00125 } catch (Exception e) {
00126 e.printStackTrace(System.err);
00127 fail("\nException on valid command: " + s_orig +
00128 "\nConstructed: " + s_constr);
00129 }
00130 }
00131
00132 public void testCompoundCommand() {
00133 String s_orig = "recite(" +
00134 "timeseries(duration=10," +
00135 "peaksense(sensor=light,rate=5.0,low threshold=2.0))" +
00136 "&&" +
00137 "timeseries(duration=10," +
00138 "peaksense(sensor=accelerometer,rate=5.0,high threshold=3.0))" +
00139 ")";
00140 String s_orig2 = "recite(" +
00141 "timeseries(duration=10," +
00142 "peaksense(sensor=accelerometer,rate=5.0,high threshold=3.0))" +
00143 "&&" +
00144 "timeseries(duration=10," +
00145 "peaksense(sensor=light,rate=5.0,low threshold=2.0))" +
00146 ")";
00147 String s_constr = null;
00148 Sensory sens;
00149 try {
00150 sens = SenseParser.senseParser(s_orig);
00151 s_constr = sens.asString();
00152 assertTrue(s_orig.equalsIgnoreCase(s_constr) ||
00153 s_orig2.equalsIgnoreCase(s_constr));
00154 } catch (Error e) {
00155 e.printStackTrace(System.err);
00156 fail("\nError on valid command: " + s_orig +
00157 "\nConstructed: " + s_constr);
00158 } catch (Exception e) {
00159 e.printStackTrace(System.err);
00160 fail("\nException on valid command: " + s_orig +
00161 "\nConstructed: " + s_constr);
00162 }
00163 }
00164
00165 public void testLongCommand() {
00166 String s_orig = "sum(level=4,spatialseries(level=4," +
00167 "mean(level=3,spatialseries(level=3," +
00168 "delta(level=2,spatialseries(level=2," +
00169 "sigma(level=1,timeseries(level=1,duration=10," +
00170 "sense(level=1,sensor=magnetometer,rate=2.0)))))))))";
00171 String s_constr = null;
00172 Sensory sens;
00173 try {
00174 sens = SenseParser.senseParser(s_orig);
00175 s_constr = sens.asString();
00176 assertTrue(s_orig.equalsIgnoreCase(s_constr));
00177 } catch (Error e) {
00178 e.printStackTrace(System.err);
00179 fail("\nError on valid command: " + s_orig +
00180 "\nConstructed: " + s_constr);
00181 } catch (Exception e) {
00182 e.printStackTrace(System.err);
00183 fail("\nException on valid command: " + s_orig +
00184 "\nConstructed: " + s_constr);
00185 }
00186 }
00187
00188 public void testMarshalling() {
00189 char c = (char)(Sensix.ALPHA & 0xff);
00190 assertTrue(c == ("\341").charAt(0));
00191 c = (char)(Sensix.BETA & 0xff);
00192 assertTrue(c == ("\342").charAt(0));
00193 c = (char)(Sensix.THETA & 0xff);
00194 assertTrue(c == ("\310").charAt(0));
00195 c = (char)(Sensix.PSI & 0xff);
00196 assertTrue(c == ("\330").charAt(0));
00197 c = (char)(Sensix.IOTA & 0xff);
00198 assertTrue(c == ("\337").charAt(0));
00199 c = (char)(Sensix.SUMMA & 0xff);
00200 assertTrue(c == ("\323").charAt(0));
00201 c = (char)(Sensix.DELTA & 0xff);
00202 assertTrue(c == ("\304").charAt(0));
00203 c = (char)(Sensix.BARX & 0xff);
00204 assertTrue(c == ("\252").charAt(0));
00205 c = (char)(Sensix.SIGMA & 0xff);
00206 assertTrue(c == ("\363").charAt(0));
00207 c = (char)(Sensix.LAMBDA & 0xff);
00208 assertTrue(c == ("\313").charAt(0));
00209 }
00210 }