Exemple #1
0
 /**
  * Call nextEvaluation() the expected number of times, and check that it returns true (or throws
  * an exception).
  */
 @Action
 public void nextEval() {
   assert data_ != null;
   debug("nextEval trying nextEval() " + data_.successes + " times.");
   if (data_.successes > 1) {
     // just check that true is returned the expected number of times.
     for (int i = data_.successes; i > 0; i--) Assert.assertTrue(pred_.nextEvaluation());
   } else if (data_.successes == 1) {
     Assert.assertTrue("nextEvaluation should be true", pred_.nextEvaluation());
     // check that the correct results were returned.
     Envir newenv = pred_.getEnvir();
     debug("nextEval returns newenv=" + newenv);
     for (int i = 0; i < names_.length; i++) {
       Assert.assertTrue(names_[i] + " undefined.", newenv.isDefined(names_[i]));
       Assert.assertEquals(
           names_[i] + " has incorrect value.", data_.args[i], newenv.lookup(names_[i]));
     }
   } else if (data_.successes == Eval.UNDEF) {
     try {
       pred_.nextEvaluation();
       Assert.fail("Should have thrown UndefException: " + pred_);
     } catch (UndefException ex) {
       // Good!
     }
   }
   // else data_.successes == 0, so we do nothing.
   data_ = null;
   state_ = State.Finished;
 }
Exemple #2
0
 // @requires env_ != null;
 public void startEval(/*@non_null@*/ Eval data) {
   // Note: we use the original env here, as given to chooseMode.
   for (int i = 0; i < names_.length; i++) {
     if (env_.isDefined(names_[i])) {
       Expr value = data.args[i];
       env_.setValue(names_[i], value);
     }
   }
   debug("startEval with env=" + env_);
   pred_.startEvaluation();
   data_ = data;
   state_ = State.Started;
 }
Exemple #3
0
 public String getState() {
   StringBuffer result = new StringBuffer();
   result.append(state_.toString());
   if (env_ != null) {
     // add the mode to the end of the state.
     for (int i = 0; i < names_.length; i++) {
       if (!env_.isDefined(names_[i])) result.append('O');
       else result.append('I'); // an input
     }
   }
   return result.toString();
 }
Exemple #4
0
 /**
  * Checks that we are in State.GotMode and that the current mode is compatible with data.modes.
  */
 protected boolean startEvalGuard(Eval data) {
   boolean result = state_ == State.GotMode;
   for (int i = 0; result && i < names_.length; i++) {
     if (env_.isDefined(names_[i])) {
       // names_[i] is an input, so 'I' or '?' is allowed.
       if (data.modes.charAt(i) == 'O') result = false;
     } else {
       // names_[i] is an output, so 'O' or '?' is allowed.
       if (data.modes.charAt(i) == 'I') result = false;
     }
   }
   return result;
 }
Exemple #5
0
  // @requires inout.length == 3;
  public void chooseMode(String inout) {
    assert inout.length() == 3;
    boolean isInput[] = new boolean[names_.length];
    env_ = new Envir();
    // Is names_[0] an input?
    isInput[0] = inout.charAt(0) == 'I';
    // Are names_[1..length-2] inputs?
    for (int i = 1; i <= names_.length - 2; i++) isInput[i] = inout.charAt(1) == 'I';
    // Is names_[length-1] an input?
    isInput[names_.length - 1] = inout.charAt(2) == 'I';

    // Now add the inputs into env.
    for (int i = 0; i < names_.length; i++) if (isInput[i]) env_ = env_.plus(names_[i], null);
    boolean shouldWork = validModes_.contains(inout);

    mode_ = pred_.chooseMode(env_);
    debug("chooseMode(" + env_ + ") --> " + mode_);
    if (shouldWork) Assert.assertNotNull("Valid mode expected, but got null", mode_);
    else Assert.assertNull("Null mode expected, but got " + mode_, mode_);
    if (mode_ == null) {
      state_ = State.NoMode;
      env_ = null;
    } else {
      // now check that mode is correct.
      for (int i = 0; i < names_.length; i++) {
        Assert.assertEquals("name[" + i + "] = " + names_[i], isInput[i], mode_.isInput(names_[i]));
      }
      pred_.setMode(mode_);
      // check that all names are defined in the output environment.
      Envir newenv = mode_.getEnvir();
      for (int i = 0; i < names_.length; i++)
        Assert.assertTrue(names_[i] + " should be defined", newenv.isDefined(names_[i]));
      state_ = State.GotMode;
      // NOTE that env_ is left as the input environment.
    }
  }