No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

101 líneas
2.0KB

  1. package position_test
  2. import (
  3. "math"
  4. "testing"
  5. "git.wtrh.nl/wouter/gopatterns/pkg/position"
  6. "git.wtrh.nl/wouter/gopatterns/pkg/vector"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestPosition_Direction(t *testing.T) {
  10. t.Parallel()
  11. type testCase struct {
  12. A, B position.Position
  13. ExpA2B, ExpB2A float64
  14. }
  15. testCases := []testCase{
  16. {
  17. A: position.Position{
  18. Vector: vector.Vector{X: 0, Y: 0},
  19. Rotation: math.Pi / 4,
  20. },
  21. B: position.Position{
  22. Vector: vector.Vector{X: 2, Y: 0},
  23. Rotation: 3 * math.Pi / 4,
  24. },
  25. ExpA2B: -math.Pi / 4,
  26. ExpB2A: math.Pi / 4,
  27. },
  28. {
  29. A: position.Position{
  30. Vector: vector.Vector{X: 0, Y: 1},
  31. Rotation: math.Pi,
  32. },
  33. B: position.Position{
  34. Vector: vector.Vector{X: 1, Y: 0},
  35. Rotation: -math.Pi / 2,
  36. },
  37. ExpA2B: 3 * math.Pi / 4,
  38. ExpB2A: -3 * math.Pi / 4,
  39. },
  40. }
  41. for _, test := range testCases {
  42. actA2B := test.A.Direction(test.B)
  43. require.InDelta(t, test.ExpA2B, actA2B, 1e-12)
  44. actB2A := test.B.Direction(test.A)
  45. require.InDelta(t, test.ExpB2A, actB2A, 1e-12)
  46. }
  47. }
  48. func TestPosition_Add(t *testing.T) {
  49. t.Parallel()
  50. tests := map[string]struct {
  51. p, q position.Position
  52. want position.Position
  53. }{
  54. "straight angle": {
  55. p: position.Position{
  56. Vector: vector.Vector{X: 1, Y: 0},
  57. Rotation: math.Pi / 2,
  58. },
  59. q: position.Position{
  60. Vector: vector.Vector{X: 1, Y: 0},
  61. Rotation: 0,
  62. },
  63. want: position.Position{
  64. Vector: vector.Vector{X: 1, Y: 1},
  65. Rotation: math.Pi / 2,
  66. },
  67. },
  68. "3-4-5 triangle": {
  69. p: position.Position{
  70. Vector: vector.Vector{X: -2, Y: 0},
  71. Rotation: math.Atan2(3, 4),
  72. },
  73. q: position.Position{
  74. Vector: vector.Vector{X: 5, Y: 0},
  75. Rotation: -math.Atan2(3, 4),
  76. },
  77. want: position.Position{
  78. Vector: vector.Vector{X: 2, Y: 3},
  79. Rotation: 0,
  80. },
  81. },
  82. }
  83. for name, tt := range tests {
  84. t.Run(name, func(t *testing.T) {
  85. t.Parallel()
  86. require.Equal(t, tt.want, tt.p.Add(tt.q))
  87. })
  88. }
  89. }