Skip to content
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

[BigInt tests] ✅🐰 Codable #259

Open
wants to merge 2 commits into
base: biginteger
Choose a base branch
from
Open
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
72 changes: 72 additions & 0 deletions Tests/BigIntTests/CodableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===--- CodableTests.swift -----------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import XCTest
@testable import BigIntModule

// The only property that we care about is that `encode -> decode` works.
// The transport format does not matter.
// Though as soon as we commit to 1 representation we have to keep it forever.
class CodableTests: XCTestCase {

func test_equalsString_radix10() {
let encoder = JSONEncoder()

for p in generateBigInts(approximateCount: 100) {
let big = p.create()

do {
let data = try encoder.encode(big)
let json = String(bytes: data, encoding: .utf8)
assert(json != nil)

let expected = "\"" + String(big, radix: 10) + "\""
XCTAssertEqual(json, expected)
} catch {
XCTFail("\(big)")
}
}
}

func test_int() {
let encoder = JSONEncoder()
let decoder = JSONDecoder()

for int in generateInts(approximateCount: 1000) {
let big = BigInt(int)

do {
let data = try encoder.encode(big)
let restored = try decoder.decode(BigInt.self, from: data)
XCTAssertEqual(big, restored)
} catch {
XCTFail("\(int)")
}
}
}

func test_big() {
let encoder = JSONEncoder()
let decoder = JSONDecoder()

for p in generateBigInts(approximateCount: 1000) {
let big = p.create()

do {
let data = try encoder.encode(big)
let restored = try decoder.decode(BigInt.self, from: data)
XCTAssertEqual(big, restored)
} catch {
XCTFail("\(big)")
}
}
}
}
69 changes: 69 additions & 0 deletions Tests/BigIntTests/Helpers/BigInt+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//===--- BigInt+Extensions.swift ------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import BigIntModule

extension BigInt {

internal typealias Word = Words.Element

internal init(isPositive: Bool, magnitude: [BigIntPrototype.Word]) {
let p = BigIntPrototype(isPositive: isPositive, magnitude: magnitude)
self = p.create()
}

internal init(_ sign: BigIntPrototype.Sign, magnitude: BigIntPrototype.Word) {
let p = BigIntPrototype(sign, magnitude: magnitude)
self = p.create()
}

internal init(_ sign: BigIntPrototype.Sign, magnitude: [BigIntPrototype.Word]) {
let p = BigIntPrototype(sign, magnitude: magnitude)
self = p.create()
}

internal func power(exponent: BigInt) -> BigInt {
precondition(exponent >= 0, "Exponent must be positive")

if exponent == 0 {
return BigInt(1)
}

if exponent == 1 {
return self
}

// This has to be after 'exp == 0', because 'pow(0, 0) -> 1'
if self == 0 {
return 0
}

var base = self
var exponent = exponent
var result = BigInt(1)

// Eventually we will arrive to most significant '1'
while exponent != 1 {
let exponentIsOdd = exponent & 0b1 == 1

if exponentIsOdd {
result *= base
}

base *= base
exponent >>= 1 // Basically divided by 2, but faster
}

// Most significant '1' is odd:
result *= base
return result
}
}
Loading