-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKDoubleWidth+Addition.swift
217 lines (177 loc) · 11 KB
/
NBKDoubleWidth+Addition.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//=----------------------------------------------------------------------------=
// 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 NBKDoubleWidthKit
import XCTest
private typealias X64 = NBK.U256X64
private typealias X32 = NBK.U256X32
//*============================================================================*
// MARK: * NBK x Double Width x Addition x Int256
//*============================================================================*
final class NBKDoubleWidthTestsOnAdditionAsInt256: XCTestCase {
typealias T = Int256
//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=
func testAddingLargeToLarge() {
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), T(x64: X64(3, 0, 0, 0)), T(x64: X64( 2, 0, 0, 1)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), T(x64: X64(0, 3, 0, 0)), T(x64: X64(~0, 2, 0, 1)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), T(x64: X64(0, 0, 3, 0)), T(x64: X64(~0, ~0, 2, 1)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), T(x64: X64(0, 0, 0, 3)), T(x64: X64(~0, ~0, ~0, 3)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), -T(x64: X64(3, 0, 0, 0)), T(x64: X64(~3, ~0, ~0, 0)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), -T(x64: X64(0, 3, 0, 0)), T(x64: X64(~0, ~3, ~0, 0)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), -T(x64: X64(0, 0, 3, 0)), T(x64: X64(~0, ~0, ~3, 0)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), -T(x64: X64(0, 0, 0, 3)), T(x64: X64(~0, ~0, ~0, ~2)))
NBKAssertAddition(T(x64: X64( 0, 0, 0, ~0)), T(x64: X64(3, 0, 0, 0)), T(x64: X64( 3, 0, 0, ~0)))
NBKAssertAddition(T(x64: X64( 0, 0, 0, ~0)), T(x64: X64(0, 3, 0, 0)), T(x64: X64( 0, 3, 0, ~0)))
NBKAssertAddition(T(x64: X64( 0, 0, 0, ~0)), T(x64: X64(0, 0, 3, 0)), T(x64: X64( 0, 0, 3, ~0)))
NBKAssertAddition(T(x64: X64( 0, 0, 0, ~0)), T(x64: X64(0, 0, 0, 3)), T(x64: X64( 0, 0, 0, 2)))
NBKAssertAddition(T(x64: X64( 0, 0, 0, ~0)), -T(x64: X64(3, 0, 0, 0)), T(x64: X64(~2, ~0, ~0, ~1)))
NBKAssertAddition(T(x64: X64( 0, 0, 0, ~0)), -T(x64: X64(0, 3, 0, 0)), T(x64: X64( 0, ~2, ~0, ~1)))
NBKAssertAddition(T(x64: X64( 0, 0, 0, ~0)), -T(x64: X64(0, 0, 3, 0)), T(x64: X64( 0, 0, ~2, ~1)))
NBKAssertAddition(T(x64: X64( 0, 0, 0, ~0)), -T(x64: X64(0, 0, 0, 3)), T(x64: X64( 0, 0, 0, ~3)))
}
func testAddingHalvesToHalvesReportingOverflow() {
NBKAssertAddition(T(high: .max, low: .max), T(-1), T(high: .max, low: .max - 1)) // carry 1st
NBKAssertAddition(T(high: .min, low: .max), T(-1), T(high: .min, low: .max - 1)) // carry 2nd
}
//=------------------------------------------------------------------------=
// MARK: Tests x Digit (and Self)
//=------------------------------------------------------------------------=
func testAddingSmallToSmall() {
NBKAssertAdditionByDigit(T( 1), Int( 2), T( 3))
NBKAssertAdditionByDigit(T( 1), Int( 1), T( 2))
NBKAssertAdditionByDigit(T( 1), Int( 0), T( 1))
NBKAssertAdditionByDigit(T( 1), Int(-1), T( 0))
NBKAssertAdditionByDigit(T( 1), Int(-2), T(-1))
NBKAssertAdditionByDigit(T( 0), Int( 2), T( 2))
NBKAssertAdditionByDigit(T( 0), Int( 1), T( 1))
NBKAssertAdditionByDigit(T( 0), Int( 0), T( 0))
NBKAssertAdditionByDigit(T( 0), Int(-1), T(-1))
NBKAssertAdditionByDigit(T( 0), Int(-2), T(-2))
NBKAssertAdditionByDigit(T(-1), Int( 2), T( 1))
NBKAssertAdditionByDigit(T(-1), Int( 1), T( 0))
NBKAssertAdditionByDigit(T(-1), Int( 0), T(-1))
NBKAssertAdditionByDigit(T(-1), Int(-1), T(-2))
NBKAssertAdditionByDigit(T(-1), Int(-2), T(-3))
}
func testAddingSmallToLarge() {
NBKAssertAdditionByDigit(T(x64: X64(~0, ~0, ~0, 0)), Int(3), T(x64: X64( 2, 0, 0, 1)))
NBKAssertAdditionByDigit(T(x64: X64(~0, ~0, ~0, 0)), -Int(3), T(x64: X64(~3, ~0, ~0, 0)))
NBKAssertAdditionByDigit(T(x64: X64( 0, 0, 0, ~0)), Int(3), T(x64: X64( 3, 0, 0, ~0)))
NBKAssertAdditionByDigit(T(x64: X64( 0, 0, 0, ~0)), -Int(3), T(x64: X64(~2, ~0, ~0, ~1)))
}
func testAddingSmallToEdgesReportingOverflow() {
NBKAssertAdditionByDigit(T.min, Int( 1), T.min + T(1))
NBKAssertAdditionByDigit(T.min, Int(-1), T.max, true)
NBKAssertAdditionByDigit(T.max, Int( 1), T.min, true)
NBKAssertAdditionByDigit(T.max, Int(-1), T.max - T(1))
}
//=------------------------------------------------------------------------=
// MARK: Tests x Miscellaneous
//=------------------------------------------------------------------------=
func testOverloadsAreUnambiguousWhenUsingIntegerLiterals() {
func becauseThisCompilesSuccessfully(_ x: inout T) {
XCTAssertNotNil(x += 0)
XCTAssertNotNil(x &+= 0)
XCTAssertNotNil(x.addReportingOverflow(0))
XCTAssertNotNil(x + 0)
XCTAssertNotNil(x &+ 0)
XCTAssertNotNil(x.addingReportingOverflow(0))
}
}
}
//*============================================================================*
// MARK: * NBK x Double Width x Addition x UInt256
//*============================================================================*
final class NBKDoubleWidthTestsOnAdditionAsUInt256: XCTestCase {
typealias T = UInt256
//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=
func testAddingLargeToLarge() {
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), T(x64: X64(3, 0, 0, 0)), T(x64: X64( 2, 0, 0, 1)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), T(x64: X64(0, 3, 0, 0)), T(x64: X64(~0, 2, 0, 1)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), T(x64: X64(0, 0, 3, 0)), T(x64: X64(~0, ~0, 2, 1)))
NBKAssertAddition(T(x64: X64(~0, ~0, ~0, 0)), T(x64: X64(0, 0, 0, 3)), T(x64: X64(~0, ~0, ~0, 3)))
}
//=------------------------------------------------------------------------=
// MARK: Tests x Digit (and Self)
//=------------------------------------------------------------------------=
func testAddingSmallToSmall() {
NBKAssertAdditionByDigit(T(0), UInt(0), T(0))
NBKAssertAdditionByDigit(T(0), UInt(1), T(1))
NBKAssertAdditionByDigit(T(0), UInt(2), T(2))
NBKAssertAdditionByDigit(T(1), UInt(0), T(1))
NBKAssertAdditionByDigit(T(1), UInt(1), T(2))
NBKAssertAdditionByDigit(T(1), UInt(2), T(3))
}
func testAddingSmallToLarge() {
NBKAssertAdditionByDigit(T(x64: X64( 0, 0, 0, 0)), UInt(3), T(x64: X64(3, 0, 0, 0)))
NBKAssertAdditionByDigit(T(x64: X64(~0, 0, 0, 0)), UInt(3), T(x64: X64(2, 1, 0, 0)))
NBKAssertAdditionByDigit(T(x64: X64(~0, ~0, 0, 0)), UInt(3), T(x64: X64(2, 0, 1, 0)))
NBKAssertAdditionByDigit(T(x64: X64(~0, ~0, ~0, 0)), UInt(3), T(x64: X64(2, 0, 0, 1)))
}
func testAddingSmallToEdgesReportingOverflow() {
NBKAssertAdditionByDigit(T.min, UInt(1), T.min + T(1))
NBKAssertAdditionByDigit(T.max, UInt(1), T.min, true)
}
//=------------------------------------------------------------------------=
// MARK: Tests x Miscellaneous
//=------------------------------------------------------------------------=
func testOverloadsAreUnambiguousWhenUsingIntegerLiterals() {
func becauseThisCompilesSuccessfully(_ x: inout T) {
XCTAssertNotNil(x += 0)
XCTAssertNotNil(x &+= 0)
XCTAssertNotNil(x.addReportingOverflow(0))
XCTAssertNotNil(x + 0)
XCTAssertNotNil(x &+ 0)
XCTAssertNotNil(x.addingReportingOverflow(0))
}
}
}
//*============================================================================*
// MARK: * NBK x Double Width x Addition x Assertions
//*============================================================================*
private func NBKAssertAddition<H: NBKFixedWidthInteger>(
_ lhs: NBKDoubleWidth<H>, _ rhs: NBKDoubleWidth<H>,
_ partialValue: NBKDoubleWidth<H>, _ overflow: Bool = false,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
if !overflow {
XCTAssertEqual( lhs + rhs, partialValue, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs += rhs; return lhs }(), partialValue, file: file, line: line)
}
//=------------------------------------------=
XCTAssertEqual( lhs &+ rhs, partialValue, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs &+= rhs; return lhs }(), partialValue, file: file, line: line)
XCTAssertEqual(lhs.addingReportingOverflow(rhs).partialValue, partialValue, file: file, line: line)
XCTAssertEqual(lhs.addingReportingOverflow(rhs).overflow, overflow, file: file, line: line)
XCTAssertEqual({ var x = lhs; let _ = x.addReportingOverflow(rhs); return x }(), partialValue, file: file, line: line)
XCTAssertEqual({ var x = lhs; let o = x.addReportingOverflow(rhs); return o }(), overflow, file: file, line: line)
}
private func NBKAssertAdditionByDigit<H: NBKFixedWidthInteger>(
_ lhs: NBKDoubleWidth<H>, _ rhs: NBKDoubleWidth<H>.Digit,
_ partialValue: NBKDoubleWidth<H>, _ overflow: Bool = false,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
NBKAssertAddition(lhs, NBKDoubleWidth<H>(digit: rhs), partialValue, overflow, file: file, line: line)
//=------------------------------------------=
if !overflow {
XCTAssertEqual( lhs + rhs, partialValue, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs += rhs; return lhs }(), partialValue, file: file, line: line)
}
//=------------------------------------------=
XCTAssertEqual( lhs &+ rhs, partialValue, file: file, line: line)
XCTAssertEqual({ var lhs = lhs; lhs &+= rhs; return lhs }(), partialValue, file: file, line: line)
XCTAssertEqual(lhs.addingReportingOverflow(rhs).partialValue, partialValue, file: file, line: line)
XCTAssertEqual(lhs.addingReportingOverflow(rhs).overflow, overflow, file: file, line: line)
XCTAssertEqual({ var x = lhs; let _ = x.addReportingOverflow(rhs); return x }(), partialValue, file: file, line: line)
XCTAssertEqual({ var x = lhs; let o = x.addReportingOverflow(rhs); return o }(), overflow, file: file, line: line)
}