-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANKCoreInteger+Complements.swift
143 lines (106 loc) · 5.13 KB
/
ANKCoreInteger+Complements.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
//=----------------------------------------------------------------------------=
// This source file is part of the AwesomeNumbersKit open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
#if DEBUG
import ANKCoreKit
import XCTest
//*============================================================================*
// MARK: * ANK x Core Integer x Complements
//*============================================================================*
final class ANKCoreIntegerTestsOnComplements: XCTestCase {
typealias T = any ANKCoreInteger.Type
//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
let types: [T] = ANKCoreIntegerTests.types
//=------------------------------------------------------------------------=
// MARK: Tests x Bit Pattern
//=------------------------------------------------------------------------=
func testToBitPattern() {
func whereIsSigned<T>(_ type: T.Type) where T: ANKCoreInteger {
typealias M = T.Magnitude
XCTAssertEqual(T( 0).bitPattern, M.min)
XCTAssertEqual(T(-1).bitPattern, M.max)
XCTAssertEqual(T.min.bitPattern, (M(1) << (T.bitWidth - 1)))
XCTAssertEqual(T.max.bitPattern, ~(M(1) << (T.bitWidth - 1)))
}
func whereIsUnsigned<T>(_ type: T.Type) where T: ANKCoreInteger {
typealias M = T.Magnitude
XCTAssertEqual(T.min.bitPattern, M.min)
XCTAssertEqual(T.max.bitPattern, M.max)
}
for type: T in types {
type.isSigned ? whereIsSigned(type) : whereIsUnsigned(type)
}
}
func testFromBitPattern() {
func whereIsSigned<T>(_ type: T.Type) where T: ANKCoreInteger {
typealias M = T.Magnitude
XCTAssertEqual(M(bitPattern: T( 0)), M.min)
XCTAssertEqual(M(bitPattern: T(-1)), M.max)
XCTAssertEqual(T(bitPattern: M.min), T( 0))
XCTAssertEqual(T(bitPattern: M.max), T(-1))
XCTAssertEqual(T(bitPattern: (M(1) << (T.bitWidth - 1))), T.min)
XCTAssertEqual(T(bitPattern: ~(M(1) << (T.bitWidth - 1))), T.max)
}
func whereIsUnsigned<T>(_ type: T.Type) where T: ANKCoreInteger {
typealias M = T.Magnitude
XCTAssertEqual(T(bitPattern: M.min), T.min)
XCTAssertEqual(T(bitPattern: M.max), T.max)
}
for type: T in types {
type.isSigned ? whereIsSigned(type) : whereIsUnsigned(type)
}
}
//=------------------------------------------------------------------------=
// MARK: Tests x Magnitude
//=------------------------------------------------------------------------=
func testMagnitude() {
func whereIsSigned<T>(_ type: T.Type) where T: ANKCoreInteger {
typealias M = T.Magnitude
XCTAssertEqual(T(-1).magnitude, M(1))
XCTAssertEqual(T( 0).magnitude, M(0))
XCTAssertEqual(T( 1).magnitude, M(1))
XCTAssertEqual(T.min.magnitude, (M(1) << (M.bitWidth - 1)))
XCTAssertEqual(T.max.magnitude, ~(M(1) << (M.bitWidth - 1)))
}
func whereIsUnsigned<T>(_ type: T.Type) where T: ANKCoreInteger {
typealias M = T.Magnitude
XCTAssertEqual(T( 0).magnitude, M( 0))
XCTAssertEqual(T( 1).magnitude, M( 1))
XCTAssertEqual(T.min.magnitude, M.min)
XCTAssertEqual(T.max.magnitude, M.max)
}
for type: T in types {
type.isSigned ? whereIsSigned(type) : whereIsUnsigned(type)
}
}
//=------------------------------------------------------------------------=
// MARK: Tests x Two's Complement
//=------------------------------------------------------------------------=
func testTwosComplement() {
func whereIsSigned<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertTwosComplement(T(-1), T( 1))
ANKAssertTwosComplement(T( 0), T( 0))
ANKAssertTwosComplement(T( 1), T(-1))
ANKAssertTwosComplement(T.min, T.min, true)
ANKAssertTwosComplement(T.max, T.min + 1)
}
func whereIsUnsigned<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertTwosComplement(T( 1), T.max - 0)
ANKAssertTwosComplement(T( 2), T.max - 1)
ANKAssertTwosComplement(T( 3), T.max - 2)
ANKAssertTwosComplement(T.min, T.min, true)
ANKAssertTwosComplement(T.max, T.min + 1)
}
for type: T in types {
type.isSigned ? whereIsSigned(type) : whereIsUnsigned(type)
}
}
}
#endif