mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Add tests
This commit is contained in:
committed by
Artem Chikin
parent
d484ec7c1f
commit
a925d6af20
21
test/ConstValues/CImports.swift
Normal file
21
test/ConstValues/CImports.swift
Normal file
@@ -0,0 +1,21 @@
|
||||
// Constant globals should "work" when referencing C-imported constants
|
||||
|
||||
// RUN: %empty-directory(%t)
|
||||
// RUN: split-file %s %t
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %t/main.swift -parse-as-library -import-bridging-header %t/bridging_header.h
|
||||
|
||||
//--- bridging_header.h
|
||||
|
||||
static const int c_integer = 42;
|
||||
static const long c_long = 42;
|
||||
|
||||
struct CStruct {
|
||||
int a, b, c, d;
|
||||
};
|
||||
|
||||
//--- main.swift
|
||||
|
||||
_const let constGlobal1: Int = Int(c_integer)
|
||||
_const let constGlobal2: Int = Int(c_long)
|
||||
_const let constGlobal3: Int = MemoryLayout<CStruct>.size
|
||||
20
test/ConstValues/DiagModules.swift
Normal file
20
test/ConstValues/DiagModules.swift
Normal file
@@ -0,0 +1,20 @@
|
||||
// Constant globals should "work" even when used across files in non-WMO builds.
|
||||
|
||||
// RUN: %empty-directory(%t)
|
||||
// RUN: split-file %s %t
|
||||
|
||||
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library
|
||||
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift -verify
|
||||
|
||||
//--- MyModule.swift
|
||||
|
||||
public func foo() -> Int {
|
||||
return 42
|
||||
}
|
||||
|
||||
//--- Main.swift
|
||||
|
||||
import MyModule
|
||||
|
||||
_const let constGlobal1: Int = foo()
|
||||
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
|
||||
13
test/ConstValues/DiagNotConst.swift
Normal file
13
test/ConstValues/DiagNotConst.swift
Normal file
@@ -0,0 +1,13 @@
|
||||
// Constant globals rejected for not being constant values
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify
|
||||
|
||||
_const let a: Bool = Bool.random()
|
||||
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
|
||||
|
||||
func foo() -> Int {
|
||||
return 42 * Int.random(in: 0 ..< 10)
|
||||
}
|
||||
|
||||
_const let b: Int = foo()
|
||||
// expected-error@-1 {{_const let should be initialized with a compile-time value}}
|
||||
8
test/ConstValues/DiagReferenceCycle.swift
Normal file
8
test/ConstValues/DiagReferenceCycle.swift
Normal file
@@ -0,0 +1,8 @@
|
||||
// Constant globals referencing other constant globals and forming a cycle
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -verify
|
||||
|
||||
_const let a: Int = c
|
||||
_const let b: Int = a
|
||||
_const let c: Int = b
|
||||
// expected-error@-1 {{cycle in definitions of constant values}}
|
||||
8
test/ConstValues/FloatintPointLiterals.swift
Normal file
8
test/ConstValues/FloatintPointLiterals.swift
Normal file
@@ -0,0 +1,8 @@
|
||||
// Constant globals on simple floating-point literals
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let constGlobal1 = 42.0
|
||||
_const let constGlobal2: Double = 42.0
|
||||
_const let constGlobal3: Float = 42.0
|
||||
_const let constGlobal4: Float16 = 42.0
|
||||
22
test/ConstValues/FunctionTypes.swift
Normal file
22
test/ConstValues/FunctionTypes.swift
Normal file
@@ -0,0 +1,22 @@
|
||||
// Constant globals on function types / function pointers
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
func foo_void_to_void() {}
|
||||
func foo_int_to_int(x: Int) -> Int { return 42 }
|
||||
|
||||
_const let constGlobalA1: ()->() = { }
|
||||
_const let constGlobalA2: @convention(c) ()->() = { }
|
||||
_const let constGlobalA3: @convention(thin) ()->() = { }
|
||||
|
||||
_const let constGlobalB1: ()->() = foo_void_to_void
|
||||
_const let constGlobalB2: @convention(c) ()->() = foo_void_to_void
|
||||
_const let constGlobalB3: @convention(thin) ()->() = foo_void_to_void
|
||||
|
||||
_const let constGlobalC1: (Int)->(Int) = { _ in return 42 }
|
||||
_const let constGlobalC2: @convention(c) (Int)->(Int) = { _ in return 42 }
|
||||
_const let constGlobalC3: @convention(thin) (Int)->(Int) = { _ in return 42 }
|
||||
|
||||
_const let constGlobalD1: (Int)->(Int) = foo_int_to_int
|
||||
_const let constGlobalD2: @convention(c) (Int)->(Int) = foo_int_to_int
|
||||
_const let constGlobalD3: @convention(thin) (Int)->(Int) = foo_int_to_int
|
||||
8
test/ConstValues/InlineArrays.swift
Normal file
8
test/ConstValues/InlineArrays.swift
Normal file
@@ -0,0 +1,8 @@
|
||||
// Constant globals on inline arrays
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -disable-availability-checking
|
||||
|
||||
_const let constGlobal1: InlineArray = [1, 2, 3]
|
||||
_const let constGlobal2: InlineArray = [1.0, 2.0, 3.0]
|
||||
_const let constGlobal3: InlineArray = constGlobal1
|
||||
_const let constGlobal4: Int = ([1, 2, 3] as InlineArray).count
|
||||
11
test/ConstValues/IntegerArithmetic.swift
Normal file
11
test/ConstValues/IntegerArithmetic.swift
Normal file
@@ -0,0 +1,11 @@
|
||||
// Constant globals on integer arithmetics
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let constGlobal1: Int = 42
|
||||
_const let constGlobal2: UInt = 42 + 42
|
||||
_const let constGlobal3: UInt = 42 * 42
|
||||
_const let constGlobal4: UInt = 42 - 42
|
||||
_const let constGlobal5: UInt = 42 / 42
|
||||
_const let constGlobal6: UInt = 42 % 2
|
||||
_const let constGlobal7: UInt = (42 % 2)
|
||||
9
test/ConstValues/IntegerExpressions.swift
Normal file
9
test/ConstValues/IntegerExpressions.swift
Normal file
@@ -0,0 +1,9 @@
|
||||
// Constant globals on integer expressions
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let constGlobal1: Int = (42 + 42 + 42) / 3
|
||||
_const let constGlobal2: Int = MemoryLayout<UInt32>.size + 4
|
||||
_const let constGlobal3: Int = Int(17.0 / 3.5)
|
||||
_const let constGlobal4: Int = constGlobal1 + 1
|
||||
_const let constGlobal5: Int = -constGlobal1 + 1
|
||||
12
test/ConstValues/IntegerLiterals.swift
Normal file
12
test/ConstValues/IntegerLiterals.swift
Normal file
@@ -0,0 +1,12 @@
|
||||
// Constant globals on simple integer literals
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let constGlobal1: Int = 42
|
||||
_const let constGlobal2: UInt = 42
|
||||
_const let constGlobal3: UInt = 0xf000f000
|
||||
#if _pointerBitWidth(_64)
|
||||
_const let constGlobal4: UInt = 0xf000f000_f000f000
|
||||
#endif
|
||||
_const let constGlobal5: UInt8 = 255
|
||||
_const let constGlobal6: UInt32 = 0xffff_ffff
|
||||
20
test/ConstValues/Modules.swift
Normal file
20
test/ConstValues/Modules.swift
Normal file
@@ -0,0 +1,20 @@
|
||||
// Constant values should be able to call "const" functions from other modules
|
||||
|
||||
// RUN: %empty-directory(%t)
|
||||
// RUN: split-file %s %t
|
||||
|
||||
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library
|
||||
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift
|
||||
|
||||
//--- MyModule.swift
|
||||
|
||||
_const
|
||||
public func foo() -> Int {
|
||||
return 42
|
||||
}
|
||||
|
||||
//--- Main.swift
|
||||
|
||||
import MyModule
|
||||
|
||||
_const let constGlobal1: Int = foo()
|
||||
14
test/ConstValues/NonWMO.swift
Normal file
14
test/ConstValues/NonWMO.swift
Normal file
@@ -0,0 +1,14 @@
|
||||
// Constant globals should "work" even when used across files in non-WMO builds.
|
||||
|
||||
// RUN: %empty-directory(%t)
|
||||
// RUN: split-file %s %t
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s/file1.swift -parse-as-library
|
||||
|
||||
//--- file1.swift
|
||||
|
||||
_const let constGlobal1: Int = 42
|
||||
|
||||
//--- file2.swift
|
||||
|
||||
_const let constGlobal2: Int = constGlobal1
|
||||
14
test/ConstValues/Optionals.swift
Normal file
14
test/ConstValues/Optionals.swift
Normal file
@@ -0,0 +1,14 @@
|
||||
// Constant globals on optionals
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let constGlobal1: Int? = 42
|
||||
_const let constGlobal2: Int? = nil
|
||||
_const let constGlobal3: UInt8? = 42
|
||||
_const let constGlobal4: UInt8? = nil
|
||||
_const let constGlobal5: Bool? = true
|
||||
_const let constGlobal6: Bool? = nil
|
||||
_const let constGlobal7: (Int, Int)? = (42, 42)
|
||||
_const let constGlobal8: (Int, Int)? = nil
|
||||
_const let constGlobal9: String? = "hello"
|
||||
_const let constGlobal10: String? = nil
|
||||
8
test/ConstValues/References.swift
Normal file
8
test/ConstValues/References.swift
Normal file
@@ -0,0 +1,8 @@
|
||||
// Constant globals referencing other constant globals in their initializer expressions
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let a: Int = 42
|
||||
_const let b: Int = a
|
||||
_const let c: Int = b
|
||||
_const let d: Int = -a
|
||||
7
test/ConstValues/Strings.swift
Normal file
7
test/ConstValues/Strings.swift
Normal file
@@ -0,0 +1,7 @@
|
||||
// Constant globals on strings
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let constGlobal1: String = "hello"
|
||||
_const let constGlobal2: StaticString = "hello"
|
||||
_const let constGlobal3: Int = "hello".count
|
||||
6
test/ConstValues/TopLevel.swift
Normal file
6
test/ConstValues/TopLevel.swift
Normal file
@@ -0,0 +1,6 @@
|
||||
// Constant globals should "work" even in top-level code mode.
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let constGlobal: Int = 42
|
||||
11
test/ConstValues/Tuples.swift
Normal file
11
test/ConstValues/Tuples.swift
Normal file
@@ -0,0 +1,11 @@
|
||||
// Constant globals on tuples
|
||||
|
||||
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library
|
||||
|
||||
_const let constGlobal1: Int = 42
|
||||
_const let constGlobal2: (Int, Int) = (42, 42)
|
||||
_const let constGlobal3: (Int, Bool) = (42, true)
|
||||
_const let constGlobal4: (Int, (Int, Int)) = (42, (42, 42))
|
||||
_const let constGlobal5: (Int, Float) = (42, 42.0)
|
||||
_const let constGlobal6: (Int, String) = (42, "Foo")
|
||||
_const let constGlobal7: (UInt64, StaticString, @convention(c) ()->Int) = (42, "hi", { return 42 })
|
||||
Reference in New Issue
Block a user