Skip to content

Commit a8be9b8

Browse files
committed
added unit tests
1 parent a5d9d1b commit a8be9b8

5 files changed

Lines changed: 79 additions & 5 deletions

File tree

docs/source/api/algorithms/xgi.algorithms.properties.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ xgi.algorithms.properties
1111
.. autofunction:: degree_histogram
1212
.. autofunction:: density
1313
.. autofunction:: edge_neighborhood
14+
.. autofunction:: equal
1415
.. autofunction:: incidence_density
1516
.. autofunction:: is_possible_order
1617
.. autofunction:: is_uniform

tests/algorithms/test_properties.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,52 @@
44
from xgi.exception import IDNotFound, XGIError
55

66

7+
def test_equality(edgelist1, edgelist2, diedgelist1, diedgelist2):
8+
# hypergraph
9+
H1 = xgi.Hypergraph(edgelist1)
10+
H2 = xgi.Hypergraph(edgelist1)
11+
H3 = xgi.Hypergraph(edgelist2)
12+
13+
assert xgi.equal(H1, H2)
14+
assert not xgi.equal(H2, H3)
15+
16+
H1["test"] = "this should break equality"
17+
assert not xgi.equal(H1, H2)
18+
19+
assert xgi.equal(H1, H2, compare_attrs=False)
20+
21+
el1 = {0: {1, 2, 3}, 1: {1, 2}}
22+
el2 = {1: {1, 2, 3}, 2: {1, 2}}
23+
24+
H1 = xgi.Hypergraph(el1)
25+
H2 = xgi.Hypergraph(el2)
26+
27+
assert not xgi.equal(H1, H2, compare_ids=True)
28+
assert xgi.equal(H1, H2, compare_ids=False)
29+
30+
# dihypergraphs
31+
H1 = xgi.DiHypergraph(diedgelist1)
32+
H2 = xgi.DiHypergraph(diedgelist1)
33+
H3 = xgi.DiHypergraph(diedgelist2)
34+
35+
assert xgi.equal(H1, H2)
36+
assert not xgi.equal(H2, H3)
37+
38+
H1["test"] = "this should break equality"
39+
assert not xgi.equal(H1, H2)
40+
41+
assert xgi.equal(H1, H2, compare_attrs=False)
42+
43+
del1 = {0: ({1, 2, 3}, {4}), 1: ({5, 6}, {6, 7, 8})}
44+
del2 = {1: ({1, 2, 3}, {4}), 2: ({5, 6}, {6, 7, 8})}
45+
46+
H1 = xgi.DiHypergraph(del1)
47+
H2 = xgi.DiHypergraph(del2)
48+
49+
assert not xgi.equal(H1, H2, compare_ids=True)
50+
assert xgi.equal(H1, H2, compare_ids=False)
51+
52+
753
def test_num_edges_order(edgelist2):
854
H = xgi.Hypergraph(edgelist2)
955

tests/core/test_dihypergraph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ def test_contains(diedgelist1):
4444
assert [0] not in H
4545

4646

47+
def test_equals(diedgelist1, diedgelist2):
48+
H1 = xgi.DiHypergraph(diedgelist1)
49+
H2 = xgi.DiHypergraph(diedgelist1)
50+
H3 = xgi.DiHypergraph(diedgelist2)
51+
52+
assert H1 == H2
53+
assert H2 != H3
54+
55+
H1["test"] = "this should break equality"
56+
assert H1 != H2
57+
58+
4759
def test_string():
4860
H1 = xgi.DiHypergraph()
4961
assert str(H1) == "Unnamed DiHypergraph with 0 nodes and 0 hyperedges"

tests/core/test_hypergraph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ def test_lshift(edgelist1, edgelist2, hyperwithattrs):
8585
assert H.edges.members(7) == {1, 2, 3}
8686

8787

88+
def test_equals(edgelist1, edgelist2):
89+
H1 = xgi.Hypergraph(edgelist1)
90+
H2 = xgi.Hypergraph(edgelist1)
91+
H3 = xgi.Hypergraph(edgelist2)
92+
93+
assert H1 == H2
94+
assert H2 != H3
95+
96+
H1["test"] = "this should break equality"
97+
assert H1 != H2
98+
99+
88100
def test_string():
89101
H1 = xgi.Hypergraph()
90102
assert str(H1) == "Unnamed Hypergraph with 0 nodes and 0 hyperedges"

xgi/algorithms/properties.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ def equal(H1, H2, compare_ids=True, compare_attrs=True):
3333
edges2_with_counts = defaultdict(lambda: 0)
3434
for e in H2.edges.members():
3535
edges2_with_counts[frozenset(e)] += 1
36-
36+
print(dict(edges1_with_counts), dict(edges2_with_counts))
3737
if edges1_with_counts != edges2_with_counts:
38+
print("hi")
3839
return False
3940

4041
if compare_attrs:
41-
if H1._hypergraph != H2._hypergraph:
42+
if H1._net_attr != H2._net_attr:
4243
return False
4344

44-
if H1._node_attrs != H2._node_attrs:
45+
if H1._node_attr != H2._node_attr:
4546
return False
4647

47-
if H1._edge_attrs != H2._edge_attrs:
48-
return False
48+
# there's no sense in comparing edge attrs if we're not matching on IDs
49+
if compare_ids:
50+
if H1._edge_attr != H2._edge_attr:
51+
return False
4952

5053
return True
5154

0 commit comments

Comments
 (0)