|
| 1 | +// RUN: rm -rf %t && mkdir -p %t |
| 2 | +// RUN: %target-build-swift %s -module-name=test -o %t/a.out |
| 3 | +// RUN: %target-run %t/a.out %S/Inputs/test.arc | %FileCheck %s |
| 4 | + |
| 5 | +// REQUIRES: executable_test |
| 6 | +// REQUIRES: objc_interop |
| 7 | +// UNSUPPORTED: OS=tvos |
| 8 | +// UNSUPPORTED: OS=watchos |
| 9 | + |
| 10 | +// This test checks if an archive, produced with the swift 3.1 compiler, can |
| 11 | +// still be read with the current compiler. |
| 12 | + |
| 13 | +import Foundation |
| 14 | + |
| 15 | +struct ABC { |
| 16 | + class NestedClass : NSObject, NSCoding { |
| 17 | + var i : Int |
| 18 | + |
| 19 | + init(_ ii: Int) { |
| 20 | + i = ii |
| 21 | + } |
| 22 | + |
| 23 | + required init(coder aDecoder: NSCoder) { |
| 24 | + i = aDecoder.decodeInteger(forKey: "i") |
| 25 | + } |
| 26 | + |
| 27 | + func encode(with aCoder: NSCoder) { |
| 28 | + aCoder.encode(i, forKey: "i") |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +private class PrivateClass : NSObject, NSCoding { |
| 34 | + var pi : Int |
| 35 | + |
| 36 | + init(_ ii: Int) { |
| 37 | + pi = ii |
| 38 | + } |
| 39 | + |
| 40 | + required init(coder aDecoder: NSCoder) { |
| 41 | + pi = aDecoder.decodeInteger(forKey: "pi") |
| 42 | + } |
| 43 | + |
| 44 | + func encode(with aCoder: NSCoder) { |
| 45 | + aCoder.encode(pi, forKey: "pi") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class GenericClass<T> : NSObject, NSCoding { |
| 50 | + var gi : Int |
| 51 | + |
| 52 | + init(_ ii: Int) { |
| 53 | + gi = ii |
| 54 | + } |
| 55 | + |
| 56 | + required init(coder aDecoder: NSCoder) { |
| 57 | + gi = aDecoder.decodeInteger(forKey: "gi") |
| 58 | + } |
| 59 | + |
| 60 | + func encode(with aCoder: NSCoder) { |
| 61 | + aCoder.encode(gi, forKey: "gi") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class TopLevel : NSObject, NSCoding { |
| 66 | + var tli : Int |
| 67 | + |
| 68 | + var nested: ABC.NestedClass? |
| 69 | + fileprivate var priv: PrivateClass? |
| 70 | + var generic : GenericClass<Int>? |
| 71 | + |
| 72 | + init(_ ii: Int) { |
| 73 | + tli = ii |
| 74 | + } |
| 75 | + |
| 76 | + required init(coder aDecoder: NSCoder) { |
| 77 | + tli = aDecoder.decodeInteger(forKey: "tli") |
| 78 | + nested = aDecoder.decodeObject(forKey: "nested") as? ABC.NestedClass |
| 79 | + priv = aDecoder.decodeObject(forKey: "priv") as? PrivateClass |
| 80 | + generic = aDecoder.decodeObject(forKey: "generic") as? GenericClass<Int> |
| 81 | + } |
| 82 | + |
| 83 | + func encode(with aCoder: NSCoder) { |
| 84 | + aCoder.encode(tli, forKey: "tli") |
| 85 | + aCoder.encode(nested, forKey: "nested") |
| 86 | + aCoder.encode(priv, forKey: "priv") |
| 87 | + aCoder.encode(generic, forKey: "generic") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func main() { |
| 92 | + |
| 93 | + let args = CommandLine.arguments |
| 94 | + |
| 95 | + let g = GenericClass<Int>(42) |
| 96 | +#if ENCODE |
| 97 | + // This is how the archive was created with the swift 3.1 compiler. |
| 98 | + let c = TopLevel(27) |
| 99 | + c.nested = ABC.NestedClass(28) |
| 100 | + c.priv = PrivateClass(29) |
| 101 | + c.generic = g |
| 102 | + |
| 103 | + NSKeyedArchiver.archiveRootObject(c, toFile: args[1]) |
| 104 | +#else |
| 105 | + if let u = NSKeyedUnarchiver.unarchiveObject(withFile: args[1]) { |
| 106 | + if let x = u as? TopLevel { |
| 107 | + // CHECK: top-level: 27 |
| 108 | + print("top-level: \(x.tli)") |
| 109 | + if let n = x.nested { |
| 110 | + // CHECK: nested: 28 |
| 111 | + print("nested: \(n.i)") |
| 112 | + } |
| 113 | + if let p = x.priv { |
| 114 | + // CHECK: private: 29 |
| 115 | + print("private: \(p.pi)") |
| 116 | + } |
| 117 | + if let g = x.generic { |
| 118 | + // CHECK: generic: 42 |
| 119 | + print("generic: \(g.gi)") |
| 120 | + } |
| 121 | + } else { |
| 122 | + print(u) |
| 123 | + } |
| 124 | + } else { |
| 125 | + print("nil") |
| 126 | + } |
| 127 | +#endif |
| 128 | +} |
| 129 | + |
| 130 | +main() |
| 131 | + |
0 commit comments