File: struct_resilience.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 (169 lines) | stat: -rw-r--r-- 5,287 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// RUN: %empty-directory(%t)

// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_struct)) -enable-library-evolution %S/../Inputs/resilient_struct.swift -emit-module -emit-module-path %t/resilient_struct.swiftmodule -module-name resilient_struct
// RUN: %target-codesign %t/%target-library-name(resilient_struct)

// RUN: %target-build-swift %s -lresilient_struct -I %t -L %t -o %t/main %target-rpath(%t)
// RUN: %target-codesign %t/main

// RUN: %target-run %t/main %t/%target-library-name(resilient_struct)

// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_struct_wmo)) -enable-library-evolution %S/../Inputs/resilient_struct.swift -emit-module -emit-module-path %t/resilient_struct.swiftmodule -module-name resilient_struct -whole-module-optimization
// RUN: %target-codesign %t/%target-library-name(resilient_struct_wmo)

// RUN: %target-build-swift %s -lresilient_struct_wmo -I %t -L %t -o %t/main2 %target-rpath(%t) -module-name main
// RUN: %target-codesign %t/main2

// RUN: %target-run %t/main2 %t/%target-library-name(resilient_struct_wmo)

// REQUIRES: executable_test

import StdlibUnittest


import resilient_struct

var ResilientStructTestSuite = TestSuite("ResilientStruct")

ResilientStructTestSuite.test("ResilientValue") {
  for b in [false, true] {
    let r = ResilientBool(b: b)
    expectEqual(b, r.b)
  }
  for i in [-12, -1, 12] {
    let r = ResilientInt(i: i)
    expectEqual(i, r.i)
  }
  for d in [-1.0, 0.0, -0.0, 1.0] {
    let r = ResilientDouble(d: d)
    expectEqual(d, r.d)
  }
}

ResilientStructTestSuite.test("StaticLayout") {
  for b1 in [false, true] {
    for i in [-12, -1, 12] {
      for b2 in [false, true] {
        for d in [-1.0, 0.0, -0.0, 1.0] {
          let r = ResilientLayoutRuntimeTest(b1: ResilientBool(b: b1),
                                             i: ResilientInt(i: i),
                                             b2: ResilientBool(b: b2),
                                             d: ResilientDouble(d: d))
          expectEqual(b1, r.b1.b)
          expectEqual(i,  r.i.i)
          expectEqual(b2, r.b2.b)
          expectEqual(d,  r.d.d)
        }
      }
    }
  }
}

// Make sure structs with dynamic layout are instantiated correctly,
// and can conform to protocols.
protocol MyResilientLayoutProtocol {
  var b1: ResilientBool { get }
}

struct MyResilientLayoutRuntimeTest : MyResilientLayoutProtocol {
  let b1: ResilientBool
  let i: ResilientInt
  let b2: ResilientBool
  let d: ResilientDouble

  init(b1: ResilientBool, i: ResilientInt, b2: ResilientBool, d: ResilientDouble) {
    self.b1 = b1
    self.i = i
    self.b2 = b2
    self.d = d
  }
}

@inline(never) func getMetadata() -> Any.Type {
  return MyResilientLayoutRuntimeTest.self
}

ResilientStructTestSuite.test("DynamicLayoutMetatype") {
  do {
    var output = ""
    let expected = "- main.MyResilientLayoutRuntimeTest #0\n"
    dump(getMetadata(), to: &output)
    expectEqual(output, expected)
  }
  do {
    expectEqual(true, getMetadata() == getMetadata())
  }
}

ResilientStructTestSuite.test("DynamicLayout") {
  for b1 in [false, true] {
    for i in [-12, -1, 12] {
      for b2 in [false, true] {
        for d in [-1.0, 0.0, -0.0, 1.0] {
          let r = MyResilientLayoutRuntimeTest(b1: ResilientBool(b: b1),
                                               i: ResilientInt(i: i),
                                               b2: ResilientBool(b: b2),
                                               d: ResilientDouble(d: d))
          expectEqual(b1, r.b1.b)
          expectEqual(i,  r.i.i)
          expectEqual(b2, r.b2.b)
          expectEqual(d,  r.d.d)
        }
      }
    }
  }
}

@inline(never) func getB(_ p: MyResilientLayoutProtocol) -> Bool {
  return p.b1.b
}

ResilientStructTestSuite.test("DynamicLayoutConformance") {
  do {
    let r = MyResilientLayoutRuntimeTest(b1: ResilientBool(b: true),
                                         i: ResilientInt(i: 0),
                                         b2: ResilientBool(b: false),
                                         d: ResilientDouble(d: 0.0))
    expectEqual(getB(r), true)
  }
}

protocol ProtocolWithAssociatedType {
  associatedtype T: MyResilientLayoutProtocol

  func getT() -> T
}

struct StructWithDependentAssociatedType : ProtocolWithAssociatedType {
  let r: MyResilientLayoutRuntimeTest

  init(r: MyResilientLayoutRuntimeTest) {
    self.r = r
  }

  func getT() -> MyResilientLayoutRuntimeTest {
    return r
  }
}

@inline(never) func getAssociatedType<T : ProtocolWithAssociatedType>(_ p: T)
    -> MyResilientLayoutProtocol.Type {
  return T.T.self
}

ResilientStructTestSuite.test("DynamicLayoutAssociatedType") {
  do {
    let r = MyResilientLayoutRuntimeTest(b1: ResilientBool(b: true),
                                         i: ResilientInt(i: 0),
                                         b2: ResilientBool(b: false),
                                         d: ResilientDouble(d: 0.0))
    let metatype: MyResilientLayoutProtocol.Type =
        MyResilientLayoutRuntimeTest.self
    let associated: MyResilientLayoutProtocol.Type =
        getAssociatedType(StructWithDependentAssociatedType(r: r));
    expectEqual(true, metatype == associated)
    expectEqual(getB(r), true)
  }
}

runAllTests()