Skip to content

Fuzzer should generate tests for simple collection with generic types #875 - #988

Merged
Markoutte merged 3 commits into
mainfrom
pelevin/875_Fuzzer_should_generate_tests_for_simple_collection_with_generic_types
Sep 26, 2022
Merged

Fuzzer should generate tests for simple collection with generic types #875#988
Markoutte merged 3 commits into
mainfrom
pelevin/875_Fuzzer_should_generate_tests_for_simple_collection_with_generic_types

Conversation

@Markoutte

Copy link
Copy Markdown
Collaborator

Description

Introduces provider to generate collections with generic types. Now, fuzzer creates tests with values for such types as Collection<Integer>, Map<MyClass, String>, and recursive types like: Collection<List<Double>>, Map<String, Set<Integer>>.

Fixes#875

Type of Change

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How Has This Been Tested?

Automated Testing

Run:

org.utbot.framework.plugin.api.ModelProviderTest
org.utbot.framework.plugin.api.CollectionModelProviderTest

Manual Scenario

Here several examples which can be tested:

publicclassCollectionExample {
/** * Should create unsorted list that will be sorted as a result. */publicstatic <TextendsNumber> Collection<T> sorted(Collection<T> source) {
returnsource.stream().sorted().collect(Collectors.toList());
}
/** * Should create at least both answers: one that finds the key, and another returns null. */publicstaticStringgetKeyForValue(Map<String, Number> map, Numbervalue) {
for (Map.Entry<String, Number> entry : map.entrySet()) {
if (Objects.equals(entry.getValue(), value)) {
returnentry.getKey();
}
}
returnnull;
}
/** * Should find a branch that returns true and size of array is greater than 1 (non-trivial). */publicstaticbooleanisDiagonal(Collection<Collection<Double>> matrix) {
intcols = matrix.size();
if (cols <= 1) {
returnfalse;
}
inti = 0;
for (Collection<Double> col : matrix) {
if (col.size() != cols) {
returnfalse;
}
intj = 0;
for (Doublevalue : col) {
if (i == j && value == 0.0) returnfalse;
if (i != j && value != 0.0) returnfalse;
j++;
}
i++;
}
returntrue;
}
/** * Checks that different collections can be created. Part 1 */publicbooleanallCollectionAreSameSize1(
Collection<Integer> c,
List<Integer> l,
Set<Integer> s,
SortedSet<Integer> ss,
Deque<Integer> d,
Iterable<Integer> i
) {
if (c.size() != l.size()) {
returnfalse;
}
if (l.size() != s.size()) {
returnfalse;
}
if (s.size() != ss.size()) {
returnfalse;
}
if (ss.size() != d.size()) {
returnfalse;
}
if (d.size() != StreamSupport.stream(i.spliterator(), false).count()) {
returnfalse;
}
returntrue;
}
/** * Checks that different collections can be created. Part 2 */publicbooleanallCollectionAreSameSize2(
Iterable<Integer> i,
Stack<Integer> st,
NavigableSet<Integer> ns,
Map<Integer, Integer> m,
SortedMap<Integer, Integer> sm,
NavigableMap<Integer, Integer> nm
) {
if (StreamSupport.stream(i.spliterator(), false).count() != st.size()) {
returnfalse;
}
if (st.size() != ns.size()) {
returnfalse;
}
if (ns.size() != m.size()) {
returnfalse;
}
if (m.size() != sm.size()) {
returnfalse;
}
if (sm.size() != nm.size()) {
returnfalse;
}
returntrue;
}
/** * Should create TreeSet without any modifications as T extends Number is not Comparable */public <TextendsNumber> booleantestTreeSetWithoutComparable(NavigableSet<T> set) {
if (set.size() > 5) {
returntrue;
}
returnfalse;
}
/** * Should create TreeSet with modifications as Integer is Comparable */publicbooleantestTreeSetWithComparable(NavigableSet<Integer> set) {
if (set.size() > 5) {
returntrue;
}
returnfalse;
}
staticclassConcreteList<T> extendsLinkedList<T> {}
/** * Should create concrete class */publicbooleantestConcreteCollectionIsCreated(ConcreteList<?> list) {
if (list.size() > 5) {
returntrue;
}
returnfalse;
}
staticclassConcreteMap<K, V> extendsHashMap<K, V> { }
/** * Should create concrete class */publicbooleantestConcreteMapIsCreated(ConcreteMap<?, ?> map) {
if (map.size() > 5) {
returntrue;
}
returnfalse;
}
}

Checklist:

  • The change followed the style guidelines of the UTBot project
  • Self-review of the code is passed
  • The change contains enough commentaries, particularly in hard-to-understand areas
  • New documentation is provided or existed one is altered
  • No new warnings
  • New tests have been added
  • All tests pass locally with my changes

@MarkoutteMarkoutte added the comp-fuzzing Issue is related to the fuzzing label Sep 21, 2022

@volivan239volivan239 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a powerful change, but I believe there still may be some room for improving variety of created models, needs discussion.

Comment threadutbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/Fuzzer.kt Outdated
* Added GenericArrayType
* Fixed the logic around number of generated values in recursive calls
@Markoutte
Markoutteforce-pushed the pelevin/875_Fuzzer_should_generate_tests_for_simple_collection_with_generic_types branch from 3ba5682 to b618d6bCompareSeptember 23, 2022 08:26
@Markoutte
Markoutte merged commit 28d1eb8 into mainSep 26, 2022
@Markoutte
Markoutte deleted the pelevin/875_Fuzzer_should_generate_tests_for_simple_collection_with_generic_types branch September 26, 2022 08:25
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-fuzzingIssue is related to the fuzzing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fuzzer should generate tests for simple collection with generic types

3 participants

@Markoutte@sergeypospelov@volivan239