0001: /**
0002: * Copyright John E. Lloyd, 2004. All rights reserved. Permission to use,
0003: * copy, modify and redistribute is granted, provided that this copyright
0004: * notice is retained and the author is given credit whenever appropriate.
0005: *
0006: * This software is distributed "as is", without any warranty, including
0007: * any implied warranty of merchantability or fitness for a particular
0008: * use. The author assumes no responsibility for, and shall not be liable
0009: * for, any special, indirect, or consequential damages, or any damages
0010: * whatsoever, arising out of or in connection with the use of this
0011: * software.
0012: */package argparser;
0013:
0014: import java.io.*;
0015:
0016: import java.lang.reflect.Array;
0017: import java.util.Vector;
0018:
0019: /**
0020: * Testing class for the class ArgParser. Executing the <code>main</code>
0021: * method of this class will perform a suite of tests to help verify correct
0022: * operation of the parser class.
0023: *
0024: * @author John E. Lloyd, Fall 2004
0025: * @see ArgParser
0026: */
0027: public class ArgParserTest {
0028: ArgParser parser;
0029:
0030: static final boolean CLOSED = true;
0031: static final boolean OPEN = false;
0032:
0033: static final boolean ONE_WORD = true;
0034: static final boolean MULTI_WORD = false;
0035:
0036: private static void verify(boolean ok, String msg) {
0037: if (!ok) {
0038: Throwable e = new Throwable();
0039: System.out.println("Verification failed:" + msg);
0040: e.printStackTrace();
0041: System.exit(1);
0042: }
0043: }
0044:
0045: private static String[] argsFromString(String s) {
0046: Vector vec = new Vector(100);
0047: try {
0048: ArgParser
0049: .stringToArgs(vec, s, /*allowQuotedStings=*/false);
0050: } catch (StringScanException e) {
0051: e.printStackTrace();
0052: System.exit(1);
0053: }
0054: String[] result = new String[vec.size()];
0055: for (int i = 0; i < vec.size(); i++) {
0056: result[i] = (String) vec.get(i);
0057: }
0058: return result;
0059: }
0060:
0061: static class RngCheck {
0062: ArgParser.RangePnt low = null;
0063: ArgParser.RangePnt high = null;
0064: int type;
0065:
0066: RngCheck(String s) {
0067: low = new ArgParser.RangePnt(s, CLOSED);
0068: type = 's';
0069: }
0070:
0071: RngCheck(double d) {
0072: low = new ArgParser.RangePnt(d, CLOSED);
0073: type = 'd';
0074: }
0075:
0076: RngCheck(long l) {
0077: low = new ArgParser.RangePnt(l, CLOSED);
0078: type = 'l';
0079: }
0080:
0081: RngCheck(boolean b) {
0082: low = new ArgParser.RangePnt(b, CLOSED);
0083: type = 'b';
0084: }
0085:
0086: RngCheck(String s1, boolean c1, String s2, boolean c2) {
0087: low = new ArgParser.RangePnt(s1, c1);
0088: high = new ArgParser.RangePnt(s2, c2);
0089: type = 's';
0090: }
0091:
0092: RngCheck(double d1, boolean c1, double d2, boolean c2) {
0093: low = new ArgParser.RangePnt(d1, c1);
0094: high = new ArgParser.RangePnt(d2, c2);
0095: type = 'd';
0096: }
0097:
0098: RngCheck(long l1, boolean c1, long l2, boolean c2) {
0099: low = new ArgParser.RangePnt(l1, c1);
0100: high = new ArgParser.RangePnt(l2, c2);
0101: type = 'l';
0102: }
0103:
0104: void check(ArgParser.RangeAtom ra) {
0105: verify((ra.low == null) == (low == null), "(ra.low==null)="
0106: + (ra.low == null) + "(low==null)=" + (low == null));
0107: verify((ra.high == null) == (high == null),
0108: "(ra.high==null)=" + (ra.high == null)
0109: + "(high==null)=" + (high == null));
0110:
0111: if (ra.low != null) {
0112: switch (type) {
0113: case 'l': {
0114: verify(ra.low.lval == low.lval, "ra.low=" + ra.low
0115: + " low=" + low);
0116: break;
0117: }
0118: case 'd': {
0119: verify(ra.low.dval == low.dval, "ra.low=" + ra.low
0120: + " low=" + low);
0121: break;
0122: }
0123: case 's': {
0124: verify(ra.low.sval.equals(low.sval), "ra.low="
0125: + ra.low + " low=" + low);
0126: break;
0127: }
0128: case 'b': {
0129: verify(ra.low.bval == low.bval, "ra.low=" + ra.low
0130: + " low=" + low);
0131: break;
0132: }
0133: }
0134: verify(ra.low.closed == low.closed, "ra.low=" + ra.low
0135: + " low=" + low);
0136: }
0137: if (ra.high != null) {
0138: switch (type) {
0139: case 'l': {
0140: verify(ra.high.lval == high.lval, "ra.high="
0141: + ra.high + " high=" + high);
0142: break;
0143: }
0144: case 'd': {
0145: verify(ra.high.dval == high.dval, "ra.high="
0146: + ra.high + " high=" + high);
0147: break;
0148: }
0149: case 's': {
0150: verify(ra.high.sval.equals(high.sval), "ra.high="
0151: + ra.high + " high=" + high);
0152: break;
0153: }
0154: case 'b': {
0155: verify(ra.high.bval == high.bval, "ra.high="
0156: + ra.high + " high=" + high);
0157: break;
0158: }
0159: }
0160: verify(ra.high.closed == high.closed, "ra.high="
0161: + ra.high + " high=" + high);
0162: }
0163: }
0164: }
0165:
0166: ArgParserTest() {
0167: parser = new ArgParser("fubar");
0168: }
0169:
0170: static void checkException(Exception e, String errmsg) {
0171: if (errmsg != null) {
0172: if (!e.getMessage().equals(errmsg)) {
0173: System.out.println("Expecting exception '" + errmsg
0174: + "' but got '" + e.getMessage() + "'");
0175: e.printStackTrace();
0176: (new Throwable()).printStackTrace();
0177: System.exit(1);
0178: }
0179: } else {
0180: System.out.println("Unexpected exception '"
0181: + e.getMessage() + "'");
0182: e.printStackTrace();
0183: (new Throwable()).printStackTrace();
0184: System.exit(1);
0185: }
0186: }
0187:
0188: void checkPrintHelp(String msg) {
0189: ByteArrayOutputStream buf = new ByteArrayOutputStream(0x10000);
0190: PrintStream ps = new PrintStream(buf);
0191: ps.println(parser.getHelpMessage());
0192: System.out.print(buf.toString());
0193: }
0194:
0195: // void checkGetSynopsis (String msg)
0196: // {
0197: // ByteArrayOutputStream buf = new ByteArrayOutputStream(0x10000);
0198: // PrintStream ps = new PrintStream(buf);
0199: // parser.printSynopsis (ps, 80);
0200: // System.out.print (buf.toString());
0201: // }
0202:
0203: void checkAdd(String s, Object resHolder, String errmsg) {
0204: checkAdd(s, resHolder, 0, 0, null, null, null, errmsg);
0205: }
0206:
0207: void add(String s, Object resHolder) {
0208: try {
0209: parser.addOption(s, resHolder);
0210: } catch (Exception e) {
0211: e.printStackTrace();
0212: System.exit(1);
0213: }
0214: }
0215:
0216: void checkStringArray(String msg, String[] strs, String[] check) {
0217: boolean dontMatch = false;
0218: if (strs.length != check.length) {
0219: dontMatch = true;
0220: } else {
0221: for (int i = 0; i < strs.length; i++) {
0222: if (!strs[i].equals(check[i])) {
0223: dontMatch = true;
0224: break;
0225: }
0226: }
0227: }
0228: if (dontMatch) {
0229: System.out.println(msg);
0230: System.out.print("Expected: ");
0231: for (int i = 0; i < check.length; i++) {
0232: System.out.print("'" + check[i] + "'");
0233: if (i < check.length - 1) {
0234: System.out.print(" ");
0235: }
0236: }
0237: System.out.println("");
0238: System.out.print("Got: ");
0239: for (int i = 0; i < strs.length; i++) {
0240: System.out.print("'" + strs[i] + "'");
0241: if (i < strs.length - 1) {
0242: System.out.print(" ");
0243: }
0244: }
0245: System.out.println("");
0246: System.exit(1);
0247: }
0248: }
0249:
0250: void checkAdd(String s, Object resHolder, int code, int numValues,
0251: Object names, RngCheck[] rngCheck, String helpMsg,
0252: String errmsg) {
0253: boolean exceptionThrown = false;
0254: String[] namelist = null;
0255: try {
0256: parser.addOption(s, resHolder);
0257: } catch (Exception e) {
0258: exceptionThrown = true;
0259: checkException(e, errmsg);
0260: }
0261: if (names instanceof String) {
0262: namelist = new String[] { (String) names };
0263: } else {
0264: namelist = (String[]) names;
0265: }
0266: if (!exceptionThrown) {
0267: verify(errmsg == null, "Expecting exception " + errmsg);
0268: ArgParser.Record rec = parser.lastMatchRecord();
0269: verify(rec.convertCode == code, "code=" + rec.convertCode
0270: + ", expecting " + code);
0271: ArgParser.NameDesc nd;
0272: int i = 0;
0273: for (nd = rec.firstNameDesc(); nd != null; nd = nd.next) {
0274: i++;
0275: }
0276: verify(i == namelist.length, "numNames=" + i
0277: + ", expecting " + namelist.length);
0278: i = 0;
0279: for (nd = rec.firstNameDesc(); nd != null; nd = nd.next) {
0280: String ss;
0281: if (!nd.oneWord) {
0282: ss = new String(nd.name) + ' ';
0283: } else {
0284: ss = nd.name;
0285: }
0286: verify(ss.equals(namelist[i]), "have name '" + ss
0287: + "', expecting '" + namelist[i] + "'");
0288: i++;
0289: }
0290: ArgParser.RangeAtom ra;
0291: i = 0;
0292: for (ra = rec.firstRangeAtom(); ra != null; ra = ra.next) {
0293: i++;
0294: }
0295: int expectedRangeNum = 0;
0296: if (rngCheck != null) {
0297: expectedRangeNum = rngCheck.length;
0298: }
0299: verify(i == expectedRangeNum, "numRangeAtoms=" + i
0300: + ", expecting " + expectedRangeNum);
0301: i = 0;
0302: for (ra = rec.firstRangeAtom(); ra != null; ra = ra.next) {
0303: rngCheck[i++].check(ra);
0304: }
0305: verify(rec.helpMsg.equals(helpMsg), "helpMsg="
0306: + rec.helpMsg + ", expecting " + helpMsg);
0307: verify(rec.numValues == numValues, "numValues="
0308: + rec.numValues + ", expecting " + numValues);
0309: }
0310: }
0311:
0312: double getDoubleValue(Object obj, int k) {
0313: if (obj instanceof DoubleHolder) {
0314: return ((DoubleHolder) obj).value;
0315: } else if (obj instanceof FloatHolder) {
0316: return ((FloatHolder) obj).value;
0317: } else if (obj instanceof double[]) {
0318: return ((double[]) obj)[k];
0319: } else if (obj instanceof float[]) {
0320: return ((float[]) obj)[k];
0321: } else {
0322: verify(false, "object doesn't contain double values");
0323: return 0;
0324: }
0325: }
0326:
0327: long getLongValue(Object obj, int k) {
0328: if (obj instanceof LongHolder) {
0329: return ((LongHolder) obj).value;
0330: } else if (obj instanceof IntHolder) {
0331: return ((IntHolder) obj).value;
0332: } else if (obj instanceof long[]) {
0333: return ((long[]) obj)[k];
0334: } else if (obj instanceof int[]) {
0335: return ((int[]) obj)[k];
0336: } else {
0337: verify(false, "object doesn't contain long values");
0338: return 0;
0339: }
0340: }
0341:
0342: String getStringValue(Object obj, int k) {
0343: if (obj instanceof StringHolder) {
0344: return ((StringHolder) obj).value;
0345: } else if (obj instanceof String[]) {
0346: return ((String[]) obj)[k];
0347: } else {
0348: verify(false, "object doesn't contain String values");
0349: return null;
0350: }
0351: }
0352:
0353: boolean getBooleanValue(Object obj, int k) {
0354: if (obj instanceof BooleanHolder) {
0355: return ((BooleanHolder) obj).value;
0356: } else if (obj instanceof boolean[]) {
0357: return ((boolean[]) obj)[k];
0358: } else {
0359: verify(false, "object doesn't contain boolean values");
0360: return false;
0361: }
0362: }
0363:
0364: char getCharValue(Object obj, int k) {
0365: if (obj instanceof CharHolder) {
0366: return ((CharHolder) obj).value;
0367: } else if (obj instanceof char[]) {
0368: return ((char[]) obj)[k];
0369: } else {
0370: verify(false, "object doesn't contain char values");
0371: return 0;
0372: }
0373: }
0374:
0375: static class MErr {
0376: int code;
0377: String valStr;
0378:
0379: MErr(int code, String valStr) {
0380: this .code = code;
0381: this .valStr = valStr;
0382: }
0383: }
0384:
0385: static class MTest {
0386: String args;
0387: Object result;
0388: int resultIdx;
0389:
0390: MTest(String args, Object result) {
0391: this (args, result, -1);
0392: }
0393:
0394: MTest(String args, Object result, int resultIdx) {
0395: this .args = args;
0396: this .result = result;
0397: this .resultIdx = resultIdx;
0398: }
0399: };
0400:
0401: void checkMatch(String args[], int idx, String errMsg) {
0402: getMatchResult(args, idx, -1, errMsg, -1);
0403: }
0404:
0405: void checkMatch(String args[], int idx, int cnt, long check,
0406: int resultIdx) {
0407: Object rholder = getMatchResult(args, idx, cnt, null, resultIdx);
0408: long result = getLongValue(rholder, 0);
0409: verify(result == check, "result " + result + " vs. " + check);
0410: }
0411:
0412: void checkMatch(String args[], int idx, int cnt, double check,
0413: int resultIdx) {
0414: Object rholder = getMatchResult(args, idx, cnt, null, resultIdx);
0415: double result = getDoubleValue(rholder, 0);
0416: verify(result == check, "result " + result + " vs. " + check);
0417: }
0418:
0419: void checkMatch(String args[], int idx, int cnt, String check,
0420: int resultIdx) {
0421: Object rholder = getMatchResult(args, idx, cnt, null, resultIdx);
0422: String result = getStringValue(rholder, 0);
0423: verify(result.equals(check), "result " + result + " vs. "
0424: + check);
0425: }
0426:
0427: void checkMatch(String args[], int idx, int cnt, boolean check,
0428: int resultIdx) {
0429: Object rholder = getMatchResult(args, idx, cnt, null, resultIdx);
0430: boolean result = getBooleanValue(rholder, 0);
0431: verify(result == check, "result " + result + " vs. " + check);
0432: }
0433:
0434: void checkMatch(String args[], int idx, int cnt, char check,
0435: int resultIdx) {
0436: Object rholder = getMatchResult(args, idx, cnt, null, resultIdx);
0437: char result = getCharValue(rholder, 0);
0438: verify(result == check, "result " + result + " vs. " + check);
0439: }
0440:
0441: void checkMatch(String args[], int idx, int cnt, Object checkArray,
0442: int resultIdx) {
0443: Object rholder = getMatchResult(args, idx, cnt, null, resultIdx);
0444: if (!checkArray.getClass().isArray()) {
0445: verify(false, "check is not an array");
0446: }
0447: for (int i = 0; i < Array.getLength(checkArray); i++) {
0448: if (checkArray instanceof long[]) {
0449: long result = getLongValue(rholder, i);
0450: long check = ((long[]) checkArray)[i];
0451: verify(result == check, "result [" + i + "] " + result
0452: + " vs. " + check);
0453: } else if (checkArray instanceof double[]) {
0454: double result = getDoubleValue(rholder, i);
0455: double check = ((double[]) checkArray)[i];
0456: verify(result == check, "result [" + i + "] " + result
0457: + " vs. " + check);
0458: } else if (checkArray instanceof String[]) {
0459: String result = getStringValue(rholder, i);
0460: String check = ((String[]) checkArray)[i];
0461: verify(result.equals(check), "result [" + i + "] "
0462: + result + " vs. " + check);
0463: } else if (checkArray instanceof boolean[]) {
0464: boolean result = getBooleanValue(rholder, i);
0465: boolean check = ((boolean[]) checkArray)[i];
0466: verify(result == check, "result [" + i + "] " + result
0467: + " vs. " + check);
0468: } else if (checkArray instanceof char[]) {
0469: char result = getCharValue(rholder, i);
0470: char check = ((char[]) checkArray)[i];
0471: verify(result == check, "result [" + i + "] " + result
0472: + " vs. " + check);
0473: } else {
0474: verify(false, "unknown type for checkArray");
0475: }
0476: }
0477: }
0478:
0479: void checkMatch(MTest test, boolean oneWord) {
0480: String[] argv;
0481: if (oneWord) {
0482: argv = new String[1];
0483: argv[0] = test.args;
0484: } else {
0485: argv = argsFromString(test.args);
0486: }
0487: if (test.result instanceof Long) {
0488: checkMatch(argv, 0, argv.length, ((Long) test.result)
0489: .longValue(), test.resultIdx);
0490: } else if (test.result instanceof Double) {
0491: checkMatch(argv, 0, argv.length, ((Double) test.result)
0492: .doubleValue(), test.resultIdx);
0493: } else if (test.result instanceof String) {
0494: checkMatch(argv, 0, argv.length, (String) test.result,
0495: test.resultIdx);
0496: } else if (test.result instanceof Boolean) {
0497: checkMatch(argv, 0, argv.length, ((Boolean) test.result)
0498: .booleanValue(), test.resultIdx);
0499: } else if (test.result instanceof Character) {
0500: checkMatch(argv, 0, argv.length, ((Character) test.result)
0501: .charValue(), test.resultIdx);
0502: } else if (test.result.getClass().isArray()) {
0503: checkMatch(argv, 0, argv.length, test.result,
0504: test.resultIdx);
0505: } else if (test.result instanceof MErr) {
0506: MErr err = (MErr) test.result;
0507: String argname = parser.getOptionName(argv[0]);
0508: String msg = "";
0509:
0510: switch (err.code) {
0511: case 'c': {
0512: msg = "requires a contiguous value";
0513: break;
0514: }
0515: case 'm': {
0516: msg = "malformed " + parser.getOptionTypeName(argv[0])
0517: + " '" + err.valStr + "'";
0518: break;
0519: }
0520: case 'r': {
0521: msg = "value '" + err.valStr + "' not in range "
0522: + parser.getOptionRangeDesc(argv[0]);
0523: break;
0524: }
0525: case 'v': {
0526: msg = "requires " + err.valStr + " values";
0527: break;
0528: }
0529: }
0530: checkMatch(argv, 0, argname + ": " + msg);
0531: } else {
0532: verify(false, "Unknown result type");
0533: }
0534: }
0535:
0536: void checkMatches(MTest[] tests, boolean oneWord) {
0537: for (int i = 0; i < tests.length; i++) {
0538: checkMatch(tests[i], oneWord);
0539: }
0540: }
0541:
0542: Object getMatchResult(String args[], int idx, int cnt,
0543: String errMsg, int resultIdx) {
0544: boolean exceptionThrown = false;
0545: int k = 0;
0546: try {
0547: k = parser.matchArg(args, idx);
0548: } catch (Exception e) {
0549: exceptionThrown = true;
0550: checkException(e, errMsg);
0551: }
0552: if (!exceptionThrown) {
0553: verify(k == idx + cnt, "Expecting result index "
0554: + (idx + cnt) + ", got " + k);
0555: Object result = parser.getResultHolder(args[0]);
0556: if (resultIdx >= 0) {
0557: verify(result instanceof Vector,
0558: "Expecting result to be stored in a vector");
0559: Vector vec = (Vector) result;
0560: verify(vec.size() == resultIdx + 1,
0561: "Expecting result vector size "
0562: + (resultIdx + 1));
0563: return vec.get(resultIdx);
0564: } else {
0565: return result;
0566: }
0567: } else {
0568: return null;
0569: }
0570: }
0571:
0572: /**
0573: * Runs a set of tests to verify correct operation of the
0574: * ArgParser class. If all the tests run correctly, the
0575: * program prints the message <code>Passed</code> and terminates.
0576: * Otherwise, diagnostic information is printed at the first
0577: * point of failure.
0578: */
0579: public static void main(String[] args) {
0580: ArgParserTest test = new ArgParserTest();
0581:
0582: BooleanHolder bh = new BooleanHolder();
0583: boolean[] b3 = new boolean[3];
0584: CharHolder ch = new CharHolder();
0585: char[] c3 = new char[3];
0586: IntHolder ih = new IntHolder();
0587: int[] i3 = new int[3];
0588: LongHolder lh = new LongHolder();
0589: long[] l3 = new long[3];
0590: FloatHolder fh = new FloatHolder();
0591: float[] f3 = new float[3];
0592: DoubleHolder dh = new DoubleHolder();
0593: double[] d3 = new double[3];
0594: StringHolder sh = new StringHolder();
0595: String[] s3 = new String[3];
0596:
0597: test.checkAdd(
0598: "-foo %i{[0,10)}X3 #sets the value of foo",
0599: // 0123456789012345
0600: i3, 'i', 3, new String[] { "-foo " },
0601: new RngCheck[] { new RngCheck(0, CLOSED, 10, OPEN) },
0602: "sets the value of foo", null);
0603:
0604: test.checkAdd("-arg1,,", null, "Null option name given");
0605: test.checkAdd("-arg1,,goo %f ", null, "Null option name given");
0606: test.checkAdd(" ", null, "Null option name given");
0607: test.checkAdd("", null, "Null option name given");
0608: test.checkAdd(" %v", null, "Null option name given");
0609: test.checkAdd("-foo ", null, "No conversion character given");
0610: test.checkAdd("-foo %", null, "No conversion character given");
0611: test.checkAdd("foo, aaa bbb ", null,
0612: "Names not separated by ','");
0613: test
0614: .checkAdd(" foo aaa %d", null,
0615: "Names not separated by ','");
0616: test.checkAdd("-arg1,-b,", null, "Null option name given");
0617: test
0618: .checkAdd("-arg1,-b", null,
0619: "No conversion character given");
0620: test.checkAdd("-arg1 ", null, "No conversion character given");
0621: test.checkAdd("-arg1, %v", null, "Null option name given");
0622: test.checkAdd("-arg1,%v", null, "Null option name given");
0623: test.checkAdd("-foo %V", null,
0624: "Conversion code 'V' not one of 'iodxcbfsvh'");
0625: test.checkAdd("-h %hX5", null,
0626: "Multipliers not supported for %h");
0627: test.checkAdd("-h %h{}", null, "Ranges not supported for %h");
0628: test.checkAdd("-help, -h %h #here is how we help you", null,
0629: 'h', 1, new String[] { "-help ", "-h " }, null,
0630: "here is how we help you", null);
0631:
0632: test.checkAdd("-arg1 ,-arg2=%d{0,3,(7,16]}X1 #x3 test", l3,
0633: 'd', 1, new String[] { "-arg1 ", "-arg2=" },
0634: new RngCheck[] { new RngCheck(0), new RngCheck(3),
0635: new RngCheck(7, OPEN, 16, CLOSED), },
0636: "x3 test", null);
0637:
0638: test.checkAdd("bbb,ccc%x{[1,2]} #X3 x3 test", l3, 'x', 1,
0639: new String[] { "bbb", "ccc" },
0640: new RngCheck[] { new RngCheck(1, CLOSED, 2, CLOSED), },
0641: "X3 x3 test", null);
0642:
0643: test.checkAdd(" bbb ,ccc, ddd ,e , f=%bX1 #x3 test", b3,
0644: 'b', 1, new String[] { "bbb ", "ccc", "ddd ", "e ",
0645: "f=" }, null, "x3 test", null);
0646:
0647: test.checkAdd(" bbb ,ccc, ddd ,e , f= %bX3 #x3 test", b3,
0648: 'b', 3, new String[] { "bbb ", "ccc ", "ddd ", "e ",
0649: "f= " }, null, "x3 test", null);
0650:
0651: test
0652: .checkAdd(
0653: "-b,--bar %s{[\"john\",\"jerry\"),fred,\"harry\"} #sets bar",
0654: sh, 's', 1, new String[] { "-b ", "--bar " },
0655: new RngCheck[] {
0656: new RngCheck("jerry", OPEN, "john",
0657: CLOSED), new RngCheck("fred"),
0658: new RngCheck("harry") }, "sets bar",
0659: null);
0660:
0661: test.checkAdd("-c ,coven%f{0.0,9.0,(6,5],[-9.1,10.2]} ", dh,
0662: 'f', 1, new String[] { "-c ", "coven" },
0663: new RngCheck[] { new RngCheck(0.0), new RngCheck(9.0),
0664: new RngCheck(5.0, CLOSED, 6.0, OPEN),
0665: new RngCheck(-9.1, CLOSED, 10.2, CLOSED) }, "",
0666: null);
0667:
0668: test.checkAdd("-b %b #a boolean value ", bh, 'b', 1,
0669: new String[] { "-b " }, new RngCheck[] {},
0670: "a boolean value ", null);
0671:
0672: test.checkAdd("-a %i", ih, 'i', 1, "-a ", null, "", null);
0673: test.checkAdd("-a %o", lh, 'o', 1, "-a ", null, "", null);
0674: test.checkAdd("-a %d", i3, 'd', 1, "-a ", null, "", null);
0675: test.checkAdd("-a %x", l3, 'x', 1, "-a ", null, "", null);
0676: test.checkAdd("-a %c", ch, 'c', 1, "-a ", null, "", null);
0677: test.checkAdd("-a %c", c3, 'c', 1, "-a ", null, "", null);
0678: test.checkAdd("-a %v", bh, 'v', 1, "-a ", null, "", null);
0679: test.checkAdd("-a %b", b3, 'b', 1, "-a ", null, "", null);
0680: test.checkAdd("-a %f", fh, 'f', 1, "-a ", null, "", null);
0681: test.checkAdd("-a %f", f3, 'f', 1, "-a ", null, "", null);
0682: test.checkAdd("-a %f", dh, 'f', 1, "-a ", null, "", null);
0683: test.checkAdd("-a %f", d3, 'f', 1, "-a ", null, "", null);
0684:
0685: test.checkAdd("-a %i", fh, 'i', 1, "-a ", null, "",
0686: "Invalid result holder for %i");
0687: test.checkAdd("-a %c", i3, 'c', 1, "-a ", null, "",
0688: "Invalid result holder for %c");
0689: test.checkAdd("-a %v", d3, 'v', 1, "-a ", null, "",
0690: "Invalid result holder for %v");
0691: test.checkAdd("-a %f", sh, 'f', 1, "-a ", null, "",
0692: "Invalid result holder for %f");
0693: test.checkAdd("-a %s", l3, 's', 1, "-a ", null, "",
0694: "Invalid result holder for %s");
0695:
0696: test
0697: .checkAdd("-foo %i{} ", ih, 'i', 1, "-foo ", null, "",
0698: null);
0699: test.checkAdd("-foo%i{}", ih, 'i', 1, "-foo", null, "", null);
0700: test.checkAdd("-foo%i{ }", ih, 'i', 1, "-foo", null, "", null);
0701: test.checkAdd("-foo%i{ }}", ih,
0702: "Illegal character(s), expecting '#'");
0703: test.checkAdd("-foo%i{ ", ih,
0704: "Unterminated range specification");
0705: test
0706: .checkAdd("-foo%i{", ih,
0707: "Unterminated range specification");
0708: test.checkAdd("-foo%i{0,9", ih,
0709: "Unterminated range specification");
0710: test.checkAdd("-foo%i{1,2,3)", ih,
0711: "Unterminated range specification");
0712:
0713: test.checkAdd("-b %f{0.9}", fh, 'f', 1, "-b ",
0714: new RngCheck[] { new RngCheck(0.9) }, "", null);
0715: test.checkAdd("-b %f{ 0.9 ,7, -0.5,-4 ,6 }", fh, 'f', 1, "-b ",
0716: new RngCheck[] { new RngCheck(0.9), new RngCheck(7.0),
0717: new RngCheck(-0.5), new RngCheck(-4.0),
0718: new RngCheck(6.0) }, "", null);
0719: test.checkAdd("-b %f{ [0.9,7), (-0.5,-4),[9,6] , (10,13.4] }",
0720: fh, 'f', 1, "-b ", new RngCheck[] {
0721: new RngCheck(0.9, CLOSED, 7.0, OPEN),
0722: new RngCheck(-4.0, OPEN, -.5, OPEN),
0723: new RngCheck(6.0, CLOSED, 9.0, CLOSED),
0724: new RngCheck(10.0, OPEN, 13.4, CLOSED), }, "",
0725: null);
0726: test.checkAdd("-b %f{(8 9]}", fh,
0727: "Missing ',' in subrange specification");
0728: test.checkAdd("-b %f{(8,9,]}", fh, "Unterminated subrange");
0729: test.checkAdd("-b %f{(8,9 ,]}", fh, "Unterminated subrange");
0730: test.checkAdd("-b %f{(8,9 8]}", fh, "Unterminated subrange");
0731: test.checkAdd("-b %f{8 9}", fh,
0732: "Range spec: ',' or '}' expected");
0733: test.checkAdd("-b %f{8 *}", fh,
0734: "Range spec: ',' or '}' expected");
0735:
0736: test.checkAdd("-b %f{8y}", fh,
0737: "Range spec: ',' or '}' expected");
0738: test.checkAdd("-b %f{.}", fh,
0739: "Malformed float '.}' in range spec");
0740: test.checkAdd("-b %f{1.0e}", fh,
0741: "Malformed float '1.0e}' in range spec");
0742: test.checkAdd("-b %f{[*]}", fh,
0743: "Malformed float '*' in range spec");
0744: test.checkAdd("-b %f{1.2e5t}", fh,
0745: "Range spec: ',' or '}' expected");
0746:
0747: test.checkAdd("-b %i{8}", ih, 'i', 1, "-b ",
0748: new RngCheck[] { new RngCheck(8) }, "", null);
0749: test.checkAdd("-b %i{8, 9,10 }", ih, 'i', 1, "-b ",
0750: new RngCheck[] { new RngCheck(8), new RngCheck(9),
0751: new RngCheck(10) }, "", null);
0752: test.checkAdd("-b %i{8, [-9,10),[-17,15],(2,-33),(8,9] }", ih,
0753: 'i', 1, "-b ", new RngCheck[] { new RngCheck(8),
0754: new RngCheck(-9, CLOSED, 10, OPEN),
0755: new RngCheck(-17, CLOSED, 15, CLOSED),
0756: new RngCheck(-33, OPEN, 2, OPEN),
0757: new RngCheck(8, OPEN, 9, CLOSED), }, "", null);
0758: test.checkAdd("-b %i{8.7}", ih,
0759: "Range spec: ',' or '}' expected");
0760: test.checkAdd("-b %i{6,[*]}", ih,
0761: "Malformed integer '*' in range spec");
0762: test.checkAdd("-b %i{g76}", ih,
0763: "Malformed integer 'g' in range spec");
0764:
0765: test.checkAdd("-b %s{foobar}", sh, 's', 1, "-b ",
0766: new RngCheck[] { new RngCheck("foobar") }, "", null);
0767: test.checkAdd("-b %s{foobar, 0x233,\" \"}", sh, 's', 1, "-b ",
0768: new RngCheck[] { new RngCheck("foobar"),
0769: new RngCheck("0x233"), new RngCheck(" ") },
0770: "", null);
0771: test.checkAdd("-b %s{foobar,(bb,aa], [\"01\",02]}", sh, 's', 1,
0772: "-b ", new RngCheck[] { new RngCheck("foobar"),
0773: new RngCheck("aa", CLOSED, "bb", OPEN),
0774: new RngCheck("01", CLOSED, "02", CLOSED), },
0775: "", null);
0776:
0777: test.checkAdd("-b %c{'a'}", ch, 'c', 1, "-b ",
0778: new RngCheck[] { new RngCheck('a') }, "", null);
0779: test.checkAdd("-b %c{'\\n', '\\002', 'B'}", ch, 'c', 1, "-b ",
0780: new RngCheck[] { new RngCheck('\n'),
0781: new RngCheck('\002'), new RngCheck('B') }, "",
0782: null);
0783: test.checkAdd("-b %c{'q',('g','a'], ['\t','\\003']}", ch, 'c',
0784: 1, "-b ", new RngCheck[] { new RngCheck('q'),
0785: new RngCheck('a', CLOSED, 'g', OPEN),
0786: new RngCheck('\003', CLOSED, '\t', CLOSED), },
0787: "", null);
0788:
0789: test.checkAdd("-b %b{true}X2", b3, 'b', 2, "-b ",
0790: new RngCheck[] { new RngCheck(true) }, "", null);
0791: test.checkAdd("-b %b{ true , false, true }", bh, 'b', 1, "-b ",
0792: new RngCheck[] { new RngCheck(true),
0793: new RngCheck(false), new RngCheck(true) }, "",
0794: null);
0795: test.checkAdd("-b %v{true,[true,false)}", bh,
0796: "Sub ranges not supported for %b or %v");
0797: test.checkAdd("-b %v{true,[]}", bh,
0798: "Sub ranges not supported for %b or %v");
0799: test.checkAdd("-b %b{tru}", bh,
0800: "Malformed boolean 'tru}' in range spec");
0801:
0802: test.checkAdd("-b %iX2", i3, 'i', 2, "-b ", null, "", null);
0803: test.checkAdd("-b %vX3", b3, 'v', 3, "-b ", null, "", null);
0804: test.checkAdd("-b %v{ }X3", b3, 'v', 3, "-b ", null, "", null);
0805:
0806: test
0807: .checkAdd("-b=%iX2", i3, 'i', 2, "-b", null, "",
0808: "Multiplier value incompatible with one word option -b=");
0809: test.checkAdd("-b %iX0", i3, 'i', 0, "-b ", null, "",
0810: "Value multiplier number must be > 0");
0811: test.checkAdd("-b %iX-6", i3, 'i', 0, "-b ", null, "",
0812: "Value multiplier number must be > 0");
0813: test.checkAdd("-b %iXy", i3, 'i', 0, "-b ", null, "",
0814: "Malformed value multiplier");
0815: test.checkAdd("-b %iX4", i3, 'i', 4, "-b ", null, "",
0816: "Result holder array must have a length >= 4");
0817: test
0818: .checkAdd("-b %iX4", ih, 'i', 4, "-b ", null, "",
0819: "Multiplier requires result holder to be an array of length >= 4");
0820:
0821: test.checkAdd("-b %i #X4", ih, 'i', 1, "-b ", null, "X4", null);
0822: test.checkAdd("-b %i #[}X4", ih, 'i', 1, "-b ", null, "[}X4",
0823: null);
0824:
0825: // test.checkPrintHelp("");
0826: // test.checkPrintUsage("");
0827:
0828: test = new ArgParserTest();
0829:
0830: test
0831: .checkAdd(
0832: "-intarg %i{1,2,(9,18],[22,27],[33,38),(45,48)} #test int arg",
0833: ih, 'i', 1, "-intarg ", new RngCheck[] {
0834: new RngCheck(1), new RngCheck(2),
0835: new RngCheck(9, OPEN, 18, CLOSED),
0836: new RngCheck(22, CLOSED, 27, CLOSED),
0837: new RngCheck(33, CLOSED, 38, OPEN),
0838: new RngCheck(45, OPEN, 48, OPEN), },
0839: "test int arg", null);
0840:
0841: MTest[] tests;
0842:
0843: tests = new MTest[] { new MTest("-intarg 1", new Long(1)),
0844: new MTest("-intarg 3", new MErr('r', "3")),
0845: new MTest("-intarg 9", new MErr('r', "9")),
0846: new MTest("-intarg 11", new Long(11)),
0847: new MTest("-intarg 18", new Long(18)),
0848: new MTest("-intarg 22", new Long(22)),
0849: new MTest("-intarg 25", new Long(25)),
0850: new MTest("-intarg 27", new Long(27)),
0851: new MTest("-intarg 33", new Long(33)),
0852: new MTest("-intarg 35", new Long(35)),
0853: new MTest("-intarg 38", new MErr('r', "38")),
0854: new MTest("-intarg 45", new MErr('r', "45")),
0855: new MTest("-intarg 46", new Long(46)),
0856: new MTest("-intarg 48", new MErr('r', "48")),
0857: new MTest("-intarg 100", new MErr('r', "100")),
0858: new MTest("-intarg 0xbeef", new MErr('r', "0xbeef")),
0859: new MTest("-intarg 0x2f", new Long(0x2f)),
0860: new MTest("-intarg 041", new Long(041)), };
0861: test.checkMatches(tests, MULTI_WORD);
0862:
0863: test
0864: .checkAdd(
0865: "-farg %f{1,2,(9,18],[22,27],[33,38),(45,48)} #test float arg",
0866: dh,
0867: 'f',
0868: 1,
0869: "-farg ",
0870: new RngCheck[] {
0871: new RngCheck(1.0),
0872: new RngCheck(2.0),
0873: new RngCheck(9.0, OPEN, 18.0, CLOSED),
0874: new RngCheck(22.0, CLOSED, 27.0, CLOSED),
0875: new RngCheck(33.0, CLOSED, 38.0, OPEN),
0876: new RngCheck(45.0, OPEN, 48.0, OPEN), },
0877: "test float arg", null);
0878:
0879: tests = new MTest[] { new MTest("-farg 1", new Double(1)),
0880: new MTest("-farg 3", new MErr('r', "3")),
0881: new MTest("-farg 9", new MErr('r', "9")),
0882: new MTest("-farg 9.0001", new Double(9.0001)),
0883: new MTest("-farg 11", new Double(11)),
0884: new MTest("-farg 18", new Double(18)),
0885: new MTest("-farg 22", new Double(22)),
0886: new MTest("-farg 25", new Double(25)),
0887: new MTest("-farg 27", new Double(27)),
0888: new MTest("-farg 33", new Double(33)),
0889: new MTest("-farg 35", new Double(35)),
0890: new MTest("-farg 37.9999", new Double(37.9999)),
0891: new MTest("-farg 38", new MErr('r', "38")),
0892: new MTest("-farg 45", new MErr('r', "45")),
0893: new MTest("-farg 45.0001", new Double(45.0001)),
0894: new MTest("-farg 46", new Double(46)),
0895: new MTest("-farg 47.9999", new Double(47.9999)),
0896: new MTest("-farg 48", new MErr('r', "48")),
0897: new MTest("-farg 100", new MErr('r', "100")),
0898: new MTest("-farg 0", new MErr('r', "0")), };
0899: test.checkMatches(tests, MULTI_WORD);
0900:
0901: test
0902: .checkAdd(
0903: "-sarg %s{1,2,(AA,AZ],[BB,BX],[C3,C8),(d5,d8)} #test string arg",
0904: s3,
0905: 's',
0906: 1,
0907: "-sarg ",
0908: new RngCheck[] {
0909: new RngCheck("1"),
0910: new RngCheck("2"),
0911: new RngCheck("AA", OPEN, "AZ", CLOSED),
0912: new RngCheck("BB", CLOSED, "BX", CLOSED),
0913: new RngCheck("C3", CLOSED, "C8", OPEN),
0914: new RngCheck("d5", OPEN, "d8", OPEN), },
0915: "test string arg", null);
0916:
0917: tests = new MTest[] { new MTest("-sarg 1", "1"),
0918: new MTest("-sarg 3", new MErr('r', "3")),
0919: new MTest("-sarg AA", new MErr('r', "AA")),
0920: new MTest("-sarg AM", "AM"),
0921: new MTest("-sarg AZ", "AZ"),
0922: new MTest("-sarg BB", "BB"),
0923: new MTest("-sarg BL", "BL"),
0924: new MTest("-sarg BX", "BX"),
0925: new MTest("-sarg C3", "C3"),
0926: new MTest("-sarg C6", "C6"),
0927: new MTest("-sarg C8", new MErr('r', "C8")),
0928: new MTest("-sarg d5", new MErr('r', "d5")),
0929: new MTest("-sarg d6", "d6"),
0930: new MTest("-sarg d8", new MErr('r', "d8")),
0931: new MTest("-sarg zzz", new MErr('r', "zzz")),
0932: new MTest("-sarg 0", new MErr('r', "0")), };
0933: test.checkMatches(tests, MULTI_WORD);
0934:
0935: test = new ArgParserTest();
0936:
0937: test
0938: .checkAdd(
0939: "-carg %c{1,2,(a,z],['A','Z'],['\\001',\\007),(4,8)}",
0940: c3, 'c', 1, "-carg ", new RngCheck[] {
0941: new RngCheck('1'),
0942: new RngCheck('2'),
0943: new RngCheck('a', OPEN, 'z', CLOSED),
0944: new RngCheck('A', CLOSED, 'Z', CLOSED),
0945: new RngCheck('\001', CLOSED, '\007',
0946: OPEN),
0947: new RngCheck('4', OPEN, '8', OPEN), },
0948: "", null);
0949:
0950: tests = new MTest[] { new MTest("-carg 1", new Character('1')),
0951: new MTest("-carg 3", new MErr('r', "3")),
0952: new MTest("-carg a", new MErr('r', "a")),
0953: new MTest("-carg m", new Character('m')),
0954: new MTest("-carg z", new Character('z')),
0955: new MTest("-carg A", new Character('A')),
0956: new MTest("-carg 'L'", new Character('L')),
0957: new MTest("-carg 'Z'", new Character('Z')),
0958: new MTest("-carg \\001", new Character('\001')),
0959: new MTest("-carg \\005", new Character('\005')),
0960: new MTest("-carg '\\007'", new MErr('r', "'\\007'")),
0961: new MTest("-carg '4'", new MErr('r', "'4'")),
0962: new MTest("-carg 6", new Character('6')),
0963: new MTest("-carg 8", new MErr('r', "8")),
0964: new MTest("-carg '\\012'", new MErr('r', "'\\012'")),
0965: new MTest("-carg 0", new MErr('r', "0")), };
0966: test.checkMatches(tests, MULTI_WORD);
0967:
0968: test
0969: .checkAdd("-foo=%i{[-50,100]}", ih, 'i', 1, "-foo=",
0970: new RngCheck[] { new RngCheck(-50, CLOSED, 100,
0971: CLOSED), }, "", null);
0972:
0973: tests = new MTest[] {
0974: new MTest("-foo=-51", new MErr('r', "-51")),
0975: new MTest("-foo=-0x32", new Long(-0x32)),
0976: new MTest("-foo=-0x33", new MErr('r', "-0x33")),
0977: new MTest("-foo=-0777", new MErr('r', "-0777")),
0978: new MTest("-foo=-07", new Long(-07)),
0979: new MTest("-foo=0", new Long(0)),
0980: new MTest("-foo=100", new Long(100)),
0981: new MTest("-foo=0x5e", new Long(0x5e)),
0982: new MTest("-foo=066", new Long(066)),
0983: new MTest("-foo=06677", new MErr('r', "06677")),
0984: new MTest("-foo=0xbeef", new MErr('r', "0xbeef")),
0985: new MTest("-foo=foo", new MErr('m', "foo")),
0986: new MTest("-foo=-51d", new MErr('m', "-51d")), };
0987: test.checkMatches(tests, ONE_WORD);
0988:
0989: test.checkAdd("-foo2=%i", ih, 'i', 1, "-foo2=", null, "", null);
0990: tests = new MTest[] { new MTest("-foo2=-51", new Long(-51)),
0991: new MTest("-foo2=-0x33", new Long(-0x33)),
0992: new MTest("-foo2=-0777", new Long(-0777)),
0993: new MTest("-foo2=06677", new Long(06677)),
0994: new MTest("-foo2=0xbeef", new Long(0xbeef)),
0995: new MTest("-foo2=foo", new MErr('m', "foo")),
0996: new MTest("-foo2=-51d", new MErr('m', "-51d")),
0997: new MTest("-foo2=-51", new Long(-51)), };
0998: test.checkMatches(tests, ONE_WORD);
0999:
1000: test.checkAdd("-foo3 %iX3", i3, 'i', 3, "-foo3 ", null, "",
1001: null);
1002: tests = new MTest[] {
1003: new MTest("-foo3 -51 678 0x45", new long[] { -51, 678,
1004: 0x45 }),
1005: new MTest("-foo3 55 16f 55", new MErr('m', "16f")),
1006: new MTest("-foo3 55 16", new MErr('v', "3")), };
1007: test.checkMatches(tests, MULTI_WORD);
1008:
1009: Vector vec = new Vector(100);
1010:
1011: test.checkAdd("-foov3 %iX3", vec, 'i', 3, "-foov3 ", null, "",
1012: null);
1013: tests = new MTest[] {
1014: new MTest("-foov3 -1 2 4", new long[] { -1, 2, 4 }, 0),
1015: new MTest("-foov3 10 3 9", new long[] { 10, 3, 9 }, 1),
1016: new MTest("-foov3 123 1 0", new long[] { 123, 1, 0 }, 2), };
1017: vec.clear();
1018: test.checkMatches(tests, MULTI_WORD);
1019: test
1020: .checkAdd("-foov %i", vec, 'i', 1, "-foov ", null, "",
1021: null);
1022: tests = new MTest[] { new MTest("-foov 11", new Long(11), 0),
1023: new MTest("-foov 12", new Long(12), 1),
1024: new MTest("-foov 13", new Long(13), 2), };
1025: vec.clear();
1026: test.checkMatches(tests, MULTI_WORD);
1027:
1028: test
1029: .checkAdd("-foo4 %i{[-50,100]}X2", i3, 'i', 2,
1030: "-foo4 ", new RngCheck[] { new RngCheck(-50,
1031: CLOSED, 100, CLOSED), }, "", null);
1032: tests = new MTest[] {
1033: new MTest("-foo4 -49 78", new long[] { -49, 78 }),
1034: new MTest("-foo4 -48 102", new MErr('r', "102")), };
1035: test.checkMatches(tests, MULTI_WORD);
1036:
1037: test
1038: .checkAdd("-oct=%o{[-062,0144]}", ih, 'o', 1, "-oct=",
1039: new RngCheck[] { new RngCheck(-50, CLOSED, 100,
1040: CLOSED), }, "", null);
1041:
1042: tests = new MTest[] {
1043: new MTest("-oct=-063", new MErr('r', "-063")),
1044: new MTest("-oct=-0x32", new MErr('m', "-0x32")),
1045: new MTest("-oct=-0777", new MErr('r', "-0777")),
1046: new MTest("-oct=-07", new Long(-07)),
1047: new MTest("-oct=0", new Long(0)),
1048: new MTest("-oct=100", new Long(64)),
1049: new MTest("-oct=0xae", new MErr('m', "0xae")),
1050: new MTest("-oct=66", new Long(066)),
1051: new MTest("-oct=06677", new MErr('r', "06677")),
1052: new MTest("-oct=0xbeef", new MErr('m', "0xbeef")),
1053: new MTest("-oct=foo", new MErr('m', "foo")),
1054: new MTest("-oct=-51d", new MErr('m', "-51d")),
1055: new MTest("-oct=78", new MErr('m', "78")), };
1056: test.checkMatches(tests, ONE_WORD);
1057:
1058: test.checkAdd("-oct2=%o", ih, 'o', 1, "-oct2=", null, "", null);
1059: tests = new MTest[] { new MTest("-oct2=-063", new Long(-063)),
1060: new MTest("-oct2=-0777", new Long(-0777)),
1061: new MTest("-oct2=06677", new Long(06677)), };
1062: test.checkMatches(tests, ONE_WORD);
1063:
1064: test
1065: .checkAdd("-dec=%d{[-0x32,0x64]}", ih, 'd', 1, "-dec=",
1066: new RngCheck[] { new RngCheck(-50, CLOSED, 100,
1067: CLOSED), }, "", null);
1068:
1069: tests = new MTest[] {
1070: new MTest("-dec=-063", new MErr('r', "-063")),
1071: new MTest("-dec=-0x32", new MErr('m', "-0x32")),
1072: new MTest("-dec=-0777", new MErr('r', "-0777")),
1073: new MTest("-dec=-07", new Long(-07)),
1074: new MTest("-dec=0", new Long(0)),
1075: new MTest("-dec=100", new Long(100)),
1076: new MTest("-dec=0xae", new MErr('m', "0xae")),
1077: new MTest("-dec=66", new Long(66)),
1078: new MTest("-dec=06677", new MErr('r', "06677")),
1079: new MTest("-dec=0xbeef", new MErr('m', "0xbeef")),
1080: new MTest("-dec=foo", new MErr('m', "foo")),
1081: new MTest("-dec=-51d", new MErr('m', "-51d")), };
1082: test.checkMatches(tests, ONE_WORD);
1083:
1084: test.checkAdd("-dec2=%d", ih, 'd', 1, "-dec2=", null, "", null);
1085: tests = new MTest[] { new MTest("-dec2=-063", new Long(-63)),
1086: new MTest("-dec2=-0777", new Long(-777)),
1087: new MTest("-dec2=06677", new Long(6677)), };
1088: test.checkMatches(tests, ONE_WORD);
1089:
1090: test
1091: .checkAdd("-hex=%x{[-0x32,0x64]}", ih, 'x', 1, "-hex=",
1092: new RngCheck[] { new RngCheck(-50, CLOSED, 100,
1093: CLOSED), }, "", null);
1094:
1095: tests = new MTest[] { new MTest("-hex=-06", new Long(-0x6)),
1096: new MTest("-hex=-0x3g2", new MErr('m', "-0x3g2")),
1097: new MTest("-hex=-0777", new MErr('r', "-0777")),
1098: new MTest("-hex=-017", new Long(-0x17)),
1099: new MTest("-hex=0", new Long(0)),
1100: new MTest("-hex=64", new Long(0x64)),
1101: new MTest("-hex=5e", new Long(0x5e)),
1102: new MTest("-hex=66", new MErr('r', "66")),
1103: new MTest("-hex=06677", new MErr('r', "06677")),
1104: new MTest("-hex=0xbeef", new MErr('m', "0xbeef")),
1105: new MTest("-hex=foo", new MErr('m', "foo")),
1106: new MTest("-hex=-51d", new MErr('r', "-51d")),
1107: new MTest("-hex=-51g", new MErr('m', "-51g")),
1108: new MTest("-hex=", new MErr('c', "")), };
1109: test.checkMatches(tests, ONE_WORD);
1110:
1111: test.checkAdd("-hex2=%x", ih, 'x', 1, "-hex2=", null, "", null);
1112: tests = new MTest[] {
1113: new MTest("-hex2=-0777", new Long(-0x777)),
1114: new MTest("-hex2=66", new Long(0x66)),
1115: new MTest("-hex2=06677", new Long(0x6677)),
1116: new MTest("-hex2=-51d", new Long(-0x51d)), };
1117: test.checkMatches(tests, ONE_WORD);
1118:
1119: test
1120: .checkAdd("-char=%c{['b','m']}", ch, 'c', 1, "-char=",
1121: new RngCheck[] { new RngCheck('b', CLOSED, 'm',
1122: CLOSED), }, "", null);
1123:
1124: tests = new MTest[] { new MTest("-char=a", new MErr('r', "a")),
1125: new MTest("-char=b", new Character('b')),
1126: new MTest("-char='b'", new Character('b')),
1127: new MTest("-char='\142'", new Character('b')),
1128: new MTest("-char='\141'", new MErr('r', "'\141'")),
1129: new MTest("-char=\142", new Character('b')),
1130: new MTest("-char=\141", new MErr('r', "\141")),
1131: new MTest("-char=m", new Character('m')),
1132: new MTest("-char=z", new MErr('r', "z")),
1133: new MTest("-char=bb", new MErr('m', "bb")),
1134: new MTest("-char='b", new MErr('m', "'b")),
1135: new MTest("-char='", new MErr('m', "'")),
1136: new MTest("-char=a'", new MErr('m', "a'")), };
1137: test.checkMatches(tests, ONE_WORD);
1138:
1139: test.checkAdd("-char2=%c", ch, 'c', 1, "-char2=", null, "",
1140: null);
1141: tests = new MTest[] {
1142: new MTest("-char2=a", new Character('a')),
1143: new MTest("-char2='\141'", new Character('\141')),
1144: new MTest("-char2=\141", new Character('\141')),
1145: new MTest("-char2=z", new Character('z')), };
1146: test.checkMatches(tests, ONE_WORD);
1147:
1148: test.checkAdd("-charv3 %cX3", vec, 'c', 3, "-charv3 ", null,
1149: "", null);
1150: tests = new MTest[] {
1151: new MTest("-charv3 a b c",
1152: new char[] { 'a', 'b', 'c' }, 0),
1153: new MTest("-charv3 'g' f '\\n'", new char[] { 'g', 'f',
1154: '\n' }, 1),
1155: new MTest("-charv3 1 \001 3", new char[] { '1', '\001',
1156: '3' }, 2), };
1157: vec.clear();
1158: test.checkMatches(tests, MULTI_WORD);
1159: test.checkAdd("-charv=%c", vec, 'c', 1, "-charv=", null, "",
1160: null);
1161: tests = new MTest[] {
1162: new MTest("-charv=d", new Character('d'), 0),
1163: new MTest("-charv='g'", new Character('g'), 1),
1164: new MTest("-charv=\111", new Character('\111'), 2), };
1165: vec.clear();
1166: test.checkMatches(tests, ONE_WORD);
1167:
1168: test.checkAdd("-bool=%b{true}", bh, 'b', 1, "-bool=",
1169: new RngCheck[] { new RngCheck(true), }, "", null);
1170:
1171: tests = new MTest[] {
1172: new MTest("-bool=true", new Boolean(true)),
1173: new MTest("-bool=false", new MErr('r', "false")),
1174: new MTest("-bool=fals", new MErr('m', "fals")),
1175: new MTest("-bool=falsem", new MErr('m', "falsem")),
1176: new MTest("-bool=truex", new MErr('m', "truex")),
1177: new MTest("-bool=foo", new MErr('m', "foo")),
1178: new MTest("-bool=1", new MErr('m', "1")), };
1179: test.checkMatches(tests, ONE_WORD);
1180:
1181: test.checkAdd("-boo2=%b{true,false}", bh, 'b', 1, "-boo2=",
1182: new RngCheck[] { new RngCheck(true),
1183: new RngCheck(false), }, "", null);
1184:
1185: tests = new MTest[] {
1186: new MTest("-boo2=true", new Boolean(true)),
1187: new MTest("-boo2=false", new Boolean(false)), };
1188: test.checkMatches(tests, ONE_WORD);
1189:
1190: test.checkAdd("-boo3=%b", bh, 'b', 1, "-boo3=", null, "", null);
1191: tests = new MTest[] {
1192: new MTest("-boo3=true", new Boolean(true)),
1193: new MTest("-boo3=false", new Boolean(false)), };
1194: test.checkMatches(tests, ONE_WORD);
1195:
1196: test.checkAdd("-boo3 %bX3", b3, 'b', 3, "-boo3 ", null, "",
1197: null);
1198: tests = new MTest[] {
1199: new MTest("-boo3 true false true", new boolean[] {
1200: true, false, true }),
1201: new MTest("-boo3 true fals true", new MErr('m', "fals")), };
1202: test.checkMatches(tests, MULTI_WORD);
1203:
1204: test.checkAdd("-boov3 %bX3", vec, 'b', 3, "-boov3 ", null, "",
1205: null);
1206: tests = new MTest[] {
1207: new MTest("-boov3 true true false", new boolean[] {
1208: true, true, false }, 0),
1209: new MTest("-boov3 false false true", new boolean[] {
1210: false, false, true }, 1), };
1211: vec.clear();
1212: test.checkMatches(tests, MULTI_WORD);
1213: test
1214: .checkAdd("-boov %b", vec, 'b', 1, "-boov ", null, "",
1215: null);
1216: tests = new MTest[] {
1217: new MTest("-boov true", new Boolean(true), 0),
1218: new MTest("-boov false", new Boolean(false), 1),
1219: new MTest("-boov true", new Boolean(true), 2), };
1220: vec.clear();
1221: test.checkMatches(tests, MULTI_WORD);
1222:
1223: test.checkAdd("-v3 %vX2", b3, 'v', 2, "-v3 ", null, "", null);
1224: tests = new MTest[] { new MTest("-v3", new boolean[] { true,
1225: true }), };
1226: test.checkMatches(tests, MULTI_WORD);
1227:
1228: test.checkAdd("-vf %v{false,true}X2", b3, 'v', 2, "-vf ",
1229: new RngCheck[] { new RngCheck(false),
1230: new RngCheck(true), }, "", null);
1231: tests = new MTest[] { new MTest("-vf", new boolean[] { false,
1232: false }), };
1233: test.checkMatches(tests, MULTI_WORD);
1234:
1235: test.checkAdd("-str=%s{(john,zzzz]}", sh, 's', 1, "-str=",
1236: new RngCheck[] { new RngCheck("john", OPEN, "zzzz",
1237: CLOSED), }, "", null);
1238:
1239: tests = new MTest[] {
1240: new MTest("-str=john", new MErr('r', "john")),
1241: new MTest("-str=joho ", "joho "),
1242: new MTest("-str=joho ", "joho "),
1243: new MTest("-str=zzzz", "zzzz"),
1244: new MTest("-str= joho", new MErr('r', " joho")),
1245: new MTest("-str=jnhn ", new MErr('r', "jnhn ")),
1246: new MTest("-str=zzzzz", new MErr('r', "zzzzz")),
1247: new MTest("-str=\"joho\"", new MErr('r', "\"joho\"")),
1248: new MTest("-str=\"joho", new MErr('r', "\"joho")),
1249: new MTest("-str=joho j", "joho j"), // new MErr('m', "joho j")),
1250: };
1251: test.checkMatches(tests, ONE_WORD);
1252:
1253: test.checkAdd("-str2=%s", sh, 's', 1, "-str2=", null, "", null);
1254: tests = new MTest[] { new MTest("-str2= jnhn", " jnhn"),
1255: new MTest("-str2=zzzzz", "zzzzz"), };
1256: test.checkMatches(tests, ONE_WORD);
1257:
1258: test.checkAdd("-str3 %sX3", s3, 's', 3, "-str3 ", null, "",
1259: null);
1260: tests = new MTest[] {
1261: new MTest("-str3 foo bar johnny", new String[] { "foo",
1262: "bar", "johnny" }),
1263: new MTest("-str3 zzzzz \"bad foo", new String[] {
1264: "zzzzz", "\"bad", "foo" }), // new MErr('m', "\"bad")),
1265: };
1266: test.checkMatches(tests, MULTI_WORD);
1267:
1268: test.checkAdd("-strv3 %sX3", vec, 's', 3, "-strv3 ", null, "",
1269: null);
1270: tests = new MTest[] {
1271: new MTest("-strv3 foo bar \"hihi\"", new String[] {
1272: "foo", "bar", "\"hihi\"" }, 0),
1273: new MTest("-strv3 a 123 gg", new String[] { "a", "123",
1274: "gg" }, 1), };
1275: vec.clear();
1276: test.checkMatches(tests, MULTI_WORD);
1277: test
1278: .checkAdd("-strv=%s", vec, 's', 1, "-strv=", null, "",
1279: null);
1280: tests = new MTest[] { new MTest("-strv=d", "d", 0),
1281: new MTest("-strv='g'", "'g'", 1),
1282: new MTest("-strv=\\111", "\\111", 2), };
1283: vec.clear();
1284: test.checkMatches(tests, ONE_WORD);
1285:
1286: test.checkAdd("-float=%f{(-0.001,1000.0]}", dh, 'f', 1,
1287: "-float=", new RngCheck[] { new RngCheck(-0.001, OPEN,
1288: 1000.0, CLOSED), }, "", null);
1289:
1290: tests = new MTest[] {
1291: new MTest("-float=-0.000999", new Double(-0.000999)),
1292: new MTest("-float=1e-3", new Double(0.001)),
1293: new MTest("-float=12.33e1", new Double(123.3)),
1294: new MTest("-float=1e3", new Double(1e3)),
1295: new MTest("-float=1000.000", new Double(1000.0)),
1296: new MTest("-float=-0.001", new MErr('r', "-0.001")),
1297: new MTest("-float=-1e-3", new MErr('r', "-1e-3")),
1298: new MTest("-float=1000.001", new MErr('r', "1000.001")),
1299: new MTest("-float=.", new MErr('m', ".")),
1300: new MTest("-float= 124.5 ", new Double(124.5)),
1301: new MTest("-float=124.5x", new MErr('m', "124.5x")),
1302: new MTest("-float= foo ", new MErr('m', " foo ")),
1303: new MTest("-float=1e1", new Double(10)),
1304: new MTest("-float=1e ", new MErr('m', "1e ")), };
1305: test.checkMatches(tests, ONE_WORD);
1306:
1307: test.checkAdd("-float2=%f", dh, 'f', 1, "-float2=", null, "",
1308: null);
1309: tests = new MTest[] {
1310: new MTest("-float2=-0.001", new Double(-0.001)),
1311: new MTest("-float2=-1e-3", new Double(-1e-3)),
1312: new MTest("-float2=1000.001", new Double(1000.001)), };
1313: test.checkMatches(tests, ONE_WORD);
1314:
1315: test.checkAdd("-f3 %fX3", d3, 'f', 3, "-f3 ", null, "", null);
1316: tests = new MTest[] {
1317: new MTest("-f3 -0.001 1.23e5 -9.88e-4", new double[] {
1318: -0.001, 1.23e5, -9.88e-4 }),
1319: new MTest("-f3 7.88 foo 9.0", new MErr('m', "foo")),
1320: new MTest("-f3 7.88 . 9.0", new MErr('m', ".")),
1321: new MTest("-f3 7.88 3.0 9.0x", new MErr('m', "9.0x")), };
1322: test.checkMatches(tests, MULTI_WORD);
1323:
1324: test
1325: .checkAdd("-fv3 %fX3", vec, 'f', 3, "-fv3 ", null, "",
1326: null);
1327: tests = new MTest[] {
1328: new MTest("-fv3 1.0 3.444 6.7", new double[] { 1.0,
1329: 3.444, 6.7 }, 0),
1330: new MTest("-fv3 13e-5 145.678 0.0001e45", new double[] {
1331: 13e-5, 145.678, 0.0001e45 }, 1),
1332: new MTest("-fv3 11.11 3.1245 -1e-4", new double[] {
1333: 11.11, 3.1245, -1e-4 }, 2),
1334: new MTest("-fv3 1.0 2 3",
1335: new double[] { 1.0, 2.0, 3.0 }, 3), };
1336: vec.clear();
1337: test.checkMatches(tests, MULTI_WORD);
1338: test.checkAdd("-fv %f", vec, 'f', 1, "-fv ", null, "", null);
1339: tests = new MTest[] {
1340: new MTest("-fv -15.1234", new Double(-15.1234), 0),
1341: new MTest("-fv -1.234e-7", new Double(-1.234e-7), 1),
1342: new MTest("-fv 0.001111", new Double(0.001111), 2), };
1343: vec.clear();
1344: test.checkMatches(tests, MULTI_WORD);
1345:
1346: IntHolder intHolder = new IntHolder();
1347: StringHolder strHolder = new StringHolder();
1348:
1349: ArgParser parser = new ArgParser("test");
1350: parser.addOption("-foo %d #an int", intHolder);
1351: parser.addOption("-bar %s #a string", strHolder);
1352: args = new String[] { "zzz", "-cat", "-foo", "123", "yyy",
1353: "-bar", "xxxx", "xxx" };
1354:
1355: String[] unmatchedCheck = new String[] { "zzz", "-cat", "yyy",
1356: "xxx" };
1357:
1358: String[] unmatched = parser.matchAllArgs(args, 0, 0);
1359: test.checkStringArray("Unmatched args:", unmatched,
1360: unmatchedCheck);
1361:
1362: vec.clear();
1363: for (int i = 0; i < args.length;) {
1364: try {
1365: i = parser.matchArg(args, i);
1366: if (parser.getUnmatchedArgument() != null) {
1367: vec.add(parser.getUnmatchedArgument());
1368: }
1369: } catch (Exception e) {
1370: }
1371: }
1372: unmatched = (String[]) vec.toArray(new String[0]);
1373: test.checkStringArray("My unmatched args:", unmatched,
1374: unmatchedCheck);
1375:
1376: System.out.println("\nPassed\n");
1377:
1378: }
1379: }
|