code: def test_add_selfloop(idtype):
      # homogeneous graph
  
      # test for fill_data is float
      g = dgl.graph(
          ([0, 0, 2], [2, 1, 0]), idtype=idtype, device=F.ctx()
      )
      g.edata["he"] = F.copy_to(
          F.tensor([1, 2, 3], dtype=idtype), ctx=F.ctx()
      )
      g.edata["he1"] = F.copy_to(
          F.tensor([[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]), ctx=F.ctx()
      )
      g.ndata["hn"] = F.copy_to(
          F.tensor([1, 2, 3], dtype=idtype), ctx=F.ctx()
      )
      g = dgl.add_self_loop(g)
      assert g.num_nodes() == 3
      assert g.num_edges() == 6
      u, v = g.edges(form="uv", order="eid")
      assert F.array_equal(
          u, F.tensor([0, 0, 2, 0, 1, 2], dtype=idtype)
      )
      assert F.array_equal(
          v, F.tensor([2, 1, 0, 0, 1, 2], dtype=idtype)
      )
      assert F.array_equal(
          g.edata["he"], F.tensor([1, 2, 3, 1, 1, 1], dtype=idtype)
      )
      assert F.array_equal(
          g.edata["he1"],
          F.tensor(
              [
                  [0.0, 1.0],
                  [2.0, 3.0],
                  [4.0, 5.0],
                  [1.0, 1.0],
                  [1.0, 1.0],
                  [1.0, 1.0],
              ]
          ),
      )
  
      # test for fill_data is int
      g = dgl.graph(
          ([0, 0, 2], [2, 1, 0]), idtype=idtype, device=F.ctx()
      )
      g.edata["he"] = F.copy_to(
          F.tensor([1, 2, 3], dtype=idtype), ctx=F.ctx()
      )
      g.edata["he1"] = F.copy_to(
          F.tensor([[0, 1], [2, 3], [4, 5]], dtype=idtype), ctx=F.ctx()
      )
      g.ndata["hn"] = F.copy_to(
          F.tensor([1, 2, 3], dtype=idtype), ctx=F.ctx()
      )
      g = dgl.add_self_loop(g, fill_data=1)
      assert g.num_nodes() == 3
      assert g.num_edges() == 6
      u, v = g.edges(form="uv", order="eid")
      assert F.array_equal(
          u, F.tensor([0, 0, 2, 0, 1, 2], dtype=idtype)
      )
      assert F.array_equal(
          v, F.tensor([2, 1, 0, 0, 1, 2], dtype=idtype)
      )
      assert F.array_equal(
          g.edata["he"], F.tensor([1, 2, 3, 1, 1, 1], dtype=idtype)
      )
      assert F.array_equal(
          g.edata["he1"],
          F.tensor(
              [[0, 1], [2, 3], [4, 5], [1, 1], [1, 1], [1, 1]],
              dtype=idtype,
          ),
      )
  
      # test for fill_data is str
      g = dgl.graph(
          ([0, 0, 2], [2, 1, 0]), idtype=idtype, device=F.ctx()
      )
      g.edata["he"] = F.copy_to(F.tensor([1.0, 2.0, 3.0]), ctx=F.ctx())
      g.edata["he1"] = F.copy_to(
          F.tensor([[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]), ctx=F.ctx()
      )
      g.ndata["hn"] = F.copy_to(
          F.tensor([1, 2, 3], dtype=idtype), ctx=F.ctx()
      )
      g = dgl.add_self_loop(g, fill_data="sum")
      assert g.num_nodes() == 3
      assert g.num_edges() == 6
      u, v = g.edges(form="uv", order="eid")
      assert F.array_equal(
          u, F.tensor([0, 0, 2, 0, 1, 2], dtype=idtype)
      )
      assert F.array_equal(
          v, F.tensor([2, 1, 0, 0, 1, 2], dtype=idtype)
      )
      assert F.array_equal(
          g.edata["he"], F.tensor([1.0, 2.0, 3.0, 3.0, 2.0, 1.0])
      )
      assert F.array_equal(
          g.edata["he1"],
          F.tensor(
              [
                  [0.0, 1.0],
                  [2.0, 3.0],
                  [4.0, 5.0],
                  [4.0, 5.0],
                  [2.0, 3.0],
                  [0.0, 1.0],
              ]
          ),
      )
  
      # bipartite graph
      g = dgl.heterograph(
          {("user", "plays", "game"): ([0, 1, 2], [1, 2, 2])},
          idtype=idtype,
          device=F.ctx(),
      )
      # nothing will happend
      raise_error = False
      try:
          g = dgl.add_self_loop(g)
      except:
          raise_error = True
      assert raise_error
  
      # test for fill_data is float
      g = create_test_heterograph5(idtype)
      g.edges["follows"].data["h1"] = F.copy_to(
          F.tensor([[0.0, 1.0], [1.0, 2.0]]), ctx=F.ctx()
      )
      g = dgl.add_self_loop(g, etype="follows")
      assert g.num_nodes("user") == 3
      assert g.num_nodes("game") == 2
      assert g.num_edges("follows") == 5
      assert g.num_edges("plays") == 2
      u, v = g.edges(form="uv", order="eid", etype="follows")
      assert F.array_equal(u, F.tensor([1, 2, 0, 1, 2], dtype=idtype))
      assert F.array_equal(v, F.tensor([0, 1, 0, 1, 2], dtype=idtype))
      assert F.array_equal(
          g.edges["follows"].data["h"],
          F.tensor([1, 2, 1, 1, 1], dtype=idtype),
      )
      assert F.array_equal(
          g.edges["follows"].data["h1"],
          F.tensor(
              [
                  [0.0, 1.0],
                  [1.0, 2.0],
                  [1.0, 1.0],
                  [1.0, 1.0],
                  [1.0, 1.0],
              ]
          ),
      )
      assert F.array_equal(
          g.edges["plays"].data["h"], F.tensor([1, 2], dtype=idtype)
      )
  
      # test for fill_data is int
      g = create_test_heterograph5(idtype)
      g.edges["follows"].data["h1"] = F.copy_to(
          F.tensor([[0, 1], [1, 2]], dtype=idtype), ctx=F.ctx()
      )
      g = dgl.add_self_loop(g, fill_data=1, etype="follows")
      assert g.num_nodes("user") == 3
      assert g.num_nodes("game") == 2
      assert g.num_edges("follows") == 5
      assert g.num_edges("plays") == 2
      u, v = g.edges(form="uv", order="eid", etype="follows")
      assert F.array_equal(u, F.tensor([1, 2, 0, 1, 2], dtype=idtype))
      assert F.array_equal(v, F.tensor([0, 1, 0, 1, 2], dtype=idtype))
      assert F.array_equal(
          g.edges["follows"].data["h"],
          F.tensor([1, 2, 1, 1, 1], dtype=idtype),
      )
      assert F.array_equal(
          g.edges["follows"].data["h1"],
          F.tensor(
              [[0, 1], [1, 2], [1, 1], [1, 1], [1, 1]], dtype=idtype
          ),
      )
      assert F.array_equal(
          g.edges["plays"].data["h"], F.tensor([1, 2], dtype=idtype)
      )
  
      # test for fill_data is str
      g = dgl.heterograph(
          {
              ("user", "follows", "user"): (
                  F.tensor([1, 2], dtype=idtype),
                  F.tensor([0, 1], dtype=idtype),
              ),
              ("user", "plays", "game"): (
                  F.tensor([0, 1], dtype=idtype),
                  F.tensor([0, 1], dtype=idtype),
              ),
          },
          idtype=idtype,
          device=F.ctx(),
      )
      g.nodes["user"].data["h"] = F.copy_to(
          F.tensor([1, 1, 1], dtype=idtype), ctx=F.ctx()
      )
      g.nodes["game"].data["h"] = F.copy_to(
          F.tensor([2, 2], dtype=idtype), ctx=F.ctx()
      )
      g.edges["follows"].data["h"] = F.copy_to(
          F.tensor([1.0, 2.0]), ctx=F.ctx()
      )
      g.edges["follows"].data["h1"] = F.copy_to(
          F.tensor([[0.0, 1.0], [1.0, 2.0]]), ctx=F.ctx()
      )
      g.edges["plays"].data["h"] = F.copy_to(
          F.tensor([1.0, 2.0]), ctx=F.ctx()
      )
      g = dgl.add_self_loop(g, fill_data="mean", etype="follows")
      assert g.num_nodes("user") == 3
      assert g.num_nodes("game") == 2
      assert g.num_edges("follows") == 5
      assert g.num_edges("plays") == 2
      u, v = g.edges(form="uv", order="eid", etype="follows")
      assert F.array_equal(u, F.tensor([1, 2, 0, 1, 2], dtype=idtype))
      assert F.array_equal(v, F.tensor([0, 1, 0, 1, 2], dtype=idtype))
      assert F.array_equal(
          g.edges["follows"].data["h"],
          F.tensor([1.0, 2.0, 1.0, 2.0, 0.0]),
      )
      assert F.array_equal(
          g.edges["follows"].data["h1"],
          F.tensor(
              [
                  [0.0, 1.0],
                  [1.0, 2.0],
                  [0.0, 1.0],
                  [1.0, 2.0],
                  [0.0, 0.0],
              ]
          ),
      )
      assert F.array_equal(
          g.edges["plays"].data["h"], F.tensor([1.0, 2.0])
      )
  
      raise_error = False
      try:
          g = dgl.add_self_loop(g, etype="plays")
      except:
          raise_error = True
      assert raise_error
  
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 29 for calling a global function with example usage: test_add_selfloop(idtype) without return types