JLChen
2021-11-04 1443556e9ccb1a19ed8e6710c16c8adc4d4f4fb3
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
//
//  Copyright © 2019 jm. All rights reserved.
//
 
import Foundation
 
protocol Arbitrary {
    static func arbitrary() -> Self
}
 
public extension Int {
    static func random(from: Int, to: Int) -> Int {
        return from + (Int(arc4random()) % (to - from))
    }
}
 
extension Character: Arbitrary {
    static func arbitrary() -> Character {
        return Character(UnicodeScalar(Int.random(from: 65, to: 90))!)
    }
}
 
extension String: Arbitrary {
    
    static func arbitrary() -> String {
        let randomLength = Int.random(from: 10, to: 24)
        let randomCharacters = tabulate(times: randomLength) { _ in
            Character.arbitrary()
        }
        return String(randomCharacters)
    }
    
    static func tabulate<T>(times: Int, transform: (Int) -> T) -> [T] {
        return (0..<times).map(transform)
    }
    
}