@@ -1280,6 +1280,83 @@ class TestDecimal: XCTestCase {
1280
1280
}
1281
1281
}
1282
1282
1283
+ func test_intValue( ) {
1284
+ // SR-7236
1285
+ XCTAssertEqual ( NSDecimalNumber ( value: - 1 ) . intValue, - 1 )
1286
+ XCTAssertEqual ( NSDecimalNumber ( value: 0 ) . intValue, 0 )
1287
+ XCTAssertEqual ( NSDecimalNumber ( value: 1 ) . intValue, 1 )
1288
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal . nan) . intValue, 0 )
1289
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( 1e50 ) ) . intValue, 0 )
1290
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( 1e-50 ) ) . intValue, 0 )
1291
+
1292
+ XCTAssertEqual ( NSDecimalNumber ( value: UInt64 . max) . uint64Value, UInt64 . max)
1293
+ XCTAssertEqual ( NSDecimalNumber ( value: UInt64 . max) . adding ( 1 ) . uint64Value, 0 )
1294
+ XCTAssertEqual ( NSDecimalNumber ( value: Int64 . max) . int64Value, Int64 . max)
1295
+ XCTAssertEqual ( NSDecimalNumber ( value: Int64 . max) . adding ( 1 ) . int64Value, Int64 . min)
1296
+ XCTAssertEqual ( NSDecimalNumber ( value: Int64 . max) . adding ( 1 ) . uint64Value, UInt64 ( Int64 . max) + 1 )
1297
+ XCTAssertEqual ( NSDecimalNumber ( value: Int64 . min) . int64Value, Int64 . min)
1298
+
1299
+ XCTAssertEqual ( NSDecimalNumber ( value: 10 ) . dividing ( by: 3 ) . intValue, 3 )
1300
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( Double . pi) ) . intValue, 3 )
1301
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( Int . max) ) . intValue, Int . max)
1302
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( Int32 . max) ) . int32Value, Int32 . max)
1303
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( Int64 . max) ) . int64Value, Int64 . max)
1304
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( Int . min) ) . intValue, Int . min)
1305
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( Int32 . min) ) . int32Value, Int32 . min)
1306
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( Int64 . min) ) . int64Value, Int64 . min)
1307
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( UInt . max) ) . uintValue, UInt . max)
1308
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( UInt32 . max) ) . uint32Value, UInt32 . max)
1309
+ XCTAssertEqual ( NSDecimalNumber ( decimal: Decimal ( UInt64 . max) ) . uint64Value, UInt64 . max)
1310
+
1311
+
1312
+ // SR-2980
1313
+ let sr2980Tests = [
1314
+ ( " 250.229953885078403 " , 250 ) ,
1315
+ ( " 103.8097165991902834008097165991902834 " , 103 ) ,
1316
+ ( " 31.541176470588235294 " , 31 ) ,
1317
+ ( " 12345.12345678901234 " , 12345 ) ,
1318
+ ( " 12345.123456789012345 " , 12345 ) ,
1319
+ ]
1320
+
1321
+ for (string, value) in sr2980Tests {
1322
+ let decimalValue = NSDecimalNumber ( string: string)
1323
+ XCTAssertEqual ( decimalValue. intValue, value)
1324
+ XCTAssertEqual ( decimalValue. int8Value, Int8 ( truncatingIfNeeded: value) )
1325
+ XCTAssertEqual ( decimalValue. int16Value, Int16 ( value) )
1326
+ XCTAssertEqual ( decimalValue. int32Value, Int32 ( value) )
1327
+ XCTAssertEqual ( decimalValue. int64Value, Int64 ( value) )
1328
+ XCTAssertEqual ( decimalValue. uintValue, UInt ( value) )
1329
+ XCTAssertEqual ( decimalValue. uint8Value, UInt8 ( truncatingIfNeeded: value) )
1330
+ XCTAssertEqual ( decimalValue. uint16Value, UInt16 ( value) )
1331
+ XCTAssertEqual ( decimalValue. uint32Value, UInt32 ( value) )
1332
+ XCTAssertEqual ( decimalValue. uint64Value, UInt64 ( value) )
1333
+ }
1334
+
1335
+ // Large mantissas, negative exponent
1336
+ let maxMantissa = ( UInt16 . max, UInt16 . max, UInt16 . max, UInt16 . max, UInt16 . max, UInt16 . max, UInt16 . max, UInt16 . max)
1337
+
1338
+ let tests = [
1339
+ ( - 34 , 0 , " 34028.2366920938463463374607431768211455 " , 34028 ) ,
1340
+ ( - 35 , 0 , " 3402.82366920938463463374607431768211455 " , 3402 ) ,
1341
+ ( - 36 , 0 , " 340.282366920938463463374607431768211455 " , 340 ) ,
1342
+ ( - 37 , 0 , " 34.0282366920938463463374607431768211455 " , 34 ) ,
1343
+ ( - 38 , 0 , " 3.40282366920938463463374607431768211455 " , 3 ) ,
1344
+ ( - 39 , 0 , " 0.340282366920938463463374607431768211455 " , 0 ) ,
1345
+ ( - 34 , 1 , " -34028.2366920938463463374607431768211455 " , - 34028 ) ,
1346
+ ( - 35 , 1 , " -3402.82366920938463463374607431768211455 " , - 3402 ) ,
1347
+ ( - 36 , 1 , " -340.282366920938463463374607431768211455 " , - 340 ) ,
1348
+ ( - 37 , 1 , " -34.0282366920938463463374607431768211455 " , - 34 ) ,
1349
+ ( - 38 , 1 , " -3.40282366920938463463374607431768211455 " , - 3 ) ,
1350
+ ( - 39 , 1 , " -0.340282366920938463463374607431768211455 " , 0 ) ,
1351
+ ]
1352
+
1353
+ for (exponent, isNegative, description, intValue) in tests {
1354
+ let d = Decimal ( _exponent: Int32 ( exponent) , _length: 8 , _isNegative: UInt32 ( isNegative) , _isCompact: 1 , _reserved: 0 , _mantissa: maxMantissa)
1355
+ XCTAssertEqual ( d. description, description)
1356
+ XCTAssertEqual ( NSDecimalNumber ( decimal: d) . intValue, intValue)
1357
+ }
1358
+ }
1359
+
1283
1360
static var allTests : [ ( String , ( TestDecimal ) -> ( ) throws -> Void ) ] {
1284
1361
return [
1285
1362
( " test_NSDecimalNumberInit " , test_NSDecimalNumberInit) ,
@@ -1310,6 +1387,7 @@ class TestDecimal: XCTestCase {
1310
1387
( " test_multiplyingByPowerOf10 " , test_multiplyingByPowerOf10) ,
1311
1388
( " test_initExactly " , test_initExactly) ,
1312
1389
( " test_NSNumberEquality " , test_NSNumberEquality) ,
1390
+ ( " test_intValue " , test_intValue) ,
1313
1391
]
1314
1392
}
1315
1393
}
0 commit comments