-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Armstrong number functions to support Int128 and update test…
… cases
- Loading branch information
1 parent
98b42de
commit 5f659a4
Showing
6 changed files
with
79 additions
and
69 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...s/practice/armstrong-numbers/.meta/Sources/ArmstrongNumbers/ArmstrongNumbersExample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 19 additions & 17 deletions
36
exercises/practice/armstrong-numbers/.meta/template.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
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 | ||
|
||
{% for case in cases %} | ||
{% if forloop.first -%} | ||
func test{{case.description |camelCase }}() { | ||
{% else -%} | ||
func test{{case.description |camelCase }}() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
{% endif -%} | ||
{%- if case.expected -%} | ||
XCTAssertTrue(isArmstrongNumber({{case.input.number}})) | ||
{%- else -%} | ||
XCTAssertFalse(isArmstrongNumber({{case.input.number}})) | ||
{%- endif %} | ||
} | ||
{% endfor -%} | ||
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
{% for case in cases %} | ||
{% if forloop.first -%} | ||
@Test("{{case.description}}") | ||
func test{{case.description |camelCase }}() { | ||
{% else -%} | ||
@Test("{{case.description}}", .enabled(if: RUNALL)) | ||
func test{{case.description |camelCase }}() throws { | ||
{% endif -%} | ||
{%- if case.expected -%} | ||
#expect(isArmstrongNumber({{case.input.number}})) | ||
{%- else -%} | ||
#expect(isArmstrongNumber({{case.input.number}})) | ||
{%- endif %} | ||
} | ||
{% endfor -%} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
exercises/practice/armstrong-numbers/Sources/ArmstrongNumbers/ArmstrongNumbers.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
func isArmstrongNumber(_ number: Int) -> Bool { | ||
func isArmstrongNumber(_ number: Int128) -> Bool { | ||
// Write your code for the 'Armstrong Numbers' exercise here. | ||
} |
104 changes: 57 additions & 47 deletions
104
exercises/practice/armstrong-numbers/Tests/ArmstrongNumbersTests/ArmstrongNumbersTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,61 @@ | ||
import XCTest | ||
import Testing | ||
import Foundation | ||
|
||
@testable import ArmstrongNumbers | ||
|
||
class ArmstrongNumbersTests: XCTestCase { | ||
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
func testZeroIsAnArmstrongNumber() { | ||
XCTAssertTrue(isArmstrongNumber(0)) | ||
} | ||
|
||
func testSingleDigitNumbersAreArmstrongNumbers() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertTrue(isArmstrongNumber(5)) | ||
} | ||
|
||
func testThereAreNoTwoDigitArmstrongNumbers() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertFalse(isArmstrongNumber(10)) | ||
} | ||
|
||
func testThreeDigitNumberThatIsAnArmstrongNumber() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertTrue(isArmstrongNumber(153)) | ||
} | ||
|
||
func testThreeDigitNumberThatIsNotAnArmstrongNumber() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertFalse(isArmstrongNumber(100)) | ||
} | ||
|
||
func testFourDigitNumberThatIsAnArmstrongNumber() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertTrue(isArmstrongNumber(9474)) | ||
} | ||
|
||
func testFourDigitNumberThatIsNotAnArmstrongNumber() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertFalse(isArmstrongNumber(9475)) | ||
} | ||
|
||
func testSevenDigitNumberThatIsAnArmstrongNumber() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertTrue(isArmstrongNumber(9_926_315)) | ||
} | ||
|
||
func testSevenDigitNumberThatIsNotAnArmstrongNumber() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertFalse(isArmstrongNumber(9_926_314)) | ||
} | ||
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
@Test("Zero is an Armstrong number") | ||
func testZeroIsAnArmstrongNumber() { | ||
#expect(isArmstrongNumber(0)) | ||
} | ||
|
||
@Test("Single-digit numbers are Armstrong numbers", .enabled(if: RUNALL)) | ||
func testSingleDigitNumbersAreArmstrongNumbers() throws { | ||
#expect(isArmstrongNumber(5)) | ||
} | ||
|
||
@Test("There are no two-digit Armstrong numbers", .enabled(if: RUNALL)) | ||
func testThereAreNoTwoDigitArmstrongNumbers() throws { | ||
#expect(isArmstrongNumber(10)) | ||
} | ||
|
||
@Test("Three-digit number that is an Armstrong number", .enabled(if: RUNALL)) | ||
func testThreeDigitNumberThatIsAnArmstrongNumber() throws { | ||
#expect(isArmstrongNumber(153)) | ||
} | ||
|
||
@Test("Three-digit number that is not an Armstrong number", .enabled(if: RUNALL)) | ||
func testThreeDigitNumberThatIsNotAnArmstrongNumber() throws { | ||
#expect(isArmstrongNumber(100)) | ||
} | ||
|
||
@Test("Four-digit number that is an Armstrong number", .enabled(if: RUNALL)) | ||
func testFourDigitNumberThatIsAnArmstrongNumber() throws { | ||
#expect(isArmstrongNumber(9474)) | ||
} | ||
|
||
@Test("Four-digit number that is not an Armstrong number", .enabled(if: RUNALL)) | ||
func testFourDigitNumberThatIsNotAnArmstrongNumber() throws { | ||
#expect(isArmstrongNumber(9475)) | ||
} | ||
|
||
@Test("Seven-digit number that is an Armstrong number", .enabled(if: RUNALL)) | ||
func testSevenDigitNumberThatIsAnArmstrongNumber() throws { | ||
#expect(isArmstrongNumber(9_926_315)) | ||
} | ||
|
||
@Test("Seven-digit number that is not an Armstrong number", .enabled(if: RUNALL)) | ||
func testSevenDigitNumberThatIsNotAnArmstrongNumber() throws { | ||
#expect(isArmstrongNumber(9_926_314)) | ||
} | ||
|
||
@Test("Armstrong number containing seven zeroes", .enabled(if: RUNALL)) | ||
func testArmstrongNumberContainingSevenZeroes() throws { | ||
#expect(isArmstrongNumber(186_709_961_001_538_790_100_634_132_976_990)) | ||
} | ||
|
||
@Test("The largest and last Armstrong number", .enabled(if: RUNALL)) | ||
func testTheLargestAndLastArmstrongNumber() throws { | ||
#expect(isArmstrongNumber(115_132_219_018_763_992_565_095_597_973_971_522_401)) | ||
} |