Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift 6]: Update Exercises batch 7 #791

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions exercises/practice/grains/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import XCTest
import Testing
import Foundation
@testable import {{exercise|camelCase}}
class {{exercise|camelCase}}Tests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct {{exercise|camelCase}}Tests {
{% outer: for case in cases %}
{% ifnot case.expected %}
{%- for subCases in case.cases %}
{%- if forloop.outer.first and forloop.first %}
func test{{subCases.description |camelCase }}() {
@Test("{{subCases.description}}")
{%- else %}
func test{{subCases.description |camelCase }}() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("{{subCases.description}}", .enabled(if: RUNALL))
{%- endif %}
func test{{subCases.description |camelCase }}() {
{%- ifnot subCases.expected.error %}
XCTAssertEqual(try! Grains.square({{subCases.input.square}}), {{subCases.expected}})
#expect(try! Grains.square({{subCases.input.square}}) == {{subCases.expected}})
{%- else %}
XCTAssertThrowsError(try Grains.square({{subCases.input.square}})) { error in
#expect(throws:
{%- if subCases.input.square < 1 %}
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooLow)
GrainsError.inputTooLow
{%- elif subCases.input.square > 64 %}
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooHigh)
GrainsError.inputTooHigh
{%- endif %}
}
){try Grains.square({{subCases.input.square}})}
{%- endif -%}

}
{% endfor -%}
{%- else %}
func test{{subCases.description |camelCase }}{{ forloop.outer.counter }}() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Grains.total, {{case.expected}})
@Test("{{subCases.description}}", .enabled(if: RUNALL))
func test{{subCases.description |camelCase }}{{ forloop.outer.counter }}() {
#expect(try! Grains.total == {{case.expected}})
}
{%- endif %}
{% endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/grains/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand Down
86 changes: 46 additions & 40 deletions exercises/practice/grains/Tests/GrainsTests/GrainsTests.swift
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
import XCTest
import Foundation
import Testing

@testable import Grains

class GrainsTests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "true"]) ?? false

@Suite struct GrainsTests {

@Test("grains on square 1")
func testGrainsOnSquare1() {
XCTAssertEqual(try! Grains.square(1), 1)
#expect(try! Grains.square(1) == 1)
}

func testGrainsOnSquare2() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Grains.square(2), 2)
@Test("grains on square 2", .enabled(if: RUNALL))
func testGrainsOnSquare2() {
#expect(try! Grains.square(2) == 2)
}

func testGrainsOnSquare3() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Grains.square(3), 4)
@Test("grains on square 3", .enabled(if: RUNALL))
func testGrainsOnSquare3() {
#expect(try! Grains.square(3) == 4)
}

func testGrainsOnSquare4() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Grains.square(4), 8)
@Test("grains on square 4", .enabled(if: RUNALL))
func testGrainsOnSquare4() {
#expect(try! Grains.square(4) == 8)
}

func testGrainsOnSquare16() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Grains.square(16), 32768)
@Test("grains on square 16", .enabled(if: RUNALL))
func testGrainsOnSquare16() {
#expect(try! Grains.square(16) == 32768)
}

func testGrainsOnSquare32() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Grains.square(32), 2_147_483_648)
@Test("grains on square 32", .enabled(if: RUNALL))
func testGrainsOnSquare32() {
#expect(try! Grains.square(32) == 2_147_483_648)
}

func testGrainsOnSquare64() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Grains.square(64), 9_223_372_036_854_775_808)
@Test("grains on square 64", .enabled(if: RUNALL))
func testGrainsOnSquare64() {
#expect(try! Grains.square(64) == 9_223_372_036_854_775_808)
}

func testSquare0IsInvalid() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Grains.square(0)) { error in
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooLow)
}
@Test("square 0 is invalid", .enabled(if: RUNALL))
func testSquare0IsInvalid() {
#expect(
throws:
GrainsError.inputTooLow
) { try Grains.square(0) }
}

func testNegativeSquareIsInvalid() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Grains.square(-1)) { error in
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooLow)
}
@Test("negative square is invalid", .enabled(if: RUNALL))
func testNegativeSquareIsInvalid() {
#expect(
throws:
GrainsError.inputTooLow
) { try Grains.square(-1) }
}

func testSquareGreaterThan64IsInvalid() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Grains.square(65)) { error in
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooHigh)
}
@Test("square greater than 64 is invalid", .enabled(if: RUNALL))
func testSquareGreaterThan64IsInvalid() {
#expect(
throws:
GrainsError.inputTooHigh
) { try Grains.square(65) }
}

func test2() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Grains.total, 18_446_744_073_709_551_615)
@Test("", .enabled(if: RUNALL))
func test2() {
#expect(try! Grains.total == 18_446_744_073_709_551_615)
}
}
11 changes: 0 additions & 11 deletions exercises/practice/hamming/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

Calculate the Hamming distance between two DNA strands.

Your body is made up of cells that contain DNA.
Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells.
In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!

When cells divide, their DNA replicates too.
Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information.
If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred.
This is known as the "Hamming distance".

We read DNA using the letters C, A, G and T.
Two strands might look like this:

Expand All @@ -20,8 +11,6 @@ Two strands might look like this:

They have 7 differences, and therefore the Hamming distance is 7.

The Hamming distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)

## Implementation notes

The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work.
12 changes: 12 additions & 0 deletions exercises/practice/hamming/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Introduction

Your body is made up of cells that contain DNA.
Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells.
In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!

When cells divide, their DNA replicates too.
Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information.
If we compare two strands of DNA and count the differences between them, we can see how many mistakes occurred.
This is known as the "Hamming distance".

The Hamming distance is useful in many areas of science, not just biology, so it's a nice phrase to be familiar with :)
2 changes: 1 addition & 1 deletion exercises/practice/hamming/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
".meta/Sources/Hamming/HammingExample.swift"
]
},
"blurb": "Calculate the Hamming difference between two DNA strands.",
"blurb": "Calculate the Hamming distance between two DNA strands.",
"source": "The Calculating Point Mutations problem at Rosalind",
"source_url": "https://rosalind.info/problems/hamm/"
}
18 changes: 10 additions & 8 deletions exercises/practice/hamming/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import XCTest
import Testing
import Foundation
@testable import {{exercise|camelCase}}
class {{exercise|camelCase}}Tests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct {{exercise|camelCase}}Tests {
{% for case in cases %}
{% if forloop.first -%}
func test{{case.description |camelCase }}() {
@Test("{{case.description}}")
{% else -%}
func test{{case.description |camelCase }}() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("{{case.description}}", .enabled(if: RUNALL))
{% endif -%}
func test{{case.description |camelCase }}() {
{%- ifnot case.expected.error -%}
let result = try! Hamming.compute("{{case.input.strand1}}", against:"{{case.input.strand2}}")!
let expected = {{case.expected}}
XCTAssertEqual(expected, result)
#expect(expected == result)
{%- else -%}
XCTAssertThrowsError(try Hamming.compute("{{case.input.strand1}}", against:"{{case.input.strand2}}"))
#expect(throws: (any Error).self) {try Hamming.compute("{{case.input.strand1}}", against:"{{case.input.strand2}}")}
{%- endif -%}
}
{% endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/hamming/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand Down
59 changes: 31 additions & 28 deletions exercises/practice/hamming/Tests/HammingTests/HammingTests.swift
Original file line number Diff line number Diff line change
@@ -1,61 +1,64 @@
import XCTest
import Foundation
import Testing

@testable import Hamming

class HammingTests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct HammingTests {

@Test("empty strands")
func testEmptyStrands() {
let result = try! Hamming.compute("", against: "")!
let expected = 0
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testSingleLetterIdenticalStrands() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("single letter identical strands", .enabled(if: RUNALL))
func testSingleLetterIdenticalStrands() {
let result = try! Hamming.compute("A", against: "A")!
let expected = 0
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testSingleLetterDifferentStrands() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("single letter different strands", .enabled(if: RUNALL))
func testSingleLetterDifferentStrands() {
let result = try! Hamming.compute("G", against: "T")!
let expected = 1
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testLongIdenticalStrands() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("long identical strands", .enabled(if: RUNALL))
func testLongIdenticalStrands() {
let result = try! Hamming.compute("GGACTGAAATCTG", against: "GGACTGAAATCTG")!
let expected = 0
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testLongDifferentStrands() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("long different strands", .enabled(if: RUNALL))
func testLongDifferentStrands() {
let result = try! Hamming.compute("GGACGGATTCTG", against: "AGGACGGATTCT")!
let expected = 9
XCTAssertEqual(expected, result)
#expect(expected == result)
}

func testDisallowFirstStrandLonger() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Hamming.compute("AATG", against: "AAA"))
@Test("disallow first strand longer", .enabled(if: RUNALL))
func testDisallowFirstStrandLonger() {
#expect(throws: (any Error).self) { try Hamming.compute("AATG", against: "AAA") }
}

func testDisallowSecondStrandLonger() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Hamming.compute("ATA", against: "AGTG"))
@Test("disallow second strand longer", .enabled(if: RUNALL))
func testDisallowSecondStrandLonger() {
#expect(throws: (any Error).self) { try Hamming.compute("ATA", against: "AGTG") }
}

func testDisallowEmptyFirstStrand() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Hamming.compute("", against: "G"))
@Test("disallow empty first strand", .enabled(if: RUNALL))
func testDisallowEmptyFirstStrand() {
#expect(throws: (any Error).self) { try Hamming.compute("", against: "G") }
}

func testDisallowEmptySecondStrand() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Hamming.compute("G", against: ""))
@Test("disallow empty second strand", .enabled(if: RUNALL))
func testDisallowEmptySecondStrand() {
#expect(throws: (any Error).self) { try Hamming.compute("G", against: "") }
}
}
5 changes: 0 additions & 5 deletions exercises/practice/hello-world/.docs/instructions.append.md

This file was deleted.

16 changes: 0 additions & 16 deletions exercises/practice/hello-world/.meta/description.md

This file was deleted.

2 changes: 1 addition & 1 deletion exercises/practice/hello-world/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:6.0

import PackageDescription

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import XCTest
import Testing

@testable import HelloWorld

class HelloWorldTests: XCTestCase {
func testHello() {
XCTAssertEqual(hello(), "Hello, World!")
}
@Test("Hello, World!")
func testHello() {
#expect(hello() == "Hello, World!")
}
Loading
Loading