math.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. -- love.math
  2. -- love.math.colorFromBytes
  3. love.test.math.colorFromBytes = function(test)
  4. -- check random value
  5. local r, g, b, a = love.math.colorFromBytes(51, 51, 51, 51)
  6. test:assertEquals(r, 0.2, 'check r from bytes')
  7. test:assertEquals(g, 0.2, 'check g from bytes')
  8. test:assertEquals(b, 0.2, 'check b from bytes')
  9. test:assertEquals(a, 0.2, 'check a from bytes')
  10. -- check "max" value
  11. r, g, b, a = love.math.colorFromBytes(255, 255, 255, 255)
  12. test:assertEquals(r, 1, 'check r from bytes')
  13. test:assertEquals(g, 1, 'check g from bytes')
  14. test:assertEquals(b, 1, 'check b from bytes')
  15. test:assertEquals(a, 1, 'check a from bytes')
  16. -- check "min" value
  17. r, g, b, a = love.math.colorFromBytes(0, 0, 0, 0)
  18. test:assertEquals(r, 0, 'check r from bytes')
  19. test:assertEquals(g, 0, 'check g from bytes')
  20. test:assertEquals(b, 0, 'check b from bytes')
  21. test:assertEquals(a, 0, 'check a from bytes')
  22. end
  23. -- love.math.colorToBytes
  24. love.test.math.colorToBytes = function(test)
  25. -- check random value
  26. local r, g, b, a = love.math.colorToBytes(0.2, 0.2, 0.2, 0.2)
  27. test:assertEquals(r, 51, 'check bytes from r')
  28. test:assertEquals(g, 51, 'check bytes from g')
  29. test:assertEquals(b, 51, 'check bytes from b')
  30. test:assertEquals(a, 51, 'check bytes from a')
  31. -- check "max" value
  32. r, g, b, a = love.math.colorToBytes(1, 1, 1, 1)
  33. test:assertEquals(r, 255, 'check bytes from r')
  34. test:assertEquals(g, 255, 'check bytes from g')
  35. test:assertEquals(b, 255, 'check bytes from b')
  36. test:assertEquals(a, 255, 'check bytes from a')
  37. -- check "min" value
  38. r, g, b, a = love.math.colorToBytes(0, 0, 0, 0)
  39. test:assertEquals(r, 0, 'check bytes from r')
  40. test:assertEquals(g, 0, 'check bytes from g')
  41. test:assertEquals(b, 0, 'check bytes from b')
  42. test:assertEquals(a, 0, 'check bytes from a')
  43. end
  44. -- love.math.gammaToLinear
  45. -- @NOTE I tried doing the same formula as the source from MathModule.cpp
  46. -- but get test failues due to slight differences
  47. love.test.math.gammaToLinear = function(test)
  48. local lr, lg, lb = love.math.gammaToLinear(1, 0.8, 0.02)
  49. --local eg = ((0.8 + 0.055) / 1.055)^2.4
  50. --local eb = 0.02 / 12.92
  51. test:assertGreaterEqual(0, lr, 'check gamma r to linear')
  52. test:assertGreaterEqual(0, lg, 'check gamma g to linear')
  53. test:assertGreaterEqual(0, lb, 'check gamma b to linear')
  54. end
  55. -- love.math.getRandomSeed
  56. -- @NOTE whenever i run this high is always 0, is that intended?
  57. love.test.math.getRandomSeed = function(test)
  58. local low, high = love.math.getRandomSeed()
  59. test:assertGreaterEqual(0, low, 'check random seed low')
  60. test:assertGreaterEqual(0, high, 'check random seed high')
  61. end
  62. -- love.math.getRandomState
  63. love.test.math.getRandomState = function(test)
  64. test:assertNotNil(love.math.getRandomState())
  65. end
  66. -- love.math.isConvex
  67. love.test.math.isConvex = function(test)
  68. local isconvex = love.math.isConvex({0, 0, 1, 0, 1, 1, 1, 0, 0, 0}) -- square
  69. local notconvex = love.math.isConvex({1, 2, 2, 4, 3, 4, 2, 1, 3, 1}) -- weird shape
  70. test:assertEquals(true, isconvex, 'check polygon convex')
  71. test:assertEquals(false, notconvex, 'check polygon not convex')
  72. end
  73. -- love.math.linearToGammer
  74. -- @NOTE I tried doing the same formula as the source from MathModule.cpp
  75. -- but get test failues due to slight differences
  76. love.test.math.linearToGamma = function(test)
  77. local gr, gg, gb = love.math.linearToGamma(1, 0.8, 0.001)
  78. --local eg = 1.055 * (0.8^1/2.4) - 0.055
  79. --local eb = 0.001 * 12.92
  80. test:assertGreaterEqual(0, gr, 'check linear r to gamme')
  81. test:assertGreaterEqual(0, gg, 'check linear g to gamme')
  82. test:assertGreaterEqual(0, gb, 'check linear b to gamme')
  83. end
  84. -- love.math.newBezierCurve
  85. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  86. love.test.math.newBezierCurve = function(test)
  87. test:assertObject(love.math.newBezierCurve({0, 0, 0, 1, 1, 1, 2, 1}))
  88. end
  89. -- love.math.newRandomGenerator
  90. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  91. love.test.math.newRandomGenerator = function(test)
  92. test:assertObject(love.math.newRandomGenerator())
  93. end
  94. -- love.math.newTransform
  95. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  96. love.test.math.newTransform = function(test)
  97. test:assertObject(love.math.newTransform())
  98. end
  99. -- love.math.perlinNoise
  100. love.test.math.perlinNoise = function(test)
  101. -- check some noise values
  102. -- output should be consistent if given the same input
  103. local noise1 = love.math.perlinNoise(100)
  104. local noise2 = love.math.perlinNoise(1, 10)
  105. local noise3 = love.math.perlinNoise(1043, 31.123, 999)
  106. local noise4 = love.math.perlinNoise(99.222, 10067, 8, 1843)
  107. -- rounded to avoid floating point issues
  108. test:assertEquals(50000, math.floor(noise1*100000), 'check noise 1 dimension')
  109. test:assertEquals(50000, math.floor(noise2*100000), 'check noise 2 dimensions')
  110. test:assertEquals(56297, math.floor(noise3*100000), 'check noise 3 dimensions')
  111. test:assertEquals(52579, math.floor(noise4*100000), 'check noise 4 dimensions')
  112. end
  113. -- love.math.simplexNoise
  114. love.test.math.simplexNoise = function(test)
  115. -- check some noise values
  116. -- output should be consistent if given the same input
  117. local noise1 = love.math.simplexNoise(100)
  118. local noise2 = love.math.simplexNoise(1, 10)
  119. local noise3 = love.math.simplexNoise(1043, 31.123, 999)
  120. local noise4 = love.math.simplexNoise(99.222, 10067, 8, 1843)
  121. -- rounded to avoid floating point issues
  122. test:assertEquals(50000, math.floor(noise1*100000), 'check noise 1 dimension')
  123. test:assertEquals(47863, math.floor(noise2*100000), 'check noise 2 dimensions')
  124. test:assertEquals(26440, math.floor(noise3*100000), 'check noise 3 dimensions')
  125. test:assertEquals(53360, math.floor(noise4*100000), 'check noise 4 dimensions')
  126. end
  127. -- love.math.random
  128. love.test.math.random = function(test)
  129. -- check some random ranges
  130. test:assertRange(love.math.random(10), 1, 10, 'check within 1 - 10')
  131. test:assertRange(love.math.random(10), 1, 10, 'check within 1 - 10')
  132. test:assertRange(love.math.random(10), 1, 10, 'check within 1 - 10')
  133. test:assertRange(love.math.random(10), 1, 10, 'check within 1 - 10')
  134. test:assertRange(love.math.random(10), 1, 10, 'check within 1 - 10')
  135. test:assertRange(love.math.random(5, 100), 5, 100, 'check within 5 - 100')
  136. test:assertRange(love.math.random(5, 100), 5, 100, 'check within 5 - 100')
  137. test:assertRange(love.math.random(5, 100), 5, 100, 'check within 5 - 100')
  138. test:assertRange(love.math.random(5, 100), 5, 100, 'check within 5 - 100')
  139. test:assertRange(love.math.random(5, 100), 5, 100, 'check within 5 - 100')
  140. end
  141. -- love.math.randomNormal
  142. -- @NOTE i dont _really_ get the range expected based on stddev + mean
  143. -- so feel free to change to be more accurate
  144. love.test.math.randomNormal = function(test)
  145. test:assertRange(love.math.randomNormal(1, 0), -3, 3, 'check within -3 - 3')
  146. end
  147. -- love.math.setRandomSeed
  148. -- @NOTE same with getRandomSeed, high is always 0 when I tested it?
  149. love.test.math.setRandomSeed = function(test)
  150. love.math.setRandomSeed(9001)
  151. local low, high = love.math.getRandomSeed()
  152. test:assertEquals(9001, low, 'check seed low set')
  153. test:assertEquals(0, high, 'check seed high set')
  154. end
  155. -- love.math.setRandomState
  156. love.test.math.setRandomState = function(test)
  157. -- check setting state matches value returned
  158. local rs1 = love.math.getRandomState()
  159. love.math.setRandomState(rs1)
  160. local rs2 = love.math.getRandomState()
  161. test:assertEquals(rs1, rs2, 'check random state set')
  162. end
  163. -- love.math.triangulate
  164. love.test.math.triangulate = function(test)
  165. local triangles1 = love.math.triangulate({0, 0, 1, 0, 1, 1, 1, 0, 0, 0}) -- square
  166. local triangles2 = love.math.triangulate({1, 2, 2, 4, 3, 4, 2, 1, 3, 1}) -- weird shape
  167. test:assertEquals(3, #triangles1, 'check polygon triangles')
  168. test:assertEquals(3, #triangles2, 'check polygon triangles')
  169. end