util_test.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from pathfinding.core.util import bresenham, raytrace, smoothen_path
  2. from pathfinding.core.grid import Grid
  3. def test_bresenham():
  4. """
  5. test bresenham path interpolation
  6. """
  7. assert bresenham([0, 0], [2, 5]) == [
  8. [0, 0], [0, 1],
  9. [1, 2], [1, 3],
  10. [2, 4], [2, 5]
  11. ]
  12. assert bresenham([0, 1], [0, 4]) == [
  13. [0, 1], [0, 2], [0, 3], [0, 4]
  14. ]
  15. def test_raytrace():
  16. """
  17. test raytrace path interpolation
  18. """
  19. assert raytrace([0, 0], [2, 5]) == [
  20. [0, 0], [0, 1],
  21. [1, 1], [1, 2], [1, 3], [1, 4],
  22. [2, 4], [2, 5]
  23. ]
  24. assert raytrace([0, 1], [0, 4]) == [
  25. [0, 1], [0, 2], [0, 3], [0, 4]
  26. ]
  27. def test_smoothen_path():
  28. matrix = [
  29. [1, 1, 1, 1, 1],
  30. [1, 1, 1, 1, 1],
  31. [1, 1, 1, 1, 1],
  32. [1, 1, 1, 1, 1],
  33. [1, 1, 1, 1, 1]
  34. ]
  35. grid = Grid(matrix=matrix)
  36. path = [
  37. [0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [3, 3], [3, 4], [4, 4]
  38. ]
  39. smooth_path = [
  40. [0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [3, 3], [4, 4]
  41. ]
  42. assert smoothen_path(grid, path) == smooth_path