File: switch_case_executable.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (152 lines) | stat: -rw-r--r-- 3,268 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/test1 -module-name main -D PATTERN_1 
// RUN: %target-build-swift %s -o %t/test2 -module-name main -D PATTERN_2
// RUN: %target-codesign %t/test1
// RUN: %target-codesign %t/test2
// RUN: %target-run %t/test1 | %FileCheck -check-prefix=CHECK -check-prefix=CHECK1 %s
// RUN: %target-run %t/test2 | %FileCheck -check-prefix=CHECK -check-prefix=CHECK2 %s

// REQUIRES: executable_test

//------------------------------------------------------------------------------
print("START: switch toplevel")
// CHECK-LABEL: START: switch toplevel

let val = 1
switch val {
  case 100:
    break
#if PATTERN_1
  case 1:
    print("output1")
#elseif PATTERN_2
  case 1:
    print("output2")
#endif
  default:
    print("never")
}

// CHECK1-NEXT: output1
// CHECK2-NEXT: output2

print("END: switch toplevel")
// CHECK-NEXT: END: switch toplevel


//------------------------------------------------------------------------------
print("START: switch func")
// CHECK-LABEL: START: switch func

enum MyEnum {
  case A, B
#if PATTERN_1
  case str(String)
#elseif PATTERN_2
  case int(Int)
#endif
}

func test1(_ val: MyEnum) {
  switch val {
  case .A, .B:
    print("never")
#if PATTERN_1
  case let .str(v):
    print("output3 - " + v)
#elseif PATTERN_2
  case let .int(v):
    print("output4 - \(v + 12)")
#endif
  }
}

#if PATTERN_1
test1(.str("foo bar"))
#elseif PATTERN_2
test1(.int(42))
#endif
// CHECK1-NEXT: output3 - foo bar
// CHECK2-NEXT: output4 - 54

print("END: switch func")
// CHECK-NEXT: END: switch func

//------------------------------------------------------------------------------
print("START: func local")
// CHECK-LABEL: func local

func test2(_ val: Int) -> () -> Void {
  let ret: () -> Void
  switch val {
#if PATTERN_1
  case let v:
    struct Foo : CustomStringConvertible {
      let val: Int
      var description: String { return "Foo(\(val))" }
    }
    func fn() {
      print("output5 - \(Foo(val:v))")
    }
    ret = fn
#elseif PATTERN_2
  case let v:
    struct Bar : CustomStringConvertible {
      let val: Int
      var description: String { return "Bar(\(val))" }
    }
    ret = { print("output6 - \(Bar(val: v))") }
#endif
  }
  return ret
}

test2(42)()
// CHECK1-NEXT: output5 - Foo(42)
// CHECK2-NEXT: output6 - Bar(42)

print("END: func local")
// CHECK-NEXT: END: func local

//------------------------------------------------------------------------------
print("START: nested directives")
// CHECK-LABEL: START: nested directives

#if PATTERN_1 || PATTERN_2
func test3() {
#if PATTERN_1 || PATTERN_2
  class Nested {
#if PATTERN_1 || PATTERN_2
    func foo(_ x: Int) {
      switch x {
#if true
#if PATTERN_1
      case 0..<42:
        print("output7 - 0..<42 \(x)")
#elseif PATTERN_2
      case 0..<42:
        print("output8 - 0..<42 \(x)")
#else
      case 0..<42:
        print("NEVER")
#endif
      default:
        print("output9 - default \(x)")
#endif
      }
    }
#endif
  }
  Nested().foo(12)
#endif
  Nested().foo(53)
}
#endif
test3()
// CHECK1-NEXT: output7 - 0..<42 12
// CHECK1-NEXT: output9 - default 53
// CHECK2-NEXT: output8 - 0..<42 12
// CHECK2-NEXT: output9 - default 53

print("END: nested directives")
// CHECK-NEXT: END: nested directives