-
Notifications
You must be signed in to change notification settings - Fork 40
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
Handling List, Map and String #24
Comments
Hi!
Correct. JDart supports (fixed size) symbolic arrays, so you can have constraints on the elements of
You can have a look in |
Thank you for the response. In TestSuiteGenerator.java line # 112: Method[] ms = cls.getMethods(); |
Hi! |
Hi, I have one more doubt. Example: Here JDart generates 2 test cases and the branch coverage shows 1/2. if(m1.get("a") > m2.get("b")) JDart is generating 3 test cases and the branch coverage shows 2/2. The same behavior is observed for List as well. Could you please tell me the reason behind it. |
There is a good reason for what you are observing: Note that this is not a limitation of JDart, but rather the semantics of the JVM. If you explicitly cast to primitive type: if((int)m1.get("a") == m2.get("b"))
//.... or store the values in temporary variables: int a = m1.get("a");
int b = m1.get("b");
if(a == b))
//.... unboxing is performed, and thus JDart will explore both branches. |
Thanks a lot for such a nice explanation. |
You are very welcome! Let me know if you have additional questions! |
Hi, Here the value part (vendor.vendorId = 23) is OK but how can I get the Key ? Example: Regards, |
Hi! |
I have two custom classes: 'Student' and 'Course'. `public class Student {
}` `public class Course {
}` The class containing main() method and the method under test is: `public class Sample {
}` My jpf file is as below. shell=gov.nasa.jpf.jdart.JDart log.finest=jdart jdart.configs.all_fields_symbolic.symbolic.include=m1[*;m2[* target=com.sam.jdart.util.Sample concolic.method=testMap listener=gov.nasa.jpf.listener.CoverageAnalyzer When I run I get the below test cases: java.lang.Integer:m1[#0].courseId=-2100273060, java.lang.Integer:m2[#0].courseId=-2100273060, java.lang.Integer:m1[#0].courseId=0, java.lang.Integer:m2[#0].courseId=-2147483647, java.lang.Integer:m1[#0].courseId=100, java.lang.Integer:m2[#0].courseId=200, The test cases are correct. But I can't create key objects as the test cases do not have enough information about the keys.. It shows only [#0]. `
` |
Hi, |
It depends on which string constraints you have. JDart can handle constraints that would amount to constraints on the elements of char[] such as However, in order to do so, you will have to make some changes to jpf-core since it has models (and peers) for the String class. Let's assume you only have constraints on string equalities. Depending on how you model String, it could look like the following: public static void main(String args[]) {
char[] a = new char[] {'a', 'b', '3'};
char[] b = new char[] {'c', 'd', 'f'};
target(a, b);
}
public static void target(char[] a, char[] b) {
String stra = new String(a);
String strb = new String(b);
if(stra.equals(strb)) {
System.out.println("equals");
} else {
System.out.println("not equals");
}
} Your config would then contain something like:
Now, for this to work, you will have to replace public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
} In addition, remove the peer |
Thank you. |
Yes, that is expected because characters in Java are encoded as UTF-16. The solver represents characters as 16-bit integers. |
I meant the keyboard characters. Thank you for the explanation. |
I have a question in continuation of your answer "It depends on which string constraints you have. JDart can handle constraints that would amount to constraints on the elements of char[] such as String.equals()...." given above. I observed it is generating test cases. But is there any other impact ? |
In jpf-core On the other hand, when removing those classes from jpf-core, your analysis will also become dependent on the JDK installation. Since different versions of the JDK (and different JDKs) can have different implementations of the Java classes, you may get different analysis results. In addition, jpf-core simplifies many of the jdk classes and takes care of the native calls with peers. In fact, a better method in your case could be to rely on the String class in jpf-core, but only substitute---as needed---the methods you would like to symbolically execute. |
Thanks a lot. |
Hi, I have a target class, which has the SUT and a main method. The SUT is called from within the main method. If I write the below code in the main method. When I right click on the .jpf file and click on 'verify', I am getting output as: Similar issue is happening for '--' operator. Please tell me what could be the reason for this issue. |
Should be fixed now. |
Hi, public static float getTotalPriceForBookBank(List<Book> books){
float totalPricePayable = 0;
for(Book book:books){
if(book.getPrice() > 2500)
totalPricePayable = totalPricePayable + (book.getPrice() * 0.2f);
else if(book.getYearOfPublication() < 2000)
totalPricePayable = totalPricePayable + (book.getPrice() * 0.05f);
else
totalPricePayable = totalPricePayable + (book.getPrice() * 0.1f);
}
return totalPricePayable;
} As I understand JDart can work on bounded list i.e. initialized list with objects. List<Book> bookList = new ArrayList<>();
bookList.add(new Book());
bookList.add(new Book());
bookList.add(new Book());
new SUT().getTotalPriceForBookBank(bookList); But if I call the SUT like below, I am not getting any test cases. List<Book> bookList = new ArrayList<Book>(){{
add(new Book());
add(new Book());
add(new Book());
}};
new SUT().getTotalPriceForBookBank(bookList); Why no test cases are generated when the SUT is called in the 2nd way ? Could you please help me. |
This is because you are effectively initializing the list in two very different ways. The latter uses the so-called double-brace initializer which creates an anonymous class for performing initialization. You can confirm this behavior by looking in the build folder; you'll see a new class with 1$.class suffix---this is the class generated by the compiler. However, if you can, I would highly recommend not using double-brace initializers. I consider it an anti-pattern. |
Hi, |
FYI, a student in my class on software verification is planning to try to add support for string to JDart. We |
Hi,
From the wiki what I understand is:
1- JDart doesn't support string as symbolic. It means I cannot get test case values for a string.
2- JDart supports List and Map but the configuration needs to be different in the jpf file.
Could you please tell me how can I get test case values of List and Map and what what kind of condition I have to write for List and Map in the method, so that Jdart will solve it and will generate some test case values.
If in case it supports string, how can I get the test case value of a string. what configuration I have to write in the jpf file ?
Regards,
S R Giri
The text was updated successfully, but these errors were encountered: