Created append ascii digit extension of Int

This commit is contained in:
Ben Davis
2016-03-12 17:04:09 +00:00
parent 33f95e0d8b
commit a081bca3b6
2 changed files with 22 additions and 0 deletions
+9
View File
@@ -82,6 +82,15 @@ extension Int {
}
extension Int {
func appendAsciiDigit(asciiDigit: Byte) throws -> Int {
let digit = Int(try asciiDigit.fromAsciiValue())
return self*10 + digit
}
}
extension Character {
func asciiValue() throws -> NSData {
@@ -73,6 +73,19 @@ class BitsAndByteTests: XCTestCase {
XCTAssertEqual(string, "\(integer)")
}
func testAppendAsciiDigit() {
doTestAppendAsciiDigit(123, digit: 4, expectedResult: 1234)
doTestAppendAsciiDigit(1, digit: 0, expectedResult: 10)
doTestAppendAsciiDigit(567, digit: 0, expectedResult: 5670)
doTestAppendAsciiDigit(0, digit: 4, expectedResult: 4)
doTestAppendAsciiDigit(0, digit: 0, expectedResult: 0)
}
func doTestAppendAsciiDigit(integer: Int, digit: UInt8, expectedResult: Int) {
let result = try! integer.appendAsciiDigit(try! digit.asciiValue())
XCTAssertEqual(result, expectedResult, "\(integer)\(digit) != \(result)")
}
func testAsciiEncodeString() {
let string = "abc"
let data = try! string.asciiValue()