docstr: Tests `get` and `getitem` functionalities with a lookback range > 0.
docstr_tok: ["Tests","`","get","`","and","`","getitem","`","functionalities","with","a","lookback","range",">","0","."]
code: def test_get_with_lookback(self):
      """Tests `get` and `getitem` functionalities with a lookback range > 0."""
      buffer = BufferWithInfiniteLookback(
          data=[0, 1, 2, 3, 4], lookback=2
      )
  
      # Test on ongoing and finalized buffer.
      for finalized in [False, True]:
          if finalized:
              buffer.finalize()
  
          self.assertTrue(len(buffer), 3)
          # No args: Expect all contents excluding lookback buffer.
          check(buffer.get(), [2, 3, 4])
          check(buffer[:], [2, 3, 4])
          # Individual negative indices (include lookback buffer).
          check(buffer.get(-1), 4)
          check(buffer[-1], 4)
          check(buffer.get(-2), 3)
          check(buffer[-2], 3)
          check(buffer.get(-4), 1)
          check(buffer[-4], 1)
          check(buffer.get([-4]), [1])
          check(buffer[-4:-3], [1])
          self.assertRaises(IndexError, lambda: buffer.get(-6))
          self.assertRaises(IndexError, lambda: buffer[-6])
          self.assertRaises(IndexError, lambda: buffer.get(-1000))
          self.assertRaises(IndexError, lambda: buffer[-1000])
          # Individual positive indices (do NOT include lookback buffer).
          check(buffer.get(0), 2)
          check(buffer[0], 2)
          check(buffer.get(1), 3)
          check(buffer[1], 3)
          check(buffer.get(2), 4)
          check(buffer[2], 4)
          check(buffer.get([2]), [4])
          check(buffer[2:3], [4])
          self.assertRaises(IndexError, lambda: buffer.get(3))
          self.assertRaises(IndexError, lambda: buffer[3])
          self.assertRaises(IndexError, lambda: buffer.get(1000))
          self.assertRaises(IndexError, lambda: buffer[1000])
          # List of negative indices (include lookback buffer).
          check(buffer.get([-4, -5]), [1, 0])
          check(buffer.get([-1]), [4])
          check(buffer[-1:], [4])
          check(buffer.get([-5]), [0])
          check(buffer[-5:-4], [0])
          self.assertRaises(IndexError, lambda: buffer.get([-6]))
          self.assertRaises(IndexError, lambda: buffer.get([-1, -6]))
          self.assertRaises(IndexError, lambda: buffer.get([-1000]))
          # List of positive indices (do NOT include lookback buffer).
          check(buffer.get([1, 0, 2]), [3, 2, 4])
          check(buffer.get([0, 2, 1]), [2, 4, 3])
          self.assertRaises(IndexError, lambda: buffer.get([6]))
          self.assertRaises(IndexError, lambda: buffer.get([1, 6]))
          self.assertRaises(IndexError, lambda: buffer.get([1000]))
          # List of positive and negative indices (do NOT include lookback buffer).
          check(buffer.get([1, 0, -2]), [3, 2, 3])
          check(buffer.get([-3, 1, -1]), [2, 3, 4])
          # Slices.
          # Type: [None:...]
          check(buffer.get(slice(None, None)), [2, 3, 4])
          check(buffer.get(slice(None, 2)), [2, 3])
          check(buffer[:2], [2, 3])
          check(buffer.get(slice(3)), [2, 3, 4])
          check(buffer[:3], [2, 3, 4])
          check(buffer.get(slice(None, -1)), [2, 3])
          check(buffer[:-1], [2, 3])
          check(buffer.get(slice(None, -2)), [2])
          check(buffer[:-2], [2])
          # Type: [...:None]
          check(buffer.get(slice(2, None)), [4])
          check(buffer[2:], [4])
          check(buffer.get(slice(2, 5)), [4])
          check(buffer[2:5], [4])
          check(buffer.get(slice(1, None)), [3, 4])
          check(buffer[1:], [3, 4])
          check(buffer.get(slice(1, 5)), [3, 4])
          check(buffer[1:5], [3, 4])
          check(buffer.get(slice(-1, None)), [4])
          check(buffer[-1:], [4])
          check(buffer.get(slice(-1, 5)), [4])
          check(buffer[-1:5], [4])
          check(buffer.get(slice(-4, None)), [1, 2, 3, 4])
          check(buffer[-4:], [1, 2, 3, 4])
          check(buffer.get(slice(-4, 5)), [1, 2, 3, 4])
          check(buffer[-4:5], [1, 2, 3, 4])
          # Type: [-n:-m]
          check(buffer.get(slice(-2, -1)), [3])
          check(buffer[-2:-1], [3])
          check(buffer.get(slice(-3, -1)), [2, 3])
          check(buffer[-3:-1], [2, 3])
          check(buffer.get(slice(-4, -2)), [1, 2])
          check(buffer[-4:-2], [1, 2])
          check(buffer.get(slice(-4, -1)), [1, 2, 3])
          check(buffer[-4:-1], [1, 2, 3])
          check(buffer.get(slice(-5, -1)), [0, 1, 2, 3])
          check(buffer[-5:-1], [0, 1, 2, 3])
          check(buffer.get(slice(-6, -2)), [0, 1, 2])
          check(buffer[-6:-2], [0, 1, 2])
          # Type: [+n:+m]
          check(buffer.get(slice(0, 1)), [2])
          check(buffer[0:1], [2])
          check(buffer.get(slice(0, 2)), [2, 3])
          check(buffer[0:2], [2, 3])
          check(buffer.get(slice(0, 3)), [2, 3, 4])
          check(buffer[0:3], [2, 3, 4])
          check(buffer.get(slice(1, 2)), [3])
          check(buffer[1:2], [3])
          check(buffer.get(slice(1, 3)), [3, 4])
          check(buffer[1:3], [3, 4])
          check(buffer.get(slice(2, 3)), [4])
          check(buffer[2:3], [4])
          # Type: [+n:-m]
          check(buffer.get(slice(0, -1)), [2, 3])
          check(buffer[0:-1], [2, 3])
          check(buffer.get(slice(0, -2)), [2])
          check(buffer[0:-2], [2])
          check(buffer.get(slice(1, -1)), [3])
          check(buffer[1:-1], [3])
  
      # Check the type on the finalized buffer (numpy arrays).
      data = buffer.get([1, 0, 2])
      self.assertTrue(isinstance(data, np.ndarray))
      check(data, [3, 2, 4])
  
file_path: ray/rllib/env/tests/test_lookback_buffer.py
imports: import unittest
  import gymnasium
  import numpy
  from ray.rllib.env.utils import BufferWithInfiniteLookback
  from ray.rllib.utils.spaces.space_utils import batch, get_dummy_batch_for_space
  from ray.rllib.utils.test_utils import check
label_desc: Use image node_id 4 for calling the TestBufferWithInfiniteLookback obj's underlying member method code with example usage: obj.test_get_with_lookback() without return types