5
5
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
6
6
//
7
7
//===----------------------------------------------------------------------===//
8
- //
9
- // RUN: %target-run-simple-swift
10
- // REQUIRES: executable_test
11
8
12
- import XCTest
9
+ import Testing
13
10
14
11
#if canImport(FoundationEssentials)
15
12
@testable import FoundationEssentials
16
- #endif
17
-
18
- #if FOUNDATION_FRAMEWORK
13
+ #else
19
14
@testable import Foundation
20
15
#endif
21
16
@@ -27,16 +22,17 @@ import Numberick
27
22
import BigInt
28
23
#endif
29
24
30
- final class BinaryIntegerFormatStyleTests : XCTestCase {
25
+ @Suite ( " BinaryIntegerFormatStyle " )
26
+ private struct BinaryIntegerFormatStyleTests {
31
27
// NSR == numericStringRepresentation
32
- func checkNSR( value: some BinaryInteger , expected: String ) {
33
- XCTAssertEqual ( String ( decoding: value. numericStringRepresentation. utf8, as: Unicode . ASCII. self) , expected)
28
+ func checkNSR( value: some BinaryInteger , expected: String , sourceLocation : SourceLocation = #_sourceLocation ) {
29
+ #expect ( String ( decoding: value. numericStringRepresentation. utf8, as: Unicode . ASCII. self) == expected)
34
30
}
35
31
36
- func testNumericStringRepresentation_builtinIntegersLimits ( ) throws {
37
- func check< I: FixedWidthInteger > ( type: I . Type = I . self, min: String , max: String ) {
38
- checkNSR ( value: I . min, expected: min)
39
- checkNSR ( value: I . max, expected: max)
32
+ @ Test func numericStringRepresentation_builtinIntegersLimits ( ) throws {
33
+ func check< I: FixedWidthInteger > ( type: I . Type = I . self, min: String , max: String , sourceLocation : SourceLocation = #_sourceLocation ) {
34
+ checkNSR ( value: I . min, expected: min, sourceLocation : sourceLocation )
35
+ checkNSR ( value: I . max, expected: max, sourceLocation : sourceLocation )
40
36
}
41
37
42
38
check ( type: Int8 . self, min: " -128 " , max: " 127 " )
@@ -52,13 +48,13 @@ final class BinaryIntegerFormatStyleTests: XCTestCase {
52
48
check ( type: UInt128 . self, min: " 0 " , max: " 340282366920938463463374607431768211455 " )
53
49
}
54
50
55
- func testNumericStringRepresentation_builtinIntegersAroundDecimalMagnitude ( ) throws {
56
- func check< I: FixedWidthInteger > ( type: I . Type = I . self, magnitude: String , oneLess: String , oneMore: String ) {
51
+ @Test func numericStringRepresentation_builtinIntegersAroundDecimalMagnitude ( ) throws {
52
+ func check< I: FixedWidthInteger > ( type: I . Type = I . self, magnitude: String , oneLess: String , oneMore: String , sourceLocation : SourceLocation = #_sourceLocation ) {
57
53
var mag = I ( 1 ) ; while !mag. multipliedReportingOverflow ( by: 10 ) . overflow { mag *= 10 }
58
54
59
- checkNSR ( value: mag, expected: magnitude)
60
- checkNSR ( value: mag - 1 , expected: oneLess)
61
- checkNSR ( value: mag + 1 , expected: oneMore)
55
+ checkNSR ( value: mag, expected: magnitude, sourceLocation : sourceLocation )
56
+ checkNSR ( value: mag - 1 , expected: oneLess, sourceLocation : sourceLocation )
57
+ checkNSR ( value: mag + 1 , expected: oneMore, sourceLocation : sourceLocation )
62
58
}
63
59
64
60
check ( type: Int8 . self, magnitude: " 100 " , oneLess: " 99 " , oneMore: " 101 " )
@@ -105,22 +101,22 @@ final class BinaryIntegerFormatStyleTests: XCTestCase {
105
101
String ( repeating: " 1234567890 " , count: 10 ) ,
106
102
String ( repeating: " 1234567890 " , count: 100 ) ] {
107
103
if let value = initialiser ( valueAsString) { // The test cases cover a wide range of values, that don't all fit into every type tested (i.e. the fixed-width types from Numberick).
108
- XCTAssertEqual ( value. description, valueAsString) // Sanity check that it initialised from the string correctly.
104
+ #expect ( value. description == valueAsString) // Sanity check that it initialised from the string correctly.
109
105
checkNSR ( value: value, expected: valueAsString)
110
106
111
107
if I . isSigned {
112
108
let negativeValueAsString = " - " + valueAsString
113
109
let negativeValue = initialiser ( negativeValueAsString) !
114
110
115
- XCTAssertEqual ( negativeValue. description, negativeValueAsString) // Sanity check that it initialised from the string correctly.
111
+ #expect ( negativeValue. description == negativeValueAsString) // Sanity check that it initialised from the string correctly.
116
112
checkNSR ( value: negativeValue, expected: negativeValueAsString)
117
113
}
118
114
}
119
115
}
120
116
}
121
117
122
118
#if canImport(Numberick)
123
- func testNumericStringRepresentation_largeIntegers ( ) throws {
119
+ @Test func numericStringRepresentation_largeIntegers ( ) throws {
124
120
check ( type: Int128 . self, initialiser: { Int128 ( $0) } )
125
121
check ( type: UInt128 . self, initialiser: { UInt128 ( $0) } )
126
122
@@ -130,19 +126,19 @@ final class BinaryIntegerFormatStyleTests: XCTestCase {
130
126
#endif
131
127
132
128
#if canImport(BigInt)
133
- func testNumericStringRepresentation_arbitraryPrecisionIntegers ( ) throws {
129
+ @Test func numericStringRepresentation_arbitraryPrecisionIntegers ( ) throws {
134
130
check ( type: BigInt . self, initialiser: { BigInt ( $0) ! } )
135
131
check ( type: BigUInt . self, initialiser: { BigUInt ( $0) ! } )
136
132
}
137
133
#endif
138
134
#endif // canImport(Numberick) || canImport(BigInt)
139
135
}
140
136
141
- final class BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords : XCTestCase {
137
+ struct BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords {
142
138
143
139
// MARK: Tests
144
140
145
- func testInt32 ( ) {
141
+ @ Test func int32 ( ) {
146
142
check ( Int32 ( truncatingIfNeeded: 0x00000000 as UInt32 ) , expectation: " 0 " )
147
143
check ( Int32 ( truncatingIfNeeded: 0x03020100 as UInt32 ) , expectation: " 50462976 " )
148
144
check ( Int32 ( truncatingIfNeeded: 0x7fffffff as UInt32 ) , expectation: " 2147483647 " ) // Int32.max
@@ -152,7 +148,7 @@ final class BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords: XCTestCase {
152
148
check ( Int32 ( truncatingIfNeeded: 0xffffffff as UInt32 ) , expectation: " -1 " )
153
149
}
154
150
155
- func testUInt32 ( ) {
151
+ @ Test func uint32 ( ) {
156
152
check ( UInt32 ( truncatingIfNeeded: 0x00000000 as UInt32 ) , expectation: " 0 " ) // UInt32.min
157
153
check ( UInt32 ( truncatingIfNeeded: 0x03020100 as UInt32 ) , expectation: " 50462976 " )
158
154
check ( UInt32 ( truncatingIfNeeded: 0x7fffffff as UInt32 ) , expectation: " 2147483647 " )
@@ -162,7 +158,7 @@ final class BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords: XCTestCase {
162
158
check ( UInt32 ( truncatingIfNeeded: 0xffffffff as UInt32 ) , expectation: " 4294967295 " ) // UInt32.max
163
159
}
164
160
165
- func testInt64 ( ) {
161
+ @ Test func int64 ( ) {
166
162
check ( Int64 ( truncatingIfNeeded: 0x0000000000000000 as UInt64 ) , expectation: " 0 " )
167
163
check ( Int64 ( truncatingIfNeeded: 0x0706050403020100 as UInt64 ) , expectation: " 506097522914230528 " )
168
164
check ( Int64 ( truncatingIfNeeded: 0x7fffffffffffffff as UInt64 ) , expectation: " 9223372036854775807 " ) // Int64.max
@@ -172,7 +168,7 @@ final class BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords: XCTestCase {
172
168
check ( Int64 ( truncatingIfNeeded: 0xffffffffffffffff as UInt64 ) , expectation: " -1 " )
173
169
}
174
170
175
- func testUInt64 ( ) {
171
+ @ Test func uint64 ( ) {
176
172
check ( UInt64 ( truncatingIfNeeded: 0x0000000000000000 as UInt64 ) , expectation: " 0 " ) // UInt64.min
177
173
check ( UInt64 ( truncatingIfNeeded: 0x0706050403020100 as UInt64 ) , expectation: " 506097522914230528 " )
178
174
check ( UInt64 ( truncatingIfNeeded: 0x7fffffffffffffff as UInt64 ) , expectation: " 9223372036854775807 " )
@@ -184,7 +180,7 @@ final class BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords: XCTestCase {
184
180
185
181
// MARK: Tests + Big Integer
186
182
187
- func testInt128 ( ) {
183
+ @ Test func int128 ( ) {
188
184
check ( x64: [ 0x0000000000000000 , 0x0000000000000000 ] as [ UInt64 ] , isSigned: true , expectation: " 0 " )
189
185
check ( x64: [ 0x0706050403020100 , 0x0f0e0d0c0b0a0908 ] as [ UInt64 ] , isSigned: true , expectation: " 20011376718272490338853433276725592320 " )
190
186
check ( x64: [ 0xffffffffffffffff , 0x7fffffffffffffff ] as [ UInt64 ] , isSigned: true , expectation: " 170141183460469231731687303715884105727 " ) // Int128.max
@@ -193,7 +189,7 @@ final class BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords: XCTestCase {
193
189
check ( x64: [ 0xffffffffffffffff , 0xffffffffffffffff ] as [ UInt64 ] , isSigned: true , expectation: " -1 " )
194
190
}
195
191
196
- func testUInt128 ( ) {
192
+ @ Test func uint128 ( ) {
197
193
check ( x64: [ 0x0000000000000000 , 0x0000000000000000 ] as [ UInt64 ] , isSigned: false , expectation: " 0 " ) // UInt128.min
198
194
check ( x64: [ 0x0706050403020100 , 0x0f0e0d0c0b0a0908 ] as [ UInt64 ] , isSigned: false , expectation: " 20011376718272490338853433276725592320 " )
199
195
check ( x64: [ 0x0000000000000000 , 0x8000000000000000 ] as [ UInt64 ] , isSigned: false , expectation: " 170141183460469231731687303715884105728 " )
@@ -204,12 +200,12 @@ final class BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords: XCTestCase {
204
200
205
201
// MARK: Tests + Big Integer + Miscellaneous
206
202
207
- func testWordsIsEmptyResultsInZero ( ) {
203
+ @ Test func wordsIsEmptyResultsInZero ( ) {
208
204
check ( words: [ ] as [ UInt ] , isSigned: true , expectation: " 0 " )
209
205
check ( words: [ ] as [ UInt ] , isSigned: false , expectation: " 0 " )
210
206
}
211
207
212
- func testSignExtendingDoesNotChangeTheResult ( ) {
208
+ @ Test func signExtendingDoesNotChangeTheResult ( ) {
213
209
check ( words: [ 0 ] as [ UInt ] , isSigned: true , expectation: " 0 " )
214
210
check ( words: [ 0 , 0 ] as [ UInt ] , isSigned: true , expectation: " 0 " )
215
211
check ( words: [ 0 , 0 , 0 ] as [ UInt ] , isSigned: true , expectation: " 0 " )
@@ -228,22 +224,22 @@ final class BinaryIntegerFormatStyleTestsUsingBinaryIntegerWords: XCTestCase {
228
224
229
225
// MARK: Assertions
230
226
231
- func check( _ integer: some BinaryInteger , expectation: String , file : StaticString = #filePath , line : UInt = #line ) {
232
- XCTAssertEqual ( integer. description, expectation, " integer description does not match expectation " , file : file , line : line )
233
- check ( ascii: integer. numericStringRepresentation. utf8, expectation: expectation, file : file , line : line )
234
- check ( words: Array ( integer. words) , isSigned: type ( of: integer) . isSigned, expectation: expectation, file : file , line : line )
227
+ func check( _ integer: some BinaryInteger , expectation: String , sourceLocation : SourceLocation = #_sourceLocation ) {
228
+ #expect ( integer. description == expectation, " integer description does not match expectation " , sourceLocation : sourceLocation )
229
+ check ( ascii: integer. numericStringRepresentation. utf8, expectation: expectation, sourceLocation : sourceLocation )
230
+ check ( words: Array ( integer. words) , isSigned: type ( of: integer) . isSigned, expectation: expectation, sourceLocation : sourceLocation )
235
231
}
236
232
237
- func check( x64: [ UInt64 ] , isSigned: Bool , expectation: String , file : StaticString = #filePath , line : UInt = #line ) {
238
- check ( words: x64. flatMap ( \. words) , isSigned: isSigned, expectation: expectation, file : file , line : line )
233
+ func check( x64: [ UInt64] , isSigned: Bool, expectation: String, sourceLocation : SourceLocation = #_sourceLocation ) {
234
+ check ( words: x64. flatMap ( \. words) , isSigned: isSigned, expectation: expectation, sourceLocation : sourceLocation )
239
235
}
240
236
241
- func check( words: [ UInt ] , isSigned: Bool , expectation: String , file : StaticString = #filePath , line : UInt = #line ) {
237
+ func check( words: [ UInt] , isSigned: Bool, expectation: String, sourceLocation : SourceLocation = #_sourceLocation ) {
242
238
let ascii = numericStringRepresentationForBinaryInteger ( words: words, isSigned: isSigned) . utf8
243
- check ( ascii: ascii, expectation: expectation, file : file , line : line )
239
+ check ( ascii: ascii, expectation: expectation, sourceLocation : sourceLocation )
244
240
}
245
241
246
- func check( ascii: some Collection < UInt8 > , expectation: String , file : StaticString = #filePath , line : UInt = #line ) {
247
- XCTAssertEqual ( String ( decoding: ascii, as: Unicode . ASCII. self) , expectation, file : file , line : line )
242
+ func check( ascii: some Collection< UInt8 > , expectation: String, sourceLocation : SourceLocation = #_sourceLocation ) {
243
+ #expect ( String ( decoding: ascii, as: Unicode . ASCII. self) == expectation, sourceLocation : sourceLocation )
248
244
}
249
245
}
0 commit comments