vec2_spec.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. local vec2 = require "modules.vec2"
  2. local DBL_EPSILON = require("modules.constants").DBL_EPSILON
  3. local abs, sqrt = math.abs, math.sqrt
  4. describe("vec2:", function()
  5. it("creates an empty vector", function()
  6. local a = vec2()
  7. assert.is.equal(0, a.x)
  8. assert.is.equal(0, a.y)
  9. assert.is_true(a:is_vec2())
  10. assert.is_true(a:is_zero())
  11. end)
  12. it("creates a vector from a number", function()
  13. local a = vec2(3)
  14. assert.is.equal(3, a.x)
  15. assert.is.equal(3, a.y)
  16. end)
  17. it("creates a vector from numbers", function()
  18. local a = vec2(3, 5)
  19. assert.is.equal(3, a.x)
  20. assert.is.equal(5, a.y)
  21. end)
  22. it("creates a vector from a list", function()
  23. local a = vec2 { 3, 5 }
  24. assert.is.equal(3, a.x)
  25. assert.is.equal(5, a.y)
  26. end)
  27. it("creates a vector from a record", function()
  28. local a = vec2 { x=3, y=5 }
  29. assert.is.equal(3, a.x)
  30. assert.is.equal(5, a.y)
  31. end)
  32. it("clones a vector", function()
  33. local a = vec2(3, 5)
  34. local b = a:clone()
  35. assert.is.equal(a, b)
  36. end)
  37. it("adds a vector to another", function()
  38. local a = vec2(3, 5)
  39. local b = vec2(7, 4)
  40. local c = a:add(b)
  41. local d = a + b
  42. assert.is.equal(10, c.x)
  43. assert.is.equal(9, c.y)
  44. assert.is.equal(c, d)
  45. end)
  46. it("subracts a vector from another", function()
  47. local a = vec2(3, 5)
  48. local b = vec2(7, 4)
  49. local c = a:sub(b)
  50. local d = a - b
  51. assert.is.equal(-4, c.x)
  52. assert.is.equal( 1, c.y)
  53. assert.is.equal( c, d)
  54. end)
  55. it("multiplies a vector by a scale factor", function()
  56. local a = vec2(3, 5)
  57. local s = 2
  58. local c = a:scale(s)
  59. local d = a * s
  60. assert.is.equal(6, c.x)
  61. assert.is.equal(10, c.y)
  62. assert.is.equal(c, d)
  63. end)
  64. it("divides a vector by another vector", function()
  65. local a = vec2(3, 5)
  66. local s = vec2(2, 2)
  67. local c = a:div(s)
  68. local d = a / s
  69. assert.is.equal(1.5, c.x)
  70. assert.is.equal(2.5, c.y)
  71. assert.is.equal(c, d)
  72. end)
  73. it("inverts a vector", function()
  74. local a = vec2(3, -5)
  75. local b = -a
  76. assert.is.equal(-a.x, b.x)
  77. assert.is.equal(-a.y, b.y)
  78. end)
  79. it("gets the length of a vector", function()
  80. local a = vec2(3, 5)
  81. assert.is.equal(sqrt(34), a:len())
  82. end)
  83. it("gets the square length of a vector", function()
  84. local a = vec2(3, 5)
  85. assert.is.equal(34, a:len2())
  86. end)
  87. it("normalizes a vector", function()
  88. local a = vec2(3, 5)
  89. local b = a:normalize()
  90. assert.is_true(abs(b:len()-1) < DBL_EPSILON)
  91. end)
  92. it("trims the length of a vector", function()
  93. local a = vec2(3, 5)
  94. local b = a:trim(0.5)
  95. assert.is_true(abs(b:len()-0.5) < DBL_EPSILON)
  96. end)
  97. it("gets the distance between two vectors", function()
  98. local a = vec2(3, 5)
  99. local b = vec2(7, 4)
  100. local c = a:dist(b)
  101. assert.is.equal(sqrt(17), c)
  102. end)
  103. it("gets the square distance between two vectors", function()
  104. local a = vec2(3, 5)
  105. local b = vec2(7, 4)
  106. local c = a:dist2(b)
  107. assert.is.equal(17, c)
  108. end)
  109. it("crosses two vectors", function()
  110. local a = vec2(3, 5)
  111. local b = vec2(7, 4)
  112. local c = a:cross(b)
  113. assert.is.equal(-23, c)
  114. end)
  115. it("dots two vectors", function()
  116. local a = vec2(3, 5)
  117. local b = vec2(7, 4)
  118. local c = a:dot(b)
  119. assert.is.equal(41, c)
  120. end)
  121. it("interpolates between two vectors", function()
  122. local a = vec2(3, 5)
  123. local b = vec2(7, 4)
  124. local s = 0.1
  125. local c = a:lerp(b, s)
  126. assert.is.equal(3.4, c.x)
  127. assert.is.equal(4.9, c.y)
  128. end)
  129. it("unpacks a vector", function()
  130. local a = vec2(3, 5)
  131. local x, y = a:unpack()
  132. assert.is.equal(3, x)
  133. assert.is.equal(5, y)
  134. end)
  135. it("rotates a vector", function()
  136. local a = vec2(3, 5)
  137. local b = a:rotate( math.pi)
  138. local c = b:rotate(-math.pi)
  139. assert.is_not.equal(a, b)
  140. assert.is.equal(a, c)
  141. end)
  142. it("converts between polar and cartesian coordinates", function()
  143. local a = vec2(3, 5)
  144. local r, t = a:to_polar()
  145. local b = vec2.from_cartesian(r, t)
  146. assert.is.equal(a.x, b.x)
  147. assert.is.equal(a.y, b.y)
  148. end)
  149. it("gets a perpendicular vector", function()
  150. local a = vec2(3, 5)
  151. local b = a:perpendicular()
  152. assert.is.equal(-5, b.x)
  153. assert.is.equal( 3, b.y)
  154. end)
  155. it("gets a string representation of a vector", function()
  156. local a = vec2()
  157. local b = a:to_string()
  158. assert.is.equal("(+0.000,+0.000)", b)
  159. end)
  160. end)