1
1
import Foundation
2
2
import XCTest
3
3
4
- func randomArray( size: Int ) -> [ Int ] {
4
+ func randomArray( _ size: Int ) -> [ Int ] {
5
5
var a = [ Int] ( )
6
6
for _ in 1 ... size {
7
7
a. append ( Int ( arc4random_uniform ( 1000 ) ) )
8
8
}
9
9
return a
10
10
}
11
11
12
- func arrayIsSortedLowToHigh( a: [ Int ] ) -> Bool {
12
+ func arrayIsSortedLowToHigh( _ a: [ Int ] ) -> Bool {
13
13
for x in 1 ..< a. count {
14
14
if a [ x - 1 ] > a [ x] { return false }
15
15
}
16
16
return true
17
17
}
18
18
19
- typealias SortFunction = [ Int ] -> [ Int ]
19
+ typealias SortFunction = ( [ Int ] ) -> [ Int ]
20
20
21
- func checkSortingRandomArray( sortFunction: SortFunction ) {
21
+ func checkSortingRandomArray( _ sortFunction: SortFunction ) {
22
22
let numberOfIterations = 100
23
23
for _ in 1 ... numberOfIterations {
24
24
let a = randomArray ( Int ( arc4random_uniform ( 100 ) ) + 1 )
@@ -28,73 +28,73 @@ func checkSortingRandomArray(sortFunction: SortFunction) {
28
28
}
29
29
}
30
30
31
- func checkSortingEmptyArray( sortFunction: SortFunction ) {
31
+ func checkSortingEmptyArray( _ sortFunction: SortFunction ) {
32
32
let a = [ Int] ( )
33
33
let s = sortFunction ( a)
34
34
XCTAssertEqual ( s. count, 0 )
35
35
}
36
36
37
- func checkSortingArrayOneElement( sortFunction: SortFunction ) {
37
+ func checkSortingArrayOneElement( _ sortFunction: SortFunction ) {
38
38
let a = [ 123 ]
39
39
let s = sortFunction ( a)
40
40
XCTAssertEqual ( s, [ 123 ] )
41
41
}
42
42
43
- func checkSortingArrayTwoElementsInOrder( sortFunction: SortFunction ) {
43
+ func checkSortingArrayTwoElementsInOrder( _ sortFunction: SortFunction ) {
44
44
let a = [ 123 , 456 ]
45
45
let s = sortFunction ( a)
46
46
XCTAssertEqual ( s, [ 123 , 456 ] )
47
47
}
48
48
49
- func checkSortingArrayTwoElementsOutOfOrder( sortFunction: SortFunction ) {
49
+ func checkSortingArrayTwoElementsOutOfOrder( _ sortFunction: SortFunction ) {
50
50
let a = [ 456 , 123 ]
51
51
let s = sortFunction ( a)
52
52
XCTAssertEqual ( s, [ 123 , 456 ] )
53
53
}
54
54
55
- func checkSortingArrayTwoEqualElements( sortFunction: SortFunction ) {
55
+ func checkSortingArrayTwoEqualElements( _ sortFunction: SortFunction ) {
56
56
let a = [ 123 , 123 ]
57
57
let s = sortFunction ( a)
58
58
XCTAssertEqual ( s, [ 123 , 123 ] )
59
59
}
60
60
61
- func checkSortingArrayThreeElementsABC( sortFunction: SortFunction ) {
61
+ func checkSortingArrayThreeElementsABC( _ sortFunction: SortFunction ) {
62
62
let a = [ 2 , 4 , 6 ]
63
63
let s = sortFunction ( a)
64
64
XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
65
65
}
66
66
67
- func checkSortingArrayThreeElementsACB( sortFunction: SortFunction ) {
67
+ func checkSortingArrayThreeElementsACB( _ sortFunction: SortFunction ) {
68
68
let a = [ 2 , 6 , 4 ]
69
69
let s = sortFunction ( a)
70
70
XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
71
71
}
72
72
73
- func checkSortingArrayThreeElementsBAC( sortFunction: SortFunction ) {
73
+ func checkSortingArrayThreeElementsBAC( _ sortFunction: SortFunction ) {
74
74
let a = [ 4 , 2 , 6 ]
75
75
let s = sortFunction ( a)
76
76
XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
77
77
}
78
78
79
- func checkSortingArrayThreeElementsBCA( sortFunction: SortFunction ) {
79
+ func checkSortingArrayThreeElementsBCA( _ sortFunction: SortFunction ) {
80
80
let a = [ 4 , 6 , 2 ]
81
81
let s = sortFunction ( a)
82
82
XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
83
83
}
84
84
85
- func checkSortingArrayThreeElementsCAB( sortFunction: SortFunction ) {
85
+ func checkSortingArrayThreeElementsCAB( _ sortFunction: SortFunction ) {
86
86
let a = [ 6 , 2 , 4 ]
87
87
let s = sortFunction ( a)
88
88
XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
89
89
}
90
90
91
- func checkSortingArrayThreeElementsCBA( sortFunction: SortFunction ) {
91
+ func checkSortingArrayThreeElementsCBA( _ sortFunction: SortFunction ) {
92
92
let a = [ 6 , 4 , 2 ]
93
93
let s = sortFunction ( a)
94
94
XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
95
95
}
96
96
97
- func checkSortAlgorithm( sortFunction: SortFunction ) {
97
+ func checkSortAlgorithm( _ sortFunction: SortFunction ) {
98
98
checkSortingEmptyArray ( sortFunction)
99
99
checkSortingArrayOneElement ( sortFunction)
100
100
checkSortingArrayTwoElementsInOrder ( sortFunction)
@@ -109,6 +109,6 @@ func checkSortAlgorithm(sortFunction: SortFunction) {
109
109
checkSortingRandomArray ( sortFunction)
110
110
}
111
111
112
- func checkSortAlgorithm( sortFunction: ( [ Int ] , ( Int , Int ) -> Bool ) -> [ Int ] ) {
112
+ func checkSortAlgorithm( _ sortFunction: @escaping ( [ Int ] , ( Int , Int ) -> Bool ) -> [ Int ] ) {
113
113
checkSortAlgorithm { a in sortFunction ( a, < ) }
114
114
}
0 commit comments