Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

488 wiersze
6.3KB

  1. package vector_test
  2. import (
  3. "math"
  4. "testing"
  5. "git.wtrh.nl/patterns/gopatterns/pkg/vector"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestVector_Abs(t *testing.T) {
  9. t.Parallel()
  10. act := vector.Vector{
  11. X: -1,
  12. Y: 1,
  13. }.Abs()
  14. exp := vector.Vector{
  15. X: 1,
  16. Y: 1,
  17. }
  18. assert.Equal(t, exp, act)
  19. }
  20. func TestVector_Abs2(t *testing.T) {
  21. t.Parallel()
  22. act := vector.Vector{
  23. X: 0,
  24. Y: -2,
  25. }.Abs()
  26. exp := vector.Vector{
  27. X: 0,
  28. Y: 2,
  29. }
  30. assert.Equal(t, exp, act)
  31. }
  32. func TestVector_Subtract(t *testing.T) {
  33. t.Parallel()
  34. v1 := vector.Vector{
  35. X: 12.5,
  36. Y: 4,
  37. }
  38. v2 := vector.Vector{
  39. X: 7,
  40. Y: -3,
  41. }
  42. act := v1.Subtract(v2)
  43. exp := vector.Vector{
  44. X: 5.5,
  45. Y: 7,
  46. }
  47. assert.Equal(t, exp, act)
  48. }
  49. func TestVector_Add(t *testing.T) {
  50. t.Parallel()
  51. v1 := vector.Vector{
  52. X: 12.5,
  53. Y: 4,
  54. }
  55. v2 := vector.Vector{
  56. X: 7,
  57. Y: -3,
  58. }
  59. act := v1.Add(v2)
  60. exp := vector.Vector{
  61. X: 19.5,
  62. Y: 1,
  63. }
  64. assert.Equal(t, exp, act)
  65. }
  66. func TestVector_Multiply(t *testing.T) {
  67. t.Parallel()
  68. v1 := vector.Vector{
  69. X: 12.5,
  70. Y: 4,
  71. }
  72. act := v1.Multiply(7)
  73. exp := vector.Vector{
  74. X: 87.5,
  75. Y: 28,
  76. }
  77. assert.Equal(t, exp, act)
  78. }
  79. func TestVector_Divide(t *testing.T) {
  80. t.Parallel()
  81. v1 := vector.Vector{
  82. X: 15,
  83. Y: 3,
  84. }
  85. act := v1.Divide(3)
  86. exp := vector.Vector{
  87. X: 5,
  88. Y: 1,
  89. }
  90. assert.Equal(t, exp, act)
  91. }
  92. func TestVector_Divide2(t *testing.T) {
  93. t.Parallel()
  94. v1 := vector.Vector{
  95. X: -15,
  96. Y: 3,
  97. }
  98. act := v1.Divide(0)
  99. exp := vector.Vector{
  100. X: math.Inf(-1),
  101. Y: math.Inf(1),
  102. }
  103. assert.Equal(t, exp, act)
  104. }
  105. func TestVector_Round(t *testing.T) {
  106. t.Parallel()
  107. v1 := vector.Vector{
  108. X: 12.5,
  109. Y: 4.3,
  110. }
  111. act := v1.Round()
  112. exp := vector.Vector{
  113. X: 13,
  114. Y: 4,
  115. }
  116. assert.Equal(t, exp, act)
  117. }
  118. func TestVector_Floor(t *testing.T) {
  119. t.Parallel()
  120. v1 := vector.Vector{
  121. X: 12.9,
  122. Y: 4.3,
  123. }
  124. act := v1.Floor()
  125. exp := vector.Vector{
  126. X: 12,
  127. Y: 4,
  128. }
  129. assert.Equal(t, exp, act)
  130. }
  131. func TestVector_Ciel(t *testing.T) {
  132. t.Parallel()
  133. v1 := vector.Vector{
  134. X: 12.9,
  135. Y: 4.3,
  136. }
  137. act := v1.Ceil()
  138. exp := vector.Vector{
  139. X: 13,
  140. Y: 5,
  141. }
  142. assert.Equal(t, exp, act)
  143. }
  144. func TestVector_Scale(t *testing.T) {
  145. t.Parallel()
  146. v1 := vector.Vector{
  147. X: 12.5,
  148. Y: 4,
  149. }
  150. v2 := vector.Vector{
  151. X: 7,
  152. Y: -3,
  153. }
  154. act := v1.Scale(v2)
  155. exp := vector.Vector{
  156. X: 87.5,
  157. Y: -12,
  158. }
  159. assert.Equal(t, exp, act)
  160. }
  161. func TestVector_ScaleDown(t *testing.T) {
  162. t.Parallel()
  163. v1 := vector.Vector{
  164. X: 12.5,
  165. Y: 4,
  166. }
  167. v2 := vector.Vector{
  168. X: 5,
  169. Y: -2,
  170. }
  171. act := v1.ScaleDown(v2)
  172. exp := vector.Vector{
  173. X: 2.5,
  174. Y: -2,
  175. }
  176. assert.Equal(t, exp, act)
  177. }
  178. func TestVector_Max(t *testing.T) {
  179. t.Parallel()
  180. v1 := vector.Vector{
  181. X: 12.5,
  182. Y: -4,
  183. }
  184. v2 := vector.Vector{
  185. X: 5,
  186. Y: 2,
  187. }
  188. act := v1.Max(v2)
  189. exp := vector.Vector{
  190. X: 12.5,
  191. Y: 2,
  192. }
  193. assert.Equal(t, exp, act)
  194. }
  195. func TestVector_Min(t *testing.T) {
  196. t.Parallel()
  197. v1 := vector.Vector{
  198. X: 12.5,
  199. Y: -4,
  200. }
  201. v2 := vector.Vector{
  202. X: 5,
  203. Y: 2,
  204. }
  205. act := v1.Min(v2)
  206. exp := vector.Vector{
  207. X: 5,
  208. Y: -4,
  209. }
  210. assert.Equal(t, exp, act)
  211. }
  212. func TestVector_String(t *testing.T) {
  213. t.Parallel()
  214. act := vector.Vector{
  215. X: 12.5,
  216. Y: 4,
  217. }.String()
  218. exp := "12.5,4"
  219. assert.Equal(t, exp, act)
  220. }
  221. func TestVector_Magnitude(t *testing.T) {
  222. t.Parallel()
  223. act := vector.Vector{
  224. X: 3,
  225. Y: 4,
  226. }.Magnitude()
  227. exp := 5.0
  228. assert.InDelta(t, exp, act, 1e-15)
  229. }
  230. func TestVector_Unit_Panic(t *testing.T) {
  231. t.Parallel()
  232. emptyVector := vector.Vector{}
  233. assert.Panics(t, func() { emptyVector.Unit() })
  234. }
  235. func TestVector_Unit(t *testing.T) {
  236. t.Parallel()
  237. act := vector.Vector{
  238. X: 3,
  239. Y: 4,
  240. }.Unit()
  241. exp := vector.Vector{
  242. X: 0.6,
  243. Y: 0.8,
  244. }
  245. assert.Equal(t, exp, act)
  246. }
  247. func TestVector_Rotate(t *testing.T) {
  248. t.Parallel()
  249. act := vector.Vector{
  250. X: 2,
  251. Y: 0,
  252. }.Rotate(math.Pi / 2)
  253. exp := vector.Vector{
  254. X: 0,
  255. Y: 2,
  256. }
  257. assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
  258. assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
  259. }
  260. func TestVector_Rotate2(t *testing.T) {
  261. t.Parallel()
  262. act := vector.Vector{
  263. X: 2,
  264. Y: 0,
  265. }.Rotate(math.Pi)
  266. exp := vector.Vector{
  267. X: -2,
  268. Y: 0,
  269. }
  270. assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
  271. assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
  272. }
  273. func TestVector_Rotate3(t *testing.T) {
  274. t.Parallel()
  275. act := vector.Vector{
  276. X: 0,
  277. Y: 2,
  278. }.Rotate(math.Pi / 2)
  279. exp := vector.Vector{
  280. X: -2,
  281. Y: 0,
  282. }
  283. assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
  284. assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
  285. }
  286. func TestVector_Rotate4(t *testing.T) {
  287. t.Parallel()
  288. act := vector.Vector{
  289. X: 0,
  290. Y: 2,
  291. }.Rotate(math.Pi)
  292. exp := vector.Vector{
  293. X: 0,
  294. Y: -2,
  295. }
  296. assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
  297. assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
  298. }
  299. func TestVector_Chord(t *testing.T) {
  300. t.Parallel()
  301. act := vector.Vector{
  302. X: 2,
  303. Y: 0,
  304. }.Span(math.Pi / 2)
  305. exp := vector.Vector{
  306. X: 2,
  307. Y: -2,
  308. }
  309. assert.InDelta(t, exp.X, act.X, math.Pow10(-15))
  310. assert.InDelta(t, exp.Y, act.Y, math.Pow10(-15))
  311. }
  312. func TestVector_Distance(t *testing.T) {
  313. t.Parallel()
  314. v1 := vector.Vector{
  315. X: 3,
  316. Y: 0,
  317. }
  318. v2 := vector.Vector{
  319. X: 0,
  320. Y: 4,
  321. }
  322. act := v1.Distance(v2)
  323. exp := 5.0
  324. assert.InDelta(t, exp, act, 1e-15)
  325. }
  326. func TestVector_Angle(t *testing.T) {
  327. t.Parallel()
  328. act := vector.Vector{
  329. X: 3,
  330. Y: 3,
  331. }.Angle()
  332. exp := math.Pi / 4
  333. assert.InDelta(t, exp, act, 1e-15)
  334. }
  335. func TestVector_Angle2(t *testing.T) {
  336. t.Parallel()
  337. act := vector.Vector{
  338. X: -3,
  339. Y: 3,
  340. }.Angle()
  341. exp := 3 * math.Pi / 4
  342. assert.InDelta(t, exp, act, 1e-15)
  343. }
  344. func TestVector_AngleBetween(t *testing.T) {
  345. t.Parallel()
  346. v1 := vector.Vector{
  347. X: -1,
  348. Y: 3,
  349. }
  350. v2 := vector.Vector{
  351. X: 2,
  352. Y: 6,
  353. }
  354. act := v1.AngleBetween(v2)
  355. exp := math.Pi / 4
  356. assert.InDelta(t, exp, act, 1e-15)
  357. }
  358. func TestVector_Above(t *testing.T) {
  359. t.Parallel()
  360. act := vector.Vector{}.Above(1)
  361. exp := vector.Vector{
  362. X: 0,
  363. Y: 1,
  364. }
  365. assert.Equal(t, exp, act)
  366. }
  367. func TestVector_Below(t *testing.T) {
  368. t.Parallel()
  369. act := vector.Vector{}.Below(1)
  370. exp := vector.Vector{
  371. X: 0,
  372. Y: -1,
  373. }
  374. assert.Equal(t, exp, act)
  375. }
  376. func TestVector_Right(t *testing.T) {
  377. t.Parallel()
  378. act := vector.Vector{}.Right(1)
  379. exp := vector.Vector{
  380. X: 1,
  381. Y: 0,
  382. }
  383. assert.Equal(t, exp, act)
  384. }
  385. func TestVector_Left(t *testing.T) {
  386. t.Parallel()
  387. act := vector.Vector{}.Left(1)
  388. exp := vector.Vector{
  389. X: -1,
  390. Y: 0,
  391. }
  392. assert.Equal(t, exp, act)
  393. }