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 5 #789

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
16 changes: 9 additions & 7 deletions exercises/practice/diamond/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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 -%}
XCTAssertEqual(Diamond.makeDiamond(letter: "{{case.input.letter}}"), {{case.expected | toStringArray}})
func test{{case.description |camelCase }}() {
#expect(Diamond.makeDiamond(letter: "{{case.input.letter}}") == {{case.expected | toStringArray}})
}
{% endfor -%}
}
2 changes: 1 addition & 1 deletion exercises/practice/diamond/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
43 changes: 23 additions & 20 deletions exercises/practice/diamond/Tests/DiamondTests/DiamondTests.swift
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import XCTest
import Foundation
import Testing

@testable import Diamond

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

@Suite struct DiamondTests {

@Test("Degenerate case with a single 'A' row")
func testDegenerateCaseWithASingleARow() {
XCTAssertEqual(Diamond.makeDiamond(letter: "A"), ["A"])
#expect(Diamond.makeDiamond(letter: "A") == ["A"])
}

func testDegenerateCaseWithNoRowContaining3DistinctGroupsOfSpaces() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(Diamond.makeDiamond(letter: "B"), [" A ", "B B", " A "])
@Test("Degenerate case with no row containing 3 distinct groups of spaces", .enabled(if: RUNALL))
func testDegenerateCaseWithNoRowContaining3DistinctGroupsOfSpaces() {
#expect(Diamond.makeDiamond(letter: "B") == [" A ", "B B", " A "])
}

func testSmallestNonDegenerateCaseWithOddDiamondSideLength() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(Diamond.makeDiamond(letter: "C"), [" A ", " B B ", "C C", " B B ", " A "])
@Test("Smallest non-degenerate case with odd diamond side length", .enabled(if: RUNALL))
func testSmallestNonDegenerateCaseWithOddDiamondSideLength() {
#expect(Diamond.makeDiamond(letter: "C") == [" A ", " B B ", "C C", " B B ", " A "])
}

func testSmallestNonDegenerateCaseWithEvenDiamondSideLength() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
Diamond.makeDiamond(letter: "D"),
[" A ", " B B ", " C C ", "D D", " C C ", " B B ", " A "])
@Test("Smallest non-degenerate case with even diamond side length", .enabled(if: RUNALL))
func testSmallestNonDegenerateCaseWithEvenDiamondSideLength() {
#expect(
Diamond.makeDiamond(letter: "D") == [
" A ", " B B ", " C C ", "D D", " C C ", " B B ", " A ",
])
}

func testLargestPossibleDiamond() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
Diamond.makeDiamond(letter: "Z"),
[
@Test("Largest possible diamond", .enabled(if: RUNALL))
func testLargestPossibleDiamond() {
#expect(
Diamond.makeDiamond(letter: "Z") == [
" A ",
" B B ",
" C C ",
Expand Down
16 changes: 9 additions & 7 deletions exercises/practice/difference-of-squares/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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 %}
{%- 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 %}
XCTAssertEqual({{subCases.expected}}, Squares({{subCases.input.number}}).{{subCases.property}})
func test{{subCases.description |camelCase }}() {
#expect({{subCases.expected}} == Squares({{subCases.input.number}}).{{subCases.property}})
}
{% endfor -%}
{% endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/difference-of-squares/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,51 +1,54 @@
import XCTest
import Foundation
import Testing

@testable import DifferenceOfSquares

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

@Suite struct DifferenceOfSquaresTests {

@Test("square of sum 1")
func testSquareOfSum1() {
XCTAssertEqual(1, Squares(1).squareOfSum)
#expect(1 == Squares(1).squareOfSum)
}

func testSquareOfSum5() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(225, Squares(5).squareOfSum)
@Test("square of sum 5", .enabled(if: RUNALL))
func testSquareOfSum5() {
#expect(225 == Squares(5).squareOfSum)
}

func testSquareOfSum100() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(25_502_500, Squares(100).squareOfSum)
@Test("square of sum 100", .enabled(if: RUNALL))
func testSquareOfSum100() {
#expect(25_502_500 == Squares(100).squareOfSum)
}

func testSumOfSquares1() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(1, Squares(1).sumOfSquares)
@Test("sum of squares 1", .enabled(if: RUNALL))
func testSumOfSquares1() {
#expect(1 == Squares(1).sumOfSquares)
}

func testSumOfSquares5() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(55, Squares(5).sumOfSquares)
@Test("sum of squares 5", .enabled(if: RUNALL))
func testSumOfSquares5() {
#expect(55 == Squares(5).sumOfSquares)
}

func testSumOfSquares100() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(338350, Squares(100).sumOfSquares)
@Test("sum of squares 100", .enabled(if: RUNALL))
func testSumOfSquares100() {
#expect(338350 == Squares(100).sumOfSquares)
}

func testDifferenceOfSquares1() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(0, Squares(1).differenceOfSquares)
@Test("difference of squares 1", .enabled(if: RUNALL))
func testDifferenceOfSquares1() {
#expect(0 == Squares(1).differenceOfSquares)
}

func testDifferenceOfSquares5() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(170, Squares(5).differenceOfSquares)
@Test("difference of squares 5", .enabled(if: RUNALL))
func testDifferenceOfSquares5() {
#expect(170 == Squares(5).differenceOfSquares)
}

func testDifferenceOfSquares100() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(25_164_150, Squares(100).differenceOfSquares)
@Test("difference of squares 100", .enabled(if: RUNALL))
func testDifferenceOfSquares100() {
#expect(25_164_150 == Squares(100).differenceOfSquares)
}
}
18 changes: 10 additions & 8 deletions exercises/practice/dominoes/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
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.input.dominoes -%}
let input = [(Int, Int)]()
{% else -%}
let input = {{case.input.dominoes | toTupleArray}}
{% endif -%}

{% if case.expected -%}
XCTAssertTrue(Dominoes(input).chained)
#expect(Dominoes(input).chained)
{% else -%}
XCTAssertFalse(Dominoes(input).chained)
#expect(!Dominoes(input).chained)
{% endif -%}
}
{% endfor -%}
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/dominoes/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
83 changes: 43 additions & 40 deletions exercises/practice/dominoes/Tests/DominoesTests/DominoesTests.swift
Original file line number Diff line number Diff line change
@@ -1,84 +1,87 @@
import XCTest
import Foundation
import Testing

@testable import Dominoes

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

@Suite struct DominoesTests {

@Test("empty input = empty output")
func testEmptyInputEmptyOutput() {
let input = [(Int, Int)]()
XCTAssertTrue(Dominoes(input).chained)
#expect(Dominoes(input).chained)
}

func testSingletonInputSingletonOutput() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("singleton input = singleton output", .enabled(if: RUNALL))
func testSingletonInputSingletonOutput() {
let input = [(1, 1)]
XCTAssertTrue(Dominoes(input).chained)
#expect(Dominoes(input).chained)
}

func testSingletonThatCantBeChained() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("singleton that can't be chained", .enabled(if: RUNALL))
func testSingletonThatCantBeChained() {
let input = [(1, 2)]
XCTAssertFalse(Dominoes(input).chained)
#expect(!Dominoes(input).chained)
}

func testThreeElements() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("three elements", .enabled(if: RUNALL))
func testThreeElements() {
let input = [(1, 2), (3, 1), (2, 3)]
XCTAssertTrue(Dominoes(input).chained)
#expect(Dominoes(input).chained)
}

func testCanReverseDominoes() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("can reverse dominoes", .enabled(if: RUNALL))
func testCanReverseDominoes() {
let input = [(1, 2), (1, 3), (2, 3)]
XCTAssertTrue(Dominoes(input).chained)
#expect(Dominoes(input).chained)
}

func testCantBeChained() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("can't be chained", .enabled(if: RUNALL))
func testCantBeChained() {
let input = [(1, 2), (4, 1), (2, 3)]
XCTAssertFalse(Dominoes(input).chained)
#expect(!Dominoes(input).chained)
}

func testDisconnectedSimple() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("disconnected - simple", .enabled(if: RUNALL))
func testDisconnectedSimple() {
let input = [(1, 1), (2, 2)]
XCTAssertFalse(Dominoes(input).chained)
#expect(!Dominoes(input).chained)
}

func testDisconnectedDoubleLoop() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("disconnected - double loop", .enabled(if: RUNALL))
func testDisconnectedDoubleLoop() {
let input = [(1, 2), (2, 1), (3, 4), (4, 3)]
XCTAssertFalse(Dominoes(input).chained)
#expect(!Dominoes(input).chained)
}

func testDisconnectedSingleIsolated() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("disconnected - single isolated", .enabled(if: RUNALL))
func testDisconnectedSingleIsolated() {
let input = [(1, 2), (2, 3), (3, 1), (4, 4)]
XCTAssertFalse(Dominoes(input).chained)
#expect(!Dominoes(input).chained)
}

func testNeedBacktrack() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("need backtrack", .enabled(if: RUNALL))
func testNeedBacktrack() {
let input = [(1, 2), (2, 3), (3, 1), (2, 4), (2, 4)]
XCTAssertTrue(Dominoes(input).chained)
#expect(Dominoes(input).chained)
}

func testSeparateLoops() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("separate loops", .enabled(if: RUNALL))
func testSeparateLoops() {
let input = [(1, 2), (2, 3), (3, 1), (1, 1), (2, 2), (3, 3)]
XCTAssertTrue(Dominoes(input).chained)
#expect(Dominoes(input).chained)
}

func testNineElements() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("nine elements", .enabled(if: RUNALL))
func testNineElements() {
let input = [(1, 2), (5, 3), (3, 1), (1, 2), (2, 4), (1, 6), (2, 3), (3, 4), (5, 6)]
XCTAssertTrue(Dominoes(input).chained)
#expect(Dominoes(input).chained)
}

func testSeparateThreeDominoLoops() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("separate three-domino loops", .enabled(if: RUNALL))
func testSeparateThreeDominoLoops() {
let input = [(1, 2), (2, 3), (3, 1), (4, 5), (5, 6), (6, 4)]
XCTAssertFalse(Dominoes(input).chained)
#expect(!Dominoes(input).chained)
}
}
Loading
Loading