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] ❌🐰 Int properties #252

Open
wants to merge 3 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
157 changes: 157 additions & 0 deletions Tests/BigIntTests/BitWidthTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//===--- BitWidthTests.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

class BitWidthTests: XCTestCase {

func test_trivial() {
let zero = BigInt(0)
XCTAssertEqual(zero.bitWidth, 1) // 0 is just 0

let plus1 = BigInt(1)
XCTAssertEqual(plus1.bitWidth, 2) // 1 needs '0' prefix -> '01'

let minus1 = BigInt(-1)
XCTAssertEqual(minus1.bitWidth, 1) // -1 is just 1
}

// MARK: - Int

private typealias IntTestCase = (value: Int, expected: Int)

private let intTestCases: [IntTestCase] = [
// zero
(0, 1),
// positive
(1, 2),
(2, 3),
(3, 3),
(4, 4),
(5, 4),
(6, 4),
(7, 4),
(8, 5),
(9, 5),
(10, 5),
(11, 5),
(12, 5),
(13, 5),
(14, 5),
(15, 5),
// negative
(-1, 1),
(-2, 2),
(-3, 3),
(-4, 3),
(-5, 4),
(-6, 4),
(-7, 4),
(-8, 4),
(-9, 5),
(-10, 5),
(-11, 5),
(-12, 5),
(-13, 5),
(-14, 5),
(-15, 5)
]

func test_ints() {
for (value, expected) in self.intTestCases {
let bigInt = BigInt(value)
XCTAssertEqual(bigInt.bitWidth, expected, "\(value)")
}
}

// MARK: - Positive power of 2

// +-----+-----------+-------+-------+
// | dec | bin | power | bit |
// | | | | width |
// +-----+-----------+-------+-------+
// | 1 | 0001 | 0 | 2 |
// | 2 | 0010 | 1 | 3 |
// | 4 | 0100 | 2 | 4 |
// | 8 | 0000 1000 | 3 | 5 |
// +-----+-----------+-------+-------+
//
// TLDR: bitWidth = power + 2
private let positivePowerOf2Correction = 2

func test_powerOf2_positive() {
for (power, int) in PositivePowersOf2(type: Int.self) {
let big = BigInt(int)
let expected = power + self.positivePowerOf2Correction
XCTAssertEqual(big.bitWidth, expected, "for \(int) (2^\(power))")
}
}

func test_powerOf2_positive_multipleWords() {
typealias Word = UInt64

for zeroWordCount in [1, 2] {
let zeroWords = [Word](repeating: 0, count: zeroWordCount)
let zeroWordsBitWidth = zeroWordCount * Word.bitWidth

for (power, value) in PositivePowersOf2(type: Word.self) {
let words = zeroWords + [value]
let proto = BigIntPrototype(.positive, magnitude: words)
let big = proto.create()

let expected = power + self.positivePowerOf2Correction + zeroWordsBitWidth
XCTAssertEqual(big.bitWidth, expected, "\(proto)")
}
}
}

// MARK: - Negative power of 2

// +-----+------+-------+-------+
// | dec | bin | power | bit |
// | | | | width |
// +-----+------+-------+-------+
// | -1 | 1111 | 0 | 1 |
// | -2 | 1110 | 1 | 2 |
// | -4 | 1100 | 2 | 3 |
// | -8 | 1000 | 3 | 4 |
// +-----+------+-------+-------+
//
// TLDR: bitWidth = power + 1
private let negativePowerOf2Correction = 1

func test_powerOf2_negative() {
for (power, int) in NegativePowersOf2(type: Int.self) {
let big = BigInt(int)
let expected = power + self.negativePowerOf2Correction
XCTAssertEqual(big.bitWidth, expected, "for \(int) (2^\(power))")
}
}

func test_powerOf2_negative_multipleWords() {
typealias Word = UInt64

for zeroWordCount in [1, 2] {
let zeroWords = [Word](repeating: 0, count: zeroWordCount)
let zeroWordsBitWidth = zeroWordCount * Word.bitWidth

for (power, value) in PositivePowersOf2(type: Word.self) {
let words = zeroWords + [value]
let proto = BigIntPrototype(.negative, magnitude: words)
let big = proto.create()

let expected = power + self.negativePowerOf2Correction + zeroWordsBitWidth
XCTAssertEqual(big.bitWidth, expected, "\(proto)")
}
}
}
}
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