-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKFlexibleWidth+Words.swift
244 lines (211 loc) · 13.2 KB
/
NBKFlexibleWidth+Words.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//=----------------------------------------------------------------------------=
// 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 Words x UIntXL
//*============================================================================*
final class NBKFlexibleWidthTestsOnWordsAsUIntXL: XCTestCase {
typealias T = UIntXL
typealias M = UIntXL
//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=
func testFromWords() {
NBKAssertFromWordsIsSigned(Array( ), true, T( ))
NBKAssertFromWordsIsSigned(Array(T( ).words), true, T( ))
NBKAssertFromWordsIsSigned(Array(T.min256.words), true, T.min256)
NBKAssertFromWordsIsSigned(Array(T.max256.words), true, nil as T?)
NBKAssertFromWordsIsSigned(Array( ), false, T( ))
NBKAssertFromWordsIsSigned(Array(T( ).words), false, T( ))
NBKAssertFromWordsIsSigned(Array(T.min256.words), false, T.min256)
NBKAssertFromWordsIsSigned(Array(T.max256.words), false, T.max256)
NBKAssertFromWordsIsSigned(Array([~0/2 + 1]), true, nil as T?)
NBKAssertFromWordsIsSigned(Array([~0/2 + 0]), true, T(words:[~0/2 + 0]))
NBKAssertFromWordsIsSigned(Array([~0/2 + 1]), false, T(words:[~0/2 + 1]))
NBKAssertFromWordsIsSigned(Array([~0/2 + 0]), false, T(words:[~0/2 + 0]))
}
func testToWords() {
NBKAssertToWords(T(words:[0 ]), [0 ])
NBKAssertToWords(T(words:[1 ]), [1 ])
NBKAssertToWords(T(words:[1, 2 ]), [1, 2 ])
NBKAssertToWords(T(words:[1, 2, 3 ]), [1, 2, 3 ])
NBKAssertToWords(T(words:[1, 2, 3, 4]), [1, 2, 3, 4])
NBKAssertToWords(T(words:[0, 0, 0, 0]), [0 ])
NBKAssertToWords(T(words:[1, 0, 0, 0]), [1 ])
NBKAssertToWords(T(words:[1, 2, 0, 0]), [1, 2 ])
NBKAssertToWords(T(words:[1, 2, 3, 0]), [1, 2, 3 ])
NBKAssertToWords(T(words:[1, 2, 3, 4]), [1, 2, 3, 4])
}
/// ```
/// ┌──────────────────────── = ───────────────────────────┐
/// │ self │ words on a 64-bit machine │
/// ├──────────────────────── = ───────────────────────────┤
/// │ IntXL( ) │ [ 0 ] │
/// │ IntXL( Int256.max) │ [~0, ~0, ~0, ~0/2 + 0 ] │
/// │ IntXL( Int256.max) + 1 │ [ 0, 0, 0, ~0/2 + 1, 0] │
/// │ IntXL( Int256.min) │ [ 0, 0, 0, ~0/2 + 1 ] │
/// │ IntXL( Int256.min) + 1 │ [~0, ~0, ~0, ~0/2 + 0, ~0] │
/// │ IntXL(UInt256.max) │ [~0, ~0, ~0, ~0/1 + 0, 0] │
/// │ IntXL(UInt256.max) + 1 │ [ 0, 0, 0, 0/1 + 0, 1] │
/// ├──────────────────────── = ───────────────────────────┤
/// │ UIntXL( ) │ [ 0 ] │
/// │ UIntXL( Int256.max) │ [~0, ~0, ~0, ~0/2 + 0 ] │
/// │ UIntXL( Int256.max) + 1 │ [ 0, 0, 0, ~0/2 + 1 ] │
/// │ UIntXL(UInt256.max) │ [~0, ~0, ~0, ~0/1 + 0 ] │
/// │ UIntXL(UInt256.max) + 1 │ [ 0, 0, 0, 0/1 + 0, 1] │
/// └──────────────────────── = ───────────────────────────┘
/// ```
func testToWordsX64() throws {
guard MemoryLayout<UInt>.size == MemoryLayout<UInt64>.size else { throw XCTSkip() }
NBKAssertToWords(T(x64:[0 ] as X64), [0 ])
NBKAssertToWords(T(x64:[1 ] as X64), [1 ])
NBKAssertToWords(T(x64:[1, 2 ] as X64), [1, 2 ])
NBKAssertToWords(T(x64:[1, 2, 3 ] as X64), [1, 2, 3 ])
NBKAssertToWords(T(x64:[1, 2, 3, 4] as X64), [1, 2, 3, 4])
NBKAssertToWords(T(x64:[0, 0, 0, 0] as X64), [0 ])
NBKAssertToWords(T(x64:[1, 0, 0, 0] as X64), [1 ])
NBKAssertToWords(T(x64:[1, 2, 0, 0] as X64), [1, 2 ])
NBKAssertToWords(T(x64:[1, 2, 3, 0] as X64), [1, 2, 3 ])
NBKAssertToWords(T(x64:[1, 2, 3, 4] as X64), [1, 2, 3, 4])
NBKAssertToWords(T(x64:[~0, ~0, ~0, ~0/2] as X64), [~0, ~0, ~0, ~0/2 + 0 ] as X) // Int256.max
NBKAssertToWords(T(x64:[~0, ~0, ~0, ~0/2] as X64) + 1, [ 0, 0, 0, ~0/2 + 1 ] as X) // Int256.max + 1
NBKAssertToWords(T(x64:[~0, ~0, ~0, ~0/1] as X64), [~0, ~0, ~0, ~0/1 + 0 ] as X) // UInt256.max
NBKAssertToWords(T(x64:[~0, ~0, ~0, ~0/1] as X64) + 1, [ 0, 0, 0, 0/1 + 0, 1] as X) // UInt256.max + 1
}
func testToWordsX32() throws {
guard MemoryLayout<UInt>.size == MemoryLayout<UInt32>.size else { throw XCTSkip() }
NBKAssertToWords(T(x32:[0 ] as X32), [0 ])
NBKAssertToWords(T(x32:[1 ] as X32), [1 ])
NBKAssertToWords(T(x32:[1, 2 ] as X32), [1, 2 ])
NBKAssertToWords(T(x32:[1, 2, 3 ] as X32), [1, 2, 3 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4 ] as X32), [1, 2, 3, 4 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 5 ] as X32), [1, 2, 3, 4, 5 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 5, 6 ] as X32), [1, 2, 3, 4, 5, 6 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 5, 6, 7 ] as X32), [1, 2, 3, 4, 5, 6, 7 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 5, 6, 7, 8] as X32), [1, 2, 3, 4, 5, 6, 7, 8])
NBKAssertToWords(T(x32:[0, 0, 0, 0, 0, 0, 0, 0] as X32), [0 ])
NBKAssertToWords(T(x32:[1, 0, 0, 0, 0, 0, 0, 0] as X32), [1 ])
NBKAssertToWords(T(x32:[1, 2, 0, 0, 0, 0, 0, 0] as X32), [1, 2 ])
NBKAssertToWords(T(x32:[1, 2, 3, 0, 0, 0, 0, 0] as X32), [1, 2, 3 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 0, 0, 0, 0] as X32), [1, 2, 3, 4 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 5, 0, 0, 0] as X32), [1, 2, 3, 4, 5 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 5, 6, 0, 0] as X32), [1, 2, 3, 4, 5, 6 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 5, 6, 7, 0] as X32), [1, 2, 3, 4, 5, 6, 7 ])
NBKAssertToWords(T(x32:[1, 2, 3, 4, 5, 6, 7, 8] as X32), [1, 2, 3, 4, 5, 6, 7, 8])
}
//=------------------------------------------------------------------------=
// MARK: Tests x Miscellaneous
//=------------------------------------------------------------------------=
func testSubscriptSignExtension() {
XCTAssertEqual(T(words:[1, 2, 3, 4] as X)[Int( 0)], 1 as UInt)
XCTAssertEqual(T(words:[1, 2, 3, 4] as X)[Int( 1)], 2 as UInt)
XCTAssertEqual(T(words:[1, 2, 3, 4] as X)[Int( 2)], 3 as UInt)
XCTAssertEqual(T(words:[1, 2, 3, 4] as X)[Int( 3)], 4 as UInt)
XCTAssertEqual(T(words:[1, 2, 3, 4] as X)[Int( 4)], 0 as UInt)
XCTAssertEqual(T(words:[1, 2, 3, 4] as X)[Int.max], 0 as UInt)
}
}
//*============================================================================*
// MARK: * NBK x Flexible Width x Words x Assertions
//*============================================================================*
private func NBKAssertFromWords<T: IntXLOrUIntXL>(
_ words: [UInt], _ integer: T?,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
XCTAssertEqual( T(words: words), integer, file: file, line: line)
XCTAssertEqual(integer.flatMap({ T(words: $0.words) }), integer, file: file, line: line)
//=------------------------------------------=
if let integer {
NBKAssertFromWordsByTruncating(words, words.count, integer)
}
}
private func NBKAssertFromWordsIsSigned<T: IntXLOrUIntXL>(
_ words: [UInt], _ isSigned: Bool, _ integer: T?,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
if isSigned == T.isSigned {
NBKAssertFromWords(words, integer, file: file, line: line)
}
//=------------------------------------------=
XCTAssertEqual( T(words: words, isSigned: isSigned), integer, file: file, line: line)
XCTAssertEqual(integer.flatMap({ T(words: $0.words, isSigned: isSigned) }), integer, file: file, line: line)
//=------------------------------------------=
if let integer {
NBKAssertFromWordsByTruncating(words, words.count, integer)
}
}
private func NBKAssertFromWordsByTruncating<T: IntXLOrUIntXL>(
_ words: [UInt], _ count: Int, _ integer: T,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
if count == words.count {
let initialized = T.uninitialized(count: count, init:{ _ = $0.initialize(from: words) })
XCTAssertEqual(initialized, integer, file: file, line: line)
}
brr: do {
let capacity: Int = count
let initialized = T.uninitialized(capacity: capacity, init:{ $1 = $0.initialize(from: words.prefix(count)).1 })
XCTAssertEqual(initialized, integer, file: file, line: line)
}
brr: do {
let capacity: Int = count + 1
let initialized = T.uninitialized(capacity: capacity, init:{ $1 = $0.initialize(from: words.prefix(count)).1 })
XCTAssertEqual(initialized, integer, file: file, line: line)
}
}
private func NBKAssertToWords<T: IntXLOrUIntXL>(
_ integer: T, _ words: [UInt],
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
var integer = integer
//=------------------------------------------=
NBKAssertFromWords(words, integer, file: file, line: line)
//=------------------------------------------=
NBKAssertElementsEqual(integer.words, words, file: file, line: line)
integer.withUnsafeBufferPointer({ NBKAssertElementsEqual($0, words, file: file, line: line) })
integer.withUnsafeMutableBufferPointer({ NBKAssertElementsEqual($0, words, file: file, line: line) })
}
//=----------------------------------------------------------------------------=
// MARK: + Collection
//=----------------------------------------------------------------------------=
private func NBKAssertElementsEqual<Base: RandomAccessCollection>(
_ base: Base, _ expectation: [Base.Element],
file: StaticString = #file, line: UInt = #line) where Base.Element: Equatable {
//=------------------------------------------=
XCTAssertEqual(Array(base), expectation, file: file, line: line)
XCTAssertEqual(Array(base.indices.map({ base[$0] })), expectation, file: file, line: line)
//=------------------------------------------=
for distance in 0 ..< base.count {
//=--------------------------------------=
let index0 = base.index(base.startIndex, offsetBy: distance + 0)
let index1 = base.index(base.startIndex, offsetBy: distance + 1)
//=--------------------------------------=
XCTAssertEqual(base[index0],expectation[distance], file: file, line: line)
//=--------------------------------------=
XCTAssertEqual(base.index(before: index1), index0, file: file, line: line)
XCTAssertEqual(base.index(after: index0), index1, file: file, line: line)
XCTAssertEqual(base.index(base.endIndex, offsetBy: distance + 0 - base.count), index0, file: file, line: line)
XCTAssertEqual(base.index(base.endIndex, offsetBy: distance + 1 - base.count), index1, file: file, line: line)
//=--------------------------------------=
XCTAssertEqual(base.distance(from: base.startIndex, to: index0), distance + 0, file: file, line: line)
XCTAssertEqual(base.distance(from: base.startIndex, to: index1), distance + 1, file: file, line: line)
XCTAssertEqual(base.distance(from: index0, to: base.endIndex), base.count - distance - 0, file: file, line: line)
XCTAssertEqual(base.distance(from: index1, to: base.endIndex), base.count - distance - 1, file: file, line: line)
}
//=------------------------------------------=
for distance in 0 ... base.count + 1 {
XCTAssert(base.prefix(distance).elementsEqual(expectation.prefix(distance)), file: file, line: line)
XCTAssert(base.suffix(distance).elementsEqual(expectation.suffix(distance)), file: file, line: line)
}
}