Skip to content

Add wrappers for queues #524 #822

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

Merged
merged 14 commits into from
Aug 31, 2022
Merged
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
package org.utbot.examples.manual

import org.utbot.common.FileUtil
import org.utbot.framework.plugin.api.ClassId
import org.utbot.framework.plugin.api.FieldId
import org.utbot.framework.plugin.api.UtModel
import org.utbot.framework.plugin.api.UtPrimitiveModel
import java.nio.file.Path

object SootUtils {
@JvmStatic
fun runSoot(clazz: Class<*>) {
val buildDir = FileUtil.locateClassPath(clazz.kotlin) ?: FileUtil.isolateClassFiles(clazz.kotlin)
val buildDirPath = buildDir.toPath()

if (buildDirPath != previousBuildDir) {
org.utbot.framework.util.runSoot(buildDirPath, null)
previousBuildDir = buildDirPath
}
org.utbot.framework.SootUtils.runSoot(clazz.kotlin)
}

private var previousBuildDir: Path? = null
}

fun fields(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.utbot.external.api.UtModelFactoryKt.classIdForType;
Expand Down Expand Up @@ -60,7 +66,6 @@ public class UtBotJavaApiTest {

@BeforeEach
public void setUp() {
SootUtils.runSoot(PrimitiveFields.class);
context = UtContext.Companion.setUtContext(new UtContext(PrimitiveFields.class.getClassLoader()));
modelFactory = new UtModelFactory();
}
Expand Down Expand Up @@ -1221,6 +1226,7 @@ public void testOnObjectWithArrayOfComplexArrays() {

@Test
public void testFuzzingSimple() {
SootUtils.runSoot(StringSwitchExample.class);
UtBotJavaApi.setStopConcreteExecutorOnExit(false);

String classpath = getClassPath(StringSwitchExample.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package org.utbot.examples.collections

import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.testcheckers.eq
import org.utbot.tests.infrastructure.CodeGeneration
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
import org.utbot.tests.infrastructure.isException

class QueueUsagesTest : UtValueTestCaseChecker(
testClass = QueueUsages::class,
testCodeGeneration = true,
languagePipelines = listOf(
CodeGenerationLanguageLastStage(CodegenLanguage.JAVA),
CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration)
)
) {
@Test
fun testCreateArrayDeque() {
checkWithException(
QueueUsages::createArrayDeque,
eq(3),
{ init, next, r -> init == null && next == null && r.isException<NullPointerException>() },
{ init, next, r -> init != null && next == null && r.isException<NullPointerException>() },
{ init, next, r -> init != null && next != null && r.getOrNull() == 2 },
)
}

@Test
fun testCreateLinkedList() {
checkWithException(
QueueUsages::createLinkedList,
eq(1),
{ _, _, r -> r.getOrNull()!! == 2 },
)
}

@Test
fun testCreateLinkedBlockingDeque() {
checkWithException(
QueueUsages::createLinkedBlockingDeque,
eq(3),
{ init, next, r -> init == null && next == null && r.isException<NullPointerException>() },
{ init, next, r -> init != null && next == null && r.isException<NullPointerException>() },
{ init, next, r -> init != null && next != null && r.getOrNull() == 2 },
)
}

@Test
fun testContainsQueue() {
checkWithException(
QueueUsages::containsQueue,
eq(3),
{ q, _, r -> q == null && r.isException<NullPointerException>() },
{ q, x, r -> x in q && r.getOrNull() == 1 },
{ q, x, r -> x !in q && r.getOrNull() == 0 },
)
}

@Test
fun testAddQueue() {
checkWithException(
QueueUsages::addQueue,
eq(3),
{ q, _, r -> q == null && r.isException<NullPointerException>() },
{ q, x, r -> q != null && x in r.getOrNull()!! },
{ q, x, r -> q != null && x == null && r.isException<NullPointerException>() }, )
}

@Test
fun testAddAllQueue() {
checkWithException(
QueueUsages::addAllQueue,
eq(3),
{ q, _, r -> q == null && r.isException<NullPointerException>() },
{ q, x, r -> q != null && x != null && x in r.getOrNull()!! },
{ q, x, r -> q != null && x == null && r.isException<NullPointerException>() },
)
}

@Test
fun testCastQueueToDeque() {
check(
QueueUsages::castQueueToDeque,
eq(2),
{ q, r -> q !is java.util.Deque<*> && r == null },
{ q, r -> q is java.util.Deque<*> && r is java.util.Deque<*> },
)
}

@Test
fun testCheckSubtypesOfQueue() {
check(
QueueUsages::checkSubtypesOfQueue,
eq(4),
{ q, r -> q == null && r == 0 },
{ q, r -> q is java.util.LinkedList<*> && r == 1 },
{ q, r -> q is java.util.ArrayDeque<*> && r == 2 },
{ q, r -> q !is java.util.LinkedList<*> && q !is java.util.ArrayDeque && r == 3 }
)
}

@Test
@Disabled("TODO: Related to https://github.com/UnitTestBot/UTBotJava/issues/820")
fun testCheckSubtypesOfQueueWithUsage() {
check(
QueueUsages::checkSubtypesOfQueueWithUsage,
eq(4),
{ q, r -> q == null && r == 0 },
{ q, r -> q is java.util.LinkedList<*> && r == 1 },
{ q, r -> q is java.util.ArrayDeque<*> && r == 2 },
{ q, r -> q !is java.util.LinkedList<*> && q !is java.util.ArrayDeque && r == 3 } // this is uncovered
)
}

@Test
fun testAddConcurrentLinkedQueue() {
checkWithException(
QueueUsages::addConcurrentLinkedQueue,
eq(3),
{ q, _, r -> q == null && r.isException<NullPointerException>() },
{ q, x, r -> q != null && x != null && x in r.getOrNull()!! },
{ q, x, r -> q != null && x == null && r.isException<NullPointerException>() },
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ import java.util.LinkedList
import java.util.TreeMap
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.testcheckers.eq
import org.utbot.tests.infrastructure.CodeGeneration

internal class StandardStructuresTest : UtValueTestCaseChecker(testClass = StandardStructures::class) {
internal class StandardStructuresTest : UtValueTestCaseChecker(
testClass = StandardStructures::class,
languagePipelines = listOf(
CodeGenerationLanguageLastStage(CodegenLanguage.JAVA),
CodeGenerationLanguageLastStage(CodegenLanguage.KOTLIN, CodeGeneration)
)
) {
@Test
@Disabled("TODO down cast for object wrapper JIRA:1480")
fun testGetList() {
Expand Down Expand Up @@ -45,7 +53,6 @@ internal class StandardStructuresTest : UtValueTestCaseChecker(testClass = Stand
}

@Test
@Disabled("TODO use correct wrapper JIRA:1495")
fun testGetDeque() {
val dequeSummary = listOf<DocStatement>(
DocPreTagStatement(
Expand All @@ -72,7 +79,7 @@ internal class StandardStructuresTest : UtValueTestCaseChecker(testClass = Stand
{ d, r -> d is LinkedList && r is LinkedList },
{ d, r -> d == null && r == null },
{ d, r ->
d !is ArrayDeque<*> && d !is LinkedList && d != null && r !is ArrayDeque<*> && r !is LinkedList && r != null
d !is java.util.ArrayDeque<*> && d !is LinkedList && d != null && r !is java.util.ArrayDeque<*> && r !is LinkedList && r != null
},
coverage = DoNotCalculate,
summaryTextChecks = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public UtLinkedList(Collection<? extends E> c) {
* <li> elementData is marked as parameter </li>
* <li> elementData.storage and it's elements are marked as parameters </li>
*/
private void preconditionCheck() {
protected void preconditionCheck() {
if (alreadyVisited(this)) {
return;
}
Expand All @@ -88,13 +88,13 @@ private void preconditionCheck() {
visit(this);
}

private void rangeCheck(int index) {
protected void rangeCheck(int index) {
if (index < 0 || index >= elementData.end - elementData.begin) {
throw new IndexOutOfBoundsException();
}
}

private void rangeCheckForAdd(int index) {
protected void rangeCheckForAdd(int index) {
if (index < 0 || index > elementData.end - elementData.begin) {
throw new IndexOutOfBoundsException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.utbot.engine.overrides.collections;

import java.util.Collection;

/**
* This list forbids inserting null elements to support some implementations of {@link java.util.Deque} like
* {@link java.util.ArrayDeque}.
*
* TODO: <a href="https://github.com/UnitTestBot/UTBotJava/issues/819">Support super calls in inherited wrappers</a>
*
* @see UtLinkedList
* @param <E>
*/
public class UtLinkedListWithNullableCheck<E> extends UtLinkedList<E> {
@SuppressWarnings("unused")
UtLinkedListWithNullableCheck(RangeModifiableUnlimitedArray<E> elementData, int fromIndex, int toIndex) {
super(elementData, fromIndex, toIndex);
for (int i = elementData.begin; i < elementData.end; i++) {
if (elementData.get(i) == null) {
throw new NullPointerException();
}
}
}

@SuppressWarnings("unused")
public UtLinkedListWithNullableCheck() {
super();
}

@SuppressWarnings("unused")
public UtLinkedListWithNullableCheck(Collection<? extends E> c) {
super(c);
}

@Override
public E set(int index, E element) {
if (element == null) {
throw new NullPointerException();
}
preconditionCheck();
rangeCheck(index);
E oldElement = elementData.get(index + elementData.begin);
elementData.set(index + elementData.begin, element);
return oldElement;
}

@Override
public void addFirst(E e) {
if (e == null) {
throw new NullPointerException();
}
preconditionCheck();
elementData.set(--elementData.begin, e);
}

@Override
public void addLast(E e) {
if (e == null) {
throw new NullPointerException();
}
preconditionCheck();
elementData.set(elementData.end++, e);
}

@Override
public boolean offerFirst(E e) {
if (e == null) {
throw new NullPointerException();
}
preconditionCheck();
elementData.set(--elementData.begin, e);
return true;
}

@Override
public boolean offerLast(E e) {
if (e == null) {
throw new NullPointerException();
}
preconditionCheck();
elementData.set(elementData.end++, e);
return true;
}

@Override
public void add(int index, E element) {
if (element == null) {
throw new NullPointerException();
}
preconditionCheck();
rangeCheckForAdd(index);
elementData.end++;
elementData.insert(index + elementData.begin, element);
}

@Override
public boolean addAll(Collection<? extends E> c) {
for (Object elem : c.toArray()) {
if (elem == null) {
throw new NullPointerException();
}
}
preconditionCheck();
elementData.setRange(elementData.end, c.toArray(), 0, c.size());
elementData.end += c.size();
return true;
}

@Override
public boolean addAll(int index, Collection<? extends E> c) {
for (Object elem : c.toArray()) {
if (elem == null) {
throw new NullPointerException();
}
}
preconditionCheck();
rangeCheckForAdd(index);
elementData.insertRange(index + elementData.begin, c.toArray(), 0, c.size());
elementData.end += c.size();
return true;
}
}
Loading