From 14d084a56ab8cdf9e238ed4721b531fc8773fa20 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 18:30:59 -0800 Subject: [PATCH 01/14] custom-set: Rename test for removeAll To avoid confusion with forthcoming test for isEmpty --- .../Tests/CustomSetTests/CustomSetTests.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index 41fd913ec..e9940b90b 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -40,15 +40,16 @@ class CustomSetTests: XCTestCase { XCTAssertFalse(CustomSet([1.0, 2.0]).isDisjoint(CustomSet([2.0, 3.0]))) } - func testEmptyMethod() { + func testRemoveAllMethod() { var expected1 = CustomSet([1, 2]) expected1.removeAll() - XCTAssertEqual(CustomSet(emptyTypedArray ), expected1 ) + XCTAssertEqual(CustomSet(emptyTypedArray), expected1) - var expected2 = CustomSet(emptyTypedArray ) + var expected2 = CustomSet(emptyTypedArray) expected2.removeAll() - XCTAssertEqual(CustomSet(emptyTypedArray ), expected2 ) + XCTAssertEqual(CustomSet(emptyTypedArray), expected2) } + func testIntersection() { XCTAssertEqual(CustomSet(["a", "c"]), CustomSet(["a", "b", "c"]).intersection(CustomSet(["a", "c", "d"]))) @@ -115,7 +116,7 @@ class CustomSetTests: XCTestCase { ("testDeleteMethod", testDeleteMethod), ("testDifference", testDifference), ("testDisjoint", testDisjoint), - ("testEmptyMethod", testEmptyMethod), + ("testRemoveAllMethod", testRemoveAllMethod), ("testIntersection", testIntersection), ("testMember", testMember), ("testPutMethod", testPutMethod), From 4d979ead4572ce71d897cbfc6506b9ae6acd4170 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 18:57:50 -0800 Subject: [PATCH 02/14] custom-set: Add isEmpty computed prop + tests --- .../Sources/CustomSet/CustomSetExample.swift | 1 + .../Tests/CustomSetTests/CustomSetTests.swift | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift index 9d52056d4..602637cbe 100644 --- a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift +++ b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift @@ -14,6 +14,7 @@ struct CustomSet: Equatable { fileprivate var contents = [Element: Bool]() + var isEmpty: Bool { return contents.count == 0 } var size: Int { return contents.count } init(_ sequence: S) where S.Iterator.Element == Element { diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index e9940b90b..da81c806c 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -4,6 +4,14 @@ import XCTest class CustomSetTests: XCTestCase { let emptyTypedArray = [Int]() + func testSetsWithNoElementsAreEmpty() { + XCTAssertTrue(CustomSet(emptyTypedArray).isEmpty) + } + + func testSetsWithElementsAreNotEmpty() { + XCTAssertFalse(CustomSet([1]).isEmpty) + } + func testEqual() { XCTAssertEqual(CustomSet([1, 3]), CustomSet([3, 1])) XCTAssertNotEqual(CustomSet([1, 3]), CustomSet([3, 1, 5])) @@ -111,6 +119,8 @@ class CustomSetTests: XCTestCase { static var allTests: [(String, (CustomSetTests) -> () throws -> Void)] { return [ + ("testSetsWithNoElementsAreEmpty", testSetsWithNoElementsAreEmpty), + ("testSetsWithElementsAreNotEmpty", testSetsWithElementsAreNotEmpty), ("testEqual", testEqual), ("testNoDuplicates", testNoDuplicates), ("testDeleteMethod", testDeleteMethod), From 58c7df9f1eabc27c2d257e47ed8c2ad56c52ba58 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 19:35:21 -0800 Subject: [PATCH 03/14] custom-set: Rename+move contains func to match problem spec --- .../Sources/CustomSet/CustomSetExample.swift | 15 +++++++------- .../Tests/CustomSetTests/CustomSetTests.swift | 20 +++++++++++++------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift index 602637cbe..a20a7147c 100644 --- a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift +++ b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift @@ -21,6 +21,14 @@ struct CustomSet: Equatable { self.contents = [:] _ = sequence.map { self.contents[$0] = true } } + + func contains(_ item: Element) -> Bool { + if contents[item] != nil { + return true + } + return false + } + mutating func put(_ item: Element) { contents[item] = true @@ -73,11 +81,4 @@ struct CustomSet: Equatable { } return true } - - func containsMember(_ item: Element) -> Bool { - if contents[item] != nil { - return true} - return false - } - } diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index da81c806c..f8a0a6505 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -12,6 +12,16 @@ class CustomSetTests: XCTestCase { XCTAssertFalse(CustomSet([1]).isEmpty) } + func testNothingIsContainedInAnEmptySet() { + XCTAssertFalse(CustomSet(emptyTypedArray).contains(1)) + } + func testWhenTheElementIsInTheSet() { + XCTAssertTrue(CustomSet([1, 2, 3]).contains(1)) + } + func testWhenTheElementIsNotInTheSet() { + XCTAssertFalse(CustomSet([1, 2, 3]).contains(4)) + } + func testEqual() { XCTAssertEqual(CustomSet([1, 3]), CustomSet([3, 1])) XCTAssertNotEqual(CustomSet([1, 3]), CustomSet([3, 1, 5])) @@ -66,11 +76,7 @@ class CustomSetTests: XCTestCase { CustomSet([1.0, 2.0, 3.0]).intersection(CustomSet([3.0]))) } - func testMember() { - XCTAssertTrue(CustomSet([1, 2, 3]).containsMember(2)) - XCTAssertTrue(CustomSet([1, 2, 3]).containsMember(3)) - XCTAssertFalse(CustomSet([1, 2, 3]).containsMember(4)) - } + func testPutMethod() { var expected1 = CustomSet([1, 2, 4]) @@ -121,6 +127,9 @@ class CustomSetTests: XCTestCase { return [ ("testSetsWithNoElementsAreEmpty", testSetsWithNoElementsAreEmpty), ("testSetsWithElementsAreNotEmpty", testSetsWithElementsAreNotEmpty), + ("testNothingIsContainedInAnEmptySet", testNothingIsContainedInAnEmptySet), + ("testWhenTheElementIsInTheSet", testWhenTheElementIsInTheSet), + ("testWhenTheElementIsNotInTheSet", testWhenTheElementIsNotInTheSet), ("testEqual", testEqual), ("testNoDuplicates", testNoDuplicates), ("testDeleteMethod", testDeleteMethod), @@ -128,7 +137,6 @@ class CustomSetTests: XCTestCase { ("testDisjoint", testDisjoint), ("testRemoveAllMethod", testRemoveAllMethod), ("testIntersection", testIntersection), - ("testMember", testMember), ("testPutMethod", testPutMethod), ("testSize", testSize), ("testSubsetMethod", testSubsetMethod), From dbe2d582c14cf05eb972d73ddb2d04d0cefa82bc Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 21:10:48 -0800 Subject: [PATCH 04/14] custom-set: Revise subset method to match spec --- .../Sources/CustomSet/CustomSetExample.swift | 11 ++++-- .../Tests/CustomSetTests/CustomSetTests.swift | 35 +++++++++++++------ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift index a20a7147c..3b1ea74bd 100644 --- a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift +++ b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift @@ -67,11 +67,16 @@ struct CustomSet: Equatable { } return CustomSet(temp.keys) } - func isSupersetOf (_ item: CustomSet) -> Bool { - - return item.contents.count == item.contents.filter { self.contents.keys.contains($0.0) }.count + func isSubsetOf(_ other: CustomSet) -> Bool { + for elem in Array(self.contents.keys) { + if !other.contents.keys.contains(elem) { + return false + } + } + return true } + func isDisjoint(_ item: CustomSet) -> Bool { for each in Array(item.contents.keys) { diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index f8a0a6505..ac3a3aa52 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -22,6 +22,25 @@ class CustomSetTests: XCTestCase { XCTAssertFalse(CustomSet([1, 2, 3]).contains(4)) } + func testEmptySetIsASubsetOfAnotherEmptySet() { + XCTAssertTrue(CustomSet(emptyTypedArray).isSubsetOf(CustomSet([]))) + } + func testEmptySetIsASubsetOfNonEmptySet() { + XCTAssertTrue(CustomSet(emptyTypedArray).isSubsetOf(CustomSet([1]))) + } + func testNonEmptySetIsNotASubsetOfEmptySet() { + XCTAssertFalse(CustomSet([1]).isSubsetOf(CustomSet(emptyTypedArray))) + } + func testSetIsASubsetOfSetWithExactSameElements() { + XCTAssertTrue(CustomSet([1, 2, 3]).isSubsetOf(CustomSet([1, 2, 3]))) + } + func testSetIsASubsetOfLargerSetWithSameElements() { + XCTAssertTrue(CustomSet([1, 2, 3]).isSubsetOf(CustomSet([4, 1, 2, 3]))) + } + func testSetIsNotASubsetOfSetThatDoesNotContainItsElements() { + XCTAssertFalse(CustomSet([1, 2, 3]).isSubsetOf(CustomSet([4, 1, 3]))) + } + func testEqual() { XCTAssertEqual(CustomSet([1, 3]), CustomSet([3, 1])) XCTAssertNotEqual(CustomSet([1, 3]), CustomSet([3, 1, 5])) @@ -96,15 +115,6 @@ class CustomSetTests: XCTestCase { XCTAssertEqual(3, CustomSet([1, 2, 3, 2]).size) } - func testSubsetMethod() { - XCTAssertTrue(CustomSet([1, 2, 3]).isSupersetOf(CustomSet([1, 2, 3]))) - XCTAssertTrue(CustomSet([4, 1, 2, 3]).isSupersetOf(CustomSet([1, 2, 3]))) - XCTAssertFalse(CustomSet([4, 1, 3]).isSupersetOf(CustomSet([1, 2, 3]))) - XCTAssertFalse(CustomSet([1, 2, 3, 4]).isSupersetOf(CustomSet([1, 2, 5]))) - XCTAssertTrue(CustomSet([4, 1, 3]).isSupersetOf(CustomSet(emptyTypedArray))) - XCTAssertTrue(CustomSet(emptyTypedArray).isSupersetOf(CustomSet(emptyTypedArray))) - } - func testToA() { XCTAssertEqual([1, 2, 3], CustomSet([3, 1, 2]).toSortedArray) XCTAssertEqual([1, 2, 3], CustomSet([3, 1, 2, 1]).toSortedArray) @@ -130,6 +140,12 @@ class CustomSetTests: XCTestCase { ("testNothingIsContainedInAnEmptySet", testNothingIsContainedInAnEmptySet), ("testWhenTheElementIsInTheSet", testWhenTheElementIsInTheSet), ("testWhenTheElementIsNotInTheSet", testWhenTheElementIsNotInTheSet), + ("testEmptySetIsASubsetOfAnotherEmptySet", testEmptySetIsASubsetOfAnotherEmptySet), + ("testEmptySetIsASubsetOfNonEmptySet", testEmptySetIsASubsetOfNonEmptySet), + ("testNonEmptySetIsNotASubsetOfEmptySet", testNonEmptySetIsNotASubsetOfEmptySet), + ("testSetIsASubsetOfSetWithExactSameElements", testSetIsASubsetOfSetWithExactSameElements), + ("testSetIsASubsetOfLargerSetWithSameElements", testSetIsASubsetOfLargerSetWithSameElements), + ("testSetIsNotASubsetOfSetThatDoesNotContainItsElements", testSetIsNotASubsetOfSetThatDoesNotContainItsElements), ("testEqual", testEqual), ("testNoDuplicates", testNoDuplicates), ("testDeleteMethod", testDeleteMethod), @@ -139,7 +155,6 @@ class CustomSetTests: XCTestCase { ("testIntersection", testIntersection), ("testPutMethod", testPutMethod), ("testSize", testSize), - ("testSubsetMethod", testSubsetMethod), ("testToA", testToA), ("testUnion", testUnion), ] From 1c8b49be73cee0a17702bc6373e0042c9f8d9e56 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 21:20:02 -0800 Subject: [PATCH 05/14] custom-set: Revise disjoint to match spec --- .../Sources/CustomSet/CustomSetExample.swift | 5 ++-- .../Tests/CustomSetTests/CustomSetTests.swift | 28 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift index 3b1ea74bd..abf553cea 100644 --- a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift +++ b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift @@ -78,9 +78,8 @@ struct CustomSet: Equatable { } func isDisjoint(_ item: CustomSet) -> Bool { - - for each in Array(item.contents.keys) { - if contents.keys.contains(each) { + for elem in Array(item.contents.keys) { + if contents.keys.contains(elem) { return false } } diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index ac3a3aa52..363326072 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -41,6 +41,22 @@ class CustomSetTests: XCTestCase { XCTAssertFalse(CustomSet([1, 2, 3]).isSubsetOf(CustomSet([4, 1, 3]))) } + func testTheEmptySetIsDisjointWithItself() { + XCTAssertTrue(CustomSet(emptyTypedArray).isDisjoint(CustomSet(emptyTypedArray))) + } + func testEmptySetIsDisjointWithNonEmptySet() { + XCTAssertTrue(CustomSet(emptyTypedArray).isDisjoint(CustomSet([1]))) + } + func testNonEmptySetIsDisjointWithEmptySet() { + XCTAssertTrue(CustomSet([1]).isDisjoint(CustomSet(emptyTypedArray))) + } + func testSetsAreNotDisjointIfTheyShareAnElement() { + XCTAssertFalse(CustomSet([1, 2]).isDisjoint(CustomSet([2, 3]))) + } + func testSetsAreDisjointIfTheyShareNoElements() { + XCTAssertTrue(CustomSet([1, 2]).isDisjoint(CustomSet([3, 4]))) + } + func testEqual() { XCTAssertEqual(CustomSet([1, 3]), CustomSet([3, 1])) XCTAssertNotEqual(CustomSet([1, 3]), CustomSet([3, 1, 5])) @@ -71,12 +87,6 @@ class CustomSetTests: XCTestCase { } - func testDisjoint() { - XCTAssertTrue(CustomSet([1, 2]).isDisjoint(CustomSet([3, 4]))) - XCTAssertFalse(CustomSet([1, 2]).isDisjoint(CustomSet([2, 3]))) - XCTAssertFalse(CustomSet([1.0, 2.0]).isDisjoint(CustomSet([2.0, 3.0]))) - } - func testRemoveAllMethod() { var expected1 = CustomSet([1, 2]) expected1.removeAll() @@ -146,11 +156,15 @@ class CustomSetTests: XCTestCase { ("testSetIsASubsetOfSetWithExactSameElements", testSetIsASubsetOfSetWithExactSameElements), ("testSetIsASubsetOfLargerSetWithSameElements", testSetIsASubsetOfLargerSetWithSameElements), ("testSetIsNotASubsetOfSetThatDoesNotContainItsElements", testSetIsNotASubsetOfSetThatDoesNotContainItsElements), + ("testTheEmptySetIsDisjointWithItself", testTheEmptySetIsDisjointWithItself), + ("testEmptySetIsDisjointWithNonEmptySet", testEmptySetIsDisjointWithNonEmptySet), + ("testNonEmptySetIsDisjointWithEmptySet", testNonEmptySetIsDisjointWithEmptySet), + ("testSetsAreNotDisjointIfTheyShareAnElement", testSetsAreNotDisjointIfTheyShareAnElement), + ("testSetsAreDisjointIfTheyShareNoElements", testSetsAreDisjointIfTheyShareNoElements), ("testEqual", testEqual), ("testNoDuplicates", testNoDuplicates), ("testDeleteMethod", testDeleteMethod), ("testDifference", testDifference), - ("testDisjoint", testDisjoint), ("testRemoveAllMethod", testRemoveAllMethod), ("testIntersection", testIntersection), ("testPutMethod", testPutMethod), From 927ee8b75f47d6d0c3915d2859942e8f75bc18d1 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 21:45:21 -0800 Subject: [PATCH 06/14] custom-set: Revise equality tests to match spec --- .../Tests/CustomSetTests/CustomSetTests.swift | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index 363326072..7046ac9f3 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -57,11 +57,23 @@ class CustomSetTests: XCTestCase { XCTAssertTrue(CustomSet([1, 2]).isDisjoint(CustomSet([3, 4]))) } - func testEqual() { - XCTAssertEqual(CustomSet([1, 3]), CustomSet([3, 1])) - XCTAssertNotEqual(CustomSet([1, 3]), CustomSet([3, 1, 5])) - XCTAssertNotEqual(CustomSet([1, 3, 5]), CustomSet([3, 1])) - XCTAssertNotEqual(CustomSet([1, 3]), CustomSet([2, 1])) + func testEmptySetsAreEqual() { + XCTAssertEqual(CustomSet(emptyTypedArray), CustomSet(emptyTypedArray)) + } + func testEmptySetIsNotEqualToNonEmptySet() { + XCTAssertNotEqual(CustomSet(emptyTypedArray), CustomSet([1, 2, 3])) + } + func testNonEmptySetIsNotEqualToEmptySet() { + XCTAssertNotEqual(CustomSet([1, 2, 3]), CustomSet(emptyTypedArray)) + } + func testSetsWithTheSameElementsAreEqual() { + XCTAssertEqual(CustomSet([1, 2]), CustomSet([2, 1])) + } + func testSetsWithDifferentElementsAreNotEqual() { + XCTAssertNotEqual(CustomSet([1, 2, 3]), CustomSet([1, 2, 4])) + } + func testSetIsNotEqualToLargerSetWithSameElements() { + XCTAssertNotEqual(CustomSet([1, 2, 3]), CustomSet([1, 2, 3, 4])) } func testNoDuplicates() { @@ -161,7 +173,12 @@ class CustomSetTests: XCTestCase { ("testNonEmptySetIsDisjointWithEmptySet", testNonEmptySetIsDisjointWithEmptySet), ("testSetsAreNotDisjointIfTheyShareAnElement", testSetsAreNotDisjointIfTheyShareAnElement), ("testSetsAreDisjointIfTheyShareNoElements", testSetsAreDisjointIfTheyShareNoElements), - ("testEqual", testEqual), + ("testEmptySetsAreEqual", testEmptySetsAreEqual), + ("testEmptySetIsNotEqualToNonEmptySet", testEmptySetIsNotEqualToNonEmptySet), + ("testNonEmptySetIsNotEqualToEmptySet", testNonEmptySetIsNotEqualToEmptySet), + ("testSetsWithTheSameElementsAreEqual", testSetsWithTheSameElementsAreEqual), + ("testSetsWithDifferentElementsAreNotEqual", testSetsWithDifferentElementsAreNotEqual), + ("testSetIsNotEqualToLargerSetWithSameElements", testSetIsNotEqualToLargerSetWithSameElements), ("testNoDuplicates", testNoDuplicates), ("testDeleteMethod", testDeleteMethod), ("testDifference", testDifference), From 1af69c40b02f8a097df99673dbbf1d32222ffde9 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 21:52:44 -0800 Subject: [PATCH 07/14] custom-set Improve equal-to function --- .../Sources/CustomSet/CustomSetExample.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift index abf553cea..8d003408e 100644 --- a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift +++ b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift @@ -1,8 +1,3 @@ -func == (lh: CustomSet, rh: CustomSet) -> Bool { - return lh.contents.keys.sorted { $0.hashValue < $1.hashValue } == rh.contents.keys.sorted { $0.hashValue < $1.hashValue } - -} - extension CustomSet where T: Comparable { var toSortedArray: [Element] { return Array(contents.keys.sorted { $0 < $1 }) } @@ -22,6 +17,12 @@ struct CustomSet: Equatable { _ = sequence.map { self.contents[$0] = true } } + static func == (lh: CustomSet, rh: CustomSet) -> Bool { + return lh.size == rh.size && + lh.contents.keys.sorted { $0.hashValue < $1.hashValue } == + rh.contents.keys.sorted { $0.hashValue < $1.hashValue } + } + func contains(_ item: Element) -> Bool { if contents[item] != nil { return true From 4f59dcf7ce52d36b73d5fb4b3cc53278bee5b6a2 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 22:17:46 -0800 Subject: [PATCH 08/14] custom-set: Revise add function to match spec --- .../Sources/CustomSet/CustomSetExample.swift | 3 +- .../Tests/CustomSetTests/CustomSetTests.swift | 37 +++++++++++-------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift index 8d003408e..49acdf984 100644 --- a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift +++ b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift @@ -30,9 +30,8 @@ struct CustomSet: Equatable { return false } - mutating func put(_ item: Element) { + mutating func add(_ item: Element) { contents[item] = true - } mutating func delete(_ item: Element) { diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index 7046ac9f3..e844ecdb0 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -76,6 +76,25 @@ class CustomSetTests: XCTestCase { XCTAssertNotEqual(CustomSet([1, 2, 3]), CustomSet([1, 2, 3, 4])) } + func testAddToEmptySet() { + let expected = CustomSet([3]) + var actual = CustomSet(emptyTypedArray) + actual.add(3) + XCTAssertEqual(expected, actual) + } + func testAddToNonEmptySet() { + let expected = CustomSet([1, 2, 3, 4]) + var actual = CustomSet([1, 2, 4]) + actual.add(3) + XCTAssertEqual(expected, actual) + } + func testAddingAnExistingElementDoesNotChangeTheSet() { + let expected = CustomSet([1, 2, 3]) + var actual = CustomSet([1, 2, 3]) + actual.add(3) + XCTAssertEqual(expected, actual) + } + func testNoDuplicates() { XCTAssertEqual(CustomSet([1, 1]), CustomSet([1])) } @@ -117,20 +136,6 @@ class CustomSetTests: XCTestCase { CustomSet([1.0, 2.0, 3.0]).intersection(CustomSet([3.0]))) } - - - func testPutMethod() { - var expected1 = CustomSet([1, 2, 4]) - expected1.put(3) - XCTAssertEqual(CustomSet([1, 2, 3, 4]), - expected1) - - var expected2 = CustomSet([1, 2, 3]) - expected2.put(3) - XCTAssertEqual(CustomSet([1, 2, 3]), - expected2) - } - func testSize() { XCTAssertEqual(0, CustomSet(emptyTypedArray).size) XCTAssertEqual(3, CustomSet([1, 2, 3]).size) @@ -179,12 +184,14 @@ class CustomSetTests: XCTestCase { ("testSetsWithTheSameElementsAreEqual", testSetsWithTheSameElementsAreEqual), ("testSetsWithDifferentElementsAreNotEqual", testSetsWithDifferentElementsAreNotEqual), ("testSetIsNotEqualToLargerSetWithSameElements", testSetIsNotEqualToLargerSetWithSameElements), + ("testAddToEmptySet", testAddToEmptySet), + ("testAddToNonEmptySet", testAddToNonEmptySet), + ("testAddingAnExistingElementDoesNotChangeTheSet", testAddingAnExistingElementDoesNotChangeTheSet), ("testNoDuplicates", testNoDuplicates), ("testDeleteMethod", testDeleteMethod), ("testDifference", testDifference), ("testRemoveAllMethod", testRemoveAllMethod), ("testIntersection", testIntersection), - ("testPutMethod", testPutMethod), ("testSize", testSize), ("testToA", testToA), ("testUnion", testUnion), From 285939b18226f40696c0652c4b8df108dd6ca119 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 22:22:55 -0800 Subject: [PATCH 09/14] custom-set: Remove now redundant test for duplicates --- .../custom-set/Tests/CustomSetTests/CustomSetTests.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index e844ecdb0..77c98924e 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -95,10 +95,6 @@ class CustomSetTests: XCTestCase { XCTAssertEqual(expected, actual) } - func testNoDuplicates() { - XCTAssertEqual(CustomSet([1, 1]), CustomSet([1])) - } - func testDeleteMethod() { var expected1 = CustomSet([3, 2, 1]) @@ -187,7 +183,6 @@ class CustomSetTests: XCTestCase { ("testAddToEmptySet", testAddToEmptySet), ("testAddToNonEmptySet", testAddToNonEmptySet), ("testAddingAnExistingElementDoesNotChangeTheSet", testAddingAnExistingElementDoesNotChangeTheSet), - ("testNoDuplicates", testNoDuplicates), ("testDeleteMethod", testDeleteMethod), ("testDifference", testDifference), ("testRemoveAllMethod", testRemoveAllMethod), From dc38332cf733c46519e6f1eb74273a102bb0b41c Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 22:46:54 -0800 Subject: [PATCH 10/14] custom-set: Revise intersection tests to match spec --- .../Tests/CustomSetTests/CustomSetTests.swift | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index 77c98924e..d01bc6878 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -95,6 +95,32 @@ class CustomSetTests: XCTestCase { XCTAssertEqual(expected, actual) } + func testIntersectionOfTwoEmptySetsIsAnEmptySet() { + let expected = CustomSet(emptyTypedArray) + let actual = CustomSet(emptyTypedArray).intersection(CustomSet(emptyTypedArray)) + XCTAssertEqual(expected, actual) + } + func testIntersectionOfAnEmptySetAndNonEmptySetIsAnEmptySet() { + let expected = CustomSet(emptyTypedArray) + let actual = CustomSet(emptyTypedArray).intersection(CustomSet([3, 2, 5])) + XCTAssertEqual(expected, actual) + } + func testIntersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet() { + let expected = CustomSet(emptyTypedArray) + let actual = CustomSet([1, 2, 3, 4]).intersection(CustomSet(emptyTypedArray)) + XCTAssertEqual(expected, actual) + } + func testIntersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet() { + let expected = CustomSet(emptyTypedArray) + let actual = CustomSet([1, 2, 3]).intersection(CustomSet([4, 5, 6])) + XCTAssertEqual(expected, actual) + } + func testIntersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements() { + let expected = CustomSet([2, 3]) + let actual = CustomSet([1, 2, 3, 4]).intersection(CustomSet([3, 2, 5])) + XCTAssertEqual(expected, actual) + } + func testDeleteMethod() { var expected1 = CustomSet([3, 2, 1]) @@ -124,14 +150,6 @@ class CustomSetTests: XCTestCase { XCTAssertEqual(CustomSet(emptyTypedArray), expected2) } - func testIntersection() { - XCTAssertEqual(CustomSet(["a", "c"]), - CustomSet(["a", "b", "c"]).intersection(CustomSet(["a", "c", "d"]))) - - XCTAssertEqual(CustomSet([3.0]), - CustomSet([1.0, 2.0, 3.0]).intersection(CustomSet([3.0]))) - } - func testSize() { XCTAssertEqual(0, CustomSet(emptyTypedArray).size) XCTAssertEqual(3, CustomSet([1, 2, 3]).size) @@ -183,10 +201,14 @@ class CustomSetTests: XCTestCase { ("testAddToEmptySet", testAddToEmptySet), ("testAddToNonEmptySet", testAddToNonEmptySet), ("testAddingAnExistingElementDoesNotChangeTheSet", testAddingAnExistingElementDoesNotChangeTheSet), + ("testIntersectionOfTwoEmptySetsIsAnEmptySet", testIntersectionOfTwoEmptySetsIsAnEmptySet), + ("testIntersectionOfAnEmptySetAndNonEmptySetIsAnEmptySet", testIntersectionOfAnEmptySetAndNonEmptySetIsAnEmptySet), + ("testIntersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet", testIntersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet), + ("testIntersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet", testIntersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet), + ("testIntersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements", testIntersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements), ("testDeleteMethod", testDeleteMethod), ("testDifference", testDifference), ("testRemoveAllMethod", testRemoveAllMethod), - ("testIntersection", testIntersection), ("testSize", testSize), ("testToA", testToA), ("testUnion", testUnion), From 5da394e24ff7aa758509ef80dc64883f7becf56c Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 22:47:33 -0800 Subject: [PATCH 11/14] custom-set: Improve intersection implementation --- .../Sources/CustomSet/CustomSetExample.swift | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift index 49acdf984..8ee8743f8 100644 --- a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift +++ b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift @@ -44,14 +44,15 @@ struct CustomSet: Equatable { } - func intersection(_ item: CustomSet) -> CustomSet { - var temp = [Element: Bool]() - for each in Array(item.contents.keys) { - guard contents[each] != nil else { continue } - temp[each] = true + func intersection(_ other: CustomSet) -> CustomSet { + var intersect = CustomSet([Element]()) + for elem in Array(other.contents.keys) { + guard contents[elem] != nil else { continue } + intersect.add(elem) } - return CustomSet(temp.keys) + return intersect } + func difference(_ item: CustomSet) -> CustomSet { var temp = contents for each in Array(item.contents.keys) { From c2373c6d1570deefd5e390ca1a94143444525269 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 23:00:29 -0800 Subject: [PATCH 12/14] custom-set: Revise difference tests to match specs --- .../Tests/CustomSetTests/CustomSetTests.swift | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index d01bc6878..5c4a1e69c 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -121,6 +121,27 @@ class CustomSetTests: XCTestCase { XCTAssertEqual(expected, actual) } + func testDifferenceOfTwoEmptySetsIsAnEmptySet() { + let expected = CustomSet(emptyTypedArray) + let actual = CustomSet(emptyTypedArray).difference(CustomSet(emptyTypedArray)) + XCTAssertEqual(expected, actual) + } + func testDifferenceOfEmptySetAndNonEmptySetIsAnEmptySet() { + let expected = CustomSet(emptyTypedArray) + let actual = CustomSet(emptyTypedArray).difference(CustomSet([3, 2, 5])) + XCTAssertEqual(expected, actual) + } + func testDifferenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() { + let expected = CustomSet([1, 2, 3, 4]) + let actual = CustomSet([1, 2, 3, 4]).difference(CustomSet(emptyTypedArray)) + XCTAssertEqual(expected, actual) + } + func testDifferenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet() { + let expected = CustomSet([1, 3]) + let actual = CustomSet([3, 2, 1]).difference(CustomSet([2, 4])) + XCTAssertEqual(expected, actual) + } + func testDeleteMethod() { var expected1 = CustomSet([3, 2, 1]) @@ -132,14 +153,6 @@ class CustomSetTests: XCTestCase { XCTAssertEqual(CustomSet([1, 2, 3]), expected2) } - func testDifference() { - XCTAssertEqual(CustomSet([1, 3]), - CustomSet([1, 2, 3]).difference(CustomSet([2, 4]))) - XCTAssertEqual(CustomSet([2, 3]), - CustomSet([1, 2, 3, 4]).difference(CustomSet([1, 4]))) - - } - func testRemoveAllMethod() { var expected1 = CustomSet([1, 2]) expected1.removeAll() @@ -206,8 +219,11 @@ class CustomSetTests: XCTestCase { ("testIntersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet", testIntersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet), ("testIntersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet", testIntersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet), ("testIntersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements", testIntersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements), + ("testDifferenceOfTwoEmptySetsIsAnEmptySet", testDifferenceOfTwoEmptySetsIsAnEmptySet), + ("testDifferenceOfEmptySetAndNonEmptySetIsAnEmptySet", testDifferenceOfEmptySetAndNonEmptySetIsAnEmptySet), + ("testDifferenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet", testDifferenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet), + ("testDifferenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet", testDifferenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet), ("testDeleteMethod", testDeleteMethod), - ("testDifference", testDifference), ("testRemoveAllMethod", testRemoveAllMethod), ("testSize", testSize), ("testToA", testToA), From e6e141e3af65b7b797724831519144f6c8a5c1b4 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 23:00:56 -0800 Subject: [PATCH 13/14] custom-set: Improve difference implementation --- .../Sources/CustomSet/CustomSetExample.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift index 8ee8743f8..5ea359232 100644 --- a/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift +++ b/exercises/custom-set/Sources/CustomSet/CustomSetExample.swift @@ -53,12 +53,13 @@ struct CustomSet: Equatable { return intersect } - func difference(_ item: CustomSet) -> CustomSet { - var temp = contents - for each in Array(item.contents.keys) { - temp[each] = nil + func difference(_ other: CustomSet) -> CustomSet { + var diff = CustomSet([Element]()) + for elem in Array(contents.keys) { + guard other.contents[elem] != true else { continue } + diff.add(elem) } - return CustomSet(temp.keys) + return diff } func union(_ item: CustomSet) -> CustomSet { From 3eb895b121df9c3ad7c9a4e0900e6a92bded6774 Mon Sep 17 00:00:00 2001 From: Thomas Hopkins Date: Thu, 5 Nov 2020 23:48:48 -0800 Subject: [PATCH 14/14] custom-set: Revise union tests --- .../Tests/CustomSetTests/CustomSetTests.swift | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift index 5c4a1e69c..783d5b938 100644 --- a/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift +++ b/exercises/custom-set/Tests/CustomSetTests/CustomSetTests.swift @@ -142,6 +142,27 @@ class CustomSetTests: XCTestCase { XCTAssertEqual(expected, actual) } + func testUnionOfEmptySetsIsAnEmptySet() { + let expected = CustomSet(emptyTypedArray) + let actual = CustomSet(emptyTypedArray).union(CustomSet(emptyTypedArray)) + XCTAssertEqual(expected, actual) + } + func testUnionOfAnEmptySetAndNonEmptySetIsTheNonEmptySet() { + let expected = CustomSet([2]) + let actual = CustomSet(emptyTypedArray).union(CustomSet([2])) + XCTAssertEqual(expected, actual) + } + func testUnionOfANonEmptySetAndEmptySetIsTheNonEmptySet() { + let expected = CustomSet([1, 3]) + let actual = CustomSet([1, 3]).union(CustomSet(emptyTypedArray)) + XCTAssertEqual(expected, actual) + } + func testUnionOfNonEmptySetsContainsAllUniqueElements() { + let expected = CustomSet([3, 2, 1]) + let actual = CustomSet([1, 3]).union(CustomSet([2, 3])) + XCTAssertEqual(expected, actual) + } + func testDeleteMethod() { var expected1 = CustomSet([3, 2, 1]) @@ -174,19 +195,6 @@ class CustomSetTests: XCTestCase { XCTAssertEqual([1, 2, 3], CustomSet([3, 1, 2, 1]).toSortedArray) } - func testUnion() { - XCTAssertEqual(CustomSet([3, 2, 1]), - CustomSet([1, 3]).union(CustomSet([2]))) - XCTAssertEqual(CustomSet([3.0, 3, 2, 1]), - CustomSet([1, 3]).union(CustomSet([2, 3.0]))) - XCTAssertEqual(CustomSet([3, 1]), - CustomSet([1, 3]).union(CustomSet(emptyTypedArray))) - XCTAssertEqual(CustomSet([2]), - CustomSet([2]).union(CustomSet(emptyTypedArray))) - XCTAssertEqual(CustomSet(emptyTypedArray), - CustomSet(emptyTypedArray).union(CustomSet(emptyTypedArray))) - } - static var allTests: [(String, (CustomSetTests) -> () throws -> Void)] { return [ ("testSetsWithNoElementsAreEmpty", testSetsWithNoElementsAreEmpty), @@ -223,11 +231,14 @@ class CustomSetTests: XCTestCase { ("testDifferenceOfEmptySetAndNonEmptySetIsAnEmptySet", testDifferenceOfEmptySetAndNonEmptySetIsAnEmptySet), ("testDifferenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet", testDifferenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet), ("testDifferenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet", testDifferenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet), + ("testUnionOfEmptySetsIsAnEmptySet", testUnionOfEmptySetsIsAnEmptySet), + ("testUnionOfAnEmptySetAndNonEmptySetIsTheNonEmptySet", testUnionOfAnEmptySetAndNonEmptySetIsTheNonEmptySet), + ("testUnionOfANonEmptySetAndEmptySetIsTheNonEmptySet", testUnionOfANonEmptySetAndEmptySetIsTheNonEmptySet), + ("testUnionOfNonEmptySetsContainsAllUniqueElements", testUnionOfNonEmptySetsContainsAllUniqueElements), ("testDeleteMethod", testDeleteMethod), ("testRemoveAllMethod", testRemoveAllMethod), ("testSize", testSize), ("testToA", testToA), - ("testUnion", testUnion), ] } }