11import Foundation
22import XCTest
33
4- func randomArray( size: Int ) -> [ Int ] {
4+ func randomArray( _ size: Int ) -> [ Int ] {
55 var a = [ Int] ( )
66 for _ in 1 ... size {
77 a. append ( Int ( arc4random_uniform ( 1000 ) ) )
88 }
99 return a
1010}
1111
12- func arrayIsSortedLowToHigh( a: [ Int ] ) -> Bool {
12+ func arrayIsSortedLowToHigh( _ a: [ Int ] ) -> Bool {
1313 for x in 1 ..< a. count {
1414 if a [ x - 1 ] > a [ x] { return false }
1515 }
1616 return true
1717}
1818
19- typealias SortFunction = [ Int ] -> [ Int ]
19+ typealias SortFunction = ( [ Int ] ) -> [ Int ]
2020
21- func checkSortingRandomArray( sortFunction: SortFunction ) {
21+ func checkSortingRandomArray( _ sortFunction: SortFunction ) {
2222 let numberOfIterations = 100
2323 for _ in 1 ... numberOfIterations {
2424 let a = randomArray ( Int ( arc4random_uniform ( 100 ) ) + 1 )
@@ -28,73 +28,73 @@ func checkSortingRandomArray(sortFunction: SortFunction) {
2828 }
2929}
3030
31- func checkSortingEmptyArray( sortFunction: SortFunction ) {
31+ func checkSortingEmptyArray( _ sortFunction: SortFunction ) {
3232 let a = [ Int] ( )
3333 let s = sortFunction ( a)
3434 XCTAssertEqual ( s. count, 0 )
3535}
3636
37- func checkSortingArrayOneElement( sortFunction: SortFunction ) {
37+ func checkSortingArrayOneElement( _ sortFunction: SortFunction ) {
3838 let a = [ 123 ]
3939 let s = sortFunction ( a)
4040 XCTAssertEqual ( s, [ 123 ] )
4141}
4242
43- func checkSortingArrayTwoElementsInOrder( sortFunction: SortFunction ) {
43+ func checkSortingArrayTwoElementsInOrder( _ sortFunction: SortFunction ) {
4444 let a = [ 123 , 456 ]
4545 let s = sortFunction ( a)
4646 XCTAssertEqual ( s, [ 123 , 456 ] )
4747}
4848
49- func checkSortingArrayTwoElementsOutOfOrder( sortFunction: SortFunction ) {
49+ func checkSortingArrayTwoElementsOutOfOrder( _ sortFunction: SortFunction ) {
5050 let a = [ 456 , 123 ]
5151 let s = sortFunction ( a)
5252 XCTAssertEqual ( s, [ 123 , 456 ] )
5353}
5454
55- func checkSortingArrayTwoEqualElements( sortFunction: SortFunction ) {
55+ func checkSortingArrayTwoEqualElements( _ sortFunction: SortFunction ) {
5656 let a = [ 123 , 123 ]
5757 let s = sortFunction ( a)
5858 XCTAssertEqual ( s, [ 123 , 123 ] )
5959}
6060
61- func checkSortingArrayThreeElementsABC( sortFunction: SortFunction ) {
61+ func checkSortingArrayThreeElementsABC( _ sortFunction: SortFunction ) {
6262 let a = [ 2 , 4 , 6 ]
6363 let s = sortFunction ( a)
6464 XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
6565}
6666
67- func checkSortingArrayThreeElementsACB( sortFunction: SortFunction ) {
67+ func checkSortingArrayThreeElementsACB( _ sortFunction: SortFunction ) {
6868 let a = [ 2 , 6 , 4 ]
6969 let s = sortFunction ( a)
7070 XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
7171}
7272
73- func checkSortingArrayThreeElementsBAC( sortFunction: SortFunction ) {
73+ func checkSortingArrayThreeElementsBAC( _ sortFunction: SortFunction ) {
7474 let a = [ 4 , 2 , 6 ]
7575 let s = sortFunction ( a)
7676 XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
7777}
7878
79- func checkSortingArrayThreeElementsBCA( sortFunction: SortFunction ) {
79+ func checkSortingArrayThreeElementsBCA( _ sortFunction: SortFunction ) {
8080 let a = [ 4 , 6 , 2 ]
8181 let s = sortFunction ( a)
8282 XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
8383}
8484
85- func checkSortingArrayThreeElementsCAB( sortFunction: SortFunction ) {
85+ func checkSortingArrayThreeElementsCAB( _ sortFunction: SortFunction ) {
8686 let a = [ 6 , 2 , 4 ]
8787 let s = sortFunction ( a)
8888 XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
8989}
9090
91- func checkSortingArrayThreeElementsCBA( sortFunction: SortFunction ) {
91+ func checkSortingArrayThreeElementsCBA( _ sortFunction: SortFunction ) {
9292 let a = [ 6 , 4 , 2 ]
9393 let s = sortFunction ( a)
9494 XCTAssertEqual ( s, [ 2 , 4 , 6 ] )
9595}
9696
97- func checkSortAlgorithm( sortFunction: SortFunction ) {
97+ func checkSortAlgorithm( _ sortFunction: SortFunction ) {
9898 checkSortingEmptyArray ( sortFunction)
9999 checkSortingArrayOneElement ( sortFunction)
100100 checkSortingArrayTwoElementsInOrder ( sortFunction)
@@ -109,6 +109,6 @@ func checkSortAlgorithm(sortFunction: SortFunction) {
109109 checkSortingRandomArray ( sortFunction)
110110}
111111
112- func checkSortAlgorithm( sortFunction: ( [ Int ] , ( Int , Int ) -> Bool ) -> [ Int ] ) {
112+ func checkSortAlgorithm( _ sortFunction: @escaping ( [ Int ] , ( Int , Int ) -> Bool ) -> [ Int ] ) {
113113 checkSortAlgorithm { a in sortFunction ( a, < ) }
114114}
0 commit comments