astar_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package astar
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. )
  7. var (
  8. config1 = Config{
  9. Width: 10,
  10. Height: 30,
  11. MapData: [][]int32{
  12. // > Column or Height
  13. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, // v Row ro Width
  14. {1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1},
  15. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1},
  16. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1},
  17. {1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1},
  18. {1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1},
  19. {1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1},
  20. {1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1},
  21. {1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1},
  22. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1},
  23. },
  24. IsDebug: true,
  25. }
  26. config2 = Config{
  27. Width: 10,
  28. Height: 5,
  29. MapData: [][]int32{
  30. // > Column or Height
  31. {1, 1, 1, 1, 1}, // v Row ro Width
  32. {1, 0, 1, 0, 0},
  33. {1, 1, 0, 1, 1},
  34. {1, 1, 1, 0, 1},
  35. {1, 1, 1, 0, 1},
  36. {1, 1, 0, 0, 1},
  37. {1, 1, 0, 1, 1},
  38. {1, 1, 0, 1, 1},
  39. {1, 0, 1, 1, 1},
  40. {1, 1, 1, 1, 1},
  41. },
  42. IsDebug: true,
  43. }
  44. )
  45. func TestAstar(t *testing.T) {
  46. astar := New(config1)
  47. // astar := New(config2)
  48. startTime := time.Now().UnixNano()
  49. fmt.Printf("开始:%d\n", startTime)
  50. path, ok := astar.FindPath(0, 0, 9, 29)
  51. // path, ok := astar.FindPath(0, 0, 9, 4)
  52. // path, ok := astar.FindPath(0, 0, 3, 8)
  53. endTime := time.Now().UnixNano()
  54. fmt.Printf("结束:%d\n", endTime)
  55. fmt.Printf("耗时:%v(ns)\n", endTime-startTime)
  56. if !ok {
  57. t.Error("find path failed")
  58. } else {
  59. astar.Print(path)
  60. }
  61. }