Exemple #1
0
  public boolean runtrace(
      String name,
      int startTest,
      int endTest,
      boolean debug,
      float subset,
      TraceReductionType reductionType,
      long seed)
      throws Exception {
    LexTokenReader ltr = new LexTokenReader(name, Dialect.VDM_SL);
    LexToken token = ltr.nextToken();
    ltr.close();
    LexNameToken lexname = null;

    switch (token.type) {
      case NAME:
        lexname = (LexNameToken) token;

        if (Settings.dialect == Dialect.VDM_SL && !lexname.module.equals(getDefaultName())) {
          setDefaultName(lexname.module);
        }
        break;

      case IDENTIFIER:
        lexname = new LexNameToken(getDefaultName(), (LexIdentifierToken) token);
        break;

      default:
        throw new Exception("Expecting trace name");
    }

    NamedTraceDefinition tracedef = findTraceDefinition(lexname);

    if (tracedef == null) {
      throw new Exception("Trace " + lexname + " not found");
    }

    long before = System.currentTimeMillis();
    TraceIterator tests = tracedef.getIterator(getTraceContext(tracedef.classDefinition));
    long after = System.currentTimeMillis();

    boolean wasDBGP = Settings.usingDBGP;
    boolean wasCMD = Settings.usingCmdLine;

    if (!debug) {
      Settings.usingCmdLine = false;
      Settings.usingDBGP = false;
    }

    if (writer == null) {
      writer = Console.out;
    }

    final int count = tests.count();

    if (endTest > count) {
      throw new Exception("Trace " + lexname + " only has " + count + " tests");
    }

    if (endTest == 0) // To the end of the tests, if specified as zero
    {
      endTest = count;
    }

    if (startTest > 0) // Suppress any reduction if a range specified
    {
      subset = 1.0F;
      reductionType = TraceReductionType.NONE;
    }

    int testNumber = 1;
    int excluded = 0;
    boolean failed = false;
    TraceFilter filter = new TraceFilter(count, subset, reductionType, seed);

    if (filter.getFilteredCount() > 0) {
      writer.print("Generated " + count + " tests, reduced to " + filter.getFilteredCount() + ",");
    } else {
      writer.print("Generated " + count + " tests");
    }

    writer.println(" in " + (double) (after - before) / 1000 + " secs. ");
    before = System.currentTimeMillis();

    while (tests.hasMoreTests()) {
      CallSequence test = tests.getNextTest();

      if (testNumber < startTest || testNumber > endTest || filter.isRemoved(test, testNumber)) {
        excluded++;
      } else if (filter.getFilteredBy(test) > 0) {
        excluded++;
        writer.println(
            "Test "
                + testNumber
                + " = "
                + test.getCallString(getTraceContext(tracedef.classDefinition)));
        writer.println("Test " + testNumber + " FILTERED by test " + filter.getFilteredBy(test));
      } else {
        // Typechecking should not be necessary with new traces
        // test.typeCheck(this, getTraceEnvironment(tracedef.classDefinition));

        init(null); // Initialize completely between every run...
        List<Object> result = runOneTrace(tracedef.classDefinition, test, debug);
        filter.update(result, test, testNumber);

        writer.println(
            "Test "
                + testNumber
                + " = "
                + test.getCallString(getTraceContext(tracedef.classDefinition)));
        writer.println("Result = " + result);

        if (result.lastIndexOf(Verdict.PASSED) == -1) {
          failed = true; // Not passed => failed.
        }
      }

      if (testNumber >= endTest) {
        excluded = count - (endTest - startTest + 1);
        break;
      }

      testNumber++;
    }

    init(null);
    Settings.usingCmdLine = wasCMD;
    Settings.usingDBGP = wasDBGP;

    if (excluded > 0) {
      writer.println("Excluded " + excluded + " tests");
    }

    after = System.currentTimeMillis();
    writer.println("Executed in " + (double) (after - before) / 1000 + " secs. ");

    return !failed;
  }