-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKFlexibleWidth+Addition.swift
121 lines (100 loc) · 6.1 KB
/
NBKFlexibleWidth+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
//=----------------------------------------------------------------------------=
// 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 Addition x UIntXL
//*============================================================================*
final class NBKFlexibleWidthTestsOnAdditionAsUIntXL: XCTestCase {
typealias T = UIntXL
//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=
func testAdding() {
NBKAssertAddition(T(0), T(0), Int(0), T(0))
NBKAssertAddition(T(0), T(1), Int(0), T(1))
NBKAssertAddition(T(0), T(2), Int(0), T(2))
NBKAssertAddition(T(1), T(0), Int(0), T(1))
NBKAssertAddition(T(1), T(1), Int(0), T(2))
NBKAssertAddition(T(1), T(2), Int(0), T(3))
}
func testAddingAtIndex() {
NBKAssertAddition(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))
NBKAssertAddition(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))
NBKAssertAddition(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))
NBKAssertAddition(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))
NBKAssertAddition(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))
NBKAssertAddition(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))
NBKAssertAddition(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))
NBKAssertAddition(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))
}
//=------------------------------------------------------------------------=
// MARK: Tests x Digit
//=------------------------------------------------------------------------=
func testAddingDigit() {
NBKAssertAdditionByDigit(T(0), UInt(0), Int(0), T(0))
NBKAssertAdditionByDigit(T(0), UInt(1), Int(0), T(1))
NBKAssertAdditionByDigit(T(0), UInt(2), Int(0), T(2))
NBKAssertAdditionByDigit(T(1), UInt(0), Int(0), T(1))
NBKAssertAdditionByDigit(T(1), UInt(1), Int(0), T(2))
NBKAssertAdditionByDigit(T(1), UInt(2), Int(0), T(3))
}
func testAddingDigitAtIndex() {
NBKAssertAdditionByDigit(T(words:[ 0, 0, 0, 0] as X), UInt(3), Int(0), T(words:[ 3, 0, 0, 0] as X))
NBKAssertAdditionByDigit(T(words:[~0, 0, 0, 0] as X), UInt(3), Int(0), T(words:[ 2, 1, 0, 0] as X))
NBKAssertAdditionByDigit(T(words:[~0, ~0, 0, 0] as X), UInt(3), Int(0), T(words:[ 2, 0, 1, 0] as X))
NBKAssertAdditionByDigit(T(words:[~0, ~0, ~0, 0] as X), UInt(3), Int(0), T(words:[ 2, 0, 0, 1] as X))
NBKAssertAdditionByDigit(T(words:[ 0, 0, 0, 0] as X), UInt(3), Int(1), T(words:[ 0, 3, 0, 0] as X))
NBKAssertAdditionByDigit(T(words:[~0, 0, 0, 0] as X), UInt(3), Int(1), T(words:[~0, 3, 0, 0] as X))
NBKAssertAdditionByDigit(T(words:[~0, ~0, 0, 0] as X), UInt(3), Int(1), T(words:[~0, 2, 1, 0] as X))
NBKAssertAdditionByDigit(T(words:[~0, ~0, ~0, 0] as X), UInt(3), Int(1), T(words:[~0, 2, 0, 1] as X))
}
//=------------------------------------------------------------------------=
// MARK: Tests x Miscellaneous
//=------------------------------------------------------------------------=
func testOverloadsAreUnambiguousWhenUsingIntegerLiterals() {
func becauseThisCompilesSuccessfully(_ x: inout T) {
XCTAssertNotNil(x += 0)
XCTAssertNotNil(x.add(0, at: 0))
XCTAssertNotNil(x + 0)
XCTAssertNotNil(x.adding(0, at: 0))
}
}
}
//*============================================================================*
// MARK: * NBK x Flexible Width x Addition x Assertions
//*============================================================================*
private func NBKAssertAddition<T: IntXLOrUIntXL>(
_ lhs: T, _ rhs: T, _ index: Int, _ partialValue: T,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
if 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.adding(rhs, at: index), partialValue, file: file, line: line)
XCTAssertEqual({ var x = lhs; let _ = x.add(rhs, at: index); return x }(), partialValue, file: file, line: line)
}
private func NBKAssertAdditionByDigit<T: IntXLOrUIntXL>(
_ lhs: T, _ rhs: T.Digit, _ index: Int, _ partialValue: T,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
if 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.adding(rhs, at: index), partialValue, file: file, line: line)
XCTAssertEqual({ var x = lhs; let _ = x.add(rhs, at: index); return x }(), partialValue, file: file, line: line)
}