Uploaded image for project: 'Commons Collections'
  1. Commons Collections
  2. COLLECTIONS-804

testCollectionToArray2() is non deterministic

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 4.4
    • None
    • Collection
    • None

    Description

      The test org.apache.commons.collections4.collection.AbstractCollectionTest.testCollectionToArray2() can fail if two toArray() calls return elements in a different order. One can check for that using the NonDex tool (https://github.com/TestingResearchIllinois/NonDex).

      Bug repoduction:

      1. Clone the repo and cd into it.
      2. mvn test -Dtest=org.apache.commons.collections4.bidimap.DualHashBidiMapTest#testCollectionToArray2 -Drat.skip This test will pass.
      3. {{mvn edu.illinois:nondex-maven-plugin:1.1.2:nondex
        -Dtest=org.apache.commons.collections4.bidimap.DualHashBidiMapTest#testCollectionToArray2
        -Drat.skip}} This command runs the NonDex tool on the the test. Near the end of the output, you will see a summary of test failures, where you will find testCollectionToArray2() function listed.

      The source of the bug:
      The test gets an array representation of the collections and then compares them, taking the order into account. However, Java Collections need not be necessarily ordered. Its member function toArray() also does not guarantee a deterministic order of the elements in its returned list. As such, we should ignore the order of the elements in the assertion.

      Proposed fix:
      We ignore the order of array lists by putting its elements into separate multisets and then comparing those multisets for equality. Multiset is not a part of the standard Java library. However, conveniently enough, this repository already had an implementation of multiset called HashBags, which I used.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              chessvivek Vivek Gupta
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: