code: def test_reverse(idtype):
      g = dgl.graph([])
      g = g.astype(idtype).to(F.ctx())
      g.add_nodes(5)
      # The graph need not to be completely connected.
      g.add_edges([0, 1, 2], [1, 2, 1])
      g.ndata["h"] = F.tensor([[0.0], [1.0], [2.0], [3.0], [4.0]])
      g.edata["h"] = F.tensor([[5.0], [6.0], [7.0]])
      rg = g.reverse()
  
      assert g.is_multigraph == rg.is_multigraph
  
      assert g.num_nodes() == rg.num_nodes()
      assert g.num_edges() == rg.num_edges()
      assert F.allclose(
          F.astype(
              rg.has_edges_between([1, 2, 1], [0, 1, 2]), F.float32
          ),
          F.ones((3,)),
      )
      assert g.edge_ids(0, 1) == rg.edge_ids(1, 0)
      assert g.edge_ids(1, 2) == rg.edge_ids(2, 1)
      assert g.edge_ids(2, 1) == rg.edge_ids(1, 2)
  
      # test dgl.reverse
      # test homogeneous graph
      g = dgl.graph((F.tensor([0, 1, 2]), F.tensor([1, 2, 0])))
      g.ndata["h"] = F.tensor([[0.0], [1.0], [2.0]])
      g.edata["h"] = F.tensor([[3.0], [4.0], [5.0]])
      g_r = dgl.reverse(g)
      assert g.num_nodes() == g_r.num_nodes()
      assert g.num_edges() == g_r.num_edges()
      u_g, v_g, eids_g = g.all_edges(form="all")
      u_rg, v_rg, eids_rg = g_r.all_edges(form="all")
      assert F.array_equal(u_g, v_rg)
      assert F.array_equal(v_g, u_rg)
      assert F.array_equal(eids_g, eids_rg)
      assert F.array_equal(g.ndata["h"], g_r.ndata["h"])
      assert len(g_r.edata) == 0
  
      # without share ndata
      g_r = dgl.reverse(g, copy_ndata=False)
      assert g.num_nodes() == g_r.num_nodes()
      assert g.num_edges() == g_r.num_edges()
      assert len(g_r.ndata) == 0
      assert len(g_r.edata) == 0
  
      # with share ndata and edata
      g_r = dgl.reverse(g, copy_ndata=True, copy_edata=True)
      assert g.num_nodes() == g_r.num_nodes()
      assert g.num_edges() == g_r.num_edges()
      assert F.array_equal(g.ndata["h"], g_r.ndata["h"])
      assert F.array_equal(g.edata["h"], g_r.edata["h"])
  
      # add new node feature to g_r
      g_r.ndata["hh"] = F.tensor([0, 1, 2])
      assert ("hh" in g.ndata) is False
      assert ("hh" in g_r.ndata) is True
  
      # add new edge feature to g_r
      g_r.edata["hh"] = F.tensor([0, 1, 2])
      assert ("hh" in g.edata) is False
      assert ("hh" in g_r.edata) is True
  
      # test heterogeneous graph
      g = dgl.heterograph(
          {
              ("user", "follows", "user"): (
                  [0, 1, 2, 4, 3, 1, 3],
                  [1, 2, 3, 2, 0, 0, 1],
              ),
              ("user", "plays", "game"): (
                  [0, 0, 2, 3, 3, 4, 1],
                  [1, 0, 1, 0, 1, 0, 0],
              ),
              ("developer", "develops", "game"): (
                  [0, 1, 1, 2],
                  [0, 0, 1, 1],
              ),
          },
          idtype=idtype,
          device=F.ctx(),
      )
      g.nodes["user"].data["h"] = F.tensor([0, 1, 2, 3, 4])
      g.nodes["user"].data["hh"] = F.tensor([1, 1, 1, 1, 1])
      g.nodes["game"].data["h"] = F.tensor([0, 1])
      g.edges["follows"].data["h"] = F.tensor([0, 1, 2, 4, 3, 1, 3])
      g.edges["follows"].data["hh"] = F.tensor([1, 2, 3, 2, 0, 0, 1])
      g_r = dgl.reverse(g)
  
      for etype_g, etype_gr in zip(
          g.canonical_etypes, g_r.canonical_etypes
      ):
          assert etype_g[0] == etype_gr[2]
          assert etype_g[1] == etype_gr[1]
          assert etype_g[2] == etype_gr[0]
          assert g.num_edges(etype_g) == g_r.num_edges(etype_gr)
      for ntype in g.ntypes:
          assert g.num_nodes(ntype) == g_r.num_nodes(ntype)
      assert F.array_equal(
          g.nodes["user"].data["h"], g_r.nodes["user"].data["h"]
      )
      assert F.array_equal(
          g.nodes["user"].data["hh"], g_r.nodes["user"].data["hh"]
      )
      assert F.array_equal(
          g.nodes["game"].data["h"], g_r.nodes["game"].data["h"]
      )
      assert len(g_r.edges["follows"].data) == 0
      u_g, v_g, eids_g = g.all_edges(
          form="all", etype=("user", "follows", "user")
      )
      u_rg, v_rg, eids_rg = g_r.all_edges(
          form="all", etype=("user", "follows", "user")
      )
      assert F.array_equal(u_g, v_rg)
      assert F.array_equal(v_g, u_rg)
      assert F.array_equal(eids_g, eids_rg)
      u_g, v_g, eids_g = g.all_edges(
          form="all", etype=("user", "plays", "game")
      )
      u_rg, v_rg, eids_rg = g_r.all_edges(
          form="all", etype=("game", "plays", "user")
      )
      assert F.array_equal(u_g, v_rg)
      assert F.array_equal(v_g, u_rg)
      assert F.array_equal(eids_g, eids_rg)
      u_g, v_g, eids_g = g.all_edges(
          form="all", etype=("developer", "develops", "game")
      )
      u_rg, v_rg, eids_rg = g_r.all_edges(
          form="all", etype=("game", "develops", "developer")
      )
      assert F.array_equal(u_g, v_rg)
      assert F.array_equal(v_g, u_rg)
      assert F.array_equal(eids_g, eids_rg)
  
      # withour share ndata
      g_r = dgl.reverse(g, copy_ndata=False)
      for etype_g, etype_gr in zip(
          g.canonical_etypes, g_r.canonical_etypes
      ):
          assert etype_g[0] == etype_gr[2]
          assert etype_g[1] == etype_gr[1]
          assert etype_g[2] == etype_gr[0]
          assert g.num_edges(etype_g) == g_r.num_edges(etype_gr)
      for ntype in g.ntypes:
          assert g.num_nodes(ntype) == g_r.num_nodes(ntype)
      assert len(g_r.nodes["user"].data) == 0
      assert len(g_r.nodes["game"].data) == 0
  
      g_r = dgl.reverse(g, copy_ndata=True, copy_edata=True)
      print(g_r)
      for etype_g, etype_gr in zip(
          g.canonical_etypes, g_r.canonical_etypes
      ):
          assert etype_g[0] == etype_gr[2]
          assert etype_g[1] == etype_gr[1]
          assert etype_g[2] == etype_gr[0]
          assert g.num_edges(etype_g) == g_r.num_edges(etype_gr)
      assert F.array_equal(
          g.edges["follows"].data["h"], g_r.edges["follows"].data["h"]
      )
      assert F.array_equal(
          g.edges["follows"].data["hh"], g_r.edges["follows"].data["hh"]
      )
  
      # add new node feature to g_r
      g_r.nodes["user"].data["hhh"] = F.tensor([0, 1, 2, 3, 4])
      assert ("hhh" in g.nodes["user"].data) is False
      assert ("hhh" in g_r.nodes["user"].data) is True
  
      # add new edge feature to g_r
      g_r.edges["follows"].data["hhh"] = F.tensor([1, 2, 3, 2, 0, 0, 1])
      assert ("hhh" in g.edges["follows"].data) is False
      assert ("hhh" in g_r.edges["follows"].data) is True
  
imports: import math
  import os
  import unittest
  import backend
  import dgl
  import dgl.function
  import dgl.partition
  import networkx
  import numpy
  import pytest
  from scipy import sparse
  from utils import parametrize_idtype
  from utils.graph_cases import get_cases
label_desc: Use image node_id 7 for calling a global function with example usage: test_reverse(idtype) without return types