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 11 #795

Draft
wants to merge 3 commits 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
20 changes: 11 additions & 9 deletions exercises/practice/nucleotide-count/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
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 }}() {
{% if case.expected.error -%}
XCTAssertThrowsError(try DNA(strand: "{{case.input.strand}}")) { error in
XCTAssertEqual(error as? NucleotideCountErrors, NucleotideCountErrors.invalidNucleotide)
#expect(throws: NucleotideCountErrors.invalidNucleotide) {
try DNA(strand: "{{case.input.strand}}")
}
{% else -%}
let dna = try! DNA(strand: "{{case.input.strand}}")
let results = dna.counts()
let expected = {{case.expected | toStringDictionary}}
XCTAssertEqual(results, expected)
#expect(results == expected)
{% endif -%}
}
{% endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/nucleotide-count/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,46 +1,49 @@
import XCTest
import Foundation
import Testing

@testable import NucleotideCount

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

@Suite struct NucleotideCountTests {

@Test("empty strand")
func testEmptyStrand() {
let dna = try! DNA(strand: "")
let results = dna.counts()
let expected = ["A": 0, "C": 0, "G": 0, "T": 0]
XCTAssertEqual(results, expected)
#expect(results == expected)
}

func testCanCountOneNucleotideInSingleCharacterInput() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("can count one nucleotide in single-character input", .enabled(if: RUNALL))
func testCanCountOneNucleotideInSingleCharacterInput() {
let dna = try! DNA(strand: "G")
let results = dna.counts()
let expected = ["A": 0, "C": 0, "G": 1, "T": 0]
XCTAssertEqual(results, expected)
#expect(results == expected)
}

func testStrandWithRepeatedNucleotide() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("strand with repeated nucleotide", .enabled(if: RUNALL))
func testStrandWithRepeatedNucleotide() {
let dna = try! DNA(strand: "GGGGGGG")
let results = dna.counts()
let expected = ["A": 0, "C": 0, "G": 7, "T": 0]
XCTAssertEqual(results, expected)
#expect(results == expected)
}

func testStrandWithMultipleNucleotides() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("strand with multiple nucleotides", .enabled(if: RUNALL))
func testStrandWithMultipleNucleotides() {
let dna = try! DNA(
strand: "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC")
let results = dna.counts()
let expected = ["A": 20, "C": 12, "G": 17, "T": 21]
XCTAssertEqual(results, expected)
#expect(results == expected)
}

func testStrandWithInvalidNucleotides() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try DNA(strand: "AGXXACT")) { error in
XCTAssertEqual(error as? NucleotideCountErrors, NucleotideCountErrors.invalidNucleotide)
@Test("strand with invalid nucleotides", .enabled(if: RUNALL))
func testStrandWithInvalidNucleotides() {
#expect(throws: NucleotideCountErrors.invalidNucleotide) {
try DNA(strand: "AGXXACT")
}
}
}
20 changes: 11 additions & 9 deletions exercises/practice/ocr-numbers/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
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 }}() {
let input = {{case.input.rows | toStringArray}}
{% if case.expected.error -%}
XCTAssertThrowsError(try OcrNumber.convert(rows: input)) {
XCTAssertEqual($0 as? OcrNumberError, .invalidInput)
#expect(throws: OcrNumberError.invalidInput) {
try OcrNumber.convert(rows: input)
}
{% else -%}
XCTAssertEqual(try! OcrNumber.convert(rows: input), "{{case.expected}}")
#expect(try! OcrNumber.convert(rows: input) == "{{case.expected}}")
{% endif -%}
}
{% endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/ocr-numbers/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,124 +1,133 @@
import XCTest
import Foundation
import Testing

@testable import OcrNumbers

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

@Suite struct OcrNumbersTests {

@Test("Recognizes 0")
func testRecognizes0() {
let input = [" _ ", "| |", "|_|", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "0")
#expect(try! OcrNumber.convert(rows: input) == "0")
}

func testRecognizes1() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 1", .enabled(if: RUNALL))
func testRecognizes1() {
let input = [" ", " |", " |", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "1")
#expect(try! OcrNumber.convert(rows: input) == "1")
}

func testUnreadableButCorrectlySizedInputsReturn() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Unreadable but correctly sized inputs return ?", .enabled(if: RUNALL))
func testUnreadableButCorrectlySizedInputsReturn() {
let input = [" ", " _", " |", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "?")
#expect(try! OcrNumber.convert(rows: input) == "?")
}

func testInputWithANumberOfLinesThatIsNotAMultipleOfFourRaisesAnError() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test(
"Input with a number of lines that is not a multiple of four raises an error",
.enabled(if: RUNALL))
func testInputWithANumberOfLinesThatIsNotAMultipleOfFourRaisesAnError() {
let input = [" _ ", "| |", " "]
XCTAssertThrowsError(try OcrNumber.convert(rows: input)) {
XCTAssertEqual($0 as? OcrNumberError, .invalidInput)
#expect(throws: OcrNumberError.invalidInput) {
try OcrNumber.convert(rows: input)
}
}

func testInputWithANumberOfColumnsThatIsNotAMultipleOfThreeRaisesAnError() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test(
"Input with a number of columns that is not a multiple of three raises an error",
.enabled(if: RUNALL))
func testInputWithANumberOfColumnsThatIsNotAMultipleOfThreeRaisesAnError() {
let input = [" ", " |", " |", " "]
XCTAssertThrowsError(try OcrNumber.convert(rows: input)) {
XCTAssertEqual($0 as? OcrNumberError, .invalidInput)
#expect(throws: OcrNumberError.invalidInput) {
try OcrNumber.convert(rows: input)
}
}

func testRecognizes110101100() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 110101100", .enabled(if: RUNALL))
func testRecognizes110101100() {
let input = [
" _ _ _ _ ", " | || | || | | || || |", " | ||_| ||_| | ||_||_|",
" ",
]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "110101100")
#expect(try! OcrNumber.convert(rows: input) == "110101100")
}

func testGarbledNumbersInAStringAreReplacedWith() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Garbled numbers in a string are replaced with ?", .enabled(if: RUNALL))
func testGarbledNumbersInAStringAreReplacedWith() {
let input = [
" _ _ _ ", " | || | || | || || |", " | | _| ||_| | ||_||_|",
" ",
]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "11?10?1?0")
#expect(try! OcrNumber.convert(rows: input) == "11?10?1?0")
}

func testRecognizes2() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 2", .enabled(if: RUNALL))
func testRecognizes2() {
let input = [" _ ", " _|", "|_ ", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "2")
#expect(try! OcrNumber.convert(rows: input) == "2")
}

func testRecognizes3() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 3", .enabled(if: RUNALL))
func testRecognizes3() {
let input = [" _ ", " _|", " _|", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "3")
#expect(try! OcrNumber.convert(rows: input) == "3")
}

func testRecognizes4() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 4", .enabled(if: RUNALL))
func testRecognizes4() {
let input = [" ", "|_|", " |", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "4")
#expect(try! OcrNumber.convert(rows: input) == "4")
}

func testRecognizes5() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 5", .enabled(if: RUNALL))
func testRecognizes5() {
let input = [" _ ", "|_ ", " _|", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "5")
#expect(try! OcrNumber.convert(rows: input) == "5")
}

func testRecognizes6() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 6", .enabled(if: RUNALL))
func testRecognizes6() {
let input = [" _ ", "|_ ", "|_|", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "6")
#expect(try! OcrNumber.convert(rows: input) == "6")
}

func testRecognizes7() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 7", .enabled(if: RUNALL))
func testRecognizes7() {
let input = [" _ ", " |", " |", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "7")
#expect(try! OcrNumber.convert(rows: input) == "7")
}

func testRecognizes8() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 8", .enabled(if: RUNALL))
func testRecognizes8() {
let input = [" _ ", "|_|", "|_|", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "8")
#expect(try! OcrNumber.convert(rows: input) == "8")
}

func testRecognizes9() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes 9", .enabled(if: RUNALL))
func testRecognizes9() {
let input = [" _ ", "|_|", " _|", " "]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "9")
#expect(try! OcrNumber.convert(rows: input) == "9")
}

func testRecognizesStringOfDecimalNumbers() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("Recognizes string of decimal numbers", .enabled(if: RUNALL))
func testRecognizesStringOfDecimalNumbers() {
let input = [
" _ _ _ _ _ _ _ _ ", " | _| _||_||_ |_ ||_||_|| |",
" ||_ _| | _||_| ||_| _||_|", " ",
]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "1234567890")
#expect(try! OcrNumber.convert(rows: input) == "1234567890")
}

func testNumbersSeparatedByEmptyLinesAreRecognizedLinesAreJoinedByCommas() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test(
"Numbers separated by empty lines are recognized. Lines are joined by commas.",
.enabled(if: RUNALL))
func testNumbersSeparatedByEmptyLinesAreRecognizedLinesAreJoinedByCommas() {
let input = [
" _ _ ", " | _| _|", " ||_ _|", " ", " _ _ ", "|_||_ |_ ", " | _||_|",
" ", " _ _ _ ", " ||_||_|", " ||_| _|", " ",
]
XCTAssertEqual(try! OcrNumber.convert(rows: input), "123,456,789")
#expect(try! OcrNumber.convert(rows: input) == "123,456,789")
}
}
Loading