code: def test_LibSVMIter(tmpdir):
      def check_libSVMIter_synthetic():
          data_path = os.path.join(str(tmpdir), "data.t")
          label_path = os.path.join(str(tmpdir), "label.t")
          with open(data_path, "w") as fout:
              fout.write("1.0 0:0.5 2:1.2\n")
              fout.write("-2.0\n")
              fout.write("-3.0 0:0.6 1:2.4 2:1.2\n")
              fout.write("4 2:-1.2\n")
  
          with open(label_path, "w") as fout:
              fout.write("1.0\n")
              fout.write("-2.0 0:0.125\n")
              fout.write("-3.0 2:1.2\n")
              fout.write("4 1:1.0 2:-1.2\n")
  
          data_dir = os.path.join(str(tmpdir), "data")
          data_train = mx.io.LibSVMIter(
              data_libsvm=data_path,
              label_libsvm=label_path,
              data_shape=(3,),
              label_shape=(3,),
              batch_size=3,
          )
  
          first = mx.nd.array(
              [[0.5, 0.0, 1.2], [0.0, 0.0, 0.0], [0.6, 2.4, 1.2]]
          )
          second = mx.nd.array(
              [[0.0, 0.0, -1.2], [0.5, 0.0, 1.2], [0.0, 0.0, 0.0]]
          )
          i = 0
          for _ in iter(data_train):
              expected = first.asnumpy() if i == 0 else second.asnumpy()
              data = data_train.getdata()
              data.check_format(True)
              assert_almost_equal(data.asnumpy(), expected)
              i += 1
  
      def check_libSVMIter_news_data():
          news_metadata = {
              "name": "news20.t",
              "origin_name": "news20.t.bz2",
              "url": "https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/news20.t.bz2",
              "feature_dim": 62060 + 1,
              "num_classes": 20,
              "num_examples": 3993,
          }
          batch_size = 33
          num_examples = news_metadata["num_examples"]
          data_dir = os.path.join(str(tmpdir), "data")
          get_bz2_data(
              data_dir,
              news_metadata["name"],
              news_metadata["url"],
              news_metadata["origin_name"],
          )
          path = os.path.join(data_dir, news_metadata["name"])
          data_train = mx.io.LibSVMIter(
              data_libsvm=path,
              data_shape=(news_metadata["feature_dim"],),
              batch_size=batch_size,
          )
          for _ in range(2):
              num_batches = 0
              for batch in data_train:
                  # check the range of labels
                  data = batch.data[0]
                  label = batch.label[0]
                  data.check_format(True)
                  assert np.sum(label.asnumpy() > 20) == 0
                  assert np.sum(label.asnumpy() <= 0) == 0
                  num_batches += 1
              expected_num_batches = num_examples / batch_size
              assert num_batches == int(
                  expected_num_batches
              ), num_batches
              data_train.reset()
  
      def check_libSVMIter_exception():
          data_path = os.path.join(str(tmpdir), "data.t")
          label_path = os.path.join(str(tmpdir), "label.t")
          with open(data_path, "w") as fout:
              fout.write("1.0 0:0.5 2:1.2\n")
              fout.write("-2.0\n")
              # Below line has a neg indice. Should throw an exception
              fout.write("-3.0 -1:0.6 1:2.4 2:1.2\n")
              fout.write("4 2:-1.2\n")
  
          with open(label_path, "w") as fout:
              fout.write("1.0\n")
              fout.write("-2.0 0:0.125\n")
              fout.write("-3.0 2:1.2\n")
              fout.write("4 1:1.0 2:-1.2\n")
          data_dir = os.path.join(str(tmpdir), "data")
          data_train = mx.io.LibSVMIter(
              data_libsvm=data_path,
              label_libsvm=label_path,
              data_shape=(3,),
              label_shape=(3,),
              batch_size=3,
          )
          for _ in iter(data_train):
              data_train.get_data().asnumpy()
  
      check_libSVMIter_synthetic()
      check_libSVMIter_news_data()
      assertRaises(MXNetError, check_libSVMIter_exception)
  
imports: import mxnet
  import mxnet.ndarray
  from mxnet.test_utils import *
  from mxnet.base import MXNetError
  import numpy
  import os
  import gzip
  import pickle
  import time
  import sys
  from common import assertRaises
  import pytest
  from itertools import zip_longest
label_desc: Use image node_id 14 for calling a global function with example usage: test_LibSVMIter(tmpdir) without return types