Skip to content

Commit

Permalink
Fix test cases for Swift (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
twodayslate authored Aug 27, 2020
1 parent 8c97ae8 commit 16858b0
Show file tree
Hide file tree
Showing 22 changed files with 180 additions and 215 deletions.
36 changes: 0 additions & 36 deletions BigNumber-iOSTests/BigNumber_iOSTests.swift

This file was deleted.

22 changes: 0 additions & 22 deletions BigNumber-iOSTests/Info.plist

This file was deleted.

4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ let package = Package(
name: "BigNumber",
dependencies: [],
path: "Sources"),
.testTarget(
name: "BigNumberTests",
dependencies: ["BigNumber"],
path: "Tests"),
]
)
6 changes: 6 additions & 0 deletions Sources/Swift-Big-Number-Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,12 @@ public struct BDouble:
} else {
safeAfterExp = safeAfterExp - beforeExp.count + 1
}
// if safeAfterExp is negative this results in a crash
// more testing and test cases needed
if safeAfterExp < 0 {
return nil
}

let den = ["1"] + [Character](repeating: "0", count: safeAfterExp)
self.init(beforeExp, over: String(den))
return
Expand Down
14 changes: 0 additions & 14 deletions Swift-BigNumber.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/* Begin PBXBuildFile section */
AE8388AF203F1CB000FAC88F /* BigNumber.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AE8388A6203F1CAF00FAC88F /* BigNumber.framework */; };
AE8388B4203F1CB000FAC88F /* BigNumber_iOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE8388B3203F1CB000FAC88F /* BigNumber_iOSTests.swift */; };
AE8388BD203F1CC800FAC88F /* Swift-Big-Number-Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2F92B421D0F7452003CDF7D /* Swift-Big-Number-Core.swift */; };
B7C37FE624EEE47E005B1987 /* MG Matrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2F92B4C1D0F7468003CDF7D /* MG Matrix.swift */; };
C26B2726207E5C13008FA649 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2F92B3B1D0F7421003CDF7D /* main.swift */; };
Expand Down Expand Up @@ -80,8 +79,6 @@
/* Begin PBXFileReference section */
AE8388A6203F1CAF00FAC88F /* BigNumber.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BigNumber.framework; sourceTree = BUILT_PRODUCTS_DIR; };
AE8388AE203F1CB000FAC88F /* BigNumber-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "BigNumber-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
AE8388B3203F1CB000FAC88F /* BigNumber_iOSTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BigNumber_iOSTests.swift; sourceTree = "<group>"; };
AE8388B5203F1CB000FAC88F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C224D2D41DCFBAFE006CD7C5 /* MG Complex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "MG Complex.swift"; sourceTree = "<group>"; };
C24E721A1E58D69F008AD14D /* MG SuperSwift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "MG SuperSwift.swift"; sourceTree = "<group>"; };
C26B274E207E701B008FA649 /* BigNumber_iOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BigNumber_iOS.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -155,15 +152,6 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
AE8388B2203F1CB000FAC88F /* BigNumber-iOSTests */ = {
isa = PBXGroup;
children = (
AE8388B3203F1CB000FAC88F /* BigNumber_iOSTests.swift */,
AE8388B5203F1CB000FAC88F /* Info.plist */,
);
path = "BigNumber-iOSTests";
sourceTree = "<group>";
};
C26B274D207E7004008FA649 /* BigNumber-iOS */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -239,7 +227,6 @@
C29F6AE41FEB1BD200AD0478 /* Tests */,
C28F71EE1DF0557800CE18C6 /* Info.plist */,
C26B274D207E7004008FA649 /* BigNumber-iOS */,
AE8388B2203F1CB000FAC88F /* BigNumber-iOSTests */,
C2A4928A1DF05294001992B1 /* Products */,
C26B2785207E8369008FA649 /* Resources */,
F64629231FF015E40074A180 /* MathGround */,
Expand Down Expand Up @@ -475,7 +462,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AE8388B4203F1CB000FAC88F /* BigNumber_iOSTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 2 additions & 0 deletions Tests/BDoubleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class BDoubleTests : XCTestCase {
}
}

#if !SWIFT_PACKAGE
func test_decimalExpansionRandom()
{
func generateDoubleString(preDecimalCount: Int, postDecimalCount: Int) -> String
Expand Down Expand Up @@ -230,6 +231,7 @@ class BDoubleTests : XCTestCase {

}
}
#endif

func testRounding() {
XCTAssertEqual(BDouble("-1.0")?.rounded(), BInt("-1"))
Expand Down
5 changes: 4 additions & 1 deletion Tests/BIntTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
@testable import BigNumber

class BIntTests: XCTestCase {

Expand All @@ -28,7 +29,8 @@ class BIntTests: XCTestCase {
"y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
]


#if !SWIFT_PACKAGE
// Randomly choose two bases and a number length, as well as a sign (+ or -)
for _ in 0..<100
{
Expand Down Expand Up @@ -57,6 +59,7 @@ class BIntTests: XCTestCase {
XCTAssert(b1 == b2)
XCTAssert(s2 == num)
}
#endif

let bigHex = "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef00"
let x = BInt(bigHex, radix: 16)!
Expand Down
3 changes: 2 additions & 1 deletion Tests/MG Math Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import Foundation

#if !SWIFT_PACKAGE
public class MG_Math_Tests
{
static func test_primesTo()
Expand Down Expand Up @@ -35,3 +35,4 @@ public class MG_Math_Tests
// }
}
}
#endif
4 changes: 2 additions & 2 deletions Tests/MG Matrix Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import Foundation

#if !SWIFT_PACKAGE
public class MG_Matrix_Tests
{
static func testSparseMatrix()
Expand Down Expand Up @@ -40,4 +40,4 @@ public class MG_Matrix_Tests
}
}
}

#endif
1 change: 1 addition & 0 deletions Tests/Test_Advanced_Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
@testable import BigNumber

class Test_Advanced_Math: XCTestCase {

Expand Down
18 changes: 14 additions & 4 deletions Tests/Test_BIntMath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
@testable import BigNumber

class Test_BIntMath: XCTestCase {

Expand All @@ -20,16 +21,25 @@ class Test_BIntMath: XCTestCase {

func test_gcd()
{
#if !SWIFT_PACKAGE
for (a, b) in (0...50)><(0...50)
{
let steinGCD = BIntMath.gcd(BInt(a), BInt(b))
let euclidGCD = BIntMath.gcdEuclid(BInt(a), BInt(b))
XCTAssert(steinGCD == euclidGCD, "SteinGcd does not work")
}

let g1 = Storage.readResource("gcdTest1", inBundle: Bundle(for: type(of: self)))
let g2 = Storage.readResource("gcdTest2", inBundle: Bundle(for: type(of: self)))
XCTAssert(BIntMath.gcd(BInt(g1)!, BInt(g2)!) == 66)
let g1 = Storage.readResource("gcdTest1", inBundle: Bundle(for: type(of: self)))
let g2 = Storage.readResource("gcdTest2", inBundle: Bundle(for: type(of: self)))
XCTAssert(BIntMath.gcd(BInt(g1)!, BInt(g2)!) == 66)
#endif

for a in 0..<50 {
for b in 0..<50 {
let steinGCD = BIntMath.gcd(BInt(a), BInt(b))
let euclidGCD = BIntMath.gcdEuclid(BInt(a), BInt(b))
XCTAssertEqual(steinGCD, euclidGCD)
}
}
}

}
12 changes: 10 additions & 2 deletions Tests/Test_Basic_Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
@testable import BigNumber

class Test_Basic_Math: XCTestCase {

Expand All @@ -29,6 +30,7 @@ class Test_Basic_Math: XCTestCase {
let arithmetricInt: [(Int, Int ) -> Int ] = [(+), (-), (*), (/), (%)]
let arithmetricBInt: [(BInt, BInt) -> BInt] = [(+), (-), (*), (/), (%)]

#if !SWIFT_PACKAGE
for (_, i_op) in (0..<iterations)><(0..<arithmetricInt.count)
{
let (x, y) = (math.random((-m)...m), math.random((-m)...m))
Expand All @@ -37,6 +39,7 @@ class Test_Basic_Math: XCTestCase {
let resBInt = (arithmetricBInt[i_op])(BInt(x), BInt(y))
XCTAssert(resInt.description == resBInt.description)
}
#endif
}

func test_Comparison_Operators_BInt()
Expand All @@ -50,13 +53,15 @@ class Test_Basic_Math: XCTestCase {
let compareInt: [(Int, Int) -> Bool] = [(<), (<=), (>), (>=), (==), (!=)]
let compareBInt: [(BInt, BInt) -> Bool] = [(<), (<=), (>), (>=), (==), (!=)]

#if !SWIFT_PACKAGE
for (_, i_op) in (0..<iterations)><(0..<compareInt.count)
{
let (x, y) = (math.random((-m)...m), math.random((-m)...m))
let resInt = (compareInt[i_op])(x, y)
let resBInt = (compareBInt[i_op])(BInt(x), BInt(y))
XCTAssert(resInt == resBInt)
}
#endif
}

func test_Shift_Operators_BInt()
Expand All @@ -67,13 +72,15 @@ class Test_Basic_Math: XCTestCase {
let shiftInt: [(UInt64, UInt64) -> UInt64] = [(<<), (>>)]
let shiftBInt: [(BInt, Int) -> BInt ] = [(<<), (>>)]

#if !SWIFT_PACKAGE
for (_, i_op) in (0..<iterations)><(0..<shiftInt.count)
{
let (x, y) = (math.random(0...58), math.random(0...58))
let resInt = (shiftInt[i_op])(UInt64(x), UInt64(y))
let resBInt = (shiftBInt[i_op])(BInt(x), y)
XCTAssert(resInt == resBInt)
}
#endif
}

func test_Negation_BInt()
Expand Down Expand Up @@ -161,6 +168,7 @@ class Test_Basic_Math: XCTestCase {

func test_Arithmetric_Operators_BDouble()
{
#if !SWIFT_PACKAGE
for _ in 0..<1000
{
let a = math.random(-10...10)
Expand Down Expand Up @@ -236,14 +244,14 @@ class Test_Basic_Math: XCTestCase {
XCTAssert(BInt(sign: a1.sign, limbs: a1.numerator).description == String(a1Int))
}
}
#endif
}

func test_Power() {
// Reference issue #41
let TWO : BInt = 2
let TWO_SIX_THREE : Int = 263
var TWO_POW_263 : BInt = TWO ** TWO_SIX_THREE
let TWO_POW_263 : BInt = TWO ** TWO_SIX_THREE
XCTAssertEqual(TWO_POW_263, BInt("14821387422376473014217086081112052205218558037201992197050570753012880593911808"))
}

}
1 change: 1 addition & 0 deletions Tests/Test_Initialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
@testable import BigNumber

class Test_Initialization: XCTestCase {

Expand Down
1 change: 1 addition & 0 deletions Tests/Test_String_Conversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
@testable import BigNumber

class Test_String_Conversions: XCTestCase {

Expand Down
Loading

0 comments on commit 16858b0

Please sign in to comment.