mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
We can use swift_name to import a static factory function as a Swift initializer. This was tested with foreign reference types but not with value types. This PR adds a test case for the latter. rdar://117531428
92 lines
2.6 KiB
Swift
92 lines
2.6 KiB
Swift
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking)
|
|
//
|
|
// REQUIRES: executable_test
|
|
|
|
import StdlibUnittest
|
|
import Constructors
|
|
|
|
var CxxConstructorTestSuite = TestSuite("CxxConstructors")
|
|
|
|
CxxConstructorTestSuite.test("ExplicitDefaultConstructor") {
|
|
let instance = ExplicitDefaultConstructor()
|
|
|
|
expectEqual(42, instance.x)
|
|
}
|
|
|
|
CxxConstructorTestSuite.test("ImplicitDefaultConstructor") {
|
|
let instance = ImplicitDefaultConstructor()
|
|
|
|
expectEqual(42, instance.x)
|
|
}
|
|
|
|
CxxConstructorTestSuite.test("DefaultedDefaultConstructor") {
|
|
let instance = DefaultedDefaultConstructor()
|
|
|
|
expectEqual(42, instance.x)
|
|
}
|
|
|
|
CxxConstructorTestSuite.test("MemberOfClassType") {
|
|
let instance = MemberOfClassType()
|
|
|
|
expectEqual(42, instance.member.x)
|
|
}
|
|
|
|
CxxConstructorTestSuite.test("ConstructorWithParam") {
|
|
let instance = ConstructorWithParam(2)
|
|
|
|
expectEqual(44, instance.x)
|
|
}
|
|
|
|
CxxConstructorTestSuite.test("TemplatedConstructor") {
|
|
let arg = ArgType(i: 2)
|
|
let instance = TemplatedConstructor(arg)
|
|
|
|
expectEqual(2, instance.value.i)
|
|
}
|
|
|
|
CxxConstructorTestSuite.test("implicit default ctor") {
|
|
// Make sure that fields of C++ structs are zeroed out.
|
|
|
|
let instance1 = ConstructorWithParam()
|
|
expectEqual(0, instance1.x)
|
|
|
|
let instance2 = IntWrapper()
|
|
expectEqual(0, instance2.x)
|
|
|
|
// CopyAndMoveConstructor is not default-initializable in C++, however, Swift
|
|
// generates an implicit deprecated default constructor for C++ structs for
|
|
// compatibility with C. This constructor will zero out the entire backing
|
|
// memory of the struct, including fields that have an init expression.
|
|
// See `SwiftDeclSynthesizer::createDefaultConstructor`.
|
|
let instance3 = CopyAndMoveConstructor()
|
|
expectEqual(0, instance3.value)
|
|
expectNil(instance3.ptr)
|
|
}
|
|
|
|
CxxConstructorTestSuite.test("MoveConstructorWithOneParamWithDefaultArg") {
|
|
let instance1 = MoveConstructorWithOneParameterWithDefaultArg(5)
|
|
let instance2 = instance1
|
|
let instance3 = MoveConstructorWithOneParameterWithDefaultArg(5)
|
|
expectTrue(instance2.value + instance3.value >= 10)
|
|
}
|
|
|
|
CxxConstructorTestSuite.test("ImportStaticFactoryAsInitializer") {
|
|
let x = ImportWithCtor()
|
|
expectEqual(x.param1, 0)
|
|
expectEqual(x.param2, 0)
|
|
let y = x
|
|
let z = ImportWithCtor(1)
|
|
expectEqual(z.param1, 1)
|
|
expectEqual(z.param2, 0)
|
|
let z2 = ImportWithCtor(2, 3)
|
|
expectEqual(z2.param1, 2)
|
|
expectEqual(z2.param2, 3)
|
|
let z3 = ImportWithCtor(2, 3, 4)
|
|
expectEqual(z3.param1, 2)
|
|
expectEqual(z3.param2, 3)
|
|
let v = Value(x: 2)
|
|
expectEqual(v.getX(), 2)
|
|
}
|
|
|
|
runAllTests()
|