Skip to content

Commit

Permalink
[NBKSignedKit] Req. init?(words:isSigned:) (#89).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Oct 4, 2023
1 parent 91df233 commit 8264de2
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Sources/NBKSignedKit/NBKSigned+Literals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ extension NBKSigned {
}

@inlinable init?(exactlyIntegerLiteral source: StaticBigInt) {
let isLessThanZero: Bool = source.signum() == -1 as Int
guard let magnitude = Magnitude(words: NBK.MaybeTwosComplement(NBKStaticBigInt(source), formTwosComplement: isLessThanZero)) else { return nil }
self.init(sign: NBK.Sign(isLessThanZero), magnitude: magnitude)
self.init(words: NBKStaticBigInt(source))
}

#else
Expand Down
2 changes: 2 additions & 0 deletions Sources/NBKSignedKit/NBKSigned+Numbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ extension NBKSigned {
//=------------------------------------------------------------------------=
// MARK: Initializers x Binary Integer
//=------------------------------------------------------------------------=
// NOTE: Using init(sign:magnitude:) is more efficient than init(words:).
//=------------------------------------------------------------------------=

@inlinable public init(_ source: some BinaryInteger) {
if let value = Self(exactly: source) { self = value } else {
Expand Down
31 changes: 31 additions & 0 deletions Sources/NBKSignedKit/NBKSigned+Words.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//=----------------------------------------------------------------------------=
// This source file is part of the Numberick open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

import NBKCoreKit

//*============================================================================*
// MARK: * NBK x Signed x Words
//*============================================================================*

extension NBKSigned {

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=

@inlinable public init?(words: some RandomAccessCollection<UInt>) {
self.init(words: words, isSigned: true)
}

@inlinable public init?(words: some RandomAccessCollection<UInt>, isSigned: Bool) {
let isLessThanZero: Bool = isSigned && words.last?.mostSignificantBit == true
guard let magnitude = Magnitude(words: NBK.MaybeTwosComplement(words, formTwosComplement: isLessThanZero)) else { return nil }
self.init(sign: Sign(isLessThanZero), magnitude: magnitude)
}
}
4 changes: 4 additions & 0 deletions Sources/NBKSignedKit/NBKSigned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Hashable, Sendable, SignedNumeric {
// MARK: Meta Data
//=------------------------------------------------------------------------=

@inlinable public static var isSigned: Bool {
true
}

/// A `description` of this type.
///
/// ```
Expand Down
4 changes: 4 additions & 0 deletions Tests/NBKSignedKitTests/NBKSigned+Negation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import NBKSignedKit
import XCTest

private typealias W = [UInt]
private typealias X = [UInt64]
private typealias Y = [UInt32]

//*============================================================================*
// MARK: * NBK x Signed x Negation
//*============================================================================*
Expand Down
4 changes: 4 additions & 0 deletions Tests/NBKSignedKitTests/NBKSigned+Numbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import NBKCoreKit
import NBKSignedKit
import XCTest

private typealias W = [UInt]
private typealias X = [UInt64]
private typealias Y = [UInt32]

//*============================================================================*
// MARK: * NBK x Signed x Numbers
//*============================================================================*
Expand Down
4 changes: 4 additions & 0 deletions Tests/NBKSignedKitTests/NBKSigned+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import NBKSignedKit
import XCTest

private typealias W = [UInt]
private typealias X = [UInt64]
private typealias Y = [UInt32]

//*============================================================================*
// MARK: * NBK x Signed x Text
//*============================================================================*
Expand Down
84 changes: 84 additions & 0 deletions Tests/NBKSignedKitTests/NBKSigned+Words.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//=----------------------------------------------------------------------------=
// This source file is part of the Numberick open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

#if DEBUG

import NBKCoreKit
import NBKSignedKit
import XCTest

private typealias W = [UInt]
private typealias X = [UInt64]
private typealias Y = [UInt32]

//*============================================================================*
// MARK: * NBK x Signed x Words
//*============================================================================*

final class NBKSignedTestsOnWords: XCTestCase {

typealias T = NBKSigned<UInt>

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

func testFromWords() {
NBKAssertFromWordsIsSigned([ ] as W, true, T(sign: .plus, magnitude: 0))
NBKAssertFromWordsIsSigned([ ] as W, false, T(sign: .plus, magnitude: 0))
NBKAssertFromWordsIsSigned([ 0 ] as W, true, T(sign: .plus, magnitude: 0))
NBKAssertFromWordsIsSigned([ 0 ] as W, false, T(sign: .plus, magnitude: 0))
NBKAssertFromWordsIsSigned([~0 ] as W, true, T(sign: .minus, magnitude: 1))
NBKAssertFromWordsIsSigned([~0 ] as W, false, T(sign: .plus, magnitude: ~0))
NBKAssertFromWordsIsSigned([~0, 0] as W, true, T(sign: .plus, magnitude: ~0))
NBKAssertFromWordsIsSigned([~0, 0] as W, false, T(sign: .plus, magnitude: ~0))
NBKAssertFromWordsIsSigned([ 1, ~0] as W, true, T(sign: .minus, magnitude: ~0))
NBKAssertFromWordsIsSigned([ 1, ~0] as W, false, nil as T?)

NBKAssertFromWordsIsSigned(W(repeating: 0, count: 2), true, 000 as T?)
NBKAssertFromWordsIsSigned(W(repeating: 1, count: 2), true, nil as T?)
NBKAssertFromWordsIsSigned(W(repeating: ~0, count: 2), true, -01 as T?)
NBKAssertFromWordsIsSigned(W(repeating: ~1, count: 2), true, nil as T?)

NBKAssertFromWordsIsSigned(W(repeating: 0, count: 2), false, 000 as T?)
NBKAssertFromWordsIsSigned(W(repeating: 1, count: 2), false, nil as T?)
NBKAssertFromWordsIsSigned(W(repeating: ~0, count: 2), false, nil as T?)
NBKAssertFromWordsIsSigned(W(repeating: ~1, count: 2), false, nil as T?)
}
}

//*============================================================================*
// MARK: * NBK x Signed x Words x Assertions
//*============================================================================*

private func NBKAssertFromWords<M: NBKUnsignedInteger>(
_ words: [UInt], _ integer: NBKSigned<M>?,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
typealias T = NBKSigned<M>
//=------------------------------------------=
NBKAssertIdentical( T(words: words), integer, file: file, line: line)
//NBKAssertIdentical(integer.flatMap({ T(words: $0.words) }), integer, file: file, line: line)
}

private func NBKAssertFromWordsIsSigned<M: NBKUnsignedInteger>(
_ words: [UInt], _ isSigned: Bool, _ integer: NBKSigned<M>?,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
typealias T = NBKSigned<M>
//=------------------------------------------=
if isSigned == T.isSigned {
NBKAssertFromWords(words, integer, file: file, line: line)
}

NBKAssertIdentical( T(words: words, isSigned: isSigned), integer, file: file, line: line)
//NBKAssertIdentical(integer.flatMap({ T(words: $0.words, isSigned: isSigned) }), integer, file: file, line: line)
}

#endif
4 changes: 4 additions & 0 deletions Tests/NBKSignedKitTests/NBKSigned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import NBKCoreKit
import NBKSignedKit
import XCTest

private typealias W = [UInt]
private typealias X = [UInt64]
private typealias Y = [UInt32]

//*============================================================================*
// MARK: * NBK x Signed
//*============================================================================*
Expand Down

0 comments on commit 8264de2

Please sign in to comment.