test_common.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import random
  2. import pytest
  3. from aphrodite.processing.block.common import RefCounter
  4. @pytest.mark.parametrize("seed", list(range(20)))
  5. @pytest.mark.parametrize("num_incrs", [1, 100])
  6. @pytest.mark.parametrize("num_blocks", [1024])
  7. def test_incr(seed: int, num_incrs: int, num_blocks: int):
  8. random.seed(seed)
  9. all_block_indices = list(range(num_blocks))
  10. counter = RefCounter(all_block_indices=all_block_indices)
  11. block_id = random.randint(0, num_blocks - 1)
  12. for i in range(num_incrs):
  13. value = counter.incr(block_id)
  14. assert value == i + 1
  15. @pytest.mark.parametrize("seed", list(range(20)))
  16. @pytest.mark.parametrize("num_incrs", [1, 100])
  17. @pytest.mark.parametrize("num_blocks", [1024])
  18. def test_incr_decr(seed: int, num_incrs: int, num_blocks: int):
  19. random.seed(seed)
  20. all_block_indices = list(range(num_blocks))
  21. counter = RefCounter(all_block_indices=all_block_indices)
  22. block_id = random.randint(0, num_blocks - 1)
  23. for i in range(num_incrs):
  24. value = counter.incr(block_id)
  25. assert value == i + 1
  26. for i in range(num_incrs):
  27. value = counter.decr(block_id)
  28. assert value == num_incrs - (i + 1)
  29. with pytest.raises(AssertionError):
  30. counter.decr(block_id)