Skip to content

Commit f3a5af8

Browse files
committed
[stdlib] Fix FloatingPoint.init(exactly:) tests for 32 bit
1 parent 3870091 commit f3a5af8

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

test/stdlib/TestNSNumberBridging.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,12 @@ func testNSNumberBridgeFromUInt() {
660660

661661
let float = (number!) as? Float
662662
let expectedFloat = Float(exactly: uint!)
663-
testFloat(expectedFloat, float)
664-
663+
if UInt.bitWidth == 32 && uint! >= UInt.max - 1 {
664+
expectNil(expectedFloat)
665+
} else {
666+
testFloat(expectedFloat, float)
667+
}
668+
665669
let double = (number!) as? Double
666670
let expectedDouble = Double(exactly: uint!)
667671
testDouble(expectedDouble, double)

validation-test/stdlib/FloatingPointConversion.swift.gyb

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,36 @@ _UnimplementedError()
125125

126126
% for int_ty in int_types:
127127
% OtherInt = int_ty.stdlib_name
128-
% OtherMin = int_ty.min
129-
% OtherMax = int_ty.max
130-
% (FloatMin, FloatMax) = getFtoIBounds(self_type.bits, int_ty.bits, int_ty.is_signed)
131128

132-
FixedPointConversionTruncations.test("${OtherInt}to${Self}")
133-
.forEach(in: [
134-
(0, 0, 0),
135-
% if int_ty.bits > self_type.significand_bits + 1:
136-
% limit = ~(~0 << (self_type.significand_bits + 1))
137-
% over = ~(~0 << (self_type.significand_bits + 2))
138-
(${limit}, ${limit}, ${limit}),
139-
(${over}, ${over + 1}, nil),
129+
extension ${OtherInt} {
130+
static var _test${Self}Conversion: [(${OtherInt}, ${OtherInt}, ${OtherInt}?)] {
131+
if bitWidth > ${Self}.significandBitCount + 1 {
132+
let bitOffset = ${Self}.significandBitCount + 1
133+
let limit: ${OtherInt} = ~(~0 << bitOffset)
134+
let over: ${OtherInt} = 1 + limit << 1
135+
return [
136+
(0, 0, 0),
137+
(limit, limit, limit),
138+
(over, over + 1, nil),
140139
% if int_ty.is_signed:
141-
(-${limit}, -${limit}, -${limit}),
142-
(-${over}, -${over + 1}, nil),
140+
(-limit, -limit, -limit),
141+
(-over, -(over + 1), nil),
143142
% end
144-
% else:
145-
(${OtherInt}.min, ${OtherInt}.min, ${OtherInt}.min),
146-
(${OtherInt}.max, ${OtherInt}.max, ${OtherInt}.max),
147-
% end
148-
] as [(${OtherInt}, ${OtherInt}, ${OtherInt}?)]) { value, roundedExpectation, exactExpectation in
143+
]
144+
} else {
145+
return [
146+
(0, 0, 0),
147+
(.min, .min, .min),
148+
(.max, .max, .max),
149+
]
150+
}
151+
}
152+
}
153+
154+
FixedPointConversionTruncations.test("${OtherInt}to${Self}")
155+
.forEach(in: ${OtherInt}._test${Self}Conversion) {
156+
value, roundedExpectation, exactExpectation in
157+
149158
let roundedResult = ${Self}(value)
150159
expectEqual(roundedResult, ${Self}(roundedExpectation))
151160

validation-test/stdlib/ValidationNSNumberBridging.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,11 @@ func testNSNumberBridgeFromUInt() {
654654

655655
let float = (number!) as? Float
656656
let expectedFloat = Float(exactly: uint!)
657-
testFloat(expectedFloat, float)
657+
if UInt.bitWidth == 32 && uint! >= UInt.max - 1 {
658+
expectNil(expectedFloat)
659+
} else {
660+
testFloat(expectedFloat, float)
661+
}
658662

659663
let double = (number!) as? Double
660664
let expectedDouble = Double(exactly: uint!)

0 commit comments

Comments
 (0)