Skip to content

Commit 6b5e88f

Browse files
authored
[Swift 6]: Update concept exercises batch 1 (#807)
* Update Swift tools version to 6.0 and testing files * Add swift numerics as a package for concept exercises as welk * bump numerics version
1 parent 2ac1785 commit 6b5e88f

File tree

9 files changed

+138
-173
lines changed

9 files changed

+138
-173
lines changed

exercises/concept/freelancer-rates/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
".meta/Sources/FreelancerRates/FreelancerRatesExemplar.swift"
1515
]
1616
},
17-
"language_versions": ">=5.0",
17+
"language_versions": ">=6.0",
1818
"forked_from": [
1919
"javascript/freelancer-rates"
2020
],

exercises/concept/freelancer-rates/Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
@@ -14,6 +14,7 @@ let package = Package(
1414
dependencies: [
1515
// Dependencies declare other packages that this package depends on.
1616
// .package(url: /* package url */, from: "1.0.0"),
17+
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.3"),
1718
],
1819
targets: [
1920
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -23,6 +24,6 @@ let package = Package(
2324
dependencies: []),
2425
.testTarget(
2526
name: "FreelancerRatesTests",
26-
dependencies: ["FreelancerRates"]),
27+
dependencies: ["FreelancerRates", .product(name: "Numerics", package: "swift-numerics"),]),
2728
]
2829
)
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
let hoursPerDay = 8
2+
let daysPerMonth = 22
3+
14
func dailyRateFrom(hourlyRate: Int) -> Double {
2-
fatalError("Please implement the dailyRateFrom(hourlyRate:) function")
5+
return Double(hourlyRate * hoursPerDay)
36
}
47

58
func monthlyRateFrom(hourlyRate: Int, withDiscount discount: Double) -> Double {
6-
fatalError("Please implement the monthlyRateFrom(hourlyRate:withDiscount:) function")
9+
let monthly = Double(hourlyRate * hoursPerDay * daysPerMonth) * (1.0 - (discount / 100))
10+
return monthly.rounded()
711
}
812

913
func workdaysIn(budget: Double, hourlyRate: Int, withDiscount discount: Double) -> Double {
10-
fatalError("Please implement the workdaysIn(budget:hourlyRate:withDiscount:) function")
14+
let fullMonthlyRate = budget / (1.0 - (discount / 100))
15+
let hours = fullMonthlyRate / Double(hourlyRate)
16+
let days = hours / Double(hoursPerDay)
17+
return days.rounded(.down)
1118
}
Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,65 @@
1-
import XCTest
1+
import Testing
2+
import Foundation
3+
import Numerics
24

35
@testable import FreelancerRates
46

5-
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
7+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
68

7-
class TaskDailyRateFrom: XCTestCase {
8-
func testdailyRateFromFor60() {
9-
XCTAssertEqual(dailyRateFrom(hourlyRate: 60), 480.0, accuracy: 0.001)
9+
@Suite struct FreelancerRatesTests {
10+
@Test("Daily rate from hourly rate")
11+
func testTaskDailyRateFrom() {
12+
#expect(dailyRateFrom(hourlyRate: 60).isApproximatelyEqual(to: 480.0, relativeTolerance: 0.002))
1013
}
1114

12-
func testdailyRateFromFor16() throws {
13-
try XCTSkipIf(true && !runAll) // change true to false to run this test
14-
XCTAssertEqual(dailyRateFrom(hourlyRate: 16), 128.0, accuracy: 0.001)
15+
@Test("Daily rate from hourly rate with 16 as rate", .enabled(if: RUNALL))
16+
func testTaskDailyRateFromFor16() {
17+
#expect(dailyRateFrom(hourlyRate: 16).isApproximatelyEqual(to: 128.0, relativeTolerance: 0.002))
1518
}
1619

17-
func testdailyRateFromFor25() throws {
18-
try XCTSkipIf(true && !runAll) // change true to false to run this test
19-
XCTAssertEqual(dailyRateFrom(hourlyRate: 25), 200.0, accuracy: 0.001)
20+
@Test("Daily rate from hourly rate with 25 as rate", .enabled(if: RUNALL))
21+
func testTaskDailyRateFromFor25() {
22+
#expect(dailyRateFrom(hourlyRate: 25).isApproximatelyEqual(to: 200.0, relativeTolerance: 0.002))
2023
}
21-
}
2224

23-
class TaskMonthlyRateFrom: XCTestCase {
24-
func testmonthlyWithWholeResult() throws {
25-
try XCTSkipIf(true && !runAll) // change true to false to run this test
26-
XCTAssertEqual(monthlyRateFrom(hourlyRate: 80, withDiscount: 50), 7040, accuracy: 0.001)
25+
@Test("Montly rate with whole result", .enabled(if: RUNALL))
26+
func testmonthlyWithWholeResult() {
27+
#expect(monthlyRateFrom(hourlyRate: 80, withDiscount: 50).isApproximatelyEqual(to: 7040, relativeTolerance: 0.001))
2728
}
2829

29-
func testmonthlyRoundDown() throws {
30-
try XCTSkipIf(true && !runAll) // change true to false to run this test
31-
XCTAssertEqual(monthlyRateFrom(hourlyRate: 77, withDiscount: 10.5), 12129, accuracy: 0.001)
30+
@Test("Montly rate round down", .enabled(if: RUNALL))
31+
func testmonthlyRoundDown() {
32+
#expect(monthlyRateFrom(hourlyRate: 77, withDiscount: 10.5).isApproximatelyEqual(to: 12129, relativeTolerance: 0.001))
3233
}
3334

34-
func testmonthlyRoundUp() throws {
35-
try XCTSkipIf(true && !runAll) // change true to false to run this test
36-
XCTAssertEqual(monthlyRateFrom(hourlyRate: 80, withDiscount: 10.5), 12602, accuracy: 0.001)
35+
@Test("Montly rate round up", .enabled(if: RUNALL))
36+
func testmonthlyRoundUp() {
37+
#expect(monthlyRateFrom(hourlyRate: 80, withDiscount: 10.5).isApproximatelyEqual(to: 12602, relativeTolerance: 0.001))
3738
}
38-
}
3939

40-
class TaskWorkdaysIn: XCTestCase {
41-
func testworkdaysInSmallBudget() throws {
42-
try XCTSkipIf(true && !runAll) // change true to false to run this test
43-
XCTAssertEqual(
44-
workdaysIn(budget: 1000, hourlyRate: 50, withDiscount: 10), 2.0, accuracy: 0.001)
40+
41+
@Test("Workdays in small budget", .enabled(if: RUNALL))
42+
func testworkdaysInSmallBudget() {
43+
#expect(workdaysIn(budget: 1000, hourlyRate: 50, withDiscount: 10).isApproximatelyEqual(to: 2.0, relativeTolerance: 0.001))
4544
}
4645

47-
func testworkdaysInMediumBudget() throws {
48-
try XCTSkipIf(true && !runAll) // change true to false to run this test
49-
XCTAssertEqual(
50-
workdaysIn(budget: 5000, hourlyRate: 60, withDiscount: 10), 11.0, accuracy: 0.001)
46+
@Test("Workdays in medium budget", .enabled(if: RUNALL))
47+
func testworkdaysInMediumBudget() {
48+
#expect(workdaysIn(budget: 5000, hourlyRate: 60, withDiscount: 10).isApproximatelyEqual(to: 11.0, relativeTolerance: 0.001))
5149
}
5250

53-
func testworkdaysLargebudget() throws {
54-
try XCTSkipIf(true && !runAll) // change true to false to run this test
55-
XCTAssertEqual(
56-
workdaysIn(budget: 25_000, hourlyRate: 80, withDiscount: 10), 43.0, accuracy: 0.001)
51+
@Test("Workdays large budget", .enabled(if: RUNALL))
52+
func testworkdaysLargebudget() {
53+
#expect(workdaysIn(budget: 25_000, hourlyRate: 80, withDiscount: 10).isApproximatelyEqual(to: 43.0, relativeTolerance: 0.001))
5754
}
5855

59-
func testworkdaysShouldRound() throws {
60-
try XCTSkipIf(true && !runAll) // change true to false to run this test
61-
XCTAssertEqual(
62-
workdaysIn(budget: 20000, hourlyRate: 80, withDiscount: 11), 35.0, accuracy: 0.001)
56+
@Test("Workdays should round", .enabled(if: RUNALL))
57+
func testworkdaysShouldRound() {
58+
#expect(workdaysIn(budget: 20000, hourlyRate: 80, withDiscount: 11).isApproximatelyEqual(to: 35.0, relativeTolerance: 0.001))
6359
}
6460

65-
func testworkdaysShouldNotRoundToNearstInteger() throws {
66-
try XCTSkipIf(true && !runAll) // change true to false to run this test
67-
XCTAssertEqual(
68-
workdaysIn(budget: 25_000, hourlyRate: 80, withDiscount: 11), 43.0, accuracy: 0.001)
61+
@Test("Workdays should not round to nearst integer", .enabled(if: RUNALL))
62+
func testworkdaysShouldNotRoundToNearstInteger() {
63+
#expect(workdaysIn(budget: 25_000, hourlyRate: 80, withDiscount: 11).isApproximatelyEqual(to: 43.0, relativeTolerance: 0.001))
6964
}
7065
}

exercises/concept/lasagna/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
".meta/Sources/Lasagna/LasagnaExemplar.swift"
1515
]
1616
},
17-
"language_versions": ">=5.0",
17+
"language_versions": ">=6.0",
1818
"forked_from": [
1919
"fsharp/lucians-luscious-lasagna"
2020
],

exercises/concept/lasagna/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,58 @@
1-
import XCTest
1+
import Testing
2+
import Foundation
23

34
@testable import Lasagna
45

5-
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
6+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
67

7-
class TaskExpectedMinutesInOvenTests: XCTestCase {
8+
@Suite struct LasagnaTests {
9+
@Test("Expected minutes in oven")
810
func testExpectedMinutes() {
9-
XCTAssertEqual(expectedMinutesInOven, 40)
11+
#expect(expectedMinutesInOven == 40)
1012
}
11-
}
1213

13-
class TaskRemainingMinutesInOven: XCTestCase {
14-
func testRemainingMinutes() throws {
15-
try XCTSkipIf(true && !runAll) // change true to false to run this test
16-
XCTAssertEqual(remainingMinutesInOven(elapsedMinutes: 13), 27)
14+
@Test("Remaining minutes in oven", .enabled(if: RUNALL))
15+
func testRemainingMinutes() {
16+
#expect(remainingMinutesInOven(elapsedMinutes: 13) == 27)
1717
}
1818

19-
func testRemainingMinutesWhenDone() throws {
20-
try XCTSkipIf(true && !runAll) // change true to false to run this test
21-
XCTAssertEqual(remainingMinutesInOven(elapsedMinutes: 40), 0)
19+
@Test("Remaining minutes in oven when done", .enabled(if: RUNALL))
20+
func testRemainingMinutesWhenDone() {
21+
#expect(remainingMinutesInOven(elapsedMinutes: 40) == 0)
2222
}
2323

24-
func testRemainingMinutesWhenLessThanOne() throws {
25-
try XCTSkipIf(true && !runAll) // change true to false to run this test
26-
XCTAssertEqual(remainingMinutesInOven(elapsedMinutes: 39), 1)
24+
@Test("Remaining minutes in oven when less than one", .enabled(if: RUNALL))
25+
func testRemainingMinutesWhenLessThanOne() {
26+
#expect(remainingMinutesInOven(elapsedMinutes: 39) == 1)
2727
}
28-
}
2928

30-
class TaskPreparationTimeInMinutes: XCTestCase {
31-
func testPreparationMinutes() throws {
32-
try XCTSkipIf(true && !runAll) // change true to false to run this test
33-
XCTAssertEqual(preparationTimeInMinutes(layers: 6), 12)
29+
@Test("Preparation time in minutes", .enabled(if: RUNALL))
30+
func testPreparationMinutes() {
31+
#expect(preparationTimeInMinutes(layers: 6) == 12)
3432
}
3533

36-
func testPreparationMinutesForOneLayer() throws {
37-
try XCTSkipIf(true && !runAll) // change true to false to run this test
38-
XCTAssertEqual(preparationTimeInMinutes(layers: 1), 2)
34+
@Test("Preparation time in minutes for one layer", .enabled(if: RUNALL))
35+
func testPreparationMinutesForOneLayer() {
36+
#expect(preparationTimeInMinutes(layers: 1) == 2)
3937
}
4038

41-
func testPreparationMinutesForZeroLayers() throws {
42-
try XCTSkipIf(true && !runAll) // change true to false to run this test
43-
XCTAssertEqual(preparationTimeInMinutes(layers: 0), 0)
39+
@Test("Preparation time in minutes for zero layers", .enabled(if: RUNALL))
40+
func testPreparationMinutesForZeroLayers() {
41+
#expect(preparationTimeInMinutes(layers: 0) == 0)
4442
}
45-
}
4643

47-
class TaskTotalTimeInMinutes: XCTestCase {
48-
func testTotalMinutes() throws {
49-
try XCTSkipIf(true && !runAll) // change true to false to run this test
50-
XCTAssertEqual(totalTimeInMinutes(layers: 1, elapsedMinutes: 30), 32)
44+
@Test("Total time in minutes", .enabled(if: RUNALL))
45+
func testTotalMinutes() {
46+
#expect(totalTimeInMinutes(layers: 1, elapsedMinutes: 30) == 32)
5147
}
5248

53-
func testTotalMinutesWhenDone() throws {
54-
try XCTSkipIf(true && !runAll) // change true to false to run this test
55-
XCTAssertEqual(totalTimeInMinutes(layers: 2, elapsedMinutes: 25), 29)
49+
@Test("Total time in minutes when done", .enabled(if: RUNALL))
50+
func testTotalMinutesWhenDone() {
51+
#expect(totalTimeInMinutes(layers: 2, elapsedMinutes: 25) == 29)
5652
}
5753

58-
func testTotalMinutesWhenLessThanOne() throws {
59-
try XCTSkipIf(true && !runAll) // change true to false to run this test
60-
XCTAssertEqual(totalTimeInMinutes(layers: 4, elapsedMinutes: 8), 16)
54+
@Test("Total time in minutes when less than one", .enabled(if: RUNALL))
55+
func testTotalMinutesWhenLessThanOne() {
56+
#expect(totalTimeInMinutes(layers: 4, elapsedMinutes: 8) == 16)
6157
}
6258
}

exercises/concept/wings-quest/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

0 commit comments

Comments
 (0)