Skip to content

Commit

Permalink
BInt and BDouble are now Codable (#50)
Browse files Browse the repository at this point in the history
* BInt and BDouble are now Codable
* fix crash with negative exp
  • Loading branch information
twodayslate authored Aug 31, 2020
1 parent 16858b0 commit 9454066
Show file tree
Hide file tree
Showing 26 changed files with 355 additions and 304 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0
4.2
2 changes: 1 addition & 1 deletion BigNumber.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "BigNumber"
s.version = "2.1.0"
s.version = "2.1.1"
s.summary = "A lightweight, high performance bignum library for Swift!"

# This description is used to generate tags and improve search results.
Expand Down
8 changes: 5 additions & 3 deletions Sources/Swift-Big-Number-Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ infix operator ** : ExponentiationPrecedence
public struct BInt:
SignedNumeric, // Implies Numeric, Equatable, ExpressibleByIntegerLiteral
BinaryInteger, // Implies Hashable, CustomStringConvertible, Strideable, Comparable
ExpressibleByFloatLiteral
ExpressibleByFloatLiteral,
Codable
{
//
//
Expand Down Expand Up @@ -2191,7 +2192,8 @@ public struct BDouble:
CustomStringConvertible,
SignedNumeric,
Comparable,
Hashable
Hashable,
Codable
{
//
//
Expand Down Expand Up @@ -2310,7 +2312,7 @@ public struct BDouble:
// if safeAfterExp is negative this results in a crash
// more testing and test cases needed
if safeAfterExp < 0 {
return nil
safeAfterExp = abs(safeAfterExp)
}

let den = ["1"] + [Character](repeating: "0", count: safeAfterExp)
Expand Down
21 changes: 21 additions & 0 deletions Tests/BDoubleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BDoubleTests : XCTestCase {
XCTAssertEqual(BDouble("-1.2e10")?.fractionDescription, "-12000000000")
XCTAssertEqual(BDouble(123000000000000000000.0), 123000000000000000000.0)
XCTAssertEqual(BDouble("1.2")?.fractionDescription, "6/5")
XCTAssertEqual(BDouble("5.7156430570677954e-05"), 5.7156430570677954e-05)

for _ in 0..<100 {
let rn = Double(Double(arc4random()) / Double(UINT32_MAX))
Expand Down Expand Up @@ -464,4 +465,24 @@ class BDoubleTests : XCTestCase {
}
}
}

func testCodable() {
for i in 0..<50 {
let one = BDouble(i)

let json = try! JSONEncoder().encode(one)

let my_one = try! JSONDecoder().decode(BDouble.self, from: json)

XCTAssertEqual(one, my_one)

let rand = BDouble(String(arc4random()), radix: 10)

let rand_json = try! JSONEncoder().encode(rand)

let my_rand = try! JSONDecoder().decode(BDouble.self, from: rand_json)

XCTAssertEqual(rand, my_rand)
}
}
}
20 changes: 20 additions & 0 deletions Tests/Test_Initialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ class Test_Initialization: XCTestCase {
let b = BInt(1) + BInt(limbs: [UInt64.max, UInt64.max, UInt64.max, UInt64.max])
XCTAssert(b.rawValue.limbs == [0, 0, 0, 0, 1])
}

func testCodable() {
for i in 0..<50 {
let one = BInt(i)

let json = try! JSONEncoder().encode(one)

let my_one = try! JSONDecoder().decode(BInt.self, from: json)

XCTAssertEqual(one, my_one)

let rand = BInt(String(arc4random()), radix: 10)

let rand_json = try! JSONEncoder().encode(rand)

let my_rand = try! JSONDecoder().decode(BInt.self, from: rand_json)

XCTAssertEqual(rand, my_rand)
}
}

func testPerformanceExample() {
// This is an example of a performance test case.
Expand Down
2 changes: 1 addition & 1 deletion docs/Extensions.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="index.html">
BigNumber 2.1.0 Docs
BigNumber 2.1.1 Docs
</a>

</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/Extensions/Double.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
BigNumber 2.1.0 Docs
BigNumber 2.1.1 Docs
</a>

</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/Extensions/Int.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="../index.html">
BigNumber 2.1.0 Docs
BigNumber 2.1.1 Docs
</a>

</p>
Expand Down
18 changes: 9 additions & 9 deletions docs/Functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="index.html">
BigNumber 2.1.0 Docs
BigNumber 2.1.1 Docs
</a>

</p>
Expand Down Expand Up @@ -191,7 +191,7 @@ <h4>Parameters</h4>
</table>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3021-L3028">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3023-L3030">Show on GitHub</a>
</div>
</section>
</div>
Expand Down Expand Up @@ -221,7 +221,7 @@ <h4>Declaration</h4>
</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3033-L3060">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3035-L3062">Show on GitHub</a>
</div>
</section>
</div>
Expand Down Expand Up @@ -251,7 +251,7 @@ <h4>Declaration</h4>
</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3065-L3095">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3067-L3097">Show on GitHub</a>
</div>
</section>
</div>
Expand Down Expand Up @@ -286,7 +286,7 @@ <h4>Declaration</h4>
</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3101-L3103">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3103-L3105">Show on GitHub</a>
</div>
</section>
</div>
Expand Down Expand Up @@ -321,7 +321,7 @@ <h4>Declaration</h4>
</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3109-L3111">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3111-L3113">Show on GitHub</a>
</div>
</section>
</div>
Expand Down Expand Up @@ -355,7 +355,7 @@ <h4>Declaration</h4>
</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3116-L3118">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3118-L3120">Show on GitHub</a>
</div>
</section>
</div>
Expand Down Expand Up @@ -385,7 +385,7 @@ <h4>Declaration</h4>
</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3123-L3128">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3125-L3130">Show on GitHub</a>
</div>
</section>
</div>
Expand Down Expand Up @@ -415,7 +415,7 @@ <h4>Declaration</h4>
</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3133-L3138">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L3135-L3140">Show on GitHub</a>
</div>
</section>
</div>
Expand Down
12 changes: 7 additions & 5 deletions docs/Structs.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="index.html">
BigNumber 2.1.0 Docs
BigNumber 2.1.1 Docs
</a>

</p>
Expand Down Expand Up @@ -188,12 +188,13 @@ <h4>Declaration</h4>
<pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">struct</span> <span class="kt">BInt</span><span class="p">:</span>
<span class="kt">SignedNumeric</span><span class="p">,</span> <span class="c1">// Implies Numeric, Equatable, ExpressibleByIntegerLiteral</span>
<span class="kt">BinaryInteger</span><span class="p">,</span> <span class="c1">// Implies Hashable, CustomStringConvertible, Strideable, Comparable</span>
<span class="kt">ExpressibleByFloatLiteral</span></code></pre>
<span class="kt">ExpressibleByFloatLiteral</span><span class="p">,</span>
<span class="kt">Codable</span></code></pre>

</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L165-L996">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L165-L997">Show on GitHub</a>
</div>
</section>
</div>
Expand Down Expand Up @@ -236,12 +237,13 @@ <h4>Declaration</h4>
<span class="kt">CustomStringConvertible</span><span class="p">,</span>
<span class="kt">SignedNumeric</span><span class="p">,</span>
<span class="kt">Comparable</span><span class="p">,</span>
<span class="kt">Hashable</span></code></pre>
<span class="kt">Hashable</span><span class="p">,</span>
<span class="kt">Codable</span></code></pre>

</div>
</div>
<div class="slightly-smaller">
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L2188-L3005">Show on GitHub</a>
<a href="https://github.com/mkrd/Swift-Big-Integer/tree/master/Sources/Swift-Big-Number-Core.swift#L2189-L3007">Show on GitHub</a>
</div>
</section>
</div>
Expand Down
Loading

0 comments on commit 9454066

Please sign in to comment.