code: def test_state_dict():
      engine = Engine(lambda e, b: 1)
      sd = engine.state_dict()
      assert isinstance(sd, Mapping) and len(sd) == 3
      assert "iteration" in sd and sd["iteration"] == 0
      assert "max_epochs" in sd and sd["max_epochs"] is None
      assert "epoch_length" in sd and sd["epoch_length"] is None
  
      def _test(state):
          engine.state = state
          sd = engine.state_dict()
          assert (
              isinstance(sd, Mapping)
              and len(sd) == len(engine._state_dict_all_req_keys) + 1
          )
          assert sd["iteration"] == engine.state.iteration
          assert sd["epoch_length"] == engine.state.epoch_length
          assert sd["max_epochs"] == engine.state.max_epochs
  
      _test(State(iteration=500, epoch_length=1000, max_epochs=100))
      _test(State(epoch=5, epoch_length=1000, max_epochs=100))
  
file_path: ignite/tests/ignite/engine/test_engine_state_dict.py
imports: from collections.abc import Mapping
  import pytest
  import torch
  from ignite.engine import Engine, Events, State
  from tests.ignite.engine import BatchChecker, EpochCounter, IterationCounter
label_desc: Use image node_id 1 for calling a global function with example usage: test_state_dict() without return types