Skip to content

Commit 7c9141f

Browse files
LiedtkeV8-internal LUCI CQ
authored andcommitted
Simplify chooseUniform to work with any collection
This allows us to also use it e.g. for Dictionary keys without having to convert them to an Array first. Change-Id: I72d8e224abc943556e14290eb566bc43f65f596d Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8587038 Auto-Submit: Matthias Liedtke <mliedtke@google.com> Commit-Queue: Matthias Liedtke <mliedtke@google.com> Reviewed-by: Manish Goregaokar <manishearth@google.com> Commit-Queue: Manish Goregaokar <manishearth@google.com>
1 parent 640f4e2 commit 7c9141f

2 files changed

Lines changed: 3 additions & 18 deletions

File tree

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4437,16 +4437,14 @@ public class ProgramBuilder {
44374437
return createObject(with: properties)
44384438
}
44394439

4440-
fileprivate static let allTimeZoneAbbreviations: [String] = Array(TimeZone.abbreviationDictionary.keys)
4441-
44424440
// Generate a random time zone identifier
44434441
@discardableResult
44444442
func randomTimeZone() -> Variable {
44454443
// Bias towards knownTimeZoneIdentifiers since it's a larger array
44464444
if probability(0.7) {
44474445
return loadString(chooseUniform(from: TimeZone.knownTimeZoneIdentifiers))
44484446
} else {
4449-
return loadString(chooseUniform(from: ProgramBuilder.allTimeZoneAbbreviations))
4447+
return loadString(chooseUniform(from: TimeZone.abbreviationDictionary.keys))
44504448
}
44514449
}
44524450

Sources/Fuzzilli/Util/Random.swift

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,9 @@ extension String {
5555
}
5656

5757
/// Returns a uniformly choosen, random element from the given collection.
58-
public func chooseUniform<E>(from collection: [E]) -> E {
58+
public func chooseUniform<C: Collection>(from collection: C) -> C.Element {
5959
assert(collection.count != 0, "cannot choose from an empty sequence")
60-
return collection[Int.random(in: 0..<collection.count)]
61-
}
62-
63-
/// Returns a uniformly choosen, random element from the given collection.
64-
public func chooseUniform<E>(from collection: ArraySlice<E>) -> E {
65-
assert(collection.count != 0, "cannot choose from an empty sequence")
66-
return collection[Int.random(in: 0..<collection.count)]
67-
}
68-
69-
/// Returns a uniformly choosen, random element from the given collection.
70-
public func chooseUniform<E>(from collection: Set<E>) -> E {
71-
assert(collection.count != 0, "cannot choose from an empty set")
72-
let i = collection.index(collection.startIndex, offsetBy: Int.random(in: 0..<collection.count))
73-
return collection[i]
60+
return collection.randomElement()!
7461
}
7562

7663
/// Returns a random element from the given collection favouring later elements by the given factor.

0 commit comments

Comments
 (0)