Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing difference (subtraction) of lengths to imply @SameLen #231

Open
mernst opened this issue Jun 2, 2018 · 0 comments
Open

Testing difference (subtraction) of lengths to imply @SameLen #231

mernst opened this issue Jun 2, 2018 · 0 comments

Comments

@mernst
Copy link
Collaborator

mernst commented Jun 2, 2018

Consider the below test case.
Without the @AssumeAssertion, the Index Checker issues the warning.

MethodComparator.java:23: error: [array.access.unsafe.high] Potentially unsafe array access: the index could be larger than the array's bound
      result = ptypes1[i].toString().compareTo(ptypes2[i].toString());
                                                       ^
  found   : @LTLengthOf(value="ptypes1", offset="0") int
  required: @IndexFor("ptypes2") or @LTLengthOf("ptypes2") -- an integer less than ptypes2's length
1 error

I think that comparing the subtraction (difference) of array lengths is somewhat common in compare and compareTo methods.

import java.lang.reflect.Method;
import java.util.Comparator;

public class MethodComparator implements Comparator<Method> {

  @Override
  public int compare(Method m1, Method m2) {
    Class<?>[] ptypes1 = m1.getParameterTypes();
    Class<?>[] ptypes2 = m2.getParameterTypes();
    int result;
    result = ptypes1.length - ptypes2.length;
    if (result != 0) {
      return result;
    }
    assert ptypes1.length == ptypes2.length : "@AssumeAssertion(index): difference of lengths is 0";
    for (int i = 0; i < ptypes1.length; i++) {
      result = ptypes1[i].toString().compareTo(ptypes2[i].toString());
      if (result != 0) {
        return result;
      }
    }
    return result;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant