|
9 | 9 |
|
10 | 10 | package org.elasticsearch.test; |
11 | 11 |
|
12 | | -import com.carrotsearch.randomizedtesting.generators.RandomNumbers; |
| 12 | +import com.carrotsearch.randomizedtesting.generators.RandomPicks; |
13 | 13 |
|
14 | 14 | import org.elasticsearch.Build; |
15 | 15 | import org.elasticsearch.Version; |
|
21 | 21 | import java.util.NavigableSet; |
22 | 22 | import java.util.Random; |
23 | 23 | import java.util.TreeSet; |
24 | | -import java.util.function.IntFunction; |
25 | 24 |
|
26 | 25 | /** Utilities for selecting versions in tests */ |
27 | 26 | public class VersionUtils { |
@@ -77,7 +76,7 @@ public static Version getFirstVersion() { |
77 | 76 |
|
78 | 77 | /** Returns a random {@link Version} from all available versions. */ |
79 | 78 | public static Version randomVersion(Random random) { |
80 | | - return randomFrom(random, ALL_VERSIONS, Version::fromId); |
| 79 | + return randomFrom(random, ALL_VERSIONS); |
81 | 80 | } |
82 | 81 |
|
83 | 82 | /** Returns a random {@link Version} from all available versions, that is compatible with the given version. */ |
@@ -106,24 +105,15 @@ public static Version randomVersionBetween(Random random, @Nullable Version minV |
106 | 105 | versions = versions.headSet(maxVersion, true); |
107 | 106 | } |
108 | 107 |
|
109 | | - return randomFrom(random, versions, Version::fromId); |
| 108 | + return randomFrom(random, versions); |
110 | 109 | } |
111 | 110 |
|
112 | 111 | /** Returns the maximum {@link Version} that is compatible with the given version. */ |
113 | 112 | public static Version maxCompatibleVersion(Version version) { |
114 | 113 | return ALL_VERSIONS.tailSet(version, true).descendingSet().stream().filter(version::isCompatible).findFirst().orElseThrow(); |
115 | 114 | } |
116 | 115 |
|
117 | | - public static <T extends VersionId<T>> T randomFrom(Random random, NavigableSet<T> set, IntFunction<T> ctor) { |
118 | | - // get the first and last id, pick a random id in the middle, then find that id in the set in O(nlogn) time |
119 | | - // this assumes the id numbers are reasonably evenly distributed in the set |
120 | | - assert set.isEmpty() == false; |
121 | | - int lowest = set.getFirst().id(); |
122 | | - int highest = set.getLast().id(); |
123 | | - |
124 | | - T randomId = ctor.apply(RandomNumbers.randomIntBetween(random, lowest, highest)); |
125 | | - // try to find the id below, then the id above. We're just looking for *some* item in the set that is close to randomId |
126 | | - T found = set.floor(randomId); |
127 | | - return found != null ? found : set.ceiling(randomId); |
| 116 | + public static <T extends VersionId<T>> T randomFrom(Random random, NavigableSet<T> set) { |
| 117 | + return RandomPicks.randomFrom(random, set); |
128 | 118 | } |
129 | 119 | } |
0 commit comments