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

Symbolic execution cannot generate tests in case of specific Random usage #2520

Closed
tyuldashev opened this issue Aug 16, 2023 · 2 comments
Closed
Assignees
Labels
comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug Issue is a bug

Comments

@tyuldashev
Copy link
Collaborator

tyuldashev commented Aug 16, 2023

Description
In some cases when random numbers are generated in fixed loop, symbolic engine cannot generate any test.

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA
  2. In settings set Symbolic = 100%
  3. Generate tests for following code
    public int randomSum(int n) {
        List<Integer> weight = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            weight.add(new Random().nextInt(n));
        }

        return weight.size();
    }

Expected behavior
Tests are generated, similar to what is listed in 'Additional context' section.

Actual behavior
No tests are generated.

Additional context
In case loop statement changed to

for (int i = 0; i < n; i++)

then it starts working similar to what is expected:

public final class RecursionTest {
    ///region Test suites for executable org.example.Recursion.randomSum

    ///region SYMBOLIC EXECUTION: SUCCESSFUL EXECUTIONS for method randomSum(int)

    /**
     * @utbot.classUnderTest {@link Recursion}
     * @utbot.methodUnderTest {@link Recursion#randomSum(int)}
     * @utbot.returnsFrom {@code return weight.size();}
     */
    @Test
    @DisplayName("randomSum: -> return weight.size()")
    public void testRandomSum_ReturnWeightSize() {
        Recursion recursion = new Recursion();

        int actual = recursion.randomSum(0);

        assertEquals(0, actual);
    }

    /**
     * @utbot.classUnderTest {@link Recursion}
     * @utbot.methodUnderTest {@link Recursion#randomSum(int)}
     * @utbot.iterates iterate the loop {@code for(int i = 0; i < n; i++)} once
     * @utbot.returnsFrom {@code return weight.size();}
     */
    @Test
    @DisplayName("randomSum: RandomNextInt -> return weight.size()")
    public void testRandomSum_ListAdd() {
        MockedConstruction mockedConstruction = null;
        try {
            mockedConstruction = mockConstruction(Random.class, (Random randomMock, Context context) -> (when(randomMock.nextInt(anyInt()))).thenReturn(0));
            /* WARNING!!! Automatically used "Mockito static mocking" framework for mocking statics
            because execution encountered flaky methods
            To change this behaviour edit [Settings -> UtBot -> Force static mocking] */
            Recursion recursion = new Recursion();

            int actual = recursion.randomSum(1);

            assertEquals(1, actual);
        } finally {
            mockedConstruction.close();
        }
    }
@tyuldashev tyuldashev added ctg-bug Issue is a bug comp-symbolic-engine Issue is related to the symbolic execution engine labels Aug 16, 2023
@alisevych alisevych added this to the September Release milestone Sep 11, 2023
@alisevych
Copy link
Member

@tyuldashev
Can you please verify this bug is reproducing on the plugin from the latest main?
Test with mock of Random is generated with Symbolic 100% for this code snippet.

@tyuldashev
Copy link
Collaborator Author

Indeed, now it's fixed.

@github-project-automation github-project-automation bot moved this from Todo to Done in UTBot Java Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug Issue is a bug
Projects
Archived in project
Development

No branches or pull requests

2 participants