Skip to content

Commit 7255f90

Browse files
committed
stdlib: move test for floating point conversion traps to the validation
testsuite Fixes for traps will follow. Swift SVN r21108
1 parent b3c2f0e commit 7255f90

11 files changed

+173
-134
lines changed

stdlib/unittest/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ set(SWIFTUNITTEST_SOURCES
77
DarwinExtras.swift
88
GetOSVersion.mm
99
InterceptTraps.cpp
10+
OpaqueIdentityFunctions.cpp
11+
OpaqueIdentityFunctions.swift
1012
PthreadBarriers.swift
1113
PthreadWrappers.swift
1214
StdlibCoreExtras.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===--- OpaqueIdentityFunctions.cpp --------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
extern "C" void *swift_stdlib_getPointer(void *x) { return x; }
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===--- OpaqueIdentityFunctions.swift ------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@asmname("swift_stdlib_getPointer")
14+
func _stdlib_getPointer(x: COpaquePointer) -> COpaquePointer
15+
16+
func _opaqueIdentity<T>(x: T) -> T {
17+
var ptr = UnsafeMutablePointer<T>.alloc(1)
18+
ptr.initialize(x)
19+
let result =
20+
UnsafeMutablePointer<T>(_stdlib_getPointer(COpaquePointer(ptr))).memory
21+
ptr.destroy()
22+
ptr.dealloc(1)
23+
return result
24+
}
25+
26+
@inline(never)
27+
public func getInt8(x: Int8) -> Int8 { return _opaqueIdentity(x) }
28+
29+
@inline(never)
30+
public func getInt16(x: Int16) -> Int16 { return _opaqueIdentity(x) }
31+
32+
@inline(never)
33+
public func getInt32(x: Int32) -> Int32 { return _opaqueIdentity(x) }
34+
35+
@inline(never)
36+
public func getInt64(x: Int64) -> Int64 { return _opaqueIdentity(x) }
37+
38+
@inline(never)
39+
public func getInt(x: Int) -> Int { return _opaqueIdentity(x) }
40+
41+
@inline(never)
42+
public func getWord(x: Word) -> Word { return _opaqueIdentity(x) }
43+
44+
@inline(never)
45+
public func getUInt8(x: UInt8) -> UInt8 { return _opaqueIdentity(x) }
46+
47+
@inline(never)
48+
public func getUInt16(x: UInt16) -> UInt16 { return _opaqueIdentity(x) }
49+
50+
@inline(never)
51+
public func getUInt32(x: UInt32) -> UInt32 { return _opaqueIdentity(x) }
52+
53+
@inline(never)
54+
public func getUInt64(x: UInt64) -> UInt64 { return _opaqueIdentity(x) }
55+
56+
@inline(never)
57+
public func getUInt(x: UInt) -> UInt { return _opaqueIdentity(x) }
58+
59+
@inline(never)
60+
public func getUWord(x: UWord) -> UWord { return _opaqueIdentity(x) }
61+
62+
@inline(never)
63+
public func getFloat32(x: Float32) -> Float32 { return _opaqueIdentity(x) }
64+
65+
@inline(never)
66+
public func getFloat64(x: Float64) -> Float64 { return _opaqueIdentity(x) }
67+
68+
@inline(never)
69+
public func getFloat80(x: Float80) -> Float80 { return _opaqueIdentity(x) }
70+

test/stdlib/FixedPointArithmeticTraps.swift.gyb

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
// RUN: mkdir -p %t
55
// RUN: %S/../../utils/gyb %s -o %t/FixedPointArithmeticTraps.swift
66
// RUN: xcrun -sdk %target-sdk-name clang++ -arch %target-cpu %S/Inputs/CatchCrashes.cpp -c -o %t/CatchCrashes.o
7-
// RUN: xcrun -sdk %target-sdk-name clang++ -arch %target-cpu %S/Inputs/OpaqueIdentityFunctions/OpaqueIdentityFunctions.m -c -o %t/OpaqueIdentityFunctions.o -g
8-
// RUN: %target-build-swift %t/FixedPointArithmeticTraps.swift -I %S/Inputs/OpaqueIdentityFunctions/ -Xlinker %t/CatchCrashes.o -Xlinker %t/OpaqueIdentityFunctions.o -o %t/a.out_Debug -Xfrontend -disable-access-control
9-
// RUN: %target-build-swift %t/FixedPointArithmeticTraps.swift -I %S/Inputs/OpaqueIdentityFunctions/ -Xlinker %t/CatchCrashes.o -Xlinker %t/OpaqueIdentityFunctions.o -o %t/a.out_Release -O -Xfrontend -disable-access-control
7+
// RUN: %target-build-swift %t/FixedPointArithmeticTraps.swift -Xlinker %t/CatchCrashes.o -o %t/a.out_Debug -Xfrontend -disable-access-control
8+
// RUN: %target-build-swift %t/FixedPointArithmeticTraps.swift -Xlinker %t/CatchCrashes.o -o %t/a.out_Release -O -Xfrontend -disable-access-control
109
//
1110
// RUN: %target-run %t/a.out_Debug 1 2>&1 | FileCheck %s -check-prefix=CHECK
1211
// RUN: %target-run %t/a.out_Debug 2 2>&1 | FileCheck %s -check-prefix=CHECK
@@ -216,7 +215,7 @@
216215
// CHECK: CRASHED: SIG{{ILL|TRAP}}
217216

218217
import Darwin
219-
import OpaqueIdentityFunctions
218+
import StdlibUnittest
220219

221220
// Note: in this file, we need to go through opaque functions to load
222221
// constants. This is to to check runtime behaviour and ensure the constant is

test/stdlib/FixedPointTruncationOverflow.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// These tests should crash (until we change overflow trapping mechanizm in stdlib).
22
// RUN: mkdir -p %t
33
// RUN: xcrun -sdk %target-sdk-name clang++ -arch %target-cpu %S/Inputs/CatchCrashes.cpp -c -o %t/CatchCrashes.o
4-
// RUN: xcrun -sdk %target-sdk-name clang++ -arch %target-cpu %S/Inputs/OpaqueIdentityFunctions/OpaqueIdentityFunctions.m -c -o %t/OpaqueIdentityFunctions.o -g
5-
// RUN: %target-build-swift %s -I %S/Inputs/OpaqueIdentityFunctions/ -Xlinker %t/CatchCrashes.o -Xlinker %t/OpaqueIdentityFunctions.o -o %t/a.out_Debug
6-
// RUN: %target-build-swift %s -I %S/Inputs/OpaqueIdentityFunctions/ -Xlinker %t/CatchCrashes.o -Xlinker %t/OpaqueIdentityFunctions.o -o %t/a.out_Release -O
4+
// RUN: %target-build-swift %s -Xlinker %t/CatchCrashes.o -o %t/a.out_Debug
5+
// RUN: %target-build-swift %s -Xlinker %t/CatchCrashes.o -o %t/a.out_Release -O
76
//
87
// RUN: %target-run %t/a.out_Debug 1 2>&1 | FileCheck %s -check-prefix=CHECK
98
// RUN: %target-run %t/a.out_Debug 2 2>&1 | FileCheck %s -check-prefix=CHECK
@@ -27,7 +26,7 @@
2726
// CHECK: CRASHED: SIG{{ILL|TRAP}}
2827

2928
import Darwin
30-
import OpaqueIdentityFunctions
29+
import StdlibUnittest
3130

3231
// Note: in this file, we need to go through opaque functions to load
3332
// constants. This is to to check runtime behaviour and ensure the constant is

test/stdlib/FloatingPointConversionOverflow.swift

-97
This file was deleted.

test/stdlib/Inputs/OpaqueIdentityFunctions/OpaqueIdentityFunctions.h

-14
This file was deleted.

test/stdlib/Inputs/OpaqueIdentityFunctions/OpaqueIdentityFunctions.m

-12
This file was deleted.

test/stdlib/Inputs/OpaqueIdentityFunctions/module.map

-3
This file was deleted.

utils/SwiftIntTypes.py

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def all_integer_types(word_bits):
2121
for signed in False, True:
2222
yield str(name), int(word_bits if name=='Int' else name), signed
2323

24+
def all_integer_type_names():
25+
for name in _all_integer_type_bitwidths:
26+
for signed in False, True:
27+
yield int_name(str(name), signed)
28+
2429
def _base_int_name(name):
2530
return 'Int' if name == 'Int' else 'Int' + str(name)
2631

@@ -33,3 +38,6 @@ def int_name(name, signed):
3338
def other_int_name(name, signed):
3439
return ('U' if signed else '') + _base_int_name(name)
3540

41+
def is_signed_int(name):
42+
return name[0] != 'U'
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: %S/../../utils/gyb %s -o %t/FloatingPointConversionTraps.swift
4+
// RUN: %target-build-swift %t/FloatingPointConversionTraps.swift -o %t/a.out_Debug
5+
// RUN: %target-build-swift %t/FloatingPointConversionTraps.swift -o %t/a.out_Release -O
6+
//
7+
// RUN: %target-run %t/a.out_Debug
8+
// RUN: %target-run %t/a.out_Release
9+
10+
import StdlibUnittest
11+
12+
%{
13+
14+
from SwiftIntTypes import *
15+
16+
}%
17+
18+
extension Float80 {
19+
public static var infinity: Float80 {
20+
return 1.0 / 0.0
21+
}
22+
}
23+
24+
var FloatingPointConversion = TestCase("FloatingPointConversion")
25+
26+
% for FloatSelf in ['Float32', 'Float64', 'Float80']:
27+
% for IntSelf in all_integer_type_names():
28+
29+
% if FloatSelf == 'Float80':
30+
#if arch(i386) || arch(x86_64)
31+
% end
32+
33+
FloatingPointConversion.test("${FloatSelf}/${IntSelf}/+inf") {
34+
var a = get${IntSelf}(${IntSelf}(get${FloatSelf}(123.0)))
35+
expectEqual(123, a)
36+
37+
expectCrashLater()
38+
get${IntSelf}(${IntSelf}(get${FloatSelf}(+${FloatSelf}.infinity)))
39+
}
40+
41+
% if is_signed_int(IntSelf):
42+
43+
FloatingPointConversion.test("${FloatSelf}/${IntSelf}/-inf") {
44+
var a = get${IntSelf}(${IntSelf}(get${FloatSelf}(-123.0)))
45+
expectEqual(-123, a)
46+
47+
expectCrashLater()
48+
get${IntSelf}(${IntSelf}(get${FloatSelf}(-${FloatSelf}.infinity)))
49+
}
50+
51+
% else:
52+
53+
FloatingPointConversion.test("${FloatSelf}/${IntSelf}/negative") {
54+
expectCrashLater()
55+
get${IntSelf}(${IntSelf}(get${FloatSelf}(-123.0)))
56+
}
57+
58+
FloatingPointConversion.test("${FloatSelf}/${IntSelf}/negative") {
59+
expectCrashLater()
60+
get${IntSelf}(${IntSelf}(get${FloatSelf}(-${FloatSelf}.infinity)))
61+
}
62+
63+
% end
64+
65+
% if FloatSelf == 'Float80':
66+
#endif
67+
% end
68+
69+
% end
70+
% end
71+
72+
runAllTests()
73+

0 commit comments

Comments
 (0)