Fix test buildV1StoredIndex
Created by: pengyuejiang
The Problem
In test com.yandex.yoctodb.util.mutable.stored.V1StoredIndexTest.buildV1StoredIndex
, a Map iterator is called implicitly. However, according to Java specification,
The
Map
interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like theTreeMap
class, make specific guarantees as to their order; others, like theHashMap
class, do not.
The plugin NonDex detects such usages that assumes a determinist order, so that when the iterator's implementation is changed, this test fails without really having a problem. Here are the steps to reproduce the issue:
git clone https://github.com/yandex/yoctodb && cd yoctodb
export JAVA_HOME=$(/usr/libexec/java_home -v1.8)
mvn clean install -pl core -am -DskipTests -Dgpg.skip
mvn edu.illinois:nondex-maven-plugin:1.1.2:nondex -pl core -Dtest=V1StoredIndexTest#buildV1StoredIndex
The Fix
The specification does guarantee that TreeMap
has a deterministic iterator. Using that information, casting the Map
to TreeMap
can guarantee that the test will yield the same result every time it runs.
After the fix, running the previous procedure will always pass. This ensures that the test case does not fail because of an implementation change for a non-deterministic specification.
Please comment on whether you think this is a viable solution, thanks :)