@@ -528,6 +528,52 @@ def test_subgraph_isomorphism(self):
528528 assert graph1 .is_mapping_valid (graph2 , mapping )
529529 assert graph1 .is_mapping_valid (graph2 , mapping )
530530
531+ def test_has_same_labels (self ):
532+ """
533+ Check the has_same_labels() function.
534+ """
535+ from rmgpy .molecule .molecule import Atom
536+
537+ graph1 = Graph ([Atom (label = "*1" ), Atom (label = "*2" ), Atom ()])
538+
539+ # Same labels, same multiplicity of each label
540+ graph2 = Graph ([Atom (label = "*2" ), Atom (label = "*1" ), Atom ()])
541+ assert graph1 .has_same_labels (graph2 )
542+ assert graph2 .has_same_labels (graph1 )
543+
544+ # A graph always has the same labels as itself
545+ assert graph1 .has_same_labels (graph1 )
546+
547+ # Missing a label entirely
548+ graph3 = Graph ([Atom (label = "*1" ), Atom (), Atom ()])
549+ assert not graph1 .has_same_labels (graph3 )
550+ assert not graph3 .has_same_labels (graph1 )
551+
552+ # Same labels present, but with a different multiplicity
553+ # (two vertices labeled "*1" instead of one "*1" and one "*2")
554+ graph4 = Graph ([Atom (label = "*1" ), Atom (label = "*1" ), Atom ()])
555+ assert not graph1 .has_same_labels (graph4 )
556+ assert not graph4 .has_same_labels (graph1 )
557+
558+ # Extra labeled vertex on one side
559+ graph5 = Graph ([Atom (label = "*1" ), Atom (label = "*2" ), Atom (label = "*3" )])
560+ assert not graph1 .has_same_labels (graph5 )
561+ assert not graph5 .has_same_labels (graph1 )
562+
563+ # ignore_labels excludes the given labels from the comparison entirely,
564+ # so a mismatch on an ignored label no longer counts against a match...
565+ assert graph1 .has_same_labels (graph3 , ignore_labels = ["*2" ])
566+ assert graph3 .has_same_labels (graph1 , ignore_labels = ["*2" ])
567+ assert graph1 .has_same_labels (graph5 , ignore_labels = ["*3" ])
568+ assert graph5 .has_same_labels (graph1 , ignore_labels = ["*3" ])
569+
570+ # ...but a mismatch on a label that wasn't ignored still counts
571+ assert not graph1 .has_same_labels (graph3 , ignore_labels = ["*1" ])
572+ assert not graph1 .has_same_labels (graph5 , ignore_labels = ["*1" ])
573+
574+ # ignoring every labeled vertex trivially matches any graph
575+ assert graph1 .has_same_labels (graph4 , ignore_labels = ["*1" , "*2" ])
576+
531577 def test_intersection_isomorphism (self ):
532578 """
533579 Check the intersection isomorphism functions.
0 commit comments