Skip to content

Commit 2f74e76

Browse files
ultramiraculousChris Williams
authored and
Chris Williams
committed
Add max/min helpers to SwiftIntTypes
1 parent ae140e6 commit 2f74e76

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

utils/SwiftIntTypes.py

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
# Number of bits in the biggest int type
1515
int_max_bits = max(_all_integer_type_bitwidths)
1616

17+
def int_max(bits, signed):
18+
bits = bits - 1 if signed else bits
19+
bits = max(bits, 0)
20+
return (1 << bits) - 1
21+
22+
def int_min(bits, signed):
23+
return (-1 * int_max(bits, signed) - 1) if signed else 0
1724

1825
class SwiftIntegerType(object):
1926

@@ -26,6 +33,9 @@ def __init__(self, is_word, bits, is_signed):
2633
self.possible_bitwidths = [32, 64]
2734
else:
2835
self.possible_bitwidths = [bits]
36+
37+
self.min = int_min(bits, is_signed)
38+
self.max = int_max(bits, is_signed)
2939

3040
# Derived properties
3141
self.stdlib_name = \
@@ -118,3 +128,4 @@ def all_integer_assignment_operator_names():
118128

119129
def all_integer_or_real_assignment_operator_names():
120130
return ['=', '*=', '/=', '+=', '-=']
131+

validation-test/stdlib/FixedPointConversion.swift.gyb

+18-28
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,23 @@ func getTooLargeMessage() -> String {
4747
int_to_int_conversion_template = gyb.parse_template("int_to_int_conversion",
4848
"""
4949
%{
50-
from SwiftIntTypes import all_integer_types
50+
from SwiftIntTypes import all_integer_types, int_max, int_min
5151

5252
floatNameToSignificandBits = { 'Float32':24, 'Float64':53, 'Float80':64 }
5353

54-
def intMax(bits, signed):
55-
bits = bits - 1 if signed else bits
56-
return (1 << bits) - 1
57-
58-
def intMin(bits, signed):
59-
return -1 * intMax(bits, signed) - 1 if signed else 0
60-
6154
}%
62-
6355
% for self_ty in all_integer_types(word_bits):
6456
% selfBits = self_ty.bits
6557
% selfSigned = self_ty.is_signed
66-
% selfMin = intMin(selfBits, selfSigned)
67-
% selfMax = intMax(selfBits, selfSigned)
58+
% selfMin = self_ty.min
59+
% selfMax = self_ty.max
6860
% Self = self_ty.stdlib_name
6961

7062
% for other_ty in all_integer_types(word_bits):
7163
% otherBits = other_ty.bits
7264
% otherSigned = other_ty.is_signed
73-
% otherMin = intMin(otherBits, otherSigned)
74-
% otherMax = intMax(otherBits, otherSigned)
65+
% otherMin = other_ty.min
66+
% otherMax = other_ty.max
7567
% Other = other_ty.stdlib_name
7668

7769
% for testValue in [selfMin, selfMax, selfMin - 1, selfMax + 1, otherMin, otherMax]:
@@ -85,8 +77,7 @@ def intMin(bits, signed):
8577
FixedPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}") {
8678
// Test that nothing interesting happens and we end up with the same result after converting.
8779
let input = get${Other}(${testValue})
88-
let result = ${Self}(input)
89-
expectEqual(${testValue}, result)
80+
expectEqual(${testValue}, ${Self}(input))
9081
}
9182

9283
/// Never-nil failable conversion from ${Other}(${testValue}) to ${Self}.
@@ -120,8 +111,8 @@ FixedPointConversionFailure.test("${Other}To${Self}Conversion/dest=${testValue}"
120111
% end # for in all_integer_types (Other)
121112

122113
% for Other, otherSignificandBits in floatNameToSignificandBits.iteritems():
123-
% otherMin = intMin(otherSignificandBits, False)
124-
% otherMax = intMax(otherSignificandBits, False)
114+
% otherMin = int_min(bits=otherSignificandBits, signed=False)
115+
% otherMax = int_max(bits=otherSignificandBits, signed=False)
125116

126117
% if Other == 'Float80':
127118
#if !os(Windows) && (arch(i386) || arch(x86_64))
@@ -149,11 +140,13 @@ FloatingPointConversionFailures.test("${Other}To${Self}FailableConversion/dest=$
149140
% else:
150141

151142
% if testValue > selfMax:
152-
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}").crashOutputMatches(getTooLargeMessage()).code {
153-
expectCrashLater()
143+
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
144+
.crashOutputMatches(getTooLargeMessage()).code {
145+
expectCrashLater()
154146
% elif testValue < selfMin:
155-
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}").crashOutputMatches(getTooSmallMessage()).code {
156-
expectCrashLater()
147+
FloatingPointConversionTraps.test("${Other}To${Self}Conversion/dest=${testValue}")
148+
.crashOutputMatches(getTooSmallMessage()).code {
149+
expectCrashLater()
157150
% else:
158151
FloatingPointConversionTruncations.test("${Other}To${Self}Conversion/dest=${testValue}") {
159152
% end
@@ -180,11 +173,10 @@ FloatingPointConversionTraps.test("${Self}/${Other}/negative")
180173
_blackHole(${Self}(get${Other}(-123.0)))
181174
}
182175

183-
FloatingPointConversionFailures.test("${Self}/${Other}/negative")
184-
expectEmpty(${Self}(exactly: get${Other}(-123.0)))
176+
FloatingPointConversionFailures.test("${Self}/${Other}/negative") {
177+
expectEmpty(${Self}(exactly: get${Other}(-123.0)))
185178
}
186179

187-
188180
% end
189181

190182
FloatingPointConversionTraps.test("${Self}/${Other}/+inf")
@@ -204,8 +196,7 @@ FloatingPointConversionTraps.test("${Self}/${Other}/-inf")
204196
}
205197

206198
FloatingPointConversionFailures.test("${Self}/${Other}/-inf") {
207-
let result = ${Self}(exactly: get${Other}(-${Other}.infinity))
208-
expectEmpty(result)
199+
expectEmpty(${Self}(exactly: get${Other}(-${Other}.infinity)))
209200
}
210201

211202
FloatingPointConversionTraps.test("${Self}/${Other}/NaN")
@@ -215,8 +206,7 @@ FloatingPointConversionTraps.test("${Self}/${Other}/NaN")
215206
}
216207

217208
FloatingPointConversionFailures.test("${Self}/${Other}/NaN") {
218-
let result = ${Self}(exactly: get${Other}(${Other}.nan))
219-
expectEmpty(result)
209+
expectEmpty(${Self}(exactly: get${Other}(${Other}.nan)))
220210
}
221211

222212
% if Other == 'Float80':

0 commit comments

Comments
 (0)