From 830927ecbb2ff44d5b6d3746a148786df8214b7c Mon Sep 17 00:00:00 2001 From: Ben Rimmington Date: Thu, 16 Jan 2020 12:19:33 +0000 Subject: [PATCH 1/5] [BigIntTests] Rewrite testMinMaxDescriptions() --- Tests/BigIntTests/BigIntTests.swift | 88 ++++++++++++++++------------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/Tests/BigIntTests/BigIntTests.swift b/Tests/BigIntTests/BigIntTests.swift index 29fe141..866d183 100644 --- a/Tests/BigIntTests/BigIntTests.swift +++ b/Tests/BigIntTests/BigIntTests.swift @@ -125,44 +125,56 @@ final class BigIntTests: XCTestCase { XCTAssertEqual(actualRange, expectedRange) } - func testMinMaxDescriptions() { - let keyValuePairs: KeyValuePairs = [ - BigInt(UInt64.min): [ - "-2", - "-1", - "0", - "1", - "2" - ], - BigInt(UInt64.max): [ - "18446744073709551613", - "18446744073709551614", - "18446744073709551615", - "18446744073709551616", - "18446744073709551617", - ], - BigInt(Int64.min): [ - "-9223372036854775810", - "-9223372036854775809", - "-9223372036854775808", - "-9223372036854775807", - "-9223372036854775806", - ], - BigInt(Int64.max): [ - "9223372036854775805", - "9223372036854775806", - "9223372036854775807", - "9223372036854775808", - "9223372036854775809", - ], - ] - for (expectedNumber, expectedStrings) in keyValuePairs { - let expectedNumbers: [BigInt] = (-2 ... 2).map({ expectedNumber + $0 }) - let actualNumbers: [BigInt] = expectedStrings.compactMap({ BigInt($0) }) - let actualStrings: [String] = actualNumbers.map({ $0.description }) - XCTAssertEqual(actualNumbers, expectedNumbers) - XCTAssertEqual(actualStrings, expectedStrings) - } + func testCustomStringConvertible() { + XCTAssertEqual("\(BigInt(UInt64.min) - 2)", "-2") + XCTAssertEqual("\(BigInt(UInt64.min) - 1)", "-1") + XCTAssertEqual("\(BigInt(UInt64.min) + 0)", "0") + XCTAssertEqual("\(BigInt(UInt64.min) + 1)", "1") + XCTAssertEqual("\(BigInt(UInt64.min) + 2)", "2") + + XCTAssertEqual("\(BigInt(UInt64.max) - 2)", "18446744073709551613") + XCTAssertEqual("\(BigInt(UInt64.max) - 1)", "18446744073709551614") + XCTAssertEqual("\(BigInt(UInt64.max) + 0)", "18446744073709551615") + XCTAssertEqual("\(BigInt(UInt64.max) + 1)", "18446744073709551616") + XCTAssertEqual("\(BigInt(UInt64.max) + 2)", "18446744073709551617") + + XCTAssertEqual("\(BigInt(Int64.min) - 2)", "-9223372036854775810") + XCTAssertEqual("\(BigInt(Int64.min) - 1)", "-9223372036854775809") + XCTAssertEqual("\(BigInt(Int64.min) + 0)", "-9223372036854775808") + XCTAssertEqual("\(BigInt(Int64.min) + 1)", "-9223372036854775807") + XCTAssertEqual("\(BigInt(Int64.min) + 2)", "-9223372036854775806") + + XCTAssertEqual("\(BigInt(Int64.max) - 2)", "9223372036854775805") + XCTAssertEqual("\(BigInt(Int64.max) - 1)", "9223372036854775806") + XCTAssertEqual("\(BigInt(Int64.max) + 0)", "9223372036854775807") + XCTAssertEqual("\(BigInt(Int64.max) + 1)", "9223372036854775808") + XCTAssertEqual("\(BigInt(Int64.max) + 2)", "9223372036854775809") + } + + func testLosslessStringConvertible() { + XCTAssertEqual(BigInt(UInt64.min) - 2, BigInt("-2")) + XCTAssertEqual(BigInt(UInt64.min) - 1, BigInt("-1")) + XCTAssertEqual(BigInt(UInt64.min) + 0, BigInt("0")) + XCTAssertEqual(BigInt(UInt64.min) + 1, BigInt("1")) + XCTAssertEqual(BigInt(UInt64.min) + 2, BigInt("2")) + + XCTAssertEqual(BigInt(UInt64.max) - 2, BigInt("18446744073709551613")) + XCTAssertEqual(BigInt(UInt64.max) - 1, BigInt("18446744073709551614")) + XCTAssertEqual(BigInt(UInt64.max) + 0, BigInt("18446744073709551615")) + XCTAssertEqual(BigInt(UInt64.max) + 1, BigInt("18446744073709551616")) + XCTAssertEqual(BigInt(UInt64.max) + 2, BigInt("18446744073709551617")) + + XCTAssertEqual(BigInt(Int64.min) - 2, BigInt("-9223372036854775810")) + XCTAssertEqual(BigInt(Int64.min) - 1, BigInt("-9223372036854775809")) + XCTAssertEqual(BigInt(Int64.min) + 0, BigInt("-9223372036854775808")) + XCTAssertEqual(BigInt(Int64.min) + 1, BigInt("-9223372036854775807")) + XCTAssertEqual(BigInt(Int64.min) + 2, BigInt("-9223372036854775806")) + + XCTAssertEqual(BigInt(Int64.max) - 2, BigInt("9223372036854775805")) + XCTAssertEqual(BigInt(Int64.max) - 1, BigInt("9223372036854775806")) + XCTAssertEqual(BigInt(Int64.max) + 0, BigInt("9223372036854775807")) + XCTAssertEqual(BigInt(Int64.max) + 1, BigInt("9223372036854775808")) + XCTAssertEqual(BigInt(Int64.max) + 2, BigInt("9223372036854775809")) } func testRandomDescriptions() { From fe212356fdc6dbde14d207c28d1fb6900c845a3a Mon Sep 17 00:00:00 2001 From: Ben Rimmington Date: Thu, 16 Jan 2020 12:40:02 +0000 Subject: [PATCH 2/5] [BigIntTests] Rename testRandomDescriptions() --- Tests/BigIntTests/BigIntTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/BigIntTests/BigIntTests.swift b/Tests/BigIntTests/BigIntTests.swift index 866d183..c6cc08d 100644 --- a/Tests/BigIntTests/BigIntTests.swift +++ b/Tests/BigIntTests/BigIntTests.swift @@ -177,7 +177,7 @@ final class BigIntTests: XCTestCase { XCTAssertEqual(BigInt(Int64.max) + 2, BigInt("9223372036854775809")) } - func testRandomDescriptions() { + func testRadicesAndNumerals() { for _ in 0 ..< 100 { let expectedNumber = BigInt(Int.random(in: .min ... .max)) for radix in 2 ... 36 { From 7ae61f775bcc6eb215ade6af3eb3a26ed0269b96 Mon Sep 17 00:00:00 2001 From: Ben Rimmington Date: Thu, 16 Jan 2020 13:04:39 +0000 Subject: [PATCH 3/5] [BigIntTests] Update testLosslessStringConvertible --- Tests/BigIntTests/BigIntTests.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Tests/BigIntTests/BigIntTests.swift b/Tests/BigIntTests/BigIntTests.swift index c6cc08d..686fe88 100644 --- a/Tests/BigIntTests/BigIntTests.swift +++ b/Tests/BigIntTests/BigIntTests.swift @@ -152,6 +152,13 @@ final class BigIntTests: XCTestCase { } func testLosslessStringConvertible() { + XCTAssertNil(BigInt("")) + XCTAssertNil(BigInt("-")) + XCTAssertNil(BigInt("+")) + XCTAssertNil(BigInt("A")) + XCTAssertNil(BigInt(" 0")) + XCTAssertNil(BigInt("0 ")) + XCTAssertEqual(BigInt(UInt64.min) - 2, BigInt("-2")) XCTAssertEqual(BigInt(UInt64.min) - 1, BigInt("-1")) XCTAssertEqual(BigInt(UInt64.min) + 0, BigInt("0")) From 2410717a17ed50ee0872a069bd64938eff2b0727 Mon Sep 17 00:00:00 2001 From: Ben Rimmington Date: Thu, 16 Jan 2020 13:12:29 +0000 Subject: [PATCH 4/5] [BigInt] Update the SignedNumeric prefix operator --- Sources/BigInt/BigInt.swift | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Sources/BigInt/BigInt.swift b/Sources/BigInt/BigInt.swift index b484ba0..32d9c4a 100644 --- a/Sources/BigInt/BigInt.swift +++ b/Sources/BigInt/BigInt.swift @@ -336,18 +336,9 @@ extension BigInt: SignedNumeric { @inlinable public static prefix func - (x: BigInt) -> BigInt { - var newWords = x.words.map { ~$0 } - let carry: UInt = 1 - for i in 0 ..< newWords.count { - let isOverflow: Bool - (newWords[i], isOverflow) = newWords[i].addingReportingOverflow(carry) - if !isOverflow { - break - } - } - - BigInt._dropExcessWords(words: &newWords) - return BigInt(words: Words(newWords)) + var result = x + result.negate() + return result } } From a6ecb7442a9d20cf48b2d2379acabb775cc110a7 Mon Sep 17 00:00:00 2001 From: Ben Rimmington Date: Thu, 16 Jan 2020 14:50:19 +0000 Subject: [PATCH 5/5] [BigIntTests] Update testRadicesAndNumerals() --- Tests/BigIntTests/BigIntTests.swift | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Tests/BigIntTests/BigIntTests.swift b/Tests/BigIntTests/BigIntTests.swift index 686fe88..62e341d 100644 --- a/Tests/BigIntTests/BigIntTests.swift +++ b/Tests/BigIntTests/BigIntTests.swift @@ -185,26 +185,25 @@ final class BigIntTests: XCTestCase { } func testRadicesAndNumerals() { - for _ in 0 ..< 100 { - let expectedNumber = BigInt(Int.random(in: .min ... .max)) - for radix in 2 ... 36 { - for uppercase in [false, true] { + for radix in 2 ... 36 { + for uppercase in [false, true] { + for _ in 0 ..< 100 { + let expectedNumber = BigInt(Int.random(in: .min ... .max)) let expectedString = String(expectedNumber, radix: radix, uppercase: uppercase) let actualNumber = BigInt(expectedString, radix: radix) XCTAssertNotNil(actualNumber) - if let actualNumber = actualNumber { - XCTAssertEqual(actualNumber, expectedNumber) - if radix == 10 { - XCTAssertEqual(actualNumber.description, expectedString) - } + XCTAssertEqual(actualNumber, expectedNumber) + if radix == 10 { + let actualString = expectedNumber.description + XCTAssertEqual(actualString, expectedString) } } } } } - + func testDivision() { let foo = BigInt("12345678901234567890123456789012345678901234567890123456789012345678901234567890")! let bar = BigInt("351235231535161613134135135135")!