Skip to content

Commit

Permalink
addChild now has value parameter to match init
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Es committed Dec 3, 2021
1 parent 2c54d99 commit e551dfa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 62 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ _SwiftSimpleTree_ is part of the [OpenAlloc](https://github.com/openalloc) famil

```swift
let foo = SimpleTree(value: "foo")
let bar = foo.addChild(for: "bar")
let baz = bar.addChild(for: "baz")
let bar = foo.addChild(value: "bar")
let baz = bar.addChild(value: "baz")

print(foo.getFirst(for: "baz")?.value)

Expand Down Expand Up @@ -53,7 +53,7 @@ public enum Traversal {

- `init(value: T)`: Initialize a new tree (containing the specified value at the root)

- `func addChild(for: T) -> Node`: Append a new node (containing the specified value) to our list of children
- `func addChild(value: T) -> Node`: Append a new node (containing the specified value) to our list of children

#### Node Retrieval

Expand Down
2 changes: 1 addition & 1 deletion Sources/SimpleTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SimpleTree<T> where T: Equatable & Hashable {
extension SimpleTree {

/// Append a new node (containing the specified value) to our list of children
public func addChild(for value: T) -> Node {
public func addChild(value: T) -> Node {
let nuChild = Node(value: value)
children.append(nuChild)
nuChild.parent = self
Expand Down
116 changes: 58 additions & 58 deletions Tests/SimpleTreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ final class SimpleTreeTests: XCTestCase {

public func testExample() {
let foo = SimpleTree(value: "foo")
let bar = foo.addChild(for: "bar")
let baz = bar.addChild(for: "baz")
let bar = foo.addChild(value: "bar")
let baz = bar.addChild(value: "baz")

XCTAssertEqual("baz", foo.getFirst(for: "baz")?.value)
XCTAssertEqual(["bar", "foo"], baz.getParentValues())
Expand All @@ -43,10 +43,10 @@ final class SimpleTreeTests: XCTestCase {

public func testGetParentValues() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let baz2 = foo.addChild(for: "bar2")
let baz = bar.addChild(for: "baz")
let blah = bar.addChild(for: "blah")
let bar = foo.addChild(value: "bar")
let baz2 = foo.addChild(value: "bar2")
let baz = bar.addChild(value: "baz")
let blah = bar.addChild(value: "blah")

XCTAssertEqual(blah.getParentValues(), ["bar", "foo"])
XCTAssertEqual(blah.getParentValues(maxDepth: 0), [])
Expand All @@ -69,10 +69,10 @@ final class SimpleTreeTests: XCTestCase {

public func testMakeParentIterator() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let baz2 = foo.addChild(for: "baz2")
let baz = bar.addChild(for: "baz")
let blah = bar.addChild(for: "blah")
let bar = foo.addChild(value: "bar")
let baz2 = foo.addChild(value: "baz2")
let baz = bar.addChild(value: "baz")
let blah = bar.addChild(value: "blah")

let blahIt = blah.makeParentIterator()
XCTAssertEqual("bar", blahIt.next()?.value)
Expand All @@ -98,12 +98,12 @@ final class SimpleTreeTests: XCTestCase {

public func testMakeChildIterator() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let baz2 = foo.addChild(for: "baz2")
let bleep = baz2.addChild(for: "bleep")
let blort = bleep.addChild(for: "blort")
let baz = bar.addChild(for: "baz")
let blah = bar.addChild(for: "blah")
let bar = foo.addChild(value: "bar")
let baz2 = foo.addChild(value: "baz2")
let bleep = baz2.addChild(value: "bleep")
let blort = bleep.addChild(value: "blort")
let baz = bar.addChild(value: "baz")
let blah = bar.addChild(value: "blah")

let blahIt = blah.makeChildIterator()
XCTAssertNil(blahIt.next()?.value)
Expand Down Expand Up @@ -140,8 +140,8 @@ final class SimpleTreeTests: XCTestCase {

public func testGetFirst() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let baz = bar.addChild(for: "baz")
let bar = foo.addChild(value: "bar")
let baz = bar.addChild(value: "baz")

XCTAssertNil(foo.getFirstChild(for: "foo"))
XCTAssertEqual(foo.getFirstChild(for: "bar"), bar)
Expand All @@ -161,25 +161,25 @@ final class SimpleTreeTests: XCTestCase {

public func testGetChildValuesOneChild() throws {
let foo = SST(value: "foo")
_ = foo.addChild(for: "bar")
_ = foo.addChild(value: "bar")
XCTAssertEqual(foo.getChildValues(), ["bar"])
}

public func testGetChildValuesOneGrandChild() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
_ = bar.addChild(for: "baz")
let bar = foo.addChild(value: "bar")
_ = bar.addChild(value: "baz")
XCTAssertEqual(foo.getChildValues(), ["bar", "baz"])
}

public func testGetChildValues() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let bar2 = foo.addChild(for: "bar2")
_ = bar2.addChild(for: "bar3")
let baz = bar.addChild(for: "baz")
let blah = bar.addChild(for: "blah")
_ = blah.addChild(for: "bleh")
let bar = foo.addChild(value: "bar")
let bar2 = foo.addChild(value: "bar2")
_ = bar2.addChild(value: "bar3")
let baz = bar.addChild(value: "baz")
let blah = bar.addChild(value: "blah")
_ = blah.addChild(value: "bleh")

for traversal in [SST.Traversal.depthFirst, SST.Traversal.breadthFirst] {
XCTAssertEqual(foo.getChildValues(traversal: traversal).sorted(), ["bar", "bar2", "bar3", "baz", "blah", "bleh"].sorted())
Expand All @@ -198,12 +198,12 @@ final class SimpleTreeTests: XCTestCase {

public func testGetSelfAndChildValues() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let bar2 = foo.addChild(for: "bar2")
_ = bar2.addChild(for: "bar3")
let baz = bar.addChild(for: "baz")
let blah = bar.addChild(for: "blah")
_ = blah.addChild(for: "bleh")
let bar = foo.addChild(value: "bar")
let bar2 = foo.addChild(value: "bar2")
_ = bar2.addChild(value: "bar3")
let baz = bar.addChild(value: "baz")
let blah = bar.addChild(value: "blah")
_ = blah.addChild(value: "bleh")

for traversal in [SST.Traversal.depthFirst, SST.Traversal.breadthFirst] {
XCTAssertEqual(foo.getSelfAndChildValues(traversal: traversal).sorted(), ["bar", "bar2", "bar3", "baz", "blah", "bleh", "foo"].sorted())
Expand All @@ -222,12 +222,12 @@ final class SimpleTreeTests: XCTestCase {

public func testGetAllChildValues() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let bar2 = foo.addChild(for: "bar2")
_ = bar2.addChild(for: "bar3")
let baz = bar.addChild(for: "baz")
let blah = bar.addChild(for: "blah")
_ = blah.addChild(for: "bleh")
let bar = foo.addChild(value: "bar")
let bar2 = foo.addChild(value: "bar2")
_ = bar2.addChild(value: "bar3")
let baz = bar.addChild(value: "baz")
let blah = bar.addChild(value: "blah")
_ = blah.addChild(value: "bleh")

XCTAssertEqual(foo.getChildValues().sorted(), ["bar", "bar2", "bar3", "baz", "blah", "bleh"].sorted())
XCTAssertEqual(bar.getChildValues().sorted(), ["baz", "blah", "bleh"].sorted())
Expand All @@ -238,11 +238,11 @@ final class SimpleTreeTests: XCTestCase {

public func testGetChildValuesExclude() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let bar2 = foo.addChild(for: "bar2")
_ = bar2.addChild(for: "bar3")
_ = bar.addChild(for: "baz")
_ = bar.addChild(for: "blah")
let bar = foo.addChild(value: "bar")
let bar2 = foo.addChild(value: "bar2")
_ = bar2.addChild(value: "bar3")
_ = bar.addChild(value: "baz")
_ = bar.addChild(value: "blah")

XCTAssertEqual(foo.getChildValues(excludeValues: ["bar"]).sorted(), ["bar2", "bar3"].sorted())
XCTAssertEqual(foo.getChildValues(excludeValues: ["bar2"]).sorted(), ["bar", "baz", "blah"].sorted())
Expand All @@ -252,11 +252,11 @@ final class SimpleTreeTests: XCTestCase {

public func testGetChildValues2Exclude() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let bar2 = foo.addChild(for: "bar2")
_ = bar2.addChild(for: "bar3")
_ = bar.addChild(for: "baz")
_ = bar.addChild(for: "blah")
let bar = foo.addChild(value: "bar")
let bar2 = foo.addChild(value: "bar2")
_ = bar2.addChild(value: "bar3")
_ = bar.addChild(value: "baz")
_ = bar.addChild(value: "blah")

//XCTAssertEqual(foo.getChildValues2(excludeValues: ["bar"]), [])
XCTAssertEqual(foo.getChildValues(excludeValues: ["bar2"]), ["bar", "baz", "blah"])
Expand All @@ -267,8 +267,8 @@ final class SimpleTreeTests: XCTestCase {

public func testGetAllValues() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
let baz = bar.addChild(for: "baz")
let bar = foo.addChild(value: "bar")
let baz = bar.addChild(value: "baz")

XCTAssertTrue(foo.getSelfAndChildValues().contains("foo"))
XCTAssertTrue(foo.getSelfAndChildValues().contains("bar"))
Expand All @@ -288,7 +288,7 @@ final class SimpleTreeTests: XCTestCase {

public func testGetAllValuesExcludeRoot() throws {
let foo = SST(value: "foo")
_ = foo.addChild(for: "bar")
_ = foo.addChild(value: "bar")

let actual = foo.getSelfAndChildValues(excludeValues: Set(["foo"]))
XCTAssertFalse(actual.contains("foo"))
Expand All @@ -297,8 +297,8 @@ final class SimpleTreeTests: XCTestCase {

public func testGetAllValuesExcludeChild() throws {
let foo = SST(value: "foo")
let bar = foo.addChild(for: "bar")
_ = bar.addChild(for: "baz")
let bar = foo.addChild(value: "bar")
_ = bar.addChild(value: "baz")

let actual = foo.getSelfAndChildValues(excludeValues: Set(["bar"]))
XCTAssertTrue(actual.contains("foo"))
Expand All @@ -308,11 +308,11 @@ final class SimpleTreeTests: XCTestCase {
public func testGetAllValuesExcludeSibling() throws {
let foo = SST(value: "foo")

let blah = foo.addChild(for: "blah")
_ = blah.addChild(for: "bleh")
let blah = foo.addChild(value: "blah")
_ = blah.addChild(value: "bleh")

let bar = foo.addChild(for: "bar")
_ = bar.addChild(for: "baz")
let bar = foo.addChild(value: "bar")
_ = bar.addChild(value: "baz")

let actual = foo.getSelfAndChildValues(excludeValues: Set(["blah"]))
XCTAssertTrue(actual.contains("foo"))
Expand Down

0 comments on commit e551dfa

Please sign in to comment.