Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/readwrite/tests/test_sparse6.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 from networkx.testing.utils import assert_edges_equal | |
7 from networkx.testing.utils import assert_nodes_equal | |
8 | |
9 | |
10 class TestSparseGraph6: | |
11 def test_from_sparse6_bytes(self): | |
12 data = b":Q___eDcdFcDeFcE`GaJ`IaHbKNbLM" | |
13 G = nx.from_sparse6_bytes(data) | |
14 assert_nodes_equal( | |
15 sorted(G.nodes()), | |
16 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], | |
17 ) | |
18 assert_edges_equal( | |
19 G.edges(), | |
20 [ | |
21 (0, 1), | |
22 (0, 2), | |
23 (0, 3), | |
24 (1, 12), | |
25 (1, 14), | |
26 (2, 13), | |
27 (2, 15), | |
28 (3, 16), | |
29 (3, 17), | |
30 (4, 7), | |
31 (4, 9), | |
32 (4, 11), | |
33 (5, 6), | |
34 (5, 8), | |
35 (5, 9), | |
36 (6, 10), | |
37 (6, 11), | |
38 (7, 8), | |
39 (7, 10), | |
40 (8, 12), | |
41 (9, 15), | |
42 (10, 14), | |
43 (11, 13), | |
44 (12, 16), | |
45 (13, 17), | |
46 (14, 17), | |
47 (15, 16), | |
48 ], | |
49 ) | |
50 | |
51 def test_from_bytes_multigraph_graph(self): | |
52 graph_data = b":An" | |
53 G = nx.from_sparse6_bytes(graph_data) | |
54 assert type(G) == nx.Graph | |
55 multigraph_data = b":Ab" | |
56 M = nx.from_sparse6_bytes(multigraph_data) | |
57 assert type(M) == nx.MultiGraph | |
58 | |
59 def test_read_sparse6(self): | |
60 data = b":Q___eDcdFcDeFcE`GaJ`IaHbKNbLM" | |
61 G = nx.from_sparse6_bytes(data) | |
62 fh = BytesIO(data) | |
63 Gin = nx.read_sparse6(fh) | |
64 assert_nodes_equal(G.nodes(), Gin.nodes()) | |
65 assert_edges_equal(G.edges(), Gin.edges()) | |
66 | |
67 def test_read_many_graph6(self): | |
68 # Read many graphs into list | |
69 data = b":Q___eDcdFcDeFcE`GaJ`IaHbKNbLM\n" b":Q___dCfDEdcEgcbEGbFIaJ`JaHN`IM" | |
70 fh = BytesIO(data) | |
71 glist = nx.read_sparse6(fh) | |
72 assert len(glist) == 2 | |
73 for G in glist: | |
74 assert_nodes_equal( | |
75 G.nodes(), | |
76 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], | |
77 ) | |
78 | |
79 | |
80 class TestWriteSparse6: | |
81 """Unit tests for writing graphs in the sparse6 format. | |
82 | |
83 Most of the test cases were checked against the sparse6 encoder in Sage. | |
84 | |
85 """ | |
86 | |
87 def test_null_graph(self): | |
88 G = nx.null_graph() | |
89 result = BytesIO() | |
90 nx.write_sparse6(G, result) | |
91 assert result.getvalue() == b">>sparse6<<:?\n" | |
92 | |
93 def test_trivial_graph(self): | |
94 G = nx.trivial_graph() | |
95 result = BytesIO() | |
96 nx.write_sparse6(G, result) | |
97 assert result.getvalue() == b">>sparse6<<:@\n" | |
98 | |
99 def test_empty_graph(self): | |
100 G = nx.empty_graph(5) | |
101 result = BytesIO() | |
102 nx.write_sparse6(G, result) | |
103 assert result.getvalue() == b">>sparse6<<:D\n" | |
104 | |
105 def test_large_empty_graph(self): | |
106 G = nx.empty_graph(68) | |
107 result = BytesIO() | |
108 nx.write_sparse6(G, result) | |
109 assert result.getvalue() == b">>sparse6<<:~?@C\n" | |
110 | |
111 def test_very_large_empty_graph(self): | |
112 G = nx.empty_graph(258049) | |
113 result = BytesIO() | |
114 nx.write_sparse6(G, result) | |
115 assert result.getvalue() == b">>sparse6<<:~~???~?@\n" | |
116 | |
117 def test_complete_graph(self): | |
118 G = nx.complete_graph(4) | |
119 result = BytesIO() | |
120 nx.write_sparse6(G, result) | |
121 assert result.getvalue() == b">>sparse6<<:CcKI\n" | |
122 | |
123 def test_no_header(self): | |
124 G = nx.complete_graph(4) | |
125 result = BytesIO() | |
126 nx.write_sparse6(G, result, header=False) | |
127 assert result.getvalue() == b":CcKI\n" | |
128 | |
129 def test_padding(self): | |
130 codes = (b":Cdv", b":DaYn", b":EaYnN", b":FaYnL", b":GaYnLz") | |
131 for n, code in enumerate(codes, start=4): | |
132 G = nx.path_graph(n) | |
133 result = BytesIO() | |
134 nx.write_sparse6(G, result, header=False) | |
135 assert result.getvalue() == code + b"\n" | |
136 | |
137 def test_complete_bipartite(self): | |
138 G = nx.complete_bipartite_graph(6, 9) | |
139 result = BytesIO() | |
140 nx.write_sparse6(G, result) | |
141 # Compared with sage | |
142 expected = b">>sparse6<<:Nk" + b"?G`cJ" * 9 + b"\n" | |
143 assert result.getvalue() == expected | |
144 | |
145 def test_read_write_inverse(self): | |
146 for i in list(range(13)) + [31, 47, 62, 63, 64, 72]: | |
147 m = min(2 * i, i * i // 2) | |
148 g = nx.random_graphs.gnm_random_graph(i, m, seed=i) | |
149 gstr = BytesIO() | |
150 nx.write_sparse6(g, gstr, header=False) | |
151 # Strip the trailing newline. | |
152 gstr = gstr.getvalue().rstrip() | |
153 g2 = nx.from_sparse6_bytes(gstr) | |
154 assert g2.order() == g.order() | |
155 assert_edges_equal(g2.edges(), g.edges()) | |
156 | |
157 def test_no_directed_graphs(self): | |
158 with pytest.raises(nx.NetworkXNotImplemented): | |
159 nx.write_sparse6(nx.DiGraph(), BytesIO()) | |
160 | |
161 def test_write_path(self): | |
162 # On Windows, we can't reopen a file that is open | |
163 # So, for test we get a valid name from tempfile but close it. | |
164 with tempfile.NamedTemporaryFile() as f: | |
165 fullfilename = f.name | |
166 # file should be closed now, so write_sparse6 can open it | |
167 nx.write_sparse6(nx.null_graph(), fullfilename) | |
168 fh = open(fullfilename, mode="rb") | |
169 assert fh.read() == b">>sparse6<<:?\n" | |
170 fh.close() | |
171 import os | |
172 | |
173 os.remove(fullfilename) |