jeudi 24 septembre 2020

If statement anomalous behavior

Below there is the C++ code I am implementing. It is a class Transition with integer oldState, integer newState and a string to represent the value on the transition.

Then I am implementing a function that adds new transitions to a list of Transitions, based on a simple comparison:

  • if the new transition is already in the list, return
  • if there is a transition in the list with same oldState and symbol as the new transition but different newState, update newState of the transition in the list and return
  • otherwise, add the new transition to the list
namespace fsm {
   class Transition {
        int oldState, newState;
        std::string symbol;
    
    public:
        Transition(){}
        Transition(int s1, int s2, std::string sym){
            oldState = s1; newState = s2; symbol = sym;
        }
    
        int getOldState() { return oldState; }
        int getNewState() { return newState; }
        std::string getSymbol() { return symbol; }
    
        void setOldState(int state) { oldState = state; }
        void setNewState(int state) { newState = state; }
        void setSymbol(std::string sym) { symbol = sym; }
    
        bool sameOld(Transition t){ return oldState == t.getOldState(); }
        bool sameNew(Transition t){ return newState == t.getNewState(); }
        bool sameSymbol(Transition t) { return symbol.compare(t.getSymbol()) == 0; }
    
        bool equal(Transition t) {
            return sameOld(t) && sameNew(t) && sameSymbol(t);
        }
    
        std::string toString(){
            std::string s = "<" + std::to_string(oldState)
                            + ", " + symbol + ", "
                            + std::to_string(newState) + ">";
            return s;
        }
    }; // Transition
    

    typedef std::list<Transition> TransitionsList;

} // namespace fsm
    
    
    fsm::TransitionsList transitions;
    
    void updateTransitions(fsm::Transition newTr){
      for (fsm::Transition t : transitions){
        if (t.equal(newTr)) return;
    
        if (t.sameOld(newTr) && t.sameSymbol(newTr) && !t.sameNew(newTr)) {
          //!(t.getOldState() == newTr.getNewState())
          t.setNewState(newTr.getNewState());
          std::cout << "\nAlmost equal: " << t.toString() << ", " << newTr.toString() << std::endl;
          std::cout << "Values equality:\n"
                    << "\t" << t.getOldState() << " = " << newTr.getOldState() << " is " << t.sameOld(newTr) << std::endl
                    << "\t" << t.getNewState() << " = " << newTr.getNewState() << " is " << t.sameNew(newTr) << std::endl 
                    << "\t" << t.getSymbol() << " = " << newTr.getSymbol() << " is " << t.sameSymbol(newTr) << std::endl;
          return;
        }
        
      }
      transitions.push_back(newTr);
    }

Now the weird behavior is in the second if statement of the function updateTransitions. It should be executed only if t.sameOld(newTr) is true and t.sameSymbol(newTr) is true and t.sameNew(newTr) is false. However, it is executed in cases in which t.sameNew(newTr) is true, and it prints stuff like:

Almost equal: <100, 0 0 3, 3000>, <100, 0 0 3, 3000>
Values equality:
    100 = 100 is 1
    3000 = 3000 is 1
    0 0 3 = 0 0 3 is 1

This should not be printed, as t.sameNew(newTr) is true and the if condition should skip this case.

How is it possible?

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire