Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/readwrite/tests/test_graph6.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 from io import BytesIO | |
2 import tempfile | |
3 import pytest | |
4 | |
5 import networkx as nx | |
6 import networkx.readwrite.graph6 as g6 | |
7 from networkx.testing.utils import assert_edges_equal | |
8 from networkx.testing.utils import assert_nodes_equal | |
9 | |
10 | |
11 class TestGraph6Utils: | |
12 def test_n_data_n_conversion(self): | |
13 for i in [0, 1, 42, 62, 63, 64, 258047, 258048, 7744773, 68719476735]: | |
14 assert g6.data_to_n(g6.n_to_data(i))[0] == i | |
15 assert g6.data_to_n(g6.n_to_data(i))[1] == [] | |
16 assert g6.data_to_n(g6.n_to_data(i) + [42, 43])[1] == [42, 43] | |
17 | |
18 | |
19 class TestFromGraph6Bytes: | |
20 def test_from_graph6_bytes(self): | |
21 data = b"DF{" | |
22 G = nx.from_graph6_bytes(data) | |
23 assert_nodes_equal(G.nodes(), [0, 1, 2, 3, 4]) | |
24 assert_edges_equal( | |
25 G.edges(), [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] | |
26 ) | |
27 | |
28 def test_read_equals_from_bytes(self): | |
29 data = b"DF{" | |
30 G = nx.from_graph6_bytes(data) | |
31 fh = BytesIO(data) | |
32 Gin = nx.read_graph6(fh) | |
33 assert_nodes_equal(G.nodes(), Gin.nodes()) | |
34 assert_edges_equal(G.edges(), Gin.edges()) | |
35 | |
36 | |
37 class TestReadGraph6: | |
38 def test_read_many_graph6(self): | |
39 """Test for reading many graphs from a file into a list.""" | |
40 data = b"DF{\nD`{\nDqK\nD~{\n" | |
41 fh = BytesIO(data) | |
42 glist = nx.read_graph6(fh) | |
43 assert len(glist) == 4 | |
44 for G in glist: | |
45 assert sorted(G) == list(range(5)) | |
46 | |
47 | |
48 class TestWriteGraph6: | |
49 """Unit tests for writing a graph to a file in graph6 format.""" | |
50 | |
51 def test_null_graph(self): | |
52 result = BytesIO() | |
53 nx.write_graph6(nx.null_graph(), result) | |
54 assert result.getvalue() == b">>graph6<<?\n" | |
55 | |
56 def test_trivial_graph(self): | |
57 result = BytesIO() | |
58 nx.write_graph6(nx.trivial_graph(), result) | |
59 assert result.getvalue() == b">>graph6<<@\n" | |
60 | |
61 def test_complete_graph(self): | |
62 result = BytesIO() | |
63 nx.write_graph6(nx.complete_graph(4), result) | |
64 assert result.getvalue() == b">>graph6<<C~\n" | |
65 | |
66 def test_large_complete_graph(self): | |
67 result = BytesIO() | |
68 nx.write_graph6(nx.complete_graph(67), result, header=False) | |
69 assert result.getvalue() == b"~?@B" + b"~" * 368 + b"w\n" | |
70 | |
71 def test_no_header(self): | |
72 result = BytesIO() | |
73 nx.write_graph6(nx.complete_graph(4), result, header=False) | |
74 assert result.getvalue() == b"C~\n" | |
75 | |
76 def test_complete_bipartite_graph(self): | |
77 result = BytesIO() | |
78 G = nx.complete_bipartite_graph(6, 9) | |
79 nx.write_graph6(G, result, header=False) | |
80 # The expected encoding here was verified by Sage. | |
81 assert result.getvalue() == b"N??F~z{~Fw^_~?~?^_?\n" | |
82 | |
83 def test_no_directed_graphs(self): | |
84 with pytest.raises(nx.NetworkXNotImplemented): | |
85 nx.write_graph6(nx.DiGraph(), BytesIO()) | |
86 | |
87 def test_length(self): | |
88 for i in list(range(13)) + [31, 47, 62, 63, 64, 72]: | |
89 g = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i) | |
90 gstr = BytesIO() | |
91 nx.write_graph6(g, gstr, header=False) | |
92 # Strip the trailing newline. | |
93 gstr = gstr.getvalue().rstrip() | |
94 assert len(gstr) == ((i - 1) * i // 2 + 5) // 6 + (1 if i < 63 else 4) | |
95 | |
96 def test_roundtrip(self): | |
97 for i in list(range(13)) + [31, 47, 62, 63, 64, 72]: | |
98 G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i) | |
99 f = BytesIO() | |
100 nx.write_graph6(G, f) | |
101 f.seek(0) | |
102 H = nx.read_graph6(f) | |
103 assert_nodes_equal(G.nodes(), H.nodes()) | |
104 assert_edges_equal(G.edges(), H.edges()) | |
105 | |
106 def test_write_path(self): | |
107 with tempfile.NamedTemporaryFile() as f: | |
108 g6.write_graph6_file(nx.null_graph(), f) | |
109 f.seek(0) | |
110 assert f.read() == b">>graph6<<?\n" | |
111 | |
112 def test_relabeling(self): | |
113 G = nx.Graph([(0, 1)]) | |
114 assert g6.to_graph6_bytes(G) == b">>graph6<<A_\n" | |
115 G = nx.Graph([(1, 2)]) | |
116 assert g6.to_graph6_bytes(G) == b">>graph6<<A_\n" | |
117 G = nx.Graph([(1, 42)]) | |
118 assert g6.to_graph6_bytes(G) == b">>graph6<<A_\n" |