|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | + |
| 10 | + |
| 11 | +func checkHashableMutations_ValueType<Item: Hashable, S: Sequence>( |
| 12 | + _ item: Item, |
| 13 | + _ keyPath: WritableKeyPath<Item, S.Element>, |
| 14 | + _ values: S, |
| 15 | + file: StaticString = #file, |
| 16 | + line: UInt = #line |
| 17 | +) { |
| 18 | + _checkHashableMutations( |
| 19 | + Item.self, Item.self, |
| 20 | + item, |
| 21 | + { $0 }, |
| 22 | + keyPath, |
| 23 | + values) |
| 24 | +} |
| 25 | + |
| 26 | +func checkHashableMutations_NSCopying<Item: NSObject & NSCopying, S: Sequence>( |
| 27 | + _ item: Item, |
| 28 | + _ keyPath: ReferenceWritableKeyPath<Item, S.Element>, |
| 29 | + _ values: S, |
| 30 | + file: StaticString = #file, |
| 31 | + line: UInt = #line |
| 32 | +) { |
| 33 | + _checkHashableMutations( |
| 34 | + Item.self, Item.self, |
| 35 | + item, |
| 36 | + { $0.copy() as! Item }, |
| 37 | + keyPath, |
| 38 | + values) |
| 39 | +} |
| 40 | + |
| 41 | +func checkHashableMutations_NSMutableCopying< |
| 42 | + Source: NSObject & NSMutableCopying, |
| 43 | + Target: NSObject & NSMutableCopying, |
| 44 | + S: Sequence |
| 45 | +>( |
| 46 | + _ item: Source, |
| 47 | + _ keyPath: ReferenceWritableKeyPath<Target, S.Element>, |
| 48 | + _ values: S, |
| 49 | + file: StaticString = #file, |
| 50 | + line: UInt = #line |
| 51 | +) { |
| 52 | + _checkHashableMutations( |
| 53 | + Source.self, Target.self, |
| 54 | + item, |
| 55 | + { $0.mutableCopy() as! Target }, |
| 56 | + keyPath, |
| 57 | + values) |
| 58 | +} |
| 59 | + |
| 60 | +// Check that mutating `object` via the specified key path affects its |
| 61 | +// hash value. |
| 62 | +func _checkHashableMutations<Source: Hashable, Target: Hashable, S: Sequence>( |
| 63 | + _ source: Source.Type, |
| 64 | + _ target: Target.Type, |
| 65 | + _ object: Source, |
| 66 | + _ copyBlock: (Source) -> Target, |
| 67 | + _ keyPath: WritableKeyPath<Target, S.Element>, |
| 68 | + _ values: S, |
| 69 | + file: StaticString = #file, |
| 70 | + line: UInt = #line |
| 71 | +) { |
| 72 | + let reference = copyBlock(object) |
| 73 | + let referenceHash = reference.hashValue |
| 74 | + |
| 75 | + XCTAssertEqual( |
| 76 | + reference.hashValue, referenceHash, |
| 77 | + "\(type(of: reference)).hashValue is nondeterministic", |
| 78 | + file: file, line: line) |
| 79 | + |
| 80 | + var found = false |
| 81 | + for value in values { |
| 82 | + var copy = copyBlock(object) |
| 83 | + XCTAssertEqual( |
| 84 | + copy, reference, |
| 85 | + "Invalid copy operation", |
| 86 | + file: file, line: line) |
| 87 | + XCTAssertEqual( |
| 88 | + copy.hashValue, referenceHash, |
| 89 | + "Invalid copy operation", |
| 90 | + file: file, line: line) |
| 91 | + copy[keyPath: keyPath] = value |
| 92 | + XCTAssertNotEqual( |
| 93 | + reference, copy, |
| 94 | + "\(keyPath) did not affect object equality", |
| 95 | + file: file, line: line) |
| 96 | + if referenceHash != copy.hashValue { |
| 97 | + found = true |
| 98 | + } |
| 99 | + } |
| 100 | + if !found { |
| 101 | + XCTFail( |
| 102 | + "\(keyPath) does not seem to contribute to the hash value", |
| 103 | + file: file, line: line) |
| 104 | + } |
| 105 | +} |
0 commit comments