Skip to content

Commit 45440c4

Browse files
committed
swaps generation tests
1 parent 29f92f0 commit 45440c4

File tree

7 files changed

+177
-18
lines changed

7 files changed

+177
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1010"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "716548802253DE8C009C41DD"
18+
BuildableName = "SortingAlgorithms.app"
19+
BlueprintName = "SortingAlgorithms"
20+
ReferencedContainer = "container:SortingAlgorithms.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
codeCoverageEnabled = "YES"
30+
shouldUseLaunchSchemeArgsEnv = "YES">
31+
<Testables>
32+
<TestableReference
33+
skipped = "NO">
34+
<BuildableReference
35+
BuildableIdentifier = "primary"
36+
BlueprintIdentifier = "716548942253DE8D009C41DD"
37+
BuildableName = "SortingAlgorithmsTests.xctest"
38+
BlueprintName = "SortingAlgorithmsTests"
39+
ReferencedContainer = "container:SortingAlgorithms.xcodeproj">
40+
</BuildableReference>
41+
</TestableReference>
42+
</Testables>
43+
<MacroExpansion>
44+
<BuildableReference
45+
BuildableIdentifier = "primary"
46+
BlueprintIdentifier = "716548802253DE8C009C41DD"
47+
BuildableName = "SortingAlgorithms.app"
48+
BlueprintName = "SortingAlgorithms"
49+
ReferencedContainer = "container:SortingAlgorithms.xcodeproj">
50+
</BuildableReference>
51+
</MacroExpansion>
52+
<AdditionalOptions>
53+
</AdditionalOptions>
54+
</TestAction>
55+
<LaunchAction
56+
buildConfiguration = "Debug"
57+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
58+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
59+
launchStyle = "0"
60+
useCustomWorkingDirectory = "NO"
61+
ignoresPersistentStateOnLaunch = "NO"
62+
debugDocumentVersioning = "YES"
63+
debugServiceExtension = "internal"
64+
allowLocationSimulation = "YES">
65+
<BuildableProductRunnable
66+
runnableDebuggingMode = "0">
67+
<BuildableReference
68+
BuildableIdentifier = "primary"
69+
BlueprintIdentifier = "716548802253DE8C009C41DD"
70+
BuildableName = "SortingAlgorithms.app"
71+
BlueprintName = "SortingAlgorithms"
72+
ReferencedContainer = "container:SortingAlgorithms.xcodeproj">
73+
</BuildableReference>
74+
</BuildableProductRunnable>
75+
<AdditionalOptions>
76+
</AdditionalOptions>
77+
</LaunchAction>
78+
<ProfileAction
79+
buildConfiguration = "Release"
80+
shouldUseLaunchSchemeArgsEnv = "YES"
81+
savedToolIdentifier = ""
82+
useCustomWorkingDirectory = "NO"
83+
debugDocumentVersioning = "YES">
84+
<BuildableProductRunnable
85+
runnableDebuggingMode = "0">
86+
<BuildableReference
87+
BuildableIdentifier = "primary"
88+
BlueprintIdentifier = "716548802253DE8C009C41DD"
89+
BuildableName = "SortingAlgorithms.app"
90+
BlueprintName = "SortingAlgorithms"
91+
ReferencedContainer = "container:SortingAlgorithms.xcodeproj">
92+
</BuildableReference>
93+
</BuildableProductRunnable>
94+
</ProfileAction>
95+
<AnalyzeAction
96+
buildConfiguration = "Debug">
97+
</AnalyzeAction>
98+
<ArchiveAction
99+
buildConfiguration = "Release"
100+
revealArchiveInOrganizer = "YES">
101+
</ArchiveAction>
102+
</Scheme>

SortingAlgorithms.xcodeproj/xcuserdata/VictorMagalhaes.xcuserdatad/xcschemes/xcschememanagement.plist

+13
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,18 @@
1010
<integer>0</integer>
1111
</dict>
1212
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>716548802253DE8C009C41DD</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
<key>716548942253DE8D009C41DD</key>
21+
<dict>
22+
<key>primary</key>
23+
<true/>
24+
</dict>
25+
</dict>
1326
</dict>
1427
</plist>

SortingAlgorithms/Algorithms/BubbleSort.swift

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import Foundation
1010

1111
final class BubbleSort: Algorithm {
12-
1312

1413
final var title: String = "Bubble Sort"
1514
final var image: String = "ic_bubble"

SortingAlgorithms/Algorithms/InsertionSort.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ final class InsertionSort: Algorithm {
2525

2626
for j in (0...i-1).reversed() {
2727
if array[pos] < array[j] {
28-
swaps.append((x0: pos, x1: j))
29-
array.swapAt(pos, j)
28+
swaps.append((x0: j, x1: pos))
29+
array.swapAt(j, pos)
3030
pos = j
3131
}
3232
}

SortingAlgorithms/Algorithms/SelectionSort.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class SelectionSort: Algorithm {
3434
}
3535
}
3636

37-
if i != minPos {
37+
if array[i] != array[minPos] {
3838
swaps.append((x0: i, x1: minPos))
3939
array.swapAt(i, minPos)
4040
}

SortingAlgorithmsTests/SortingAlgorithmsTests.swift

+59-14
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,69 @@ import XCTest
1111

1212
class SortingAlgorithmsTests: XCTestCase {
1313

14-
override func setUp() {
15-
// Put setup code here. This method is called before the invocation of each test method in the class.
16-
}
14+
private let bubbleSort = BubbleSort()
15+
private let insertionSort = InsertionSort()
16+
private let selectionSort = SelectionSort()
1717

18-
override func tearDown() {
19-
// Put teardown code here. This method is called after the invocation of each test method in the class.
18+
func test_BubbleSort_non_sorted() {
19+
let swaps: [(x0: Int, x1: Int)] = bubbleSort.generateSwaps(from: [1,3,2,5,4])
20+
21+
XCTAssert(swaps.count == 2)
22+
XCTAssert((x0: 1, x1: 2) == swaps[0])
23+
XCTAssert((x0: 3, x1: 4) == swaps[1])
2024
}
21-
22-
func testExample() {
23-
// This is an example of a functional test case.
24-
// Use XCTAssert and related functions to verify your tests produce the correct results.
25+
26+
func test_BubbleSort_already_sorted() {
27+
let swaps: [(x0: Int, x1: Int)] = bubbleSort.generateSwaps(from: [1,2,3,4,5])
28+
29+
XCTAssert(swaps.count == 0)
2530
}
31+
32+
func test_BubbleSort_equal_values() {
33+
let swaps: [(x0: Int, x1: Int)] = bubbleSort.generateSwaps(from: [1,1,1,1,1])
2634

27-
func testPerformanceExample() {
28-
// This is an example of a performance test case.
29-
self.measure {
30-
// Put the code you want to measure the time of here.
31-
}
35+
XCTAssert(swaps.count == 0)
36+
}
37+
38+
func test_InsertionSort_non_sorted() {
39+
let swaps: [(x0: Int, x1: Int)] = insertionSort.generateSwaps(from: [1,3,2,5,4])
40+
41+
XCTAssert(swaps.count == 2)
42+
XCTAssert((x0: 2, x1: 1) == swaps[0])
43+
XCTAssert((x0: 4, x1: 3) == swaps[1])
44+
}
45+
46+
func test_InsertionSort_already_sorted() {
47+
let swaps: [(x0: Int, x1: Int)] = insertionSort.generateSwaps(from: [1,2,3,4,5])
48+
49+
XCTAssert(swaps.count == 0)
50+
}
51+
52+
func test_InsertionSort_equal_values() {
53+
let swaps: [(x0: Int, x1: Int)] = insertionSort.generateSwaps(from: [1,1,1,1,1])
54+
55+
XCTAssert(swaps.count == 0)
56+
}
57+
58+
func test_SelectionSort_non_sorted() {
59+
let swaps: [(x0: Int, x1: Int)] = selectionSort.generateSwaps(from: [1,3,2,5,4])
60+
print(swaps)
61+
XCTAssert(swaps.count == 2)
62+
XCTAssert((x0: 1, x1: 2) == swaps[0])
63+
XCTAssert((x0: 3, x1: 4) == swaps[1])
64+
}
65+
66+
func test_SelectionSort_already_sorted() {
67+
let swaps: [(x0: Int, x1: Int)] = selectionSort.generateSwaps(from: [1,2,3,4,5])
68+
69+
XCTAssert(swaps.count == 0)
70+
}
71+
72+
func test_SelectionSort_equal_values() {
73+
let swaps: [(x0: Int, x1: Int)] = selectionSort.generateSwaps(from: [1,1,1,1,1])
74+
print(swaps)
75+
76+
XCTAssert(swaps.count == 0)
3277
}
3378

3479
}

0 commit comments

Comments
 (0)