1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import CompletionScoring
import Foundation
import SwiftExtensions
package struct RepeatableRandomNumberGenerator: RandomNumberGenerator {
private let seed: [UInt64]
private static let startIndex = (0, 1, 2, 3)
private var nextIndex = Self.startIndex
package init() {
self.seed = try! PropertyListDecoder().decode(
[UInt64].self,
from: loadTestResource(name: "RandomSeed", withExtension: "plist")
)
}
@discardableResult
func increment(value: inout Int, range: Range<Int>) -> Bool {
value += 1
let wrapped = (value == range.upperBound)
if wrapped {
value = range.lowerBound
}
return wrapped
}
mutating func advance() {
// This iterates through "K choose N" or "seed.count choose 4" unique combinations of 4 seed indexes. Given 1024 values in seed, that produces 45,545,029,376 unique combination of the values.
if increment(value: &nextIndex.3, range: (nextIndex.2 + 1)..<(seed.count - 0)) {
if increment(value: &nextIndex.2, range: (nextIndex.1 + 1)..<(seed.count - 1)) {
if increment(value: &nextIndex.1, range: (nextIndex.0 + 1)..<(seed.count - 2)) {
if increment(value: &nextIndex.0, range: 0..<(seed.count - 3)) {
nextIndex = Self.startIndex
return
}
nextIndex.1 = (nextIndex.0 + 1)
}
nextIndex.2 = (nextIndex.1 + 1)
}
nextIndex.3 = (nextIndex.2 + 1)
}
}
package mutating func next() -> UInt64 {
let result = seed[nextIndex.0] ^ seed[nextIndex.1] ^ seed[nextIndex.2] ^ seed[nextIndex.3]
advance()
return result
}
static func generateSeed() {
let numbers: [UInt64] = (0..<1024).map { _ in
let lo = UInt64.random(in: 0...UInt64.max)
let hi = UInt64.random(in: 0...UInt64.max)
return (hi << 32) | lo
}
let header =
"""
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
"""
let body = numbers.map { number in " <integer>\(number)</integer>" }
let footer =
"""
</array>
</plist>
"""
print(([header] + body + [footer]).joined(separator: "\n"))
}
package mutating func randomLowercaseASCIIString(lengthRange: ClosedRange<Int>) -> String {
let length = lengthRange.randomElement(using: &self)
let utf8Bytes = (0..<length).map { _ in
UTF8Byte.lowercaseAZ.randomElement(using: &self)
}
return String(bytes: utf8Bytes, encoding: .utf8).unwrap(orFail: "ASCII strings are always valid UTF8 sequences")
}
package mutating func randomLowercaseASCIIStrings(
countRange: ClosedRange<Int>,
lengthRange: ClosedRange<Int>
) -> [String] {
let count = countRange.randomElement(using: &self)
var strings: [String] = []
for _ in (0..<count) {
strings.append(randomLowercaseASCIIString(lengthRange: lengthRange))
}
return strings
}
}
/// The bundle of the currently executing test.
private let testBundle: Bundle = {
#if os(macOS)
if let bundle = Bundle.allBundles.first(where: { $0.bundlePath.hasSuffix(".xctest") }) {
return bundle
}
fatalError("couldn't find the test bundle")
#else
return Bundle.main
#endif
}()
/// The path to the built products directory, ie. `.build/debug/arm64-apple-macosx` or the platform-specific equivalent.
private let productsDirectory: URL = {
#if os(macOS)
return testBundle.bundleURL.deletingLastPathComponent()
#else
return testBundle.bundleURL
#endif
}()
/// The path to the INPUTS directory of shared test projects.
private let skTestSupportInputsDirectory: URL = {
#if os(macOS)
var resources =
productsDirectory
.appendingPathComponent("SourceKitLSP_CompletionScoringTestSupport.bundle")
.appendingPathComponent("Contents")
.appendingPathComponent("Resources")
if !FileManager.default.fileExists(at: resources) {
// Xcode and command-line swiftpm differ about the path.
resources.deleteLastPathComponent()
resources.deleteLastPathComponent()
}
#else
let resources =
productsDirectory
.appendingPathComponent("SourceKitLSP_CompletionScoringTestSupport.resources")
#endif
guard FileManager.default.fileExists(at: resources) else {
fatalError("missing resources \(resources)")
}
return resources.appendingPathComponent("INPUTS", isDirectory: true).standardizedFileURL
}()
func loadTestResource(name: String, withExtension ext: String) throws -> Data {
let file =
skTestSupportInputsDirectory
.appendingPathComponent("\(name).\(ext)")
return try Data(contentsOf: file)
}
extension ClosedRange {
func randomElement<Generator: RandomNumberGenerator>(using randomness: inout Generator) -> Element {
return randomElement(using: &randomness)! // Closed ranges always have a value
}
}
|