code: def test_topk(g, idtype, descending):
      g = g.astype(idtype).to(F.ctx())
      g.ndata["x"] = F.randn((g.num_nodes(), 3))
  
      # Test.1: to test the case where k > number of nodes.
      dgl.topk_nodes(g, "x", 100, sortby=-1)
  
      # Test.2: test correctness
      min_nnodes = F.asnumpy(g.batch_num_nodes()).min()
      if min_nnodes <= 1:
          return
      k = min_nnodes - 1
      val, indices = dgl.topk_nodes(
          g, "x", k, descending=descending, sortby=-1
      )
      print(k)
      print(g.ndata["x"])
      print("val", val)
      print("indices", indices)
      subg = dgl.unbatch(g)
      subval, subidx = [], []
      for sg in subg:
          subx = F.asnumpy(sg.ndata["x"])
          ai = np.argsort(subx[:, -1:].flatten())
          if descending:
              ai = np.ascontiguousarray(ai[::-1])
          subx = np.expand_dims(subx[ai[:k]], 0)
          subval.append(F.tensor(subx))
          subidx.append(F.tensor(np.expand_dims(ai[:k], 0)))
      print(F.cat(subval, dim=0))
      assert F.allclose(val, F.cat(subval, dim=0))
      assert F.allclose(indices, F.cat(subidx, dim=0))
  
      # Test.3: sorby=None
      dgl.topk_nodes(g, "x", k, sortby=None)
  
      g.edata["x"] = F.randn((g.num_edges(), 3))
  
      # Test.4: topk edges where k > number of edges.
      dgl.topk_edges(g, "x", 100, sortby=-1)
  
      # Test.5: topk edges test correctness
      min_nedges = F.asnumpy(g.batch_num_edges()).min()
      if min_nedges <= 1:
          return
      k = min_nedges - 1
      val, indices = dgl.topk_edges(
          g, "x", k, descending=descending, sortby=-1
      )
      print(k)
      print(g.edata["x"])
      print("val", val)
      print("indices", indices)
      subg = dgl.unbatch(g)
      subval, subidx = [], []
      for sg in subg:
          subx = F.asnumpy(sg.edata["x"])
          ai = np.argsort(subx[:, -1:].flatten())
          if descending:
              ai = np.ascontiguousarray(ai[::-1])
          subx = np.expand_dims(subx[ai[:k]], 0)
          subval.append(F.tensor(subx))
          subidx.append(F.tensor(np.expand_dims(ai[:k], 0)))
      print(F.cat(subval, dim=0))
      assert F.allclose(val, F.cat(subval, dim=0))
      assert F.allclose(indices, F.cat(subidx, dim=0))
  
file_path: dgl/tests/python/common/test_readout.py
imports: import unittest
  import backend
  import dgl
  import networkx
  import numpy
  import pytest
  from utils import parametrize_idtype
  from utils.graph_cases import get_cases
label_desc: Use image node_id 4 for calling a global function with example usage: test_topk(g, idtype, descending) without return types