Ignore:
Timestamp:
2021-08-01T22:03:28+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #21172 - Add method to create geometric distances from a point (patch by taylor.smock)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/TestUtils.java

    r17372 r18109  
    2323import java.time.format.DateTimeFormatter;
    2424import java.time.temporal.Temporal;
     25import java.util.ArrayList;
    2526import java.util.Arrays;
    2627import java.util.Collection;
     
    3132import java.util.concurrent.ExecutionException;
    3233import java.util.concurrent.ThreadPoolExecutor;
     34import java.util.concurrent.atomic.AtomicInteger;
    3335import java.util.function.Function;
    3436import java.util.stream.Collectors;
     37import java.util.stream.IntStream;
    3538import java.util.stream.Stream;
    3639
     
    169172    }
    170173
     174    /**
     175     * Create a test matrix for parameterized tests.
     176     * <br />
     177     * <b>WARNING:</b> This can quickly become <i>very</i> large (this is combinatorial,
     178     * so the returned {@link Stream} length is the size of the object collections multiplied by each other.
     179     * So if you have three lists of size 3, 4, and 5, the stream size would be {@code 3 * 4 * 5} or 60 elements.
     180     * <br />
     181     * Generally speaking, you should avoid putting expected values into the test matrix.
     182     *
     183     * @param objectCollections The collections of objects. May include/provide {@code null}.
     184     * @return The object arrays to be used as arguments. Note: The returned stream might not be thread-safe.
     185     */
     186    public static Stream<Object[]> createTestMatrix(List<?>... objectCollections) {
     187        // Create the original object arrays
     188        final AtomicInteger size = new AtomicInteger(1);
     189        Stream.of(objectCollections).mapToInt(Collection::size).forEach(i -> size.set(size.get() * i));
     190        final List<Object[]> testMatrix = new ArrayList<>(size.get());
     191        final int[] indexes = IntStream.range(0, objectCollections.length).map(i -> 0).toArray();
     192
     193        // It is important to make a new object array each time (we modify them)
     194        return IntStream.range(0, size.get()).mapToObj(index -> new Object[objectCollections.length]).peek(args -> {
     195            // Just in case someone tries to make this parallel, synchronize on indexes to avoid most issues.
     196            synchronized (indexes) {
     197                // Set the args
     198                for (int listIndex = 0; listIndex < objectCollections.length; listIndex++) {
     199                    args[listIndex] = objectCollections[listIndex].get(indexes[listIndex]);
     200                }
     201                // Increment indexes
     202                for (int listIndex = 0; listIndex < objectCollections.length; listIndex++) {
     203                    indexes[listIndex] = indexes[listIndex] + 1;
     204                    if (indexes[listIndex] >= objectCollections[listIndex].size()) {
     205                        indexes[listIndex] = 0;
     206                    } else {
     207                        break;
     208                    }
     209                }
     210            }
     211        });
     212    }
     213
    171214    private static <T> String getFailMessage(T o1, T o2, int a, int b) {
    172215        return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
Note: See TracChangeset for help on using the changeset viewer.