Changeset 18109 in josm for trunk/test/unit/org/openstreetmap/josm/TestUtils.java
- Timestamp:
- 2021-08-01T22:03:28+02:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/TestUtils.java
r17372 r18109 23 23 import java.time.format.DateTimeFormatter; 24 24 import java.time.temporal.Temporal; 25 import java.util.ArrayList; 25 26 import java.util.Arrays; 26 27 import java.util.Collection; … … 31 32 import java.util.concurrent.ExecutionException; 32 33 import java.util.concurrent.ThreadPoolExecutor; 34 import java.util.concurrent.atomic.AtomicInteger; 33 35 import java.util.function.Function; 34 36 import java.util.stream.Collectors; 37 import java.util.stream.IntStream; 35 38 import java.util.stream.Stream; 36 39 … … 169 172 } 170 173 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 171 214 private static <T> String getFailMessage(T o1, T o2, int a, int b) { 172 215 return new StringBuilder("Compared\no1: ").append(o1).append("\no2: ")
Note:
See TracChangeset
for help on using the changeset viewer.