Skip to content

Commit 3cd0f6c

Browse files
petercorkeclaude
andcommitted
test: adjacent() is canonical; neighbours/neighbors/isneighbour* are silent aliases
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent f34c61e commit 3cd0f6c

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

‎tests/test_adjacent.py‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""
2+
``adjacent()`` is the canonical name for the vertices at the far end of a
3+
vertex's edges, because "adjacent" is the graph-theory term and has one
4+
spelling. ``neighbours`` and ``neighbors`` (and ``isneighbour`` /
5+
``isneighbor``) remain as silent aliases: they are the same function, not a
6+
second implementation, and they do not warn.
7+
"""
8+
9+
import unittest
10+
import warnings
11+
12+
from pgraph import DGraph, UGraph, DVertex, UVertex
13+
from pgraph.PGraph import BaseVertex
14+
15+
16+
class TestAliases(unittest.TestCase):
17+
def test_aliases_are_the_same_function(self):
18+
for cls in (BaseVertex, UVertex, DVertex):
19+
with self.subTest(cls=cls.__name__):
20+
self.assertIs(cls.neighbours, cls.adjacent)
21+
self.assertIs(cls.neighbors, cls.adjacent)
22+
self.assertIs(cls.isneighbour, cls.isadjacent)
23+
self.assertIs(cls.isneighbor, cls.isadjacent)
24+
25+
def test_no_warnings(self):
26+
g = UGraph()
27+
a, b = g.add_vertex(name="a"), g.add_vertex(name="b")
28+
a.connect(b)
29+
with warnings.catch_warnings():
30+
warnings.simplefilter("error") # any warning is an error
31+
for name in ("adjacent", "neighbours", "neighbors"):
32+
self.assertEqual(getattr(a, name)(), [b])
33+
for name in ("isadjacent", "isneighbour", "isneighbor"):
34+
self.assertTrue(getattr(a, name)(b))
35+
36+
def test_docstring_names_the_aliases(self):
37+
doc = BaseVertex.adjacent.__doc__
38+
for name in ("neighbours", "neighbors"):
39+
self.assertIn(name, doc)
40+
41+
42+
class _Adjacent:
43+
Graph: type
44+
45+
def setUp(self):
46+
self.g = self.Graph()
47+
self.a = self.g.add_vertex([0, 0], name="a")
48+
self.b = self.g.add_vertex([1, 0], name="b")
49+
self.c = self.g.add_vertex([2, 0], name="c")
50+
51+
def test_simple(self):
52+
self.a.connect(self.b)
53+
self.assertEqual(self.a.adjacent(), [self.b])
54+
self.assertTrue(self.a.isadjacent(self.b))
55+
self.assertFalse(self.a.isadjacent(self.c))
56+
57+
def test_returns_a_new_list(self):
58+
self.a.connect(self.b)
59+
r = self.a.adjacent()
60+
r.clear()
61+
self.assertEqual(self.a.adjacent(), [self.b])
62+
63+
def test_parallel_edges_listed_once_per_edge(self):
64+
for _ in range(3):
65+
self.a.connect(self.b)
66+
self.assertEqual(self.a.adjacent(), [self.b, self.b, self.b])
67+
68+
def test_matches_a_row_of_the_adjacency_matrix(self):
69+
# the point of the name: row i of adjacency() is the vertices adjacent
70+
# to vertex i, with multiplicity
71+
self.a.connect(self.b)
72+
self.a.connect(self.b)
73+
self.a.connect(self.c)
74+
A = self.g.adjacency()
75+
for i, v in enumerate(self.g):
76+
counts = {w: v.adjacent().count(w) for w in self.g}
77+
for j, w in enumerate(self.g):
78+
self.assertEqual(A[i, j], counts[w], (v.name, w.name))
79+
80+
81+
class TestUGraphAdjacent(_Adjacent, unittest.TestCase):
82+
Graph = UGraph
83+
84+
def test_both_ends(self):
85+
self.a.connect(self.b)
86+
self.assertEqual(self.b.adjacent(), [self.a])
87+
self.assertTrue(self.b.isadjacent(self.a))
88+
89+
def test_self_loop_listed_twice(self):
90+
self.a.connect(self.a)
91+
self.assertEqual(self.a.adjacent(), [self.a, self.a])
92+
93+
94+
class TestDGraphAdjacent(_Adjacent, unittest.TestCase):
95+
Graph = DGraph
96+
97+
def test_outgoing_only(self):
98+
self.a.connect(self.b)
99+
self.assertEqual(self.b.adjacent(), [])
100+
self.assertFalse(self.b.isadjacent(self.a))
101+
102+
def test_self_loop_listed_once(self):
103+
self.a.connect(self.a)
104+
self.assertEqual(self.a.adjacent(), [self.a])
105+
106+
107+
if __name__ == "__main__":
108+
unittest.main()

0 commit comments

Comments
 (0)