Skip to content

Commit 00e374e

Browse files
committed
FixedPointConversion validation test: Unbreak completely hosed tests
- Use lit.cfg's %target-ptrsize instead of nested gyb template; faster, cleaner, and provides correct line-directive output. - Fp/integer comparisons were using Python string/int comparison, undefined in Python 2 and illegal in 3, preventing large number of tests from being generated at all. Replace with actual numeric comparisons. - Work around edge case where inability of Python to represent `Int64.min` exactly in fp (insufficient precision) caused incorrect test failure. - Fix inverted assertion in tests for `.init(exactly:)` returning non-nil. - Respect "round-towards-zero then check range" behavior of truncating `.init(_:)` initializers, prevents incorrect failures at extreme ranges of int types.
1 parent ecebce8 commit 00e374e

File tree

3 files changed

+30
-44
lines changed

3 files changed

+30
-44
lines changed

test/stdlib/Inputs/FixedPointConversion.swift.gyb

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// FIXME(integers): add tests that perform the same checks in generic code
22

33
%{
4-
import gyb
4+
from SwiftIntTypes import all_integer_types, int_max, int_min
5+
from SwiftFloatingPointTypes import all_floating_point_types
56
}%
67

78
import StdlibUnittest
@@ -34,22 +35,15 @@ func getTooLargeMessage() -> String {
3435
return ""
3536
}
3637

37-
%{
38-
39-
int_to_int_conversion_template = gyb.parse_template("int_to_int_conversion",
40-
"""
41-
%{
42-
from SwiftIntTypes import all_integer_types, int_max, int_min
43-
from SwiftFloatingPointTypes import all_floating_point_types
44-
45-
}%
38+
% word_bits = int(target_ptrsize)
4639
% for self_ty in all_integer_types(word_bits):
4740
% selfBits = self_ty.bits
4841
% selfSigned = self_ty.is_signed
4942
% selfMin = self_ty.min
5043
% selfMax = self_ty.max
5144
% Self = self_ty.stdlib_name
5245

46+
% # Test conversion behaviors for all integer types
5347
% for other_ty in all_integer_types(word_bits):
5448
% otherBits = other_ty.bits
5549
% otherSigned = other_ty.is_signed
@@ -58,11 +52,11 @@ from SwiftFloatingPointTypes import all_floating_point_types
5852
% Other = other_ty.stdlib_name
5953

6054
% for testValue in [selfMin, selfMax, selfMin - 1, selfMax + 1, otherMin, otherMax]:
61-
6255
% if testValue < otherMin or testValue > otherMax:
6356
% # Can't construct `other` value, do nothing and continue.
64-
57+
% pass
6558
% elif testValue >= selfMin and testValue <= selfMax:
59+
% # Test value can be represented by Self, test conversion succeeds
6660

6761
/// Always-safe conversion from ${Other}(${testValue}) to ${Self}.
6862
FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}") {
@@ -80,6 +74,7 @@ FixedPointConversionFailure.test("${Other}To${Self}FailableConversion/dest=${tes
8074
}
8175

8276
% else:
77+
% # Test value is out of range of Self, test conversion fails
8378

8479
/// Always-failing conversion from ${Other}(${testValue}) to ${Self}.
8580
FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}") {
@@ -96,11 +91,12 @@ FixedPointConversionFailure.test("${Other}To${Self}Conversion/dest=${testValue}"
9691
let input = get${Other}(${testValue})
9792
expectNil(${Self}(exactly: input))
9893
}
99-
% end
10094

95+
% end
10196
% end # for testValue in ...
10297
% end # for in all_integer_types (Other)
10398

99+
% # Test conversion behaviors for all floating-point types
104100
% for other_type in all_floating_point_types():
105101
% Other = "Float" + str(other_type.bits)
106102
% otherMin = -int_max(bits=other_type.explicit_significand_bits, signed=False)
@@ -110,12 +106,22 @@ FixedPointConversionFailure.test("${Other}To${Self}Conversion/dest=${testValue}"
110106
#if !os(Windows) && (arch(i386) || arch(x86_64))
111107
% end
112108

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]]:
109+
% # Int64.min - 0.1 can not be fully represented by a double-precision
110+
% # value. Python can not leverage extended precision, so the resulting
111+
% # value incorrectly rounds to an integer which compares as less than
112+
% # Int64.min, even though in Swift a Float80 literal will have the correct
113+
% # value since Swift 5. Skip the bad case for now.
114+
% testValues = [selfMin, selfMax, selfMin - 0.1, selfMax + 0.1, otherMin, otherMax, 0.0, -0.0, 0.1, -0.1]
115+
% if Other == 'Float80' and selfBits > 63 and selfSigned:
116+
% del testValues[2]
117+
% end
114118

119+
% for testValue in testValues:
115120
% if testValue < otherMin or testValue > otherMax:
116121
% # Can't construct `other` value to test from, do nothing and continue.
117-
118-
% elif testValue >= selfMin and testValue <= selfMax and testValue % 1 == 0 and testValue != -0.0:
122+
% pass
123+
% elif testValue >= selfMin and testValue <= selfMax and testValue % 1.0 == 0:
124+
% # Test value can be represented exactly by Self, test two-way conversion
119125

120126
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
121127
let input = get${Other}(${testValue})
@@ -126,20 +132,22 @@ FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${test
126132

127133
FloatingPointConversionFailures.test("${Other}To${Self}FailableConversion/dest=${testValue}") {
128134
let input = get${Other}(${testValue})
129-
expectNil(${Self}(exactly: input))
135+
expectNotNil(${Self}(exactly: input))
130136
}
131137

132138
% else:
133-
134-
% if testValue > selfMax:
139+
% if int(testValue) > selfMax:
140+
% # Test value exceeds maximum value of Self, test for too large message
135141
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
136142
.crashOutputMatches(getTooLargeMessage()).code {
137143
expectCrashLater()
138-
% elif testValue < selfMin:
144+
% elif int(testValue) < selfMin:
145+
% # Test value doesn't reach minimum value of Self, test for too small message
139146
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
140147
.crashOutputMatches(getTooSmallMessage()).code {
141148
expectCrashLater()
142149
% else:
150+
% # Test value can be represented inexactly by Self, test for truncation
143151
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
144152
% end
145153
let input = get${Other}(${testValue})
@@ -208,26 +216,4 @@ FloatingPointConversionFailures.test("${Self}/${Other}/NaN") {
208216
% end # for in all_floating_point_types (Other)
209217
% end # for in all_integer_types (Self)
210218

211-
""")
212-
213-
}%
214-
215-
#if arch(i386) || arch(arm)
216-
217-
${gyb.execute_template(
218-
int_to_int_conversion_template,
219-
word_bits=32)}
220-
221-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
222-
223-
${gyb.execute_template(
224-
int_to_int_conversion_template,
225-
word_bits=64)}
226-
227-
#else
228-
229-
_UnimplementedError()
230-
231-
#endif
232-
233219
runAllTests()

validation-test/stdlib/FixedPointConversion_Debug.test-sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %gyb %S/Inputs/FixedPointConversion.swift.gyb -o %t/FixedPointConversion.swift
2+
// RUN: %gyb %S/Inputs/FixedPointConversion.swift.gyb -Dtarget_ptrsize=%target-ptrsize -o %t/FixedPointConversion.swift
33
// RUN: %line-directive %t/FixedPointConversion.swift -- %target-build-swift %t/FixedPointConversion.swift -o %t/a.out_Debug -Onone
44
// RUN: %target-codesign %t/a.out_Debug
55
//

validation-test/stdlib/FixedPointConversion_Release.test-sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %gyb %S/Inputs/FixedPointConversion.swift.gyb -o %t/FixedPointConversion.swift
2+
// RUN: %gyb %S/Inputs/FixedPointConversion.swift.gyb -Dtarget_ptrsize=%target-ptrsize -o %t/FixedPointConversion.swift
33
// RUN: %line-directive %t/FixedPointConversion.swift -- %target-build-swift %t/FixedPointConversion.swift -o %t/a.out_Release -O
44
// RUN: %target-codesign %t/a.out_Release
55
//

0 commit comments

Comments
 (0)