From 4f3dacae3a26542f31dd54a5ffd952e310c40adc Mon Sep 17 00:00:00 2001 From: Christian Bieniak Date: Mon, 13 Mar 2017 13:31:31 +1100 Subject: [PATCH 1/4] [SR-4005] Allow heterogenous comparisons in elementsEqual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a user is supplying a predicate to compare the type equivalence isn’t required --- stdlib/public/core/SequenceAlgorithms.swift.gyb | 6 ++++-- test/IDE/complete_from_stdlib.swift | 2 +- test/api-digester/source-stability.swift.expected | 1 + 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/stdlib/public/core/SequenceAlgorithms.swift.gyb b/stdlib/public/core/SequenceAlgorithms.swift.gyb index 1728740360cd2..fbfb96eb87e6d 100644 --- a/stdlib/public/core/SequenceAlgorithms.swift.gyb +++ b/stdlib/public/core/SequenceAlgorithms.swift.gyb @@ -338,12 +338,14 @@ ${equivalenceExplanation} public func elementsEqual( _ other: OtherSequence${"," if preds else ""} % if preds: - by areEquivalent: (Element, Element) throws -> Bool + by areEquivalent: (Element, OtherSequence.Element) throws -> Bool % end ) ${rethrows_}-> Bool where - OtherSequence: Sequence, + OtherSequence: Sequence${" {" if preds else ","} +% if not preds: OtherSequence.Element == Element { +% end var iter1 = self.makeIterator() var iter2 = other.makeIterator() diff --git a/test/IDE/complete_from_stdlib.swift b/test/IDE/complete_from_stdlib.swift index eb3e0f5cffc70..06d81b99271a9 100644 --- a/test/IDE/complete_from_stdlib.swift +++ b/test/IDE/complete_from_stdlib.swift @@ -170,7 +170,7 @@ func testArchetypeReplacement3 (_ a : [Int]) { // PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceVar]/Super: first[#Int?#] // PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: map({#(transform): (Int) throws -> T##(Int) throws -> T#})[' rethrows'][#[T]#] // PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: dropLast({#(n): Int#})[#ArraySlice#] -// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: elementsEqual({#(other): Sequence#}, {#by: (Int, Int) throws -> Bool##(Int, Int) throws -> Bool#})[' rethrows'][#Bool#] +// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: elementsEqual({#(other): Sequence#}, {#by: (Int, IteratorProtocol.Element) throws -> Bool##(Int, IteratorProtocol.Element) throws -> Bool#})[' rethrows'][#Bool#] protocol P2 { diff --git a/test/api-digester/source-stability.swift.expected b/test/api-digester/source-stability.swift.expected index 3d113b5d58bc0..8acded0980cba 100644 --- a/test/api-digester/source-stability.swift.expected +++ b/test/api-digester/source-stability.swift.expected @@ -328,6 +328,7 @@ Func ReversedRandomAccessCollection.index(_:offsetBy:) has return type change fr Func ReversedRandomAccessCollection.index(_:offsetBy:limitedBy:) has return type change from ReversedRandomAccessIndex? to ReversedRandomAccessCollection.Index? Func ReversedRandomAccessCollection.index(after:) has return type change from ReversedRandomAccessIndex to ReversedRandomAccessCollection.Index Func ReversedRandomAccessCollection.index(before:) has return type change from ReversedRandomAccessIndex to ReversedRandomAccessCollection.Index +Func Sequence.elementsEqual(_:by:) has 2nd parameter type change from (Self.Iterator.Element, Self.Iterator.Element) throws -> Bool to (Self.Iterator.Element, OtherSequence.Iterator.Element) throws -> Bool Func Set.formSymmetricDifference(_:) has parameter 0 type change from Set to Set Func Set.index(after:) has return type change from SetIndex to Set.Index Func Set.index(of:) has return type change from SetIndex? to Set.Index? From f05b3944c98155d354681eab9e3fb840cc993139 Mon Sep 17 00:00:00 2001 From: Christian Bieniak Date: Sun, 30 Apr 2017 17:04:48 +1000 Subject: [PATCH 2/4] elementsEqualWithPredicate tests Compares a string of a number with an integer value by using the elementsEqualPredicate closure --- .../CheckSequenceType.swift | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift b/stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift index 721cba0a6a974..e1551c40cccfa 100644 --- a/stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift +++ b/stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift @@ -78,6 +78,33 @@ public struct ElementsEqualTest { } } +public struct ElementsEqualWithPredicateTest { + public let expected: Bool + public let sequence: [Int] + public let other: [String] + public let predicate: (Int, String) -> Bool + public let expectedLeftoverSequence: [Int] + public let expectedLeftoverOther: [String] + public let loc: SourceLoc + + public init( + _ expected: Bool, _ sequence: [Int], _ other: [String], + _ predicate: @escaping (Int, String) -> Bool, + _ expectedLeftoverSequence: [Int], + _ expectedLeftoverOther: [String], + file: String = #file, line: UInt = #line, + comment: String = "" + ) { + self.expected = expected + self.sequence = sequence + self.other = other + self.predicate = predicate + self.expectedLeftoverSequence = expectedLeftoverSequence + self.expectedLeftoverOther = expectedLeftoverOther + self.loc = SourceLoc(file, line, comment: "test data" + comment) + } +} + public struct EnumerateTest { public let expected: [(Int, Int)] public let sequence: [Int] @@ -450,6 +477,30 @@ public let elementsEqualTests: [ElementsEqualTest] = [ ElementsEqualTest(false, [ 1, 2 ], [ 1, 2, 3, 4 ], [], [ 4 ]), ].flatMap { [ $0, $0.flip() ] } +func elementsEqualPredicate(_ x: Int, y: String) -> Bool { + if let intVal = Int(y) { + return x == intVal + } else { + return false + } +} + +public let elementsEqualWithPredicateTests: [ElementsEqualWithPredicateTest] = [ + ElementsEqualWithPredicateTest(true, [], [], elementsEqualPredicate, [], []), + + ElementsEqualWithPredicateTest(false, [ 1 ], [], elementsEqualPredicate, [ 1 ], []), + ElementsEqualWithPredicateTest(false, [], [ "1" ], elementsEqualPredicate, [], [ "1" ]), + + ElementsEqualWithPredicateTest(false, [ 1, 2 ], [], elementsEqualPredicate, [ 1, 2 ], []), + ElementsEqualWithPredicateTest(false, [], [ "1", "2" ], elementsEqualPredicate, [], [ "1", "2" ]), + + ElementsEqualWithPredicateTest(false, [ 1, 2, 3, 4 ], [ "1", "2" ], elementsEqualPredicate, [ 3, 4 ], []), + ElementsEqualWithPredicateTest(false, [ 1, 2 ], [ "1", "2", "3", "4" ], elementsEqualPredicate, [], [ "3", "4" ]), + + ElementsEqualWithPredicateTest(true, [ 1, 2, 3, 4 ], [ "1", "2", "3", "4" ], elementsEqualPredicate, [], []), + ElementsEqualWithPredicateTest(true, [ 1, 2 ], [ "1", "2" ], elementsEqualPredicate, [], []), +] + public let enumerateTests = [ EnumerateTest([], []), EnumerateTest([ (0, 10) ], [ 10 ]), From abf26338a42dd8c25876b1b317c9d7fb9fb6c812 Mon Sep 17 00:00:00 2001 From: Christian Bieniak Date: Sat, 3 Jun 2017 14:14:05 +1000 Subject: [PATCH 3/4] Update test expectations to use new sequence element types --- test/api-digester/source-stability.swift.expected | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/api-digester/source-stability.swift.expected b/test/api-digester/source-stability.swift.expected index 8acded0980cba..e1b3e5f90aeb2 100644 --- a/test/api-digester/source-stability.swift.expected +++ b/test/api-digester/source-stability.swift.expected @@ -328,7 +328,7 @@ Func ReversedRandomAccessCollection.index(_:offsetBy:) has return type change fr Func ReversedRandomAccessCollection.index(_:offsetBy:limitedBy:) has return type change from ReversedRandomAccessIndex? to ReversedRandomAccessCollection.Index? Func ReversedRandomAccessCollection.index(after:) has return type change from ReversedRandomAccessIndex to ReversedRandomAccessCollection.Index Func ReversedRandomAccessCollection.index(before:) has return type change from ReversedRandomAccessIndex to ReversedRandomAccessCollection.Index -Func Sequence.elementsEqual(_:by:) has 2nd parameter type change from (Self.Iterator.Element, Self.Iterator.Element) throws -> Bool to (Self.Iterator.Element, OtherSequence.Iterator.Element) throws -> Bool +Func Sequence.elementsEqual(_:by:) has parameter 1 type change from (Self.Iterator.Element, Self.Iterator.Element) throws -> Bool to (Self.Element, OtherSequence.Element) throws -> Bool Func Set.formSymmetricDifference(_:) has parameter 0 type change from Set to Set Func Set.index(after:) has return type change from SetIndex to Set.Index Func Set.index(of:) has return type change from SetIndex? to Set.Index? From 211a338569c5b7fc9c1dd95ed47b4085857645c9 Mon Sep 17 00:00:00 2001 From: Christian Bieniak Date: Tue, 28 Nov 2017 20:26:05 +1100 Subject: [PATCH 4/4] Update hardcoded test to reference sequence --- test/IDE/complete_from_stdlib.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/IDE/complete_from_stdlib.swift b/test/IDE/complete_from_stdlib.swift index 06d81b99271a9..b129421c1594e 100644 --- a/test/IDE/complete_from_stdlib.swift +++ b/test/IDE/complete_from_stdlib.swift @@ -170,7 +170,8 @@ func testArchetypeReplacement3 (_ a : [Int]) { // PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceVar]/Super: first[#Int?#] // PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: map({#(transform): (Int) throws -> T##(Int) throws -> T#})[' rethrows'][#[T]#] // PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: dropLast({#(n): Int#})[#ArraySlice#] -// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: elementsEqual({#(other): Sequence#}, {#by: (Int, IteratorProtocol.Element) throws -> Bool##(Int, IteratorProtocol.Element) throws -> Bool#})[' rethrows'][#Bool#] +// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: elementsEqual({#(other): Sequence#}, {#by: (Int, Sequence.Element) throws -> Bool##(Int, Sequence.Element) throws -> Bool#})[' rethrows'][#Bool#]; name=elementsEqual(other: Sequence, by: (Int, Sequence.Element) throws -> Bool) rethrows +// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: elementsEqual({#(other): Sequence#})[#Bool#]; name=elementsEqual(other: Sequence) protocol P2 {