@@ -36,7 +36,7 @@ final class DocumentTests: XCTestCase {
36
36
( " testEquatable " , testEquatable) ,
37
37
( " testRawBSON " , testRawBSON) ,
38
38
( " testValueBehavior " , testValueBehavior) ,
39
- ( " testInvalidInt " , testInvalidInt ) ,
39
+ ( " testIntEncodesAsInt32OrInt64 " , testIntEncodesAsInt32OrInt64 ) ,
40
40
( " testBSONCorpus " , testBSONCorpus) ,
41
41
( " testMerge " , testMerge)
42
42
]
@@ -198,11 +198,32 @@ final class DocumentTests: XCTestCase {
198
198
XCTAssertNotEqual ( doc1, doc2)
199
199
}
200
200
201
- func testInvalidInt( ) {
202
- let doc1 = Document ( )
203
- let v = Int ( Int32 . max) + 1
204
- expect ( try v. encode ( to: doc1. data, forKey: " x " ) ) . to ( throwError ( ) )
201
+ func testIntEncodesAsInt32OrInt64( ) {
202
+ /* Skip this test on 32-bit platforms. Use MemoryLayout instead of
203
+ * Int.bitWidth to avoid a compiler warning.
204
+ * See: https://forums.swift.org/t/how-can-i-condition-on-the-size-of-int/9080/4 */
205
+ if MemoryLayout< Int> . size == 4 {
206
+ return
207
+ }
208
+
209
+ let int32min_sub1 = Int64 ( Int32 . min) - Int64( 1 )
210
+ let int32max_add1 = Int64 ( Int32 . max) + Int64( 1 )
211
+
212
+ var doc : Document = [
213
+ " int32min " : Int ( Int32 . min) ,
214
+ " int32max " : Int ( Int32 . max) ,
215
+ " int32min-1 " : Int ( int32min_sub1) ,
216
+ " int32max+1 " : Int ( int32max_add1) ,
217
+ " int64min " : Int ( Int64 . min) ,
218
+ " int64max " : Int ( Int64 . max)
219
+ ]
205
220
221
+ expect ( doc [ " int32min " ] as? Int ) . to ( equal ( Int ( Int32 . min) ) )
222
+ expect ( doc [ " int32max " ] as? Int ) . to ( equal ( Int ( Int32 . max) ) )
223
+ expect ( doc [ " int32min-1 " ] as? Int64 ) . to ( equal ( int32min_sub1) )
224
+ expect ( doc [ " int32max+1 " ] as? Int64 ) . to ( equal ( int32max_add1) )
225
+ expect ( doc [ " int64min " ] as? Int64 ) . to ( equal ( Int64 . min) )
226
+ expect ( doc [ " int64max " ] as? Int64 ) . to ( equal ( Int64 . max) )
206
227
}
207
228
208
229
// swiftlint:disable:next cyclomatic_complexity
0 commit comments