File: archive_attributes.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 (171 lines) | stat: -rw-r--r-- 3,978 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
170
171
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name=test -DENCODE -o %t/encode
// RUN: %target-run %t/encode %t/test.arc
// RUN: plutil -p %t/test.arc | %FileCheck -check-prefix=CHECK-ARCHIVE %s

// RUN: %target-build-swift %s -module-name=test -o %t/decode
// RUN: %target-run %t/decode %t/test.arc --stdlib-unittest-in-process

// REQUIRES: executable_test
// REQUIRES: objc_interop
// REQUIRES: CPU=i386 || CPU=x86_64

// See also archive_attributes_stable_abi.swift, for the stable ABI
// deployment target test.

import Foundation
import StdlibUnittest

struct ABC {
  // CHECK-ARCHIVE-DAG: "$classname" => "nested_class_coding"
  @objc(nested_class_coding)
  class NestedClass : NSObject, NSCoding {
    var i : Int

    init(_ ii: Int) {
      i = ii
    }

    required init(coder aDecoder: NSCoder) {
      i = aDecoder.decodeInteger(forKey: "i")
    }

    func encode(with aCoder: NSCoder) {
      aCoder.encode(i, forKey: "i")
    }
  }
}

// CHECK-ARCHIVE-DAG: "$classname" => "private_class_coding"
@objc(private_class_coding)
private class PrivateClass : NSObject, NSCoding {
  var pi : Int

  init(_ ii: Int) {
    pi = ii
  }

  required init(coder aDecoder: NSCoder) {
    pi = aDecoder.decodeInteger(forKey: "pi")
  }

  func encode(with aCoder: NSCoder) {
    aCoder.encode(pi, forKey: "pi")
  }
}

class GenericClass<T> : NSObject, NSCoding {
  var gi : T? = nil

  override init() {
  }

  required init(coder aDecoder: NSCoder) {
  }

  func encode(with aCoder: NSCoder) {
  }
}

// CHECK-ARCHIVE-DAG: "$classname" => "test.IntClass"
class IntClass : GenericClass<Int> {

  init(ii: Int) {
    super.init()
    gi = ii
  }

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    gi = aDecoder.decodeInteger(forKey: "gi")
  }

  override func encode(with aCoder: NSCoder) {
    aCoder.encode(gi!, forKey: "gi")
  }
}

// CHECK-ARCHIVE-DAG: "$classname" => "double_class_coding"
@objc(double_class_coding)
class DoubleClass : GenericClass<Double> {

  init(dd: Double) {
    super.init()
    gi = dd
  }

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    gi = aDecoder.decodeDouble(forKey: "gi")
  }

  override func encode(with aCoder: NSCoder) {
    aCoder.encode(gi!, forKey: "gi")
  }
}

// CHECK-ARCHIVE-DAG: "$classname" => "top_level_coding"
@objc(top_level_coding)
class TopLevel : NSObject, NSCoding {
  var tli : Int

  var nested: ABC.NestedClass?
  fileprivate var priv: PrivateClass?
  var intc : IntClass?
  var doublec : DoubleClass?

  init(_ ii: Int) {
    tli = ii
  }

  required init(coder aDecoder: NSCoder) {
    tli = aDecoder.decodeInteger(forKey: "tli")
    nested = aDecoder.decodeObject(forKey: "nested") as? ABC.NestedClass
    priv = aDecoder.decodeObject(forKey: "priv") as? PrivateClass
    intc = aDecoder.decodeObject(forKey: "int") as? IntClass
    doublec = aDecoder.decodeObject(forKey: "double") as? DoubleClass
  }

  func encode(with aCoder: NSCoder) {
    aCoder.encode(tli, forKey: "tli")
    aCoder.encode(nested, forKey: "nested")
    aCoder.encode(priv, forKey: "priv")
    aCoder.encode(intc, forKey: "int")
    aCoder.encode(doublec, forKey: "double")
  }
}

#if ENCODE
let c = TopLevel(27)
c.nested = ABC.NestedClass(28)
c.priv = PrivateClass(29)
c.intc = IntClass(ii: 42)
c.doublec = DoubleClass(dd: 3.14)

NSKeyedArchiver.archiveRootObject(c, toFile: CommandLine.arguments[1])
#else
var DecodeTestSuite = TestSuite("Decode")

DecodeTestSuite.test("Decode") {
  func doIt() {
    let u = NSKeyedUnarchiver.unarchiveObject(withFile: CommandLine.arguments[1])!
    let x = u as! TopLevel
    expectEqual(27, x.tli)
    expectEqual(28, x.nested!.i)
    expectEqual(29, x.priv!.pi)
    expectEqual(42, x.intc!.gi!)
    expectEqual(3.14, x.doublec!.gi!)
  }

  if CommandLine.arguments[2] == "NEW" {
    if #available(macOS 10.14.4, iOS 12.2, tvOS 12.2, watchOS 5.2, *) {
      doIt()
    }
    return
  }

  doIt()
}

runAllTests()
#endif