-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANKCoreInteger+Text.swift
210 lines (160 loc) · 6.76 KB
/
ANKCoreInteger+Text.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
//=----------------------------------------------------------------------------=
// 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 Text
//*============================================================================*
final class ANKCoreIntegerTestsOnText: XCTestCase {
typealias T = any ANKCoreInteger.Type
//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
let types: [T] = ANKCoreIntegerTests.types
//=------------------------------------------------------------------------=
// MARK: Tests x Decode
//=------------------------------------------------------------------------=
func testDecodingRadix10() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertDecodeText(T( 123), 10, "123")
ANKAssertDecodeText(T( 123), 10, "+123")
guard type.isSigned else { return }
ANKAssertDecodeText(T(-123), 10, "-123")
}
for type: T in types {
whereIs(type)
}
}
func testDecodingRadix16() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertDecodeText(T( 123), 16, "7b")
ANKAssertDecodeText(T( 123), 16, "+7b")
guard type.isSigned else { return }
ANKAssertDecodeText(T(-123), 16, "-7b")
}
for type: T in types {
whereIs(type)
}
}
func testDecodingRadixLiteralAsNumber() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertDecodeText(T( 33), 36, "0x")
ANKAssertDecodeText(T( 24), 36, "0o")
ANKAssertDecodeText(T( 11), 36, "0b")
ANKAssertDecodeText(T( 33), 36, "+0x")
ANKAssertDecodeText(T( 24), 36, "+0o")
ANKAssertDecodeText(T( 11), 36, "+0b")
guard type.isSigned else { return }
ANKAssertDecodeText(T(-33), 36, "-0x")
ANKAssertDecodeText(T(-24), 36, "-0o")
ANKAssertDecodeText(T(-11), 36, "-0b")
}
for type: T in types {
whereIs(type)
}
}
func testDecodingRadixLiteralAsRadixReturnsNil() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertDecodeText(T?.none, 10, "0x10")
ANKAssertDecodeText(T?.none, 10, "0o10")
ANKAssertDecodeText(T?.none, 10, "0b10")
ANKAssertDecodeText(T?.none, 10, "+0x10")
ANKAssertDecodeText(T?.none, 10, "+0o10")
ANKAssertDecodeText(T?.none, 10, "+0b10")
guard type.isSigned else { return }
ANKAssertDecodeText(T?.none, 10, "-0x10")
ANKAssertDecodeText(T?.none, 10, "-0o10")
ANKAssertDecodeText(T?.none, 10, "-0b10")
}
for type: T in types {
whereIs(type)
}
}
func testDecodingStringsWithAndWithoutSign() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertDecodeText(T( 123), 10, "123")
ANKAssertDecodeText(T( 123), 10, "+123")
guard type.isSigned else { return }
ANKAssertDecodeText(T(-123), 10, "-123")
}
for type: T in types {
whereIs(type)
}
}
func testDecodingPrefixingZerosHasNoEffect() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertDecodeText(T(0), 10, String(repeating: "0", count: 99) + "0")
ANKAssertDecodeText(T(1), 10, String(repeating: "0", count: 99) + "1")
}
for type: T in types {
whereIs(type)
}
}
func testDecodingStringsWithoutDigitsReturnsNil() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
ANKAssertDecodeText(T?.none, 10, "")
ANKAssertDecodeText(T?.none, 10, "+")
ANKAssertDecodeText(T?.none, 10, "-")
ANKAssertDecodeText(T?.none, 10, "~")
ANKAssertDecodeText(T?.none, 16, "")
ANKAssertDecodeText(T?.none, 16, "+")
ANKAssertDecodeText(T?.none, 16, "-")
ANKAssertDecodeText(T?.none, 16, "~")
}
for type: T in types {
whereIs(type)
}
}
func testDecodingValueOutsideOfRepresentableRangeReturnsNil() {
let positive = "+" + String(repeating: "1", count: 65)
let negative = "-" + String(repeating: "1", count: 65)
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
for radix in 2 ... 36 {
ANKAssertDecodeText(T?.none, radix, positive)
ANKAssertDecodeText(T?.none, radix, negative)
}
}
for type: T in types {
whereIs(type)
}
}
//=------------------------------------------------------------------------=
// MARK: Tests x Encode
//=------------------------------------------------------------------------=
func testEncodingRadix10() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
let max = T(123)
let min = T(exactly: -123)
ANKAssertEncodeText(max, 10, false, "123")
ANKAssertEncodeText(max, 10, true , "123")
guard let min else { return }
ANKAssertEncodeText(min, 10, false, "-123")
ANKAssertEncodeText(min, 10, true , "-123")
}
for type: T in types {
whereIs(type)
}
}
func testEncodingRadix16() {
func whereIs<T>(_ type: T.Type) where T: ANKCoreInteger {
let max = T(123)
let min = T(exactly: -123)
ANKAssertEncodeText(max, 16, false, "7b")
ANKAssertEncodeText(max, 16, true , "7B")
guard let min else { return }
ANKAssertEncodeText(min, 16, false, "-7b")
ANKAssertEncodeText(min, 16, true , "-7B")
}
for type: T in types {
whereIs(type)
}
}
}
#endif