|
1 | 1 | //: Playground - noun: a place where people can play
|
2 | 2 |
|
3 | 3 | import Cocoa
|
| 4 | +import Foundation |
| 5 | + |
| 6 | +let treeOfStrings = TernarySearchTree<String>() |
| 7 | + |
| 8 | +//Random string generator from: |
| 9 | +//http://stackoverflow.com/questions/26845307/generate-random-alphanumeric-string-in-swift/26845710 |
| 10 | +func randomAlphaNumericString(length: Int) -> String { |
| 11 | + |
| 12 | + let allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 13 | + let allowedCharsCount = UInt32(allowedChars.characters.count) |
| 14 | + var randomString = "" |
| 15 | + |
| 16 | + for _ in (0..<length) { |
| 17 | + let randomNum = Int(arc4random_uniform(allowedCharsCount)) |
| 18 | + let newCharacter = allowedChars[allowedChars.startIndex.advancedBy(randomNum)] |
| 19 | + randomString += String(newCharacter) |
| 20 | + } |
| 21 | + |
| 22 | + return randomString |
| 23 | +} |
| 24 | + |
| 25 | +var testStrings: [(key:String, data:String)] = [] |
| 26 | +let testCount = 30 |
| 27 | +for _ in (1...testCount) { |
| 28 | + let randomLength = Int(arc4random_uniform(10)) |
| 29 | + let key = randomAlphaNumericString(randomLength) |
| 30 | + let data = randomAlphaNumericString(randomLength) |
| 31 | +// print("Key: \(key) Data: \(data)") |
| 32 | + |
| 33 | + if key != "" && data != "" { |
| 34 | + testStrings.append((key, data)) |
| 35 | + treeOfStrings.insert(data, withKey: key) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +for aTest in testStrings { |
| 40 | + let data = treeOfStrings.find(aTest.key) |
| 41 | + |
| 42 | + if data == nil { |
| 43 | + print("TEST FAILED. Key: \(aTest.key) Data: \(aTest.data)") |
| 44 | + } |
| 45 | + if data != aTest.data { |
| 46 | + print("TEST FAILED. Key: \(aTest.key) Data: \(aTest.data)") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +var testNums: [(key: String, data: Int)] = [] |
| 51 | +let treeOfInts = TernarySearchTree<Int>() |
| 52 | +for _ in (1...testCount) { |
| 53 | + let randomNum = Int(arc4random_uniform(UInt32.max)) |
| 54 | + let randomLength = Int(arc4random_uniform(10)) |
| 55 | + let key = randomAlphaNumericString(randomLength) |
| 56 | + |
| 57 | + if key != "" { |
| 58 | + testNums.append((key, randomNum)) |
| 59 | + treeOfInts.insert(randomNum, withKey: key) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +for aTest in testNums { |
| 64 | + let data = treeOfInts.find(aTest.key) |
| 65 | + |
| 66 | + if data == nil { |
| 67 | + print("TEST FAILED. Key: \(aTest.key) Data: \(aTest.data)") |
| 68 | + } |
| 69 | + if data != aTest.data { |
| 70 | + print("TEST FAILED. Key: \(aTest.key) Data: \(aTest.data)") |
| 71 | + } |
| 72 | +} |
4 | 73 |
|
0 commit comments