Skip to content

Commit

Permalink
[NBKSignedKit] Tests (#81).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Sep 15, 2023
1 parent 1115e42 commit 759f2e5
Show file tree
Hide file tree
Showing 7 changed files with 433 additions and 19 deletions.
17 changes: 11 additions & 6 deletions Sources/NBKSignedKit/NBKSigned+Comparisons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ extension NBKSigned {
// MARK: Accessors
//=------------------------------------------------------------------------=

/// Returns `1` if this value is positive, `-1` if it is negative, and `0` otherwise.
@inlinable public func signum() -> Int {
self.isZero ? 0 : self.sign == Sign.plus ? 1 : -1
}

/// Returns whether this value is equal to zero.
@inlinable public var isZero: Bool {
self.magnitude.isZero
Expand All @@ -39,6 +34,16 @@ extension NBKSigned {
self.sign == Sign.plus && !self.isZero
}

/// Returns whether this value is a power of two.
@inlinable public var isPowerOf2: Bool {
self.sign == Sign.plus && self.magnitude.isPowerOf2
}

/// Returns `1` if this value is positive, `-1` if it is negative, and `0` otherwise.
@inlinable public func signum() -> Int {
self.isZero ? 0 : self.sign == Sign.plus ? 1 : -1
}

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
Expand Down Expand Up @@ -77,7 +82,7 @@ extension NBKSigned {
}
}

@inlinable public func compared(to other: Digit) -> Int {
@_disfavoredOverload @inlinable public func compared(to other: Digit) -> Int {
if self.sign != other.sign {
return self.isZero && other.isZero ? 0 : self.sign == Sign.plus ? 1 : -1
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ final class NBKFlexibleWidthTestsOnDivisionAsIntXL: XCTestCase {
NBKAssertDivision(T(words:[1, 2, 3, 4 + 1 << 63] as W), T(words:[ 2, 3, 4, 5 &+ 1 << 63] as W), T(1), -T(words:[1, 1, 1, 1] as W))
NBKAssertDivision(T(words:[1, 2, 3, 4 + 1 << 63] as W), T(words:[ 3, 4, 5, 6 &+ 1 << 63] as W), T(1), -T(words:[2, 2, 2, 2] as W))
NBKAssertDivision(T(words:[1, 2, 3, 4 + 1 << 63] as W), T(words:[ 4, 5, 6, 7 &+ 1 << 63] as W), T(1), -T(words:[3, 3, 3, 3] as W))

NBKAssertDivision(T(words:[1, 2, 3, 4 + 1 << 63] as W), T(words:[~0, ~2, ~3, ~4 &+ 1 << 63] as W), -T(1), -T(words:[0, 0, 0, 0] as W))
NBKAssertDivision(T(words:[1, 2, 3, 4 + 1 << 63] as W), T(words:[~1, ~3, ~4, ~5 &+ 1 << 63] as W), -T(1), -T(words:[1, 1, 1, 1] as W))
NBKAssertDivision(T(words:[1, 2, 3, 4 + 1 << 63] as W), T(words:[~2, ~4, ~5, ~6 &+ 1 << 63] as W), -T(1), -T(words:[2, 2, 2, 2] as W))
Expand Down Expand Up @@ -190,7 +189,6 @@ final class NBKFlexibleWidthTestsOnDivisionAsUIntXL: XCTestCase {
NBKAssertDivision(T(x64:[1, 2, 3, 4 + 1 << 63] as X), T(x64:[ 0, 1, 2, 3 &+ 1 << 63] as X), T(1), T(x64:[1, 1, 1, 1] as X))
NBKAssertDivision(T(x64:[1, 2, 3, 4 + 1 << 63] as X), T(x64:[~0, ~0, 0, 2 &+ 1 << 63] as X), T(1), T(x64:[2, 2, 2, 2] as X))
NBKAssertDivision(T(x64:[1, 2, 3, 4 + 1 << 63] as X), T(x64:[~1, ~1, ~0, 0 &+ 1 << 63] as X), T(1), T(x64:[3, 3, 3, 3] as X))

NBKAssertDivision(T(x64:[1, 2, 3, 4 + 1 << 63] as X), T(x64:[~2, ~2, ~1, ~0 &+ 1 << 63] as X), T(1), T(x64:[4, 4, 4, 4] as X))
NBKAssertDivision(T(x64:[1, 2, 3, 4 + 1 << 63] as X), T(x64:[~3, ~3, ~2, ~1 &+ 1 << 63] as X), T(1), T(x64:[5, 5, 5, 5] as X))
NBKAssertDivision(T(x64:[1, 2, 3, 4 + 1 << 63] as X), T(x64:[~4, ~4, ~3, ~2 &+ 1 << 63] as X), T(1), T(x64:[6, 6, 6, 6] as X))
Expand Down
12 changes: 6 additions & 6 deletions Tests/NBKSignedKitTests/NBKSigned+Addition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ final class NBKSignedTestsOnAdditionAsSIntXL: XCTestCase {
// MARK: Tests
//=------------------------------------------------------------------------=

func testAdding() {
func testAddingLargeToLarge() {
NBKAssertAddition( T(M(x64:[~0, ~0, ~0, 0] as X)), T(M(x64:[3, 0, 0, 0] as X)), T(M(x64:[ 2, 0, 0, 1] as X)))
NBKAssertAddition( T(M(x64:[~0, ~0, ~0, 0] as X)), T(M(x64:[0, 3, 0, 0] as X)), T(M(x64:[~0, 2, 0, 1] as X)))
NBKAssertAddition( T(M(x64:[~0, ~0, ~0, 0] as X)), T(M(x64:[0, 0, 3, 0] as X)), T(M(x64:[~0, ~0, 2, 1] as X)))
NBKAssertAddition( T(M(x64:[~0, ~0, ~0, 0] as X)), T(M(x64:[0, 0, 0, 3] as X)), T(M(x64:[~0, ~0, ~0, 3] as X)))

NBKAssertAddition( T(M(x64:[~0, ~0, ~0, 0] as X)), -T(M(x64:[3, 0, 0, 0] as X)), T(M(x64:[~3, ~0, ~0, 0] as X)))
NBKAssertAddition( T(M(x64:[~0, ~0, ~0, 0] as X)), -T(M(x64:[0, 3, 0, 0] as X)), T(M(x64:[~0, ~3, ~0, 0] as X)))
NBKAssertAddition( T(M(x64:[~0, ~0, ~0, 0] as X)), -T(M(x64:[0, 0, 3, 0] as X)), T(M(x64:[~0, ~0, ~3, 0] as X)))
Expand All @@ -56,10 +56,10 @@ final class NBKSignedTestsOnAdditionAsSIntXL: XCTestCase {
}

//=------------------------------------------------------------------------=
// MARK: Tests x Digit
// MARK: Tests x Digit (and Self)
//=------------------------------------------------------------------------=

func testAddingDigit() {
func testAddingSmallToSmall() {
NBKAssertAdditionByDigit( T(M(1)), D(U(2)), T(M(3)))
NBKAssertAdditionByDigit( T(M(1)), D(U(1)), T(M(2)))
NBKAssertAdditionByDigit( T(M(1)), D(U(0)), T(M(1)))
Expand Down Expand Up @@ -116,11 +116,11 @@ file: StaticString = #file, line: UInt = #line) {
private func NBKAssertAdditionByDigit<M: NBKUnsignedInteger>(
_ lhs: NBKSigned<M>, _ rhs: NBKSigned<M>.Digit, _ partialValue: NBKSigned<M>,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
NBKAssertAddition(lhs, NBKSigned(digit: rhs), partialValue)
//=------------------------------------------=
NBKAssertIdentical( lhs + rhs, partialValue, file: file, line: line)
NBKAssertIdentical({ var lhs = lhs; lhs += rhs; return lhs }(), partialValue, file: file, line: line)
//=------------------------------------------=
NBKAssertAddition(lhs, NBKSigned(digit: rhs), partialValue)
}

#endif
173 changes: 173 additions & 0 deletions Tests/NBKSignedKitTests/NBKSigned+Comparisons.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
//=----------------------------------------------------------------------------=
// 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 NBKFlexibleWidthKit
import NBKSignedKit
import XCTest

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

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

final class NBKSignedTestsOnComparisonsAsSIntXL: XCTestCase {

typealias T = NBKSigned<UIntXL>
typealias M = NBKSigned<UIntXL>.Magnitude
typealias D = NBKSigned<UIntXL>.Digit
typealias U = NBKSigned<UIntXL>.Digit.Magnitude

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

func testIsZero() {
XCTAssertTrue (( T(0)).isZero)
XCTAssertTrue ((-T(0)).isZero)
XCTAssertFalse(( T(1)).isZero)
XCTAssertFalse((-T(1)).isZero)
}

func testIsLessThanZero() {
XCTAssertFalse(( T(0)).isLessThanZero)
XCTAssertFalse((-T(0)).isLessThanZero)
XCTAssertFalse(( T(1)).isLessThanZero)
XCTAssertTrue ((-T(1)).isLessThanZero)
}

func testIsMoreThanZero() {
XCTAssertFalse(( T(0)).isMoreThanZero)
XCTAssertFalse((-T(0)).isMoreThanZero)
XCTAssertTrue (( T(1)).isMoreThanZero)
XCTAssertFalse((-T(1)).isMoreThanZero)
}

func testSignum() {
NBKAssertSignum(( T(0)), Int( 0))
NBKAssertSignum((-T(0)), Int( 0))
NBKAssertSignum(( T(1)), Int( 1))
NBKAssertSignum((-T(1)), Int(-1))
}

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

func testHashing() {
var union = Set<T>()
union.insert( T(0))
union.insert( T(0))
union.insert(-T(0))
union.insert(-T(0))
union.insert( T(1))
union.insert( T(1))
union.insert(-T(1))
union.insert(-T(1))
XCTAssertEqual(union.count, 3)
}

func testComparingLargeWithLarge() {
NBKAssertComparisons(T(M(words:[0, 2, 3, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1))
NBKAssertComparisons(T(M(words:[1, 0, 3, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1))
NBKAssertComparisons(T(M(words:[1, 2, 0, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1))
NBKAssertComparisons(T(M(words:[1, 2, 3, 0] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1))
NBKAssertComparisons(T(M(words:[0, 2, 3, 4] as W)), T(M(words:[0, 2, 3, 4] as W)), Int(0))
NBKAssertComparisons(T(M(words:[1, 0, 3, 4] as W)), T(M(words:[1, 0, 3, 4] as W)), Int(0))
NBKAssertComparisons(T(M(words:[1, 2, 0, 4] as W)), T(M(words:[1, 2, 0, 4] as W)), Int(0))
NBKAssertComparisons(T(M(words:[1, 2, 3, 0] as W)), T(M(words:[1, 2, 3, 0] as W)), Int(0))
NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[0, 2, 3, 4] as W)), Int(1))
NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 0, 3, 4] as W)), Int(1))
NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 2, 0, 4] as W)), Int(1))
NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 2, 3, 0] as W)), Int(1))
}

//=------------------------------------------------------------------------=
// MARK: Tests x Digit (and Self)
//=------------------------------------------------------------------------=

func testComparingSmallWithSmall() {
NBKAssertComparisonsByDigit( T(0), D(0), Int(0))
NBKAssertComparisonsByDigit( T(0), -D(0), Int(0))
NBKAssertComparisonsByDigit(-T(0), D(0), Int(0))
NBKAssertComparisonsByDigit(-T(0), -D(0), Int(0))

NBKAssertComparisonsByDigit( T(1), D(1), Int(0))
NBKAssertComparisonsByDigit( T(1), -D(1), Int(1))
NBKAssertComparisonsByDigit(-T(1), D(1), -Int(1))
NBKAssertComparisonsByDigit(-T(1), -D(1), Int(0))

NBKAssertComparisonsByDigit( T(2), D(3), -Int(1))
NBKAssertComparisonsByDigit( T(2), -D(3), Int(1))
NBKAssertComparisonsByDigit(-T(2), D(3), -Int(1))
NBKAssertComparisonsByDigit(-T(2), -D(3), Int(1))

NBKAssertComparisonsByDigit( T(3), D(2), Int(1))
NBKAssertComparisonsByDigit( T(3), -D(2), Int(1))
NBKAssertComparisonsByDigit(-T(3), D(2), -Int(1))
NBKAssertComparisonsByDigit(-T(3), -D(2), -Int(1))
}

//=------------------------------------------------------------------------=
// MARK: Tests x Miscellaneous
//=------------------------------------------------------------------------=

func testOverloadsAreUnambiguous() {
func becauseThisCompilesSuccessfully(_ x: inout T) {
XCTAssertNotNil(x.signum())
}
}

func testOverloadsAreUnambiguousWhenUsingIntegerLiterals() {
func becauseThisCompilesSuccessfully(_ x: inout T) {
XCTAssertNotNil(x.compared(to: 0))
}
}
}

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

private func NBKAssertSignum<M>(
_ operand: NBKSigned<M>, _ signum: Int,
file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(Int(operand.signum() as Int), signum, file: file, line: line)
}

private func NBKAssertComparisons<M>(
_ lhs: NBKSigned<M>, _ rhs: NBKSigned<M>, _ signum: Int,
file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(lhs == rhs, signum == 0, file: file, line: line)
XCTAssertEqual(lhs != rhs, signum != 0, file: file, line: line)

XCTAssertEqual(lhs < rhs, signum == -1, file: file, line: line)
XCTAssertEqual(lhs <= rhs, signum != 1, file: file, line: line)

XCTAssertEqual(lhs > rhs, signum == 1, file: file, line: line)
XCTAssertEqual(lhs >= rhs, signum != -1, file: file, line: line)

XCTAssertEqual(lhs.compared(to: rhs), signum, file: file, line: line)
}

private func NBKAssertComparisonsByDigit<M>(
_ lhs: NBKSigned<M>, _ rhs: NBKSigned<M>.Digit, _ signum: Int,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
NBKAssertComparisons(lhs, NBKSigned<M>(digit: rhs), signum)
//=------------------------------------------=
XCTAssertEqual(lhs.compared(to: rhs), signum, file: file, line: line)
}

#endif
Loading

0 comments on commit 759f2e5

Please sign in to comment.