@@ -3,6 +3,90 @@ import XCTest
3
3
4
4
class SwiftNodesTests : XCTestCase {
5
5
6
+ func testCodeExamplesFromREADME( ) throws {
7
+ let graph = Graph < String , Int > { " id \( $0) " } // NodeID == String, NodeValue == Int
8
+ let node1 = graph. insert ( 1 ) // node1.id == "id1"
9
+
10
+ let valueForID1 = graph. value ( for: " id1 " ) // valueForID1 == 1
11
+ let nodeForID1 = graph. node ( for: " id1 " ) // nodeForID1 === node1
12
+
13
+ XCTAssertEqual ( valueForID1, node1. value)
14
+ XCTAssertIdentical ( nodeForID1, node1)
15
+ }
16
+
17
+ func testAddingEdges( ) throws {
18
+ let graph = Graph < String , Int > { " id \( $0) " }
19
+ let node1 = graph. insert ( 1 )
20
+ let node2 = graph. insert ( 2 )
21
+ graph. addEdge ( from: node1, to: node2)
22
+
23
+ XCTAssertNotNil ( graph. edge ( from: node1, to: node2) )
24
+ XCTAssertNil ( graph. edge ( from: node2, to: node1) )
25
+ XCTAssertEqual ( graph. edge ( from: node1, to: node2) ? . count, 1 )
26
+ }
27
+
28
+ func testAddingEdgeWithBigCount( ) throws {
29
+ let graph = Graph < String , Int > { " id \( $0) " }
30
+ let node1 = graph. insert ( 1 )
31
+ let node2 = graph. insert ( 2 )
32
+
33
+ graph. addEdge ( from: node1, to: node2, count: 42 )
34
+ XCTAssertEqual ( graph. edge ( from: node1, to: node2) ? . count, 42 )
35
+
36
+ graph. addEdge ( from: node1, to: node2, count: 58 )
37
+ XCTAssertEqual ( graph. edge ( from: node1, to: node2) ? . count, 100 )
38
+ }
39
+
40
+ func testEdgesAreDirected( ) throws {
41
+ let graph = Graph < String , Int > { " id \( $0) " }
42
+ let node1 = graph. insert ( 1 )
43
+ let node2 = graph. insert ( 2 )
44
+ XCTAssertNotNil ( graph. addEdge ( from: node1, to: node2) )
45
+ XCTAssertNil ( graph. edge ( from: node2, to: node1) )
46
+ }
47
+
48
+ func testUUIDAsID( ) throws {
49
+ let graph = Graph < UUID , Int > { _ in UUID ( ) } // NodeID == UUID, NodeValue == Int
50
+ let node1 = graph. insert ( 1 )
51
+ let node2 = graph. insert ( 1 )
52
+ XCTAssertNotIdentical ( node1, node2)
53
+ XCTAssertEqual ( node1. value, node2. value)
54
+ XCTAssertNotEqual ( node1. id, node2. id)
55
+ }
56
+
57
+ func testOmittingClosureForIdentifiableValues( ) throws {
58
+ struct IdentifiableValue : Identifiable { let id = UUID ( ) }
59
+ let graph = Graph < UUID , IdentifiableValue > ( ) // NodeID == NodeValue.ID == UUID
60
+ let node = graph. insert ( IdentifiableValue ( ) ) // node.id == node.value.id
61
+ XCTAssertEqual ( node. id, node. value. id)
62
+ }
63
+
64
+ func testSorting( ) throws {
65
+ let graph = Graph < Int , Int > ( )
66
+
67
+ let node1 = graph. insert ( 1 )
68
+ let node2 = graph. insert ( 2 )
69
+ let edge = graph. addEdge ( from: node1, to: node2)
70
+
71
+ graph. remove ( edge)
72
+ graph. removeEdge ( with: edge. id)
73
+ graph. removeEdge ( with: . init( node1, node2) )
74
+ graph. removeEdge ( with: . init( node1. id, node2. id) )
75
+ graph. removeEdge ( from: node1, to: node2)
76
+ graph. removeEdge ( from: node1. id, to: node2. id)
77
+ }
78
+
79
+ func testWaysToRemoveAnEdgeCompile( ) {
80
+ let graph = Graph < Int , Int > ( )
81
+ graph. insert ( 5 )
82
+ graph. insert ( 3 )
83
+
84
+ graph. remove ( edge)
85
+ graph. removeEdge ( from: node1, to: node2)
86
+ graph. removeEdge ( from: node1. id, to: node2. id)
87
+ graph. removeEdge ( with: edge. id)
88
+ }
89
+
6
90
func testInsertingConnectingAndDisconnectingValues( ) throws {
7
91
let graph = Graph < String , Int > { " id \( $0) " }
8
92
XCTAssertNil ( graph. node ( for: " id1 " ) )
0 commit comments