Program: 51067
Project: 51067
Buggy source:
/** 
 * Returns the member with the highest solve order according to AS2005 scoping rules. <p>By default, cube calculated members are resolved before any session scope calculated members, and session scope members are resolved before any query defined calculation.  The SOLVE_ORDER value only applies within the scope in which it was defined. <p>The aggregate function is always applied to base members; i.e. as if SOLVE_ORDER was defined to be the lowest value in a given evaluation in a SSAS2000 sense.
 */
private RolapCalculation getScopedMaxSolveOrder(){
  RolapCalculation maxSolveMember=null;
  ScopedMaxSolveOrderFinderState state=ScopedMaxSolveOrderFinderState.START;
  for (int i=0; i < calcMemberCount; i++) {
    RolapCalculation member=calcMembers[i];
switch (state) {
case START:
      maxSolveMember=member;
    if (maxSolveMember.containsAggregateFunction()) {
      state=ScopedMaxSolveOrderFinderState.AGG_SCOPE;
    }
 else     if (maxSolveMember.isCalculatedInQuery()) {
      state=ScopedMaxSolveOrderFinderState.QUERY_SCOPE;
    }
 else {
      state=ScopedMaxSolveOrderFinderState.CUBE_SCOPE;
    }
  break;
case AGG_SCOPE:
if (maxSolveMember.containsAggregateFunction()) {
  if (expandsBefore(member,maxSolveMember)) {
    maxSolveMember=member;
  }
}
 else if (member.isCalculatedInQuery()) {
  maxSolveMember=member;
  state=ScopedMaxSolveOrderFinderState.QUERY_SCOPE;
}
 else {
  maxSolveMember=member;
  state=ScopedMaxSolveOrderFinderState.CUBE_SCOPE;
}
break;
case CUBE_SCOPE:
if (maxSolveMember.containsAggregateFunction()) {
continue;
}
if (member.isCalculatedInQuery()) {
maxSolveMember=member;
state=ScopedMaxSolveOrderFinderState.QUERY_SCOPE;
}
 else if (expandsBefore(member,maxSolveMember)) {
maxSolveMember=member;
}
break;
case QUERY_SCOPE:
if (maxSolveMember.containsAggregateFunction()) {
continue;
}
if (member.isCalculatedInQuery()) {
if (expandsBefore(member,maxSolveMember)) {
maxSolveMember=member;
}
}
break;
}
}
return maxSolveMember;
}

Fixed version (ground truth):
/** 
 * Returns the member with the highest solve order according to AS2005 scoping rules. <p>By default, cube calculated members are resolved before any session scope calculated members, and session scope members are resolved before any query defined calculation.  The SOLVE_ORDER value only applies within the scope in which it was defined. <p>The aggregate function is always applied to base members; i.e. as if SOLVE_ORDER was defined to be the lowest value in a given evaluation in a SSAS2000 sense.
 */
private RolapCalculation getScopedMaxSolveOrder(){
  RolapCalculation maxSolveMember=null;
  ScopedMaxSolveOrderFinderState state=ScopedMaxSolveOrderFinderState.START;
  for (int i=0; i < calcMemberCount; i++) {
    RolapCalculation member=calcMembers[i];
switch (state) {
case START:
      maxSolveMember=member;
    if (maxSolveMember.containsAggregateFunction()) {
      state=ScopedMaxSolveOrderFinderState.AGG_SCOPE;
    }
 else     if (maxSolveMember.isCalculatedInQuery()) {
      state=ScopedMaxSolveOrderFinderState.QUERY_SCOPE;
    }
 else {
      state=ScopedMaxSolveOrderFinderState.CUBE_SCOPE;
    }
  break;
case AGG_SCOPE:
if (member.containsAggregateFunction()) {
  if (expandsBefore(member,maxSolveMember)) {
    maxSolveMember=member;
  }
}
 else if (member.isCalculatedInQuery()) {
  maxSolveMember=member;
  state=ScopedMaxSolveOrderFinderState.QUERY_SCOPE;
}
 else {
  maxSolveMember=member;
  state=ScopedMaxSolveOrderFinderState.CUBE_SCOPE;
}
break;
case CUBE_SCOPE:
if (member.containsAggregateFunction()) {
continue;
}
if (member.isCalculatedInQuery()) {
maxSolveMember=member;
state=ScopedMaxSolveOrderFinderState.QUERY_SCOPE;
}
 else if (expandsBefore(member,maxSolveMember)) {
maxSolveMember=member;
}
break;
case QUERY_SCOPE:
if (member.containsAggregateFunction()) {
continue;
}
if (member.isCalculatedInQuery()) {
if (expandsBefore(member,maxSolveMember)) {
maxSolveMember=member;
}
}
break;
}
}
return maxSolveMember;
}