Skip to content

Commit 0c6a2c3

Browse files
committed
[test] Fix FixedPointConversion tests
1 parent 6e89ae8 commit 0c6a2c3

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

test/stdlib/Inputs/FixedPointConversion.swift.gyb

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ int_to_int_conversion_template = gyb.parse_template("int_to_int_conversion",
4040
"""
4141
%{
4242
from SwiftIntTypes import all_integer_types, int_max, int_min
43-
from SwiftFloatingPointTypes import all_floating_point_types
43+
from SwiftFloatingPointTypes import all_floating_point_types, getFtoIBounds
44+
45+
from decimal import Decimal
46+
4447

4548
}%
4649
% for self_ty in all_integer_types(word_bits):
@@ -105,51 +108,53 @@ FixedPointConversionFailure.test("${Other}To${Self}Conversion/dest=${testValue}"
105108
% Other = "Float" + str(other_type.bits)
106109
% otherMin = -int_max(bits=other_type.explicit_significand_bits, signed=False)
107110
% otherMax = int_max(bits=other_type.explicit_significand_bits, signed=False)
111+
% (selfFtoIMin, selfFtoIMax) = getFtoIBounds(other_type.bits, selfBits, selfSigned)
108112

109113
% if Other == 'Float80':
110114
#if !os(Windows) && (arch(i386) || arch(x86_64))
111115
% end
112116

113-
% for testValue in [repr(value) for value in [selfMin, selfMax, selfMin - 0.1, selfMax + 0.1, otherMin, otherMax, 0.0, -0.0, 0.1, -0.1]]:
117+
% for testValue in [Decimal(selfMin), Decimal(selfMax), Decimal(selfFtoIMin) - Decimal('0.1'), Decimal(selfFtoIMax) + Decimal('0.1'), Decimal(otherMin), Decimal(otherMax), Decimal('0.0'), Decimal('-0.0'), Decimal('0.1'), Decimal('-0.1')]:
118+
% testValueStr = str(testValue)
114119

115120
% if testValue < otherMin or testValue > otherMax:
116121
% # Can't construct `other` value to test from, do nothing and continue.
117122

118-
% elif testValue >= selfMin and testValue <= selfMax and testValue % 1 == 0 and testValue != -0.0:
123+
% elif testValue >= selfFtoIMin and testValue <= selfFtoIMax and (testValue % 1).is_zero():
119124

120-
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
121-
let input = get${Other}(${testValue})
125+
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValueStr}") {
126+
let input = get${Other}(${testValueStr})
122127
let result = ${Self}(input)
123128
var resultConvertedBack = ${Other}(result)
124-
expectEqual(${testValue}, resultConvertedBack)
129+
expectEqual(${testValueStr}, resultConvertedBack)
125130
}
126131

127-
FloatingPointConversionFailures.test("${Other}To${Self}FailableConversion/dest=${testValue}") {
128-
let input = get${Other}(${testValue})
129-
expectNil(${Self}(exactly: input))
132+
FloatingPointConversionFailures.test("${Other}To${Self}FailableConversion/dest=${testValueStr}") {
133+
let input = get${Other}(${testValueStr})
134+
expectNotNil(${Self}(exactly: input))
130135
}
131136

132137
% else:
133138

134-
% if testValue > selfMax:
135-
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
139+
% if testValue > selfFtoIMax:
140+
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValueStr}")
136141
.crashOutputMatches(getTooLargeMessage()).code {
137142
expectCrashLater()
138-
% elif testValue < selfMin:
139-
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
143+
% elif testValue < selfFtoIMin:
144+
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValueStr}")
140145
.crashOutputMatches(getTooSmallMessage()).code {
141146
expectCrashLater()
142147
% else:
143-
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
148+
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValueStr}") {
144149
% end
145-
let input = get${Other}(${testValue})
150+
let input = get${Other}(${testValueStr})
146151
var result = ${Self}(input)
147152
var resultConvertedBack = ${Other}(result)
148153
expectNotEqual(input, resultConvertedBack)
149154
}
150155

151-
FloatingPointConversionFailures.test("${Other}To${Self}Conversion/dest=${testValue}") {
152-
let input = get${Other}(${testValue})
156+
FloatingPointConversionFailures.test("${Other}To${Self}Conversion/dest=${testValueStr}") {
157+
let input = get${Other}(${testValueStr})
153158
expectNil(${Self}(exactly: input))
154159
}
155160
% end

0 commit comments

Comments
 (0)