@@ -10,14 +10,23 @@ import XCTest
10
10
11
11
class TernarySearchTreeTests : XCTestCase {
12
12
13
- let treeOfStrings = TernarySearchTree < String > ( )
13
+
14
14
let testCount = 30
15
15
16
16
func testCanFindStringInTree( ) {
17
17
var testStrings : [ ( key: String , data: String ) ] = [ ]
18
+ let treeOfStrings = TernarySearchTree < String > ( )
19
+
18
20
for _ in ( 1 ... testCount) {
19
- let randomLength = Int ( arc4random_uniform ( 10 ) )
20
- let key = Utils . shared. randomAlphaNumericString ( withLength: randomLength)
21
+ var randomLength = Int ( arc4random_uniform ( 10 ) )
22
+
23
+ var key = Utils . shared. randomAlphaNumericString ( withLength: randomLength)
24
+
25
+ while testStrings. contains ( where: { $0. key == key} ) {
26
+ //That key is taken, so we generate a new one with another length
27
+ randomLength = Int ( arc4random_uniform ( 10 ) )
28
+ key = Utils . shared. randomAlphaNumericString ( withLength: randomLength)
29
+ }
21
30
let data = Utils . shared. randomAlphaNumericString ( withLength: randomLength)
22
31
// print("Key: \(key) Data: \(data)")
23
32
@@ -29,7 +38,6 @@ class TernarySearchTreeTests: XCTestCase {
29
38
30
39
for aTest in testStrings {
31
40
let data = treeOfStrings. find ( key: aTest. key)
32
-
33
41
XCTAssertNotNil ( data)
34
42
XCTAssertEqual ( data, aTest. data)
35
43
}
@@ -40,8 +48,13 @@ class TernarySearchTreeTests: XCTestCase {
40
48
let treeOfInts = TernarySearchTree < Int > ( )
41
49
for _ in ( 1 ... testCount) {
42
50
let randomNum = Int ( arc4random_uniform ( UInt32 . max) )
43
- let randomLength = Int ( arc4random_uniform ( 10 ) )
44
- let key = Utils . shared. randomAlphaNumericString ( withLength: randomLength)
51
+ var randomLength = Int ( arc4random_uniform ( 10 ) )
52
+ var key = Utils . shared. randomAlphaNumericString ( withLength: randomLength)
53
+ while testNums. contains ( where: { $0. key == key} ) {
54
+ //That key is taken, so we generate a new one with another length
55
+ randomLength = Int ( arc4random_uniform ( 10 ) )
56
+ key = Utils . shared. randomAlphaNumericString ( withLength: randomLength)
57
+ }
45
58
46
59
if key != " " {
47
60
testNums. append ( ( key, randomNum) )
0 commit comments