Skip to content

Commit 759f2e5

Browse files
committed
[NBKSignedKit] Tests (#81).
1 parent 1115e42 commit 759f2e5

7 files changed

+433
-19
lines changed

Sources/NBKSignedKit/NBKSigned+Comparisons.swift

+11-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ extension NBKSigned {
1919
// MARK: Accessors
2020
//=------------------------------------------------------------------------=
2121

22-
/// Returns `1` if this value is positive, `-1` if it is negative, and `0` otherwise.
23-
@inlinable public func signum() -> Int {
24-
self.isZero ? 0 : self.sign == Sign.plus ? 1 : -1
25-
}
26-
2722
/// Returns whether this value is equal to zero.
2823
@inlinable public var isZero: Bool {
2924
self.magnitude.isZero
@@ -39,6 +34,16 @@ extension NBKSigned {
3934
self.sign == Sign.plus && !self.isZero
4035
}
4136

37+
/// Returns whether this value is a power of two.
38+
@inlinable public var isPowerOf2: Bool {
39+
self.sign == Sign.plus && self.magnitude.isPowerOf2
40+
}
41+
42+
/// Returns `1` if this value is positive, `-1` if it is negative, and `0` otherwise.
43+
@inlinable public func signum() -> Int {
44+
self.isZero ? 0 : self.sign == Sign.plus ? 1 : -1
45+
}
46+
4247
//=------------------------------------------------------------------------=
4348
// MARK: Utilities
4449
//=------------------------------------------------------------------------=
@@ -77,7 +82,7 @@ extension NBKSigned {
7782
}
7883
}
7984

80-
@inlinable public func compared(to other: Digit) -> Int {
85+
@_disfavoredOverload @inlinable public func compared(to other: Digit) -> Int {
8186
if self.sign != other.sign {
8287
return self.isZero && other.isZero ? 0 : self.sign == Sign.plus ? 1 : -1
8388
} else {

Tests/NBKFlexibleWidthKitTests/NBKFlexibleWidth+Division.swift

-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ final class NBKFlexibleWidthTestsOnDivisionAsIntXL: XCTestCase {
6868
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))
6969
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))
7070
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))
71-
7271
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))
7372
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))
7473
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))
@@ -190,7 +189,6 @@ final class NBKFlexibleWidthTestsOnDivisionAsUIntXL: XCTestCase {
190189
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))
191190
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))
192191
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))
193-
194192
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))
195193
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))
196194
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))

Tests/NBKSignedKitTests/NBKSigned+Addition.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ final class NBKSignedTestsOnAdditionAsSIntXL: XCTestCase {
3333
// MARK: Tests
3434
//=------------------------------------------------------------------------=
3535

36-
func testAdding() {
36+
func testAddingLargeToLarge() {
3737
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)))
3838
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)))
3939
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)))
4040
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)))
41-
41+
4242
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)))
4343
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)))
4444
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)))
@@ -56,10 +56,10 @@ final class NBKSignedTestsOnAdditionAsSIntXL: XCTestCase {
5656
}
5757

5858
//=------------------------------------------------------------------------=
59-
// MARK: Tests x Digit
59+
// MARK: Tests x Digit (and Self)
6060
//=------------------------------------------------------------------------=
6161

62-
func testAddingDigit() {
62+
func testAddingSmallToSmall() {
6363
NBKAssertAdditionByDigit( T(M(1)), D(U(2)), T(M(3)))
6464
NBKAssertAdditionByDigit( T(M(1)), D(U(1)), T(M(2)))
6565
NBKAssertAdditionByDigit( T(M(1)), D(U(0)), T(M(1)))
@@ -116,11 +116,11 @@ file: StaticString = #file, line: UInt = #line) {
116116
private func NBKAssertAdditionByDigit<M: NBKUnsignedInteger>(
117117
_ lhs: NBKSigned<M>, _ rhs: NBKSigned<M>.Digit, _ partialValue: NBKSigned<M>,
118118
file: StaticString = #file, line: UInt = #line) {
119+
//=------------------------------------------=
120+
NBKAssertAddition(lhs, NBKSigned(digit: rhs), partialValue)
119121
//=------------------------------------------=
120122
NBKAssertIdentical( lhs + rhs, partialValue, file: file, line: line)
121123
NBKAssertIdentical({ var lhs = lhs; lhs += rhs; return lhs }(), partialValue, file: file, line: line)
122-
//=------------------------------------------=
123-
NBKAssertAddition(lhs, NBKSigned(digit: rhs), partialValue)
124124
}
125125

126126
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
//=----------------------------------------------------------------------------=
2+
// This source file is part of the Numberick open source project.
3+
//
4+
// Copyright (c) 2023 Oscar Byström Ericsson
5+
// Licensed under Apache License, Version 2.0
6+
//
7+
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
8+
//=----------------------------------------------------------------------------=
9+
10+
#if DEBUG
11+
12+
import NBKCoreKit
13+
import NBKFlexibleWidthKit
14+
import NBKSignedKit
15+
import XCTest
16+
17+
private typealias W = [UInt]
18+
private typealias X = [UInt64]
19+
private typealias Y = [UInt32]
20+
21+
//*============================================================================*
22+
// MARK: * NBK x Signed x Comparisons
23+
//*============================================================================*
24+
25+
final class NBKSignedTestsOnComparisonsAsSIntXL: XCTestCase {
26+
27+
typealias T = NBKSigned<UIntXL>
28+
typealias M = NBKSigned<UIntXL>.Magnitude
29+
typealias D = NBKSigned<UIntXL>.Digit
30+
typealias U = NBKSigned<UIntXL>.Digit.Magnitude
31+
32+
//=------------------------------------------------------------------------=
33+
// MARK: Tests
34+
//=------------------------------------------------------------------------=
35+
36+
func testIsZero() {
37+
XCTAssertTrue (( T(0)).isZero)
38+
XCTAssertTrue ((-T(0)).isZero)
39+
XCTAssertFalse(( T(1)).isZero)
40+
XCTAssertFalse((-T(1)).isZero)
41+
}
42+
43+
func testIsLessThanZero() {
44+
XCTAssertFalse(( T(0)).isLessThanZero)
45+
XCTAssertFalse((-T(0)).isLessThanZero)
46+
XCTAssertFalse(( T(1)).isLessThanZero)
47+
XCTAssertTrue ((-T(1)).isLessThanZero)
48+
}
49+
50+
func testIsMoreThanZero() {
51+
XCTAssertFalse(( T(0)).isMoreThanZero)
52+
XCTAssertFalse((-T(0)).isMoreThanZero)
53+
XCTAssertTrue (( T(1)).isMoreThanZero)
54+
XCTAssertFalse((-T(1)).isMoreThanZero)
55+
}
56+
57+
func testSignum() {
58+
NBKAssertSignum(( T(0)), Int( 0))
59+
NBKAssertSignum((-T(0)), Int( 0))
60+
NBKAssertSignum(( T(1)), Int( 1))
61+
NBKAssertSignum((-T(1)), Int(-1))
62+
}
63+
64+
//=------------------------------------------------------------------------=
65+
// MARK: Tests
66+
//=------------------------------------------------------------------------=
67+
68+
func testHashing() {
69+
var union = Set<T>()
70+
union.insert( T(0))
71+
union.insert( T(0))
72+
union.insert(-T(0))
73+
union.insert(-T(0))
74+
union.insert( T(1))
75+
union.insert( T(1))
76+
union.insert(-T(1))
77+
union.insert(-T(1))
78+
XCTAssertEqual(union.count, 3)
79+
}
80+
81+
func testComparingLargeWithLarge() {
82+
NBKAssertComparisons(T(M(words:[0, 2, 3, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1))
83+
NBKAssertComparisons(T(M(words:[1, 0, 3, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1))
84+
NBKAssertComparisons(T(M(words:[1, 2, 0, 4] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1))
85+
NBKAssertComparisons(T(M(words:[1, 2, 3, 0] as W)), T(M(words:[1, 2, 3, 4] as W)), -Int(1))
86+
NBKAssertComparisons(T(M(words:[0, 2, 3, 4] as W)), T(M(words:[0, 2, 3, 4] as W)), Int(0))
87+
NBKAssertComparisons(T(M(words:[1, 0, 3, 4] as W)), T(M(words:[1, 0, 3, 4] as W)), Int(0))
88+
NBKAssertComparisons(T(M(words:[1, 2, 0, 4] as W)), T(M(words:[1, 2, 0, 4] as W)), Int(0))
89+
NBKAssertComparisons(T(M(words:[1, 2, 3, 0] as W)), T(M(words:[1, 2, 3, 0] as W)), Int(0))
90+
NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[0, 2, 3, 4] as W)), Int(1))
91+
NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 0, 3, 4] as W)), Int(1))
92+
NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 2, 0, 4] as W)), Int(1))
93+
NBKAssertComparisons(T(M(words:[1, 2, 3, 4] as W)), T(M(words:[1, 2, 3, 0] as W)), Int(1))
94+
}
95+
96+
//=------------------------------------------------------------------------=
97+
// MARK: Tests x Digit (and Self)
98+
//=------------------------------------------------------------------------=
99+
100+
func testComparingSmallWithSmall() {
101+
NBKAssertComparisonsByDigit( T(0), D(0), Int(0))
102+
NBKAssertComparisonsByDigit( T(0), -D(0), Int(0))
103+
NBKAssertComparisonsByDigit(-T(0), D(0), Int(0))
104+
NBKAssertComparisonsByDigit(-T(0), -D(0), Int(0))
105+
106+
NBKAssertComparisonsByDigit( T(1), D(1), Int(0))
107+
NBKAssertComparisonsByDigit( T(1), -D(1), Int(1))
108+
NBKAssertComparisonsByDigit(-T(1), D(1), -Int(1))
109+
NBKAssertComparisonsByDigit(-T(1), -D(1), Int(0))
110+
111+
NBKAssertComparisonsByDigit( T(2), D(3), -Int(1))
112+
NBKAssertComparisonsByDigit( T(2), -D(3), Int(1))
113+
NBKAssertComparisonsByDigit(-T(2), D(3), -Int(1))
114+
NBKAssertComparisonsByDigit(-T(2), -D(3), Int(1))
115+
116+
NBKAssertComparisonsByDigit( T(3), D(2), Int(1))
117+
NBKAssertComparisonsByDigit( T(3), -D(2), Int(1))
118+
NBKAssertComparisonsByDigit(-T(3), D(2), -Int(1))
119+
NBKAssertComparisonsByDigit(-T(3), -D(2), -Int(1))
120+
}
121+
122+
//=------------------------------------------------------------------------=
123+
// MARK: Tests x Miscellaneous
124+
//=------------------------------------------------------------------------=
125+
126+
func testOverloadsAreUnambiguous() {
127+
func becauseThisCompilesSuccessfully(_ x: inout T) {
128+
XCTAssertNotNil(x.signum())
129+
}
130+
}
131+
132+
func testOverloadsAreUnambiguousWhenUsingIntegerLiterals() {
133+
func becauseThisCompilesSuccessfully(_ x: inout T) {
134+
XCTAssertNotNil(x.compared(to: 0))
135+
}
136+
}
137+
}
138+
139+
//*============================================================================*
140+
// MARK: * NBK x Signed x Comparisons x Assertions
141+
//*============================================================================*
142+
143+
private func NBKAssertSignum<M>(
144+
_ operand: NBKSigned<M>, _ signum: Int,
145+
file: StaticString = #file, line: UInt = #line) {
146+
XCTAssertEqual(Int(operand.signum() as Int), signum, file: file, line: line)
147+
}
148+
149+
private func NBKAssertComparisons<M>(
150+
_ lhs: NBKSigned<M>, _ rhs: NBKSigned<M>, _ signum: Int,
151+
file: StaticString = #file, line: UInt = #line) {
152+
XCTAssertEqual(lhs == rhs, signum == 0, file: file, line: line)
153+
XCTAssertEqual(lhs != rhs, signum != 0, file: file, line: line)
154+
155+
XCTAssertEqual(lhs < rhs, signum == -1, file: file, line: line)
156+
XCTAssertEqual(lhs <= rhs, signum != 1, file: file, line: line)
157+
158+
XCTAssertEqual(lhs > rhs, signum == 1, file: file, line: line)
159+
XCTAssertEqual(lhs >= rhs, signum != -1, file: file, line: line)
160+
161+
XCTAssertEqual(lhs.compared(to: rhs), signum, file: file, line: line)
162+
}
163+
164+
private func NBKAssertComparisonsByDigit<M>(
165+
_ lhs: NBKSigned<M>, _ rhs: NBKSigned<M>.Digit, _ signum: Int,
166+
file: StaticString = #file, line: UInt = #line) {
167+
//=------------------------------------------=
168+
NBKAssertComparisons(lhs, NBKSigned<M>(digit: rhs), signum)
169+
//=------------------------------------------=
170+
XCTAssertEqual(lhs.compared(to: rhs), signum, file: file, line: line)
171+
}
172+
173+
#endif

0 commit comments

Comments
 (0)