diff --git a/tests/test_api_enchance.py b/tests/test_api_enchance.py index f4e7cc0..18dc91e 100644 --- a/tests/test_api_enchance.py +++ b/tests/test_api_enchance.py @@ -74,14 +74,14 @@ def test_remove_edge(): g.e['color'][4,0] = 1 g[0,4] = 0 g[1,0] = 0 - with pytest.raises(IndexError, match='No such edge'): + with pytest.raises(IndexError, match='Missing at least one edge.'): g.e['color'][1,4] = 6 - with pytest.raises(IndexError, match='No such edge.'): + with pytest.raises(IndexError, match='Missing at least one edge.'): g.e['color'][0,1] assert g.e['color'][1,2] == 4 assert g.e['color'][2,3] == 5 assert g.e['color'][3,4] == 6 - with pytest.raises(IndexError, match='No such edge.'): + with pytest.raises(IndexError, match='Missing at least one edge.'): g.e['color'][4,0] def test_permute(): @@ -110,4 +110,4 @@ def test_permute(): assert not graph_equality(g2, pG11) assert graph_equality(g2, pG12) - assert graph_equality(g1, pG13) \ No newline at end of file + assert graph_equality(g1, pG13) diff --git a/tests/test_lib_basics.py b/tests/test_lib_basics.py index 5c92f04..dc42892 100644 --- a/tests/test_lib_basics.py +++ b/tests/test_lib_basics.py @@ -207,7 +207,7 @@ def test_graph_props(): Simple tests of per-graph properties """ - + g1 = tg.TinyGraph(10) g1.props['foo'] = 'bar' @@ -217,9 +217,9 @@ def test_graph_props(): assert tg.util.graph_equality(g1, g2) g2.props['baz'] = 7 - + assert not tg.util.graph_equality(g1, g2) - + @pytest.mark.parametrize("test_name", [k for k in suite.keys()]) def test_copy_suite(test_name): @@ -228,5 +228,112 @@ def test_copy_suite(test_name): """ for g in suite[test_name]: g1 = g.copy() - assert tg.util.graph_equality(g1, g) - + assert tg.util.graph_equality(g, g1) + + # Change g1 but not g + g1.add_vert_prop('useless_vertex_property', np.bool) + assert not tg.util.graph_equality(g, g1) + + +def test_sequence_adjacency(): + """ + Ensure that sequences can be used to modify the adjacency matrix compactly + """ + g = tg.TinyGraph(3, np.int) + # Edges: + g[[0, 1, 2], [1, 2, 0]] = np.array([1, 2, 3]) + + # Desired effect on adjacency matrix + assert np.array_equal(g.adjacency, np.array([[0, 1, 3], [1, 0, 2], [3, 2, 0]])) + + # And __getitem__ fetches just a 3-item array rather than something weird + assert np.array_equal(g[[0, 1, 2], [1, 2, 0]], [1, 2, 3]) + + # Conflicting writes are dealt with + g[[0, 1], [1, 0]] = [7, 8] + # still symmetric! + assert np.array_equal(g.adjacency, g.adjacency.T) + + # Add an edge from 0 to 2 + g[range(1), range(2, 3)] = 13 + assert g.adjacency[0, 2] == 13 + + # Zero-out everything + g[[0, 1, 2], [1, 2, 0]] = 0 + assert np.all(g.adjacency == 0) + + # Ensure tuples work + g[(0,), (1,)] = 1 + assert g.adjacency[1, 0] == 1 + g[(0, 1), (1, 2)] = 2 + assert g.adjacency[1, 0] == 2 + assert g.adjacency[2, 1] == 2 + g[(0, 1), (1, 2)] = (3, 5) + assert g.adjacency[1, 0] == 3 + assert g.adjacency[2, 1] == 5 + +def test_sequence_adjacency_errors(): + """ + Test the error-handling for poorly setting adjacency matrix values + """ + g = tg.TinyGraph(3, np.int) + with pytest.raises(KeyError): + g[[0, 0, 0], [1, 2]] = 1 + + with pytest.raises(IndexError): + # Attempt to set self-edges + g[[0, 1], [0, 1]] = 5 + + with pytest.raises(ValueError): + # length mismatch -> numpy shape mismatch + g[[0, 1], [1, 2]] = [5, 2, 6] + +def test_sequence_vprop(): + """ + Test that sequences can be used for assigning vertex properties + """ + g = tg.TinyGraph(3, vp_types={'name':np.dtype(' 1: + reorder = lambda x, y: (x, y) if x 1: + reorder = lambda x, y: (x, y) if x