-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKFlexibleWidth+Subtraction.swift
180 lines (151 loc) · 10.5 KB
/
NBKFlexibleWidth+Subtraction.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//=----------------------------------------------------------------------------=
// 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
import NBKFlexibleWidthKit
import XCTest
private typealias X = [UInt]
private typealias X64 = [UInt64]
private typealias X32 = [UInt32]
//*============================================================================*
// MARK: * NBK x Flexible Width x Subtraction x UIntXL
//*============================================================================*
final class NBKFlexibleWidthTestsOnSubtractionAsUIntXL: XCTestCase {
typealias T = UIntXL
//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=
func testSubtracting() {
NBKAssertSubtraction(T(3), T(0), Int(0), T(3))
NBKAssertSubtraction(T(3), T(1), Int(0), T(2))
NBKAssertSubtraction(T(3), T(2), Int(0), T(1))
NBKAssertSubtraction(T(3), T(3), Int(0), T(0))
}
func testSubtractingReportingOverflow() {
NBKAssertSubtraction(T(1), T(0), Int(0), T(words:[ 1] as X))
NBKAssertSubtraction(T(1), T(1), Int(0), T(words:[ 0] as X))
NBKAssertSubtraction(T(1), T(2), Int(0), T(words:[~0] as X), true)
NBKAssertSubtraction(T(1), T(3), Int(0), T(words:[~1] as X), true)
}
func testSubtractingAtIndex() {
NBKAssertSubtraction(T(words:[~0, ~0, ~0, ~0] as X), T(words:[ 1, 2, 3, 0] as X), Int(0), T(words:[~1, ~2, ~3, ~0] as X))
NBKAssertSubtraction(T(words:[ 0, ~0, ~0, ~0] as X), T(words:[ 1, 2, 3, 0] as X), Int(0), T(words:[~0, ~3, ~3, ~0] as X))
NBKAssertSubtraction(T(words:[ 0, 0, ~0, ~0] as X), T(words:[ 1, 2, 3, 0] as X), Int(0), T(words:[~0, ~2, ~4, ~0] as X))
NBKAssertSubtraction(T(words:[ 0, 0, 0, ~0] as X), T(words:[ 1, 2, 3, 0] as X), Int(0), T(words:[~0, ~2, ~3, ~1] as X))
NBKAssertSubtraction(T(words:[~0, ~0, ~0, ~0] as X), T(words:[ 1, 2, 3, 0] as X), Int(1), T(words:[~0, ~1, ~2, ~3] as X))
NBKAssertSubtraction(T(words:[ 0, ~0, ~0, ~0] as X), T(words:[ 1, 2, 3, 0] as X), Int(1), T(words:[ 0, ~1, ~2, ~3] as X))
NBKAssertSubtraction(T(words:[ 0, 0, ~0, ~0] as X), T(words:[ 1, 2, 3, 0] as X), Int(1), T(words:[ 0, ~0, ~3, ~3] as X))
NBKAssertSubtraction(T(words:[ 0, 0, 0, ~0] as X), T(words:[ 1, 2, 3, 0] as X), Int(1), T(words:[ 0, ~0, ~2, ~4] as X))
}
func testSubtractingAtIndexReportingOverflow() {
NBKAssertSubtraction(T(words:[ 1, 2, 3, 0] as X), T(words:[ 4, 5, 0, 0] as X), Int(0), T(words:[~2, ~3, 2, 0] as X))
NBKAssertSubtraction(T(words:[ 1, 2, 3, 0] as X), T(words:[ 4, 5, 0, 0] as X), Int(1), T(words:[ 1, ~1, ~2, 0] as X), true)
NBKAssertSubtraction(T(words:[ 1, 2, 3, 0] as X), T(words:[ 4, 5, 0, 0] as X), Int(2), T(words:[ 1, 2, ~0, ~5] as X), true)
}
//=------------------------------------------------------------------------=
// MARK: Tests x Digit
//=------------------------------------------------------------------------=
func testSubtractingDigit() {
NBKAssertSubtractionByDigit(T(3), UInt(0), Int(0), T(3))
NBKAssertSubtractionByDigit(T(3), UInt(1), Int(0), T(2))
NBKAssertSubtractionByDigit(T(3), UInt(2), Int(0), T(1))
NBKAssertSubtractionByDigit(T(3), UInt(3), Int(0), T(0))
}
func testSubtractingDigitReportingOverflow() {
NBKAssertSubtractionByDigit(T(1), UInt(0), Int(0), T(words:[ 1] as X))
NBKAssertSubtractionByDigit(T(1), UInt(1), Int(0), T(words:[ 0] as X))
NBKAssertSubtractionByDigit(T(1), UInt(2), Int(0), T(words:[~0] as X), true)
NBKAssertSubtractionByDigit(T(1), UInt(3), Int(0), T(words:[~1] as X), true)
}
func testSubtractingDigitAtIndex() {
NBKAssertSubtractionByDigit(T(words:[~0, ~0, ~0, ~0] as X), UInt(3), Int(0), T(words:[~3, ~0, ~0, ~0] as X))
NBKAssertSubtractionByDigit(T(words:[ 0, ~0, ~0, ~0] as X), UInt(3), Int(0), T(words:[~2, ~1, ~0, ~0] as X))
NBKAssertSubtractionByDigit(T(words:[ 0, 0, ~0, ~0] as X), UInt(3), Int(0), T(words:[~2, ~0, ~1, ~0] as X))
NBKAssertSubtractionByDigit(T(words:[ 0, 0, 0, ~0] as X), UInt(3), Int(0), T(words:[~2, ~0, ~0, ~1] as X))
NBKAssertSubtractionByDigit(T(words:[~0, ~0, ~0, ~0] as X), UInt(3), Int(1), T(words:[~0, ~3, ~0, ~0] as X))
NBKAssertSubtractionByDigit(T(words:[ 0, ~0, ~0, ~0] as X), UInt(3), Int(1), T(words:[ 0, ~3, ~0, ~0] as X))
NBKAssertSubtractionByDigit(T(words:[ 0, 0, ~0, ~0] as X), UInt(3), Int(1), T(words:[ 0, ~2, ~1, ~0] as X))
NBKAssertSubtractionByDigit(T(words:[ 0, 0, 0, ~0] as X), UInt(3), Int(1), T(words:[ 0, ~2, ~0, ~1] as X))
}
func testSubtractingDigitAtIndexReportingOverflow() {
NBKAssertSubtractionByDigit(T(words:[ 1, 2, 3, 0] as X), UInt(5), Int(0), T(words:[~3, 1, 3, 0] as X))
NBKAssertSubtractionByDigit(T(words:[ 1, 2, 3, 0] as X), UInt(5), Int(1), T(words:[ 1, ~2, 2, 0] as X))
NBKAssertSubtractionByDigit(T(words:[ 1, 2, 3, 0] as X), UInt(5), Int(2), T(words:[ 1, 2, ~1, 0] as X), true)
NBKAssertSubtractionByDigit(T(words:[ 1, 2, 3, 0] as X), UInt(5), Int(3), T(words:[ 1, 2, 3, ~4] as X), true)
}
//=------------------------------------------------------------------------=
// MARK: Tests x Miscellaneous
//=------------------------------------------------------------------------=
func testOverloadsAreUnambiguousWhenUsingIntegerLiterals() {
func becauseThisCompilesSuccessfully(_ x: inout T) {
XCTAssertNotNil(x -= 0)
XCTAssertNotNil(x.subtract(0, at: 0))
XCTAssertNotNil(x.subtractReportingOverflow(0, at: 0))
XCTAssertNotNil(x - 0)
XCTAssertNotNil(x.subtracting(0, at: 0))
XCTAssertNotNil(x.subtractingReportingOverflow(0, at: 0))
}
}
}
//*============================================================================*
// MARK: * NBK x Flexible Width x Subtraction x Assertions
//*============================================================================*
private func NBKAssertSubtraction<T: IntXLOrUIntXL>(
_ lhs: T, _ rhs: T, _ index: Int, _ partialValue: T, _ overflow: Bool = false,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
if !overflow, index.isZero {
XCTAssertEqual( lhs - rhs, partialValue, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs -= rhs; return lhs }(), partialValue, file: file, line: line)
XCTAssertEqual(lhs.subtracting(rhs, at: Int.zero), partialValue, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs.subtract(rhs, at: Int.zero); return lhs }(), partialValue, file: file, line: line)
}
//=------------------------------------------=
guard let lhs = lhs as? UIntXL, let rhs = rhs as? UIntXL, let partialValue = partialValue as? UIntXL else {
return precondition(T.isSigned)
}
//=------------------------------------------=
if index.isZero {
XCTAssertEqual(lhs.subtractingReportingOverflow(rhs).partialValue, partialValue, file: file, line: line)
XCTAssertEqual(lhs.subtractingReportingOverflow(rhs).overflow, overflow, file: file, line: line)
XCTAssertEqual({ var x = lhs; let _ = x.subtractReportingOverflow(rhs); return x }(), partialValue, file: file, line: line)
XCTAssertEqual({ var x = lhs; let o = x.subtractReportingOverflow(rhs); return o }(), overflow, file: file, line: line)
}
//=------------------------------------------=
XCTAssertEqual(lhs.subtractingReportingOverflow(rhs, at: index).partialValue, partialValue, file: file, line: line)
XCTAssertEqual(lhs.subtractingReportingOverflow(rhs, at: index).overflow, overflow, file: file, line: line)
XCTAssertEqual({ var x = lhs; let _ = x.subtractReportingOverflow(rhs, at: index); return x }(), partialValue, file: file, line: line)
XCTAssertEqual({ var x = lhs; let o = x.subtractReportingOverflow(rhs, at: index); return o }(), overflow, file: file, line: line)
}
private func NBKAssertSubtractionByDigit<T: IntXLOrUIntXL>(
_ lhs: T, _ rhs: T.Digit, _ index: Int, _ partialValue: T, _ overflow: Bool = false,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
if !overflow, index.isZero {
XCTAssertEqual( lhs - rhs, partialValue, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs -= rhs; return lhs }(), partialValue, file: file, line: line)
XCTAssertEqual(lhs.subtracting(rhs, at: Int.zero), partialValue, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs.subtract(rhs, at: Int.zero); return lhs }(), partialValue, file: file, line: line)
}
//=------------------------------------------=
guard let lhs = lhs as? UIntXL, let rhs = rhs as? UIntXL.Digit, let partialValue = partialValue as? UIntXL else {
return precondition(T.isSigned)
}
//=------------------------------------------=
if index.isZero {
XCTAssertEqual(lhs.subtractingReportingOverflow(rhs).partialValue, partialValue, file: file, line: line)
XCTAssertEqual(lhs.subtractingReportingOverflow(rhs).overflow, overflow, file: file, line: line)
XCTAssertEqual({ var x = lhs; let _ = x.subtractReportingOverflow(rhs); return x }(), partialValue, file: file, line: line)
XCTAssertEqual({ var x = lhs; let o = x.subtractReportingOverflow(rhs); return o }(), overflow, file: file, line: line)
}
//=------------------------------------------=
XCTAssertEqual(lhs.subtractingReportingOverflow(rhs, at: index).partialValue, partialValue, file: file, line: line)
XCTAssertEqual(lhs.subtractingReportingOverflow(rhs, at: index).overflow, overflow, file: file, line: line)
XCTAssertEqual({ var x = lhs; let _ = x.subtractReportingOverflow(rhs, at: index); return x }(), partialValue, file: file, line: line)
XCTAssertEqual({ var x = lhs; let o = x.subtractReportingOverflow(rhs, at: index); return o }(), overflow, file: file, line: line)
}