testing UnitTests define: #Digraph &parents: {TestCase}. tc@(UnitTests Digraph traits) newNode: obj [ Digraph Node new `>> [object: obj. ] ]. tc@(UnitTests Digraph traits) generatingBlockBetween: lower and: upper [ [| :node obj | obj: node object. obj > lower /\ [obj < upper] ifTrue: [{obj - 1. obj + 1}] ifFalse: [ obj > lower ifTrue: [{obj - 1}] ifFalse: [{obj + 1}]]] ]. tc@(UnitTests Digraph traits) testCreationByNonGeneratingBlock [| node graph | node: (tc newNode: 5). graph: (Digraph newFrom: node walking: [| :n | {}]). tc assert: graph allNodes = (Set newWith: node) description: 'Method #newFrom:walking: failed for non-generative block.'. ]. tc@(UnitTests Digraph traits) testCreationByWalkingWithABlock [| nodes graph | nodes: ({1. 2. 3. 4. 5. 6. 7. 8. 9} as: Set). graph: (Digraph newFrom: (tc newNode: 5) walking: (tc generatingBlockBetween: 1 and: 9)). tc assert: nodes = (graph allNodes collect: [| :e | e object]) description: 'Method #newFrom:walking: failed (symmetric generation).'. ]. tc@(UnitTests Digraph traits) testAsymmetricCreationByWalkingWithABlock [| nodes graph block | nodes: ({1. 2. 3. 4. 5. 6} as: Set). block: (tc generatingBlockBetween: 1 and: 6). graph: (Digraph newFrom: (tc newNode: 1) walking: block). tc assert: nodes = (graph allNodes collect: [| :e | e object]) description: 'Method #newFrom:walking: failed (asymmetric generation 1).'. graph: (Digraph newFrom: (tc newNode: 6) walking: block). tc assert: nodes = (graph allNodes collect: [| :e | e object]) description: 'Method #newFrom:walking: failed (asymmetric generation 2).'. ]. tc@(UnitTests Digraph traits) suite [ tc suiteForSelectors: { #testCreationByNonGeneratingBlock. #testCreationByWalkingWithABlock. #testAsymmetricCreationByWalkingWithABlock } ]. testing UnitTests define: #KeyedDigraph &parents: {UnitTests Digraph}. tc@(UnitTests KeyedDigraph traits) newNode: obj [ KeyedDigraph Node new `>> [object: obj. ] ]. tc@(UnitTests KeyedDigraph traits) keysBetween: lower and: upper [| answer | answer: SortedSet new. lower upTo: upper do: [| :i | answer add: i]. answer ]. tc@(UnitTests KeyedDigraph traits) testNodeTransitionMatching [| node1 node2 targets upperBound | node1: (tc newNode: 1). node2: (tc newNode: 2). targets: {tc newNode: 3. tc newNode: 4. tc newNode: 5}. " tc assert: (node1 transitionsMatch: node2) = False description: 'Unrelated nodes should not match.'." upperBound: 2. targets do: [| :targetNode | node1 addEdgeTo: targetNode keys: (tc keysBetween: 1 and: upperBound). node2 addEdgeTo: targetNode keys: (tc keysBetween: 1 and: upperBound). upperBound: upperBound + 1]. tc assert: (node1 transitionsMatch: node2) = True description: 'Nodes with same targets and transitions should match'. node1 transitions do: [| :edge | edge keys add: 4]. tc assert: (node1 transitionsMatch: node2) = False description: 'Nodes with differently keyed transitions should not match.'. node2 transitions do: [| :edge | edge keys add: 4]. node1 addEdgeTo: node2 keys: (tc keysBetween: 1 and: 5). node2 addEdgeTo: node1 keys: (tc keysBetween: 1 and: 5). tc assert: (node1 transitionsMatch: node2) = True description: 'Nodes with symmetric transitions should match.'. ]. tc@(UnitTests KeyedDigraph traits) testCommutativityOFNodeTransitionMatching [| node1 node2 targets upperBound | node1: (tc newNode: 1). node2: (tc newNode: 2). targets: {tc newNode: 3. tc newNode: 4. tc newNode: 5}. upperBound: 2. targets do: [| :targetNode | node1 addEdgeTo: targetNode keys: (tc keysBetween: 1 and: upperBound). node2 addEdgeTo: targetNode keys: (tc keysBetween: 1 and: upperBound). upperBound: upperBound + 1]. tc assert: (node1 transitionsMatch: node2) = (node2 transitionsMatch: node1) description: 'Node transition matching should be commutative (both True).'. node1 transitions do: [| :edge | edge keys add: 4]. tc assert: (node1 transitionsMatch: node2) = (node2 transitionsMatch: node1) description: 'Node transition matching should be commutative (both False).'. node2 transitions do: [| :edge | edge keys add: 4]. node1 addEdgeTo: node2 keys: (tc keysBetween: 1 and: 5). node2 addEdgeTo: node1 keys: (tc keysBetween: 1 and: 5). tc assert: (node1 transitionsMatch: node2) = (node2 transitionsMatch: node1) description: 'Node transition matching should be commutative (both True.'. ]. tc@(UnitTests KeyedDigraph traits) testEdgeMerging [| startNode endNode mergedKeys | startNode: (tc newNode: 'start'). endNode: (tc newNode: 'end'). startNode addEdgeTo: endNode keys: (tc keysBetween: 1 and: 3). startNode addEdgeTo: endNode keys: (tc keysBetween: 4 and: 7). mergedKeys: (tc keysBetween: 1 and: 7). startNode mergeTransitions. tc assert: startNode transitions size = 1 /\ [(startNode transitions detect: [| :edge | edge keys = mergedKeys]) ifNil: [False] ifNotNil: [True]] description: 'Edges should have been merged.'. ]. tc@(UnitTests KeyedDigraph traits) testDuplicateDeletion [| startNode intermediate dupl1 dupl2 endNode mergedKeys | startNode: (tc newNode: 'start'). intermediate: (tc newNode: 'intermediate'). dupl1: (tc newNode: 'first'). dupl2: (tc newNode: 'second'). endNode: (tc newNode: 'end'). startNode addEdgeTo: intermediate keys: (tc keysBetween: 1 and: 3). startNode addEdgeTo: intermediate keys: (tc keysBetween: 4 and: 7). intermediate addEdgeTo: dupl1 keys: (tc keysBetween: 8 and: 9). startNode addEdgeTo: dupl2 keys: (tc keysBetween: 3 and: 5). dupl1 addEdgeTo: endNode keys: (tc keysBetween: 2 and: 4). dupl2 addEdgeTo: endNode keys: (tc keysBetween: 2 and: 4). startNode removeDuplicateNodes. tc assert: dupl1 == dupl2 description: 'Duplicate removal should make duplicates the same object.'. ]. tc@(UnitTests KeyedDigraph traits) suite [ tc suiteForSelectors: { #testNodeTransitionMatching. #testCommutativityOFNodeTransitionMatching. #testEdgeMerging. #testDuplicateDeletion } ].