Skip to content

Commit ae494b7

Browse files
committed
Merge branch 'master' of github.com:apple/swift-corelibs-foundation into codable-urlcomponents-2
2 parents b45abfc + 1a068fb commit ae494b7

File tree

6 files changed

+165
-62
lines changed

6 files changed

+165
-62
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,15 +1316,18 @@ _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (*
13161316
return thread;
13171317
}
13181318

1319-
CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
1319+
CF_SWIFT_EXPORT int _CFThreadSetName(pthread_t thread, const char *_Nonnull name) {
13201320
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
1321-
pthread_setname_np(name);
1321+
if (pthread_equal(pthread_self(), thread)) {
1322+
return pthread_setname_np(name);
1323+
}
1324+
return EINVAL;
13221325
#elif DEPLOYMENT_TARGET_LINUX
1323-
pthread_setname_np(pthread_self(), name);
1326+
return pthread_setname_np(thread, name);
13241327
#endif
13251328
}
13261329

1327-
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length) {
1330+
CF_SWIFT_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length) {
13281331
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
13291332
return pthread_getname_np(pthread_self(), buf, length);
13301333
#elif DEPLOYMENT_TARGET_ANDROID

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ typedef pthread_t _CFThreadRef;
316316

317317
CF_EXPORT _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *restrict _Nullable context);
318318

319-
CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name);
320-
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length);
319+
CF_SWIFT_EXPORT int _CFThreadSetName(pthread_t thread, const char *_Nonnull name);
320+
CF_SWIFT_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length);
321321

322322
CF_EXPORT Boolean _CFCharacterSetIsLongCharacterMember(CFCharacterSetRef theSet, UTF32Char theChar);
323323
CF_EXPORT CFCharacterSetRef _CFCharacterSetCreateCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet);

Foundation/NSKeyedArchiverHelpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal class _NSKeyedArchiverUID : NSObject {
2929
}
3030

3131
open override var hash: Int {
32-
return Int(bitPattern: CFHash(_cfObject as CFTypeRef!))
32+
return Int(bitPattern: CFHash(_cfObject as CFTypeRef?))
3333
}
3434

3535
open override func isEqual(_ object: Any?) -> Bool {

Foundation/Thread.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ internal enum _NSThreadStatus {
4343
private func NSThreadStart(_ context: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
4444
let thread: Thread = NSObject.unretainedReference(context!)
4545
Thread._currentThread.set(thread)
46+
if let name = thread.name {
47+
_CFThreadSetName(pthread_self(), name)
48+
}
4649
thread._status = .executing
4750
thread.main()
4851
thread._status = .finished
@@ -141,11 +144,12 @@ open class Thread : NSObject {
141144
}
142145

143146
internal var _main: () -> Void = {}
144-
#if os(OSX) || os(iOS) || CYGWIN
145-
private var _thread: pthread_t? = nil
146-
#elseif os(Linux) || os(Android)
147+
#if os(Android)
147148
private var _thread = pthread_t()
149+
#else
150+
private var _thread: pthread_t? = nil
148151
#endif
152+
149153
#if CYGWIN
150154
internal var _attr : pthread_attr_t? = nil
151155
#else
@@ -202,8 +206,8 @@ open class Thread : NSObject {
202206

203207
open var name: String? {
204208
didSet {
205-
if _thread == Thread.current._thread {
206-
_CFThreadSetName(name)
209+
if let thread = _thread {
210+
_CFThreadSetName(thread, name ?? "" )
207211
}
208212
}
209213
}

TestFoundation/TestCodable.swift

Lines changed: 95 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,17 @@ private func makePersonNameComponents(namePrefix: String? = nil,
3333
return result
3434
}
3535

36-
func expectRoundTripEquality<T : Codable>(of value: T, encode: (T) throws -> Data, decode: (Data) throws -> T) where T : Equatable {
37-
let data: Data
36+
func expectRoundTripEquality<T : Codable>(of value: T, encode: (T) throws -> Data, decode: (Data) throws -> T) throws where T : Equatable {
3837
do {
39-
data = try encode(value)
40-
} catch {
41-
fatalError("Unable to encode \(T.self) <\(value)>: \(error)")
42-
}
43-
44-
let decoded: T
45-
do {
46-
decoded = try decode(data)
47-
} catch {
48-
fatalError("Unable to decode \(T.self) <\(value)>: \(error)")
38+
let data = try encode(value)
39+
let decoded: T = try decode(data)
40+
if value != decoded {
41+
throw NSError(domain: "Decode mismatch", code: 0, userInfo: ["msg": "Decoded \(T.self) <\(decoded)> not equal to original <\(value)>"])
42+
}
4943
}
50-
51-
XCTAssertEqual(value, decoded, "Decoded \(T.self) <\(decoded)> not equal to original <\(value)>")
5244
}
5345

54-
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) where T : Equatable {
46+
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) throws where T : Equatable {
5547
let inf = "INF", negInf = "-INF", nan = "NaN"
5648
let encode = { (_ value: T) throws -> Data in
5749
let encoder = JSONEncoder()
@@ -69,7 +61,7 @@ func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) where T : Equ
6961
return try decoder.decode(T.self, from: data)
7062
}
7163

72-
expectRoundTripEquality(of: value, encode: encode, decode: decode)
64+
try expectRoundTripEquality(of: value, encode: encode, decode: decode)
7365
}
7466

7567
// MARK: - Helper Types
@@ -98,7 +90,11 @@ class TestCodable : XCTestCase {
9890

9991
func test_PersonNameComponents_JSON() {
10092
for components in personNameComponentsValues {
101-
expectRoundTripEqualityThroughJSON(for: components)
93+
do {
94+
try expectRoundTripEqualityThroughJSON(for: components)
95+
} catch let error {
96+
XCTFail("\(error) for \(components)")
97+
}
10298
}
10399
}
104100

@@ -113,7 +109,11 @@ class TestCodable : XCTestCase {
113109
func test_UUID_JSON() {
114110
for uuid in uuidValues {
115111
// We have to wrap the UUID since we cannot have a top-level string.
116-
expectRoundTripEqualityThroughJSON(for: UUIDCodingWrapper(uuid))
112+
do {
113+
try expectRoundTripEqualityThroughJSON(for: UUIDCodingWrapper(uuid))
114+
} catch let error {
115+
XCTFail("\(error) for \(uuid)")
116+
}
117117
}
118118
}
119119

@@ -128,7 +128,11 @@ class TestCodable : XCTestCase {
128128

129129
func test_URL_JSON() {
130130
for url in urlValues {
131-
expectRoundTripEqualityThroughJSON(for: url)
131+
do {
132+
try expectRoundTripEqualityThroughJSON(for: url)
133+
} catch let error {
134+
XCTFail("\(error) for \(url)")
135+
}
132136
}
133137
}
134138

@@ -141,7 +145,11 @@ class TestCodable : XCTestCase {
141145

142146
func test_NSRange_JSON() {
143147
for range in nsrangeValues {
144-
expectRoundTripEqualityThroughJSON(for: range)
148+
do {
149+
try expectRoundTripEqualityThroughJSON(for: range)
150+
} catch let error {
151+
XCTFail("\(error) for \(range)")
152+
}
145153
}
146154
}
147155

@@ -159,7 +167,11 @@ class TestCodable : XCTestCase {
159167

160168
func test_Locale_JSON() {
161169
for locale in localeValues {
162-
expectRoundTripEqualityThroughJSON(for: locale)
170+
do {
171+
try expectRoundTripEqualityThroughJSON(for: locale)
172+
} catch let error {
173+
XCTFail("\(error) for \(locale)")
174+
}
163175
}
164176
}
165177

@@ -172,7 +184,11 @@ class TestCodable : XCTestCase {
172184

173185
func test_IndexSet_JSON() {
174186
for indexSet in indexSetValues {
175-
expectRoundTripEqualityThroughJSON(for: indexSet)
187+
do {
188+
try expectRoundTripEqualityThroughJSON(for: indexSet)
189+
} catch let error {
190+
XCTFail("\(error) for \(indexSet)")
191+
}
176192
}
177193
}
178194

@@ -186,7 +202,11 @@ class TestCodable : XCTestCase {
186202

187203
func test_IndexPath_JSON() {
188204
for indexPath in indexPathValues {
189-
expectRoundTripEqualityThroughJSON(for: indexPath)
205+
do {
206+
try expectRoundTripEqualityThroughJSON(for: indexPath)
207+
} catch let error {
208+
XCTFail("\(error) for \(indexPath)")
209+
}
190210
}
191211
}
192212

@@ -210,7 +230,11 @@ class TestCodable : XCTestCase {
210230

211231
func test_AffineTransform_JSON() {
212232
for transform in affineTransformValues {
213-
expectRoundTripEqualityThroughJSON(for: transform)
233+
do {
234+
try expectRoundTripEqualityThroughJSON(for: transform)
235+
} catch let error {
236+
XCTFail("\(error) for \(transform)")
237+
}
214238
}
215239
}
216240

@@ -226,7 +250,11 @@ class TestCodable : XCTestCase {
226250

227251
func test_Decimal_JSON() {
228252
for decimal in decimalValues {
229-
expectRoundTripEqualityThroughJSON(for: decimal)
253+
do {
254+
try expectRoundTripEqualityThroughJSON(for: decimal)
255+
} catch let error {
256+
XCTFail("\(error) for \(decimal)")
257+
}
230258
}
231259
}
232260

@@ -242,7 +270,11 @@ class TestCodable : XCTestCase {
242270

243271
func test_CGPoint_JSON() {
244272
for point in cgpointValues {
245-
expectRoundTripEqualityThroughJSON(for: point)
273+
do {
274+
try expectRoundTripEqualityThroughJSON(for: point)
275+
} catch let error {
276+
XCTFail("\(error) for \(point)")
277+
}
246278
}
247279
}
248280

@@ -258,7 +290,11 @@ class TestCodable : XCTestCase {
258290

259291
func test_CGSize_JSON() {
260292
for size in cgsizeValues {
261-
expectRoundTripEqualityThroughJSON(for: size)
293+
do {
294+
try expectRoundTripEqualityThroughJSON(for: size)
295+
} catch let error {
296+
XCTFail("\(error) for \(size)")
297+
}
262298
}
263299
}
264300

@@ -275,7 +311,11 @@ class TestCodable : XCTestCase {
275311

276312
func test_CGRect_JSON() {
277313
for rect in cgrectValues {
278-
expectRoundTripEqualityThroughJSON(for: rect)
314+
do {
315+
try expectRoundTripEqualityThroughJSON(for: rect)
316+
} catch let error {
317+
XCTFail("\(error) for \(rect)")
318+
}
279319
}
280320
}
281321

@@ -301,7 +341,11 @@ class TestCodable : XCTestCase {
301341

302342
func test_CharacterSet_JSON() {
303343
for characterSet in characterSetValues {
304-
expectRoundTripEqualityThroughJSON(for: characterSet)
344+
do {
345+
try expectRoundTripEqualityThroughJSON(for: characterSet)
346+
} catch let error {
347+
XCTFail("\(error) for \(characterSet)")
348+
}
305349
}
306350
}
307351

@@ -330,7 +374,11 @@ class TestCodable : XCTestCase {
330374

331375
func test_TimeZone_JSON() {
332376
for timeZone in timeZoneValues {
333-
expectRoundTripEqualityThroughJSON(for: timeZone)
377+
do {
378+
try expectRoundTripEqualityThroughJSON(for: timeZone)
379+
} catch let error {
380+
XCTFail("\(error) for \(timeZone)")
381+
}
334382
}
335383
}
336384

@@ -366,7 +414,11 @@ class TestCodable : XCTestCase {
366414

367415
func test_Calendar_JSON() {
368416
for calendar in calendarValues {
369-
expectRoundTripEqualityThroughJSON(for: calendar)
417+
do {
418+
try expectRoundTripEqualityThroughJSON(for: calendar)
419+
} catch let error {
420+
XCTFail("\(error) for \(calendar)")
421+
}
370422
}
371423
}
372424

@@ -403,14 +455,22 @@ class TestCodable : XCTestCase {
403455
#endif
404456

405457
let components = calendar.dateComponents(dateComponents, from: Date(timeIntervalSince1970: 1501283776))
406-
expectRoundTripEqualityThroughJSON(for: components)
458+
do {
459+
try expectRoundTripEqualityThroughJSON(for: components)
460+
} catch let error {
461+
XCTFail("\(error)")
462+
}
407463
}
408464

409465
// MARK: - Measurement
410466
func test_Measurement_JSON() {
411-
expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitAcceleration.metersPerSecondSquared))
412-
expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitMass.kilograms))
413-
expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitLength.miles))
467+
do {
468+
try expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitAcceleration.metersPerSecondSquared))
469+
try expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitMass.kilograms))
470+
try expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitLength.miles))
471+
} catch let error {
472+
XCTFail("\(error)")
473+
}
414474
}
415475

416476
// MARK: - URLComponents

0 commit comments

Comments
 (0)