Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

142 řádky
2.7KB

  1. package position_test
  2. import (
  3. "math"
  4. "testing"
  5. "git.wtrh.nl/patterns/gopatterns/pkg/position"
  6. "git.wtrh.nl/patterns/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. }
  90. func TestPosition_Translate(t *testing.T) {
  91. tests := map[string]struct {
  92. p, q position.Position
  93. result float64
  94. }{
  95. "origin to 1,1": {
  96. p: position.Position{
  97. Vector: vector.Vector{
  98. X: 1, Y: 1,
  99. },
  100. },
  101. q: position.Position{
  102. Vector: vector.Vector{
  103. X: 0, Y: 0,
  104. },
  105. },
  106. result: math.Sqrt(2),
  107. },
  108. "0,2 to 4,5 -> 5": {
  109. p: position.Position{
  110. Vector: vector.Vector{
  111. X: 0, Y: 2,
  112. },
  113. },
  114. q: position.Position{
  115. Vector: vector.Vector{
  116. X: 4, Y: 5,
  117. },
  118. },
  119. result: 5,
  120. },
  121. }
  122. for name, tt := range tests {
  123. t.Run(name, func(t *testing.T) {
  124. translateResult := tt.p.Distance(tt.q)
  125. require.InDelta(t, tt.result, translateResult, 1e-10)
  126. })
  127. }
  128. }