Skip to content

Rewrote tests to follow canonical data; reworked example to pass tests (fixes #232) #299

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

Merged
merged 1 commit into from
Oct 11, 2017
Merged
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
43 changes: 17 additions & 26 deletions exercises/luhn/Sources/LuhnExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@ import Foundation
struct Luhn {

var number: Int64 = 0
var addends: [Int] { return addendsFunc(number) }
var numberString: String = ""
var addends: [Int] { return addendsFunc(numberString) }
var checksum: Int { return addends.reduce(0, +) }
var isValid: Bool { return checksum % 10 == 0 }

init(_ num: Int64) {
self.number = num
}

static func create (_ num: Int64) -> Double {

func createCheckDigit(_ value: Int) -> Int {
let nearestTen = Int(ceil((Double(value) / 10.00)) * 10)
return nearestTen - value
var isValid: Bool = false

init(_ num: String ) {
self.numberString = num.replacingOccurrences(of: " ", with: "")
let num = Int64(numberString)
if self.numberString.utf16.count <= 1 {
self.isValid = false
} else if num == nil {
isValid = false
} else {
self.number = num!
isValid = checksum % 10 == 0
}

let zeroCheckDigitNumber = num * 10
let luhn = Luhn(zeroCheckDigitNumber)

if luhn.isValid {
return Double(zeroCheckDigitNumber)}

return Double((zeroCheckDigitNumber) + Int64(createCheckDigit(luhn.checksum)))

}

func addendsFunc(_ num: Int64) -> [Int] {
func addendsFunc(_ num: String) -> [Int] {
func oddIndexInt64Minus9( _ input: [Int]) -> [Int] {
var input = input
input = Array(input.reversed())
Expand All @@ -37,7 +30,7 @@ struct Luhn {
var tempEach: Int = each
if (inx+1) % 2 == 0 {
tempEach *= 2
if tempEach > 10 {
if tempEach >= 10 {
tempEach -= 9
}
tempArray.insert(tempEach, at: 0)
Expand All @@ -52,9 +45,7 @@ struct Luhn {
return tempInt
}

let tempString = "\(num)"

return oddIndexInt64Minus9(Array(tempString.characters).map { char2Int($0) })
return oddIndexInt64Minus9(Array(num.characters).map { char2Int($0) })
}

}
2 changes: 1 addition & 1 deletion exercises/luhn/Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import XCTest
@testable import LuhnTests

XCTMain([
testCase(LuhnTests.allTests),
testCase(LuhnTests.allTests)
])
94 changes: 59 additions & 35 deletions exercises/luhn/Tests/LuhnTests/LuhnTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,86 @@ import XCTest

class LuhnTests: XCTestCase {

func testAddends() {
let luhn = Luhn(12_121)
XCTAssertEqual([1, 4, 1, 4, 1], luhn.addends)
func testSingleDigitInvalid() {
let luhn = Luhn("1")
XCTAssertEqual (false, luhn.isValid)
}

func testTooLargeAddend() {
let luhn = Luhn(8631)
XCTAssertEqual([7, 6, 6, 1], luhn.addends)
func testSingleZeroInvalid() {
let luhn = Luhn("0")
XCTAssertEqual (false, luhn.isValid)
}

func testChecksum() {
let luhn = Luhn(4913)
XCTAssertEqual(22, luhn.checksum)
func testSimpleReversableValid() {
let luhn = Luhn("059")
XCTAssertEqual (true, luhn.isValid)
}

func testChecksumAgain() {
let luhn = Luhn(201_773)
XCTAssertEqual(21, luhn.checksum)
func testSimpleIrreversableValid() {
let luhn = Luhn("59")
XCTAssertEqual (true, luhn.isValid)
}

func testInvalidNumber() {
let luhn = Luhn(738)
func testCanadianValid() {
let luhn = Luhn("055 444 285")
XCTAssertEqual (true, luhn.isValid)
}

func testCanadianInvalid() {
let luhn = Luhn("055 444 286")
XCTAssertEqual (false, luhn.isValid)
}

func testValidNumber() {
let luhn = Luhn(8_739_567)
XCTAssertEqual (true, luhn.isValid)
func testCreditCardInvalid() {
let luhn = Luhn("8273 1232 7352 0569")
XCTAssertEqual (false, luhn.isValid)
}

func testCreateValidNumber() {
let number = Luhn.create(123)
XCTAssertEqual(1230, number)
func testNonDigitInvalid() {
let luhn = Luhn("055a 444 285")
XCTAssertEqual (false, luhn.isValid)
}

func testCreateOtherValidNumber() {
let number = Luhn.create(873_956)
XCTAssertEqual(8_739_567, number)
func testPunctuationInvalid() {
let luhn = Luhn("055-444-285")
XCTAssertEqual (false, luhn.isValid)
}

func testCreateYetAnotherValidNumber() {
let number = Luhn.create(837_263_756)
XCTAssertEqual(8_372_637_564, number)
func testSymbolsInvalid() {
let luhn = Luhn("055£ 444$ 285")
XCTAssertEqual (false, luhn.isValid)
}

func testZeroWithSpaceInvalid() {
let luhn = Luhn(" 0")
XCTAssertEqual (false, luhn.isValid)
}

func testMultipleZeroesValid() {
let luhn = Luhn("0000 0")
XCTAssertEqual (true, luhn.isValid)
}

func testInputNineOutputNine() {
let luhn = Luhn("091")
XCTAssertEqual (true, luhn.isValid)
}

static var allTests: [(String, (LuhnTests) -> () throws -> Void)] {
return [
("testAddends", testAddends),
("testTooLargeAddend", testTooLargeAddend),
("testChecksum", testChecksum),
("testChecksumAgain", testChecksumAgain),
("testInvalidNumber", testInvalidNumber),
("testValidNumber", testValidNumber),
("testCreateValidNumber", testCreateValidNumber),
("testCreateOtherValidNumber", testCreateOtherValidNumber),
("testCreateYetAnotherValidNumber", testCreateYetAnotherValidNumber),
("testSingleDigitInvalid", testSingleDigitInvalid),
("testSingleZeroInvalid", testSingleZeroInvalid),
("testSimpleReversableValid", testSimpleReversableValid),
("testSimpleIrreversableValid", testSimpleIrreversableValid),
("testCanadianValid", testCanadianValid),
("testCanadianInvalid", testCanadianInvalid),
("testCreditCardInvalid", testCreditCardInvalid),
("testNonDigitInvalid", testNonDigitInvalid),
("testPunctuationInvalid", testPunctuationInvalid),
("testSymbolsInvalid", testSymbolsInvalid),
("testZeroWithSpaceInvalid", testZeroWithSpaceInvalid),
("testMultipleZeroesValid", testMultipleZeroesValid),
("testInputNineOutputNine", testInputNineOutputNine)
]
}
}