Skip to content
Browse files

Removing nodes from a graph should probably remove connections to oth…

…er nodes in the graph.
  • Loading branch information...
mohiji committed Jul 13, 2015
1 parent bd574f3 commit a509d4d987b3b3c835ae4b9157b130edf6596c62
Showing with 28 additions and 0 deletions.
  1. +6 −0 JLFGameplayKit/JLFGKGraph.m
  2. +22 −0 Tests/PathfindingTests.m
@@ -63,6 +63,12 @@ - (void)connectNodeToLowestCostNode:(JLFGKGraphNode *)node bidirectional:(BOOL)b
- (void)removeNodes:(NSArray *)nodes
NSAssert(nodes != nil, @"JLFGKGraph -removeNodes: Cannot be called with a nil nodes array.");

// Take out any connections from nodes still within the graph to the one being removed.
for (JLFGKGraphNode *node in nodes) {
[node removeConnectionsToNodes:self.realNodes bidirectional:YES];

[self.realNodes removeObjectsInArray:nodes];

@@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
#import "JLFGKGraphNode.h"
#import "JLFGKGraph.h"

@interface PathfindingTests : XCTestCase
@@ -228,4 +229,25 @@ - (void)testCantConnectNodeToItself
XCTAssertEqual(n1.connectedNodes.count, 0);

- (void)testRemovingNodesFromGraphRemovesConnections
JLFGKGraphNode *n1, *n2, *n3;

n1 = [[JLFGKGraphNode alloc] init];
n2 = [[JLFGKGraphNode alloc] init];
n3 = [[JLFGKGraphNode alloc] init];

[n1 addConnectionsToNodes:@[n2, n3] bidirectional:YES];
JLFGKGraph *graph = [JLFGKGraph graphWithNodes:@[n1, n2]];

XCTAssertEqual([n1.connectedNodes containsObject:n2], YES);
XCTAssertEqual([n1.connectedNodes containsObject:n3], YES);
[graph removeNodes:@[n2]];

XCTAssertEqual([n1.connectedNodes containsObject:n2], NO);
XCTAssertEqual([n2.connectedNodes containsObject:n1], NO);
XCTAssertEqual([n1.connectedNodes containsObject:n3], YES);
XCTAssertEqual([n3.connectedNodes containsObject:n1], YES);


0 comments on commit a509d4d

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.