Skip to content

[Swift 6]: Update Exercises batch 1 #785

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/acronym/.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("{{case.expected}}", Acronym.abbreviate("{{case.input.phrase}}"))
func test{{case.description |camelCase }}() {
#expect("{{case.expected}}" == Acronym.abbreviate("{{case.input.phrase}}"))
}
{% endfor -%}
}
2 changes: 1 addition & 1 deletion exercises/practice/acronym/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

65 changes: 34 additions & 31 deletions exercises/practice/acronym/Tests/AcronymTests/AcronymTests.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
import XCTest
import Foundation
import Testing

@testable import Acronym

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

@Suite struct AcronymTests {

@Test("basic")
func testBasic() {
XCTAssertEqual("PNG", Acronym.abbreviate("Portable Network Graphics"))
#expect("PNG" == Acronym.abbreviate("Portable Network Graphics"))
}

func testLowercaseWords() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("ROR", Acronym.abbreviate("Ruby on Rails"))
@Test("lowercase words", .enabled(if: RUNALL))
func testLowercaseWords() {
#expect("ROR" == Acronym.abbreviate("Ruby on Rails"))
}

func testPunctuation() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("FIFO", Acronym.abbreviate("First In, First Out"))
@Test("punctuation", .enabled(if: RUNALL))
func testPunctuation() {
#expect("FIFO" == Acronym.abbreviate("First In, First Out"))
}

func testAllCapsWord() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("GIMP", Acronym.abbreviate("GNU Image Manipulation Program"))
@Test("all caps word", .enabled(if: RUNALL))
func testAllCapsWord() {
#expect("GIMP" == Acronym.abbreviate("GNU Image Manipulation Program"))
}

func testPunctuationWithoutWhitespace() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("CMOS", Acronym.abbreviate("Complementary metal-oxide semiconductor"))
@Test("punctuation without whitespace", .enabled(if: RUNALL))
func testPunctuationWithoutWhitespace() {
#expect("CMOS" == Acronym.abbreviate("Complementary metal-oxide semiconductor"))
}

func testVeryLongAbbreviation() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
"ROTFLSHTMDCOALM",
Acronym.abbreviate(
"Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"))
@Test("very long abbreviation", .enabled(if: RUNALL))
func testVeryLongAbbreviation() {
#expect(
"ROTFLSHTMDCOALM"
== Acronym.abbreviate(
"Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"))
}

func testConsecutiveDelimiters() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("SIMUFTA", Acronym.abbreviate("Something - I made up from thin air"))
@Test("consecutive delimiters", .enabled(if: RUNALL))
func testConsecutiveDelimiters() {
#expect("SIMUFTA" == Acronym.abbreviate("Something - I made up from thin air"))
}

func testApostrophes() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("HC", Acronym.abbreviate("Halley's Comet"))
@Test("apostrophes", .enabled(if: RUNALL))
func testApostrophes() {
#expect("HC" == Acronym.abbreviate("Halley's Comet"))
}

func testUnderscoreEmphasis() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual("TRNT", Acronym.abbreviate("The Road _Not_ Taken"))
@Test("underscore emphasis", .enabled(if: RUNALL))
func testUnderscoreEmphasis() {
#expect("TRNT" == Acronym.abbreviate("The Road _Not_ Taken"))
}
}
28 changes: 15 additions & 13 deletions exercises/practice/all-your-base/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
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 Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}})) { error in
{%- if case.input.inputBase < 2 %}
XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase)
#expect(throws: BaseError.invalidInputBase)
{%- elif case.input.outputBase < 2 %}
XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase)
#expect(throws: BaseError.invalidOutputBase)
{%- elif case.input.digits | any:"isNegative" %}
XCTAssertEqual(error as? BaseError, BaseError.negativeDigit)
#expect(throws: BaseError.negativeDigit)
{%- else %}
XCTAssertEqual(error as? BaseError, BaseError.invalidPositiveDigit)
#expect(throws: BaseError.invalidPositiveDigit)
{%- endif -%}
}
{try Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}})}
{%- else -%}
XCTAssertEqual(try! Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}}), {{case.expected}})
#expect(try! Base.outputDigits(inputBase: {{case.input.inputBase}}, inputDigits: {{case.input.digits}}, outputBase: {{case.input.outputBase}}) == {{case.expected}})
{%- endif -%}
}
{% endfor -%}
2 changes: 1 addition & 1 deletion exercises/practice/all-your-base/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

Original file line number Diff line number Diff line change
@@ -1,150 +1,144 @@
import XCTest
import Foundation
import Testing

@testable import AllYourBase

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

@Suite struct AllYourBaseTests {

@Test("single bit one to decimal")
func testSingleBitOneToDecimal() {
XCTAssertEqual(try! Base.outputDigits(inputBase: 2, inputDigits: [1], outputBase: 10), [1])
#expect(try! Base.outputDigits(inputBase: 2, inputDigits: [1], outputBase: 10) == [1])
}

func testBinaryToSingleDecimal() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1], outputBase: 10), [5])
@Test("binary to single decimal", .enabled(if: RUNALL))
func testBinaryToSingleDecimal() {
#expect(try! Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1], outputBase: 10) == [5])
}

func testSingleDecimalToBinary() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 10, inputDigits: [5], outputBase: 2), [1, 0, 1])
@Test("single decimal to binary", .enabled(if: RUNALL))
func testSingleDecimalToBinary() {
#expect(try! Base.outputDigits(inputBase: 10, inputDigits: [5], outputBase: 2) == [1, 0, 1])
}

func testBinaryToMultipleDecimal() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1, 0, 1, 0], outputBase: 10), [4, 2])
@Test("binary to multiple decimal", .enabled(if: RUNALL))
func testBinaryToMultipleDecimal() {
#expect(
try! Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1, 0, 1, 0], outputBase: 10) == [
4, 2,
])
}

func testDecimalToBinary() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 10, inputDigits: [4, 2], outputBase: 2), [1, 0, 1, 0, 1, 0])
@Test("decimal to binary", .enabled(if: RUNALL))
func testDecimalToBinary() {
#expect(
try! Base.outputDigits(inputBase: 10, inputDigits: [4, 2], outputBase: 2) == [
1, 0, 1, 0, 1, 0,
])
}

func testTrinaryToHexadecimal() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 3, inputDigits: [1, 1, 2, 0], outputBase: 16), [2, 10])
@Test("trinary to hexadecimal", .enabled(if: RUNALL))
func testTrinaryToHexadecimal() {
#expect(
try! Base.outputDigits(inputBase: 3, inputDigits: [1, 1, 2, 0], outputBase: 16) == [2, 10])
}

func testHexadecimalToTrinary() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 16, inputDigits: [2, 10], outputBase: 3), [1, 1, 2, 0])
@Test("hexadecimal to trinary", .enabled(if: RUNALL))
func testHexadecimalToTrinary() {
#expect(
try! Base.outputDigits(inputBase: 16, inputDigits: [2, 10], outputBase: 3) == [1, 1, 2, 0])
}

func test15BitInteger() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 97, inputDigits: [3, 46, 60], outputBase: 73), [6, 10, 45])
@Test("15-bit integer", .enabled(if: RUNALL))
func test15BitInteger() {
#expect(
try! Base.outputDigits(inputBase: 97, inputDigits: [3, 46, 60], outputBase: 73) == [
6, 10, 45,
])
}

func testEmptyList() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Base.outputDigits(inputBase: 2, inputDigits: [], outputBase: 10), [0])
@Test("empty list", .enabled(if: RUNALL))
func testEmptyList() {
#expect(try! Base.outputDigits(inputBase: 2, inputDigits: [], outputBase: 10) == [0])
}

func testSingleZero() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(try! Base.outputDigits(inputBase: 10, inputDigits: [0], outputBase: 2), [0])
@Test("single zero", .enabled(if: RUNALL))
func testSingleZero() {
#expect(try! Base.outputDigits(inputBase: 10, inputDigits: [0], outputBase: 2) == [0])
}

func testMultipleZeros() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 10, inputDigits: [0, 0, 0], outputBase: 2), [0])
@Test("multiple zeros", .enabled(if: RUNALL))
func testMultipleZeros() {
#expect(try! Base.outputDigits(inputBase: 10, inputDigits: [0, 0, 0], outputBase: 2) == [0])
}

func testLeadingZeros() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertEqual(
try! Base.outputDigits(inputBase: 7, inputDigits: [0, 6, 0], outputBase: 10), [4, 2])
@Test("leading zeros", .enabled(if: RUNALL))
func testLeadingZeros() {
#expect(
try! Base.outputDigits(inputBase: 7, inputDigits: [0, 6, 0], outputBase: 10) == [4, 2])
}

func testInputBaseIsOne() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Base.outputDigits(inputBase: 1, inputDigits: [0], outputBase: 10)) {
error in
XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase)
@Test("input base is one", .enabled(if: RUNALL))
func testInputBaseIsOne() {
#expect(throws: BaseError.invalidInputBase) {
try Base.outputDigits(inputBase: 1, inputDigits: [0], outputBase: 10)
}
}

func testInputBaseIsZero() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Base.outputDigits(inputBase: 0, inputDigits: [], outputBase: 10)) {
error in
XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase)
@Test("input base is zero", .enabled(if: RUNALL))
func testInputBaseIsZero() {
#expect(throws: BaseError.invalidInputBase) {
try Base.outputDigits(inputBase: 0, inputDigits: [], outputBase: 10)
}
}

func testInputBaseIsNegative() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Base.outputDigits(inputBase: -2, inputDigits: [1], outputBase: 10)) {
error in
XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase)
@Test("input base is negative", .enabled(if: RUNALL))
func testInputBaseIsNegative() {
#expect(throws: BaseError.invalidInputBase) {
try Base.outputDigits(inputBase: -2, inputDigits: [1], outputBase: 10)
}
}

func testNegativeDigit() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(
@Test("negative digit", .enabled(if: RUNALL))
func testNegativeDigit() {
#expect(throws: BaseError.negativeDigit) {
try Base.outputDigits(inputBase: 2, inputDigits: [1, -1, 1, 0, 1, 0], outputBase: 10)
) { error in
XCTAssertEqual(error as? BaseError, BaseError.negativeDigit)
}
}

func testInvalidPositiveDigit() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(
@Test("invalid positive digit", .enabled(if: RUNALL))
func testInvalidPositiveDigit() {
#expect(throws: BaseError.invalidPositiveDigit) {
try Base.outputDigits(inputBase: 2, inputDigits: [1, 2, 1, 0, 1, 0], outputBase: 10)
) { error in
XCTAssertEqual(error as? BaseError, BaseError.invalidPositiveDigit)
}
}

func testOutputBaseIsOne() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(
@Test("output base is one", .enabled(if: RUNALL))
func testOutputBaseIsOne() {
#expect(throws: BaseError.invalidOutputBase) {
try Base.outputDigits(inputBase: 2, inputDigits: [1, 0, 1, 0, 1, 0], outputBase: 1)
) { error in
XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase)
}
}

func testOutputBaseIsZero() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Base.outputDigits(inputBase: 10, inputDigits: [7], outputBase: 0)) {
error in
XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase)
@Test("output base is zero", .enabled(if: RUNALL))
func testOutputBaseIsZero() {
#expect(throws: BaseError.invalidOutputBase) {
try Base.outputDigits(inputBase: 10, inputDigits: [7], outputBase: 0)
}
}

func testOutputBaseIsNegative() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Base.outputDigits(inputBase: 2, inputDigits: [1], outputBase: -7)) {
error in
XCTAssertEqual(error as? BaseError, BaseError.invalidOutputBase)
@Test("output base is negative", .enabled(if: RUNALL))
func testOutputBaseIsNegative() {
#expect(throws: BaseError.invalidOutputBase) {
try Base.outputDigits(inputBase: 2, inputDigits: [1], outputBase: -7)
}
}

func testBothBasesAreNegative() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
XCTAssertThrowsError(try Base.outputDigits(inputBase: -2, inputDigits: [1], outputBase: -7)) {
error in
XCTAssertEqual(error as? BaseError, BaseError.invalidInputBase)
@Test("both bases are negative", .enabled(if: RUNALL))
func testBothBasesAreNegative() {
#expect(throws: BaseError.invalidInputBase) {
try Base.outputDigits(inputBase: -2, inputDigits: [1], outputBase: -7)
}
}
}