mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
review changes
- spaced out object class tests and added more defined comment groups - changed reusing vars in case it causes unexpected asserts in future - fixed some shape:point test having wrong params - added assertTrue + assertFalse for basic checks - used assertRange more for some of the physics + audio tests
This commit is contained in:
+71
-49
@@ -10,30 +10,36 @@
|
||||
|
||||
-- BezierCurve (love.math.newBezierCurve)
|
||||
love.test.math.BezierCurve = function(test)
|
||||
|
||||
-- create obj
|
||||
local curve = love.math.newBezierCurve(1, 1, 2, 2, 3, 1)
|
||||
local px, py = curve:getControlPoint(2)
|
||||
test:assertObject(curve)
|
||||
|
||||
-- check initial properties
|
||||
test:assertCoords({2, 2}, {px, py}, 'check point x/y')
|
||||
test:assertEquals(3, curve:getControlPointCount(), 'check 3 points')
|
||||
test:assertEquals(2, curve:getDegree(), 'check degree is points-1')
|
||||
|
||||
-- check some values on the curve
|
||||
test:assertEquals(1, curve:evaluate(0), 'check curve evaluation 0')
|
||||
test:assertEquals(120, math.floor(curve:evaluate(0.1)*100), 'check curve evaluation 0.1')
|
||||
test:assertEquals(140, math.floor(curve:evaluate(0.2)*100), 'check curve evaluation 0.2')
|
||||
test:assertEquals(200, math.floor(curve:evaluate(0.5)*100), 'check curve evaluation 0.5')
|
||||
test:assertRange(curve:evaluate(0.1), 1.2, 1.3, 'check curve evaluation 0.1')
|
||||
test:assertRange(curve:evaluate(0.2), 1.4, 1.5, 'check curve evaluation 0.2')
|
||||
test:assertRange(curve:evaluate(0.5), 2, 2.1, 'check curve evaluation 0.5')
|
||||
test:assertEquals(3, curve:evaluate(1), 'check curve evaluation 1')
|
||||
|
||||
-- check derivative
|
||||
local deriv = curve:getDerivative()
|
||||
test:assertObject(deriv)
|
||||
test:assertEquals(2, deriv:getControlPointCount(), 'check deriv points')
|
||||
test:assertEquals(200, math.floor(deriv:evaluate(0.1)*100), 'check deriv evaluation 0.1')
|
||||
test:assertRange(deriv:evaluate(0.1), 2, 2.1, 'check deriv evaluation 0.1')
|
||||
|
||||
-- check segment
|
||||
local segment = curve:getSegment(0, 0.5)
|
||||
test:assertObject(segment)
|
||||
test:assertEquals(3, segment:getControlPointCount(), 'check segment points')
|
||||
test:assertEquals(109, math.floor(segment:evaluate(0.1)*100), 'check segment evaluation 0.1')
|
||||
test:assertRange(segment:evaluate(0.1), 1, 1.1, 'check segment evaluation 0.1')
|
||||
|
||||
-- mess with control points
|
||||
curve:removeControlPoint(2)
|
||||
curve:insertControlPoint(4, 1, -1)
|
||||
@@ -47,11 +53,13 @@ love.test.math.BezierCurve = function(test)
|
||||
test:assertCoords({1, 1}, {px1, py1}, 'check modified point 1')
|
||||
test:assertCoords({5, 3}, {px2, py2}, 'check modified point 1')
|
||||
test:assertCoords({3, 1}, {px3, py3}, 'check modified point 1')
|
||||
|
||||
-- check render lists
|
||||
local coords1 = curve:render(5)
|
||||
local coords2 = curve:renderSegment(0, 0.1, 5)
|
||||
test:assertEquals(196, #coords1, 'check coords')
|
||||
test:assertEquals(20, #coords2, 'check segment coords')
|
||||
|
||||
-- check translation values
|
||||
px, py = curve:getControlPoint(2)
|
||||
test:assertCoords({3, 2}, {px, py}, 'check pretransform x/y')
|
||||
@@ -64,6 +72,7 @@ love.test.math.BezierCurve = function(test)
|
||||
curve:translate(5, -5)
|
||||
px, py = curve:getControlPoint(2)
|
||||
test:assertCoords({1, 1}, {px, py}, 'check translated x/y')
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -71,32 +80,40 @@ end
|
||||
-- @NOTE as this checks random numbers the chances this fails is very unlikely, but not 0...
|
||||
-- if you've managed to proc it congrats! your prize is to rerun the testsuite again
|
||||
love.test.math.RandomGenerator = function(test)
|
||||
|
||||
-- create object
|
||||
local rng1 = love.math.newRandomGenerator(3418323524, 20529293)
|
||||
test:assertObject(rng1)
|
||||
|
||||
-- check set properties
|
||||
local low, high = rng1:getSeed()
|
||||
test:assertEquals(3418323524, low, 'check seed low')
|
||||
test:assertEquals(20529293, high, 'check seed high')
|
||||
|
||||
-- check states
|
||||
local rng2 = love.math.newRandomGenerator(1448323524, 10329293)
|
||||
test:assertNotEquals(rng1:random(), rng2:random(), 'check not matching states')
|
||||
test:assertNotEquals(rng1:randomNormal(), rng2:randomNormal(), 'check not matching states')
|
||||
|
||||
-- check setting state works
|
||||
rng2:setState(rng1:getState())
|
||||
test:assertEquals(rng1:random(), rng2:random(), 'check now matching')
|
||||
|
||||
-- check overwriting seed works, should change output
|
||||
rng1:setSeed(os.time())
|
||||
test:assertNotEquals(rng1:random(), rng2:random(), 'check not matching states')
|
||||
test:assertNotEquals(rng1:randomNormal(), rng2:randomNormal(), 'check not matching states')
|
||||
|
||||
end
|
||||
|
||||
|
||||
-- Transform (love.math.Transform)
|
||||
love.test.math.Transform = function(test)
|
||||
|
||||
-- create obj
|
||||
local transform = love.math.newTransform(0, 0, 0, 1, 1, 0, 0, 0, 0)
|
||||
test:assertObject(transform)
|
||||
|
||||
-- set some values and check the matrix and transformPoint values
|
||||
transform:translate(10, 8)
|
||||
transform:scale(2, 3)
|
||||
@@ -107,16 +124,19 @@ love.test.math.Transform = function(test)
|
||||
transform:reset()
|
||||
px, py = transform:transformPoint(1, 1)
|
||||
test:assertCoords({1, 1}, {px, py}, 'check reset')
|
||||
|
||||
-- apply a transform to another transform
|
||||
local transform2 = love.math.newTransform()
|
||||
transform2:translate(5, 3)
|
||||
transform:apply(transform2)
|
||||
px, py = transform:transformPoint(1, 1)
|
||||
test:assertCoords({6, 4}, {px, py}, 'check apply other transform')
|
||||
|
||||
-- check cloning a transform
|
||||
local transform3 = transform:clone()
|
||||
px, py = transform3:transformPoint(1, 1)
|
||||
test:assertCoords({6, 4}, {px, py}, 'check clone transform')
|
||||
|
||||
-- check inverse and inverseTransform
|
||||
transform:reset()
|
||||
transform:translate(-14, 6)
|
||||
@@ -124,6 +144,7 @@ love.test.math.Transform = function(test)
|
||||
transform:inverse()
|
||||
px, py = transform:transformPoint(0, 0)
|
||||
test:assertCoords({-px, -py}, {ipx, ipy}, 'check inverse points transform')
|
||||
|
||||
-- check matrix manipulation
|
||||
transform:setTransformation(0, 0, 0, 1, 1, 0, 0, 0, 0)
|
||||
transform:translate(4, 4)
|
||||
@@ -134,13 +155,15 @@ love.test.math.Transform = function(test)
|
||||
transform:setMatrix(m1, m2, m3, 3, m5, m6, m7, 1, m9, m10, m11, m12, m13, m14, m15, m16)
|
||||
px, py = transform:transformPoint(1, 1)
|
||||
test:assertCoords({4, 2}, {px, py}, 'check set matrix')
|
||||
|
||||
-- check affine vs non affine
|
||||
transform:reset()
|
||||
test:assertEquals(true, transform:isAffine2DTransform(), 'check affine 1')
|
||||
test:assertTrue(transform:isAffine2DTransform(), 'check affine 1')
|
||||
transform:translate(4, 3)
|
||||
test:assertEquals(true, transform:isAffine2DTransform(), 'check affine 2')
|
||||
test:assertTrue(transform:isAffine2DTransform(), 'check affine 2')
|
||||
transform:setMatrix(1, 3, 4, 5.5, 1, 4.5, 2, 1, 3.4, 5.1, 4.1, 13, 1, 1, 2, 3)
|
||||
test:assertEquals(false, transform:isAffine2DTransform(), 'check not affine')
|
||||
test:assertFalse(transform:isAffine2DTransform(), 'check not affine')
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -154,46 +177,46 @@ end
|
||||
-- love.math.colorFromBytes
|
||||
love.test.math.colorFromBytes = function(test)
|
||||
-- check random value
|
||||
local r, g, b, a = love.math.colorFromBytes(51, 51, 51, 51)
|
||||
test:assertEquals(r, 0.2, 'check r from bytes')
|
||||
test:assertEquals(g, 0.2, 'check g from bytes')
|
||||
test:assertEquals(b, 0.2, 'check b from bytes')
|
||||
test:assertEquals(a, 0.2, 'check a from bytes')
|
||||
local r1, g1, b1, a1 = love.math.colorFromBytes(51, 51, 51, 51)
|
||||
test:assertEquals(r1, 0.2, 'check r from bytes')
|
||||
test:assertEquals(g1, 0.2, 'check g from bytes')
|
||||
test:assertEquals(b1, 0.2, 'check b from bytes')
|
||||
test:assertEquals(a1, 0.2, 'check a from bytes')
|
||||
-- check "max" value
|
||||
r, g, b, a = love.math.colorFromBytes(255, 255, 255, 255)
|
||||
test:assertEquals(r, 1, 'check r from bytes')
|
||||
test:assertEquals(g, 1, 'check g from bytes')
|
||||
test:assertEquals(b, 1, 'check b from bytes')
|
||||
test:assertEquals(a, 1, 'check a from bytes')
|
||||
local r2, g2, b2, a2 = love.math.colorFromBytes(255, 255, 255, 255)
|
||||
test:assertEquals(r2, 1, 'check r from bytes')
|
||||
test:assertEquals(g2, 1, 'check g from bytes')
|
||||
test:assertEquals(b2, 1, 'check b from bytes')
|
||||
test:assertEquals(a2, 1, 'check a from bytes')
|
||||
-- check "min" value
|
||||
r, g, b, a = love.math.colorFromBytes(0, 0, 0, 0)
|
||||
test:assertEquals(r, 0, 'check r from bytes')
|
||||
test:assertEquals(g, 0, 'check g from bytes')
|
||||
test:assertEquals(b, 0, 'check b from bytes')
|
||||
test:assertEquals(a, 0, 'check a from bytes')
|
||||
local r3, g3, b3, a3 = love.math.colorFromBytes(0, 0, 0, 0)
|
||||
test:assertEquals(r3, 0, 'check r from bytes')
|
||||
test:assertEquals(g3, 0, 'check g from bytes')
|
||||
test:assertEquals(b3, 0, 'check b from bytes')
|
||||
test:assertEquals(a3, 0, 'check a from bytes')
|
||||
end
|
||||
|
||||
|
||||
-- love.math.colorToBytes
|
||||
love.test.math.colorToBytes = function(test)
|
||||
-- check random value
|
||||
local r, g, b, a = love.math.colorToBytes(0.2, 0.2, 0.2, 0.2)
|
||||
test:assertEquals(r, 51, 'check bytes from r')
|
||||
test:assertEquals(g, 51, 'check bytes from g')
|
||||
test:assertEquals(b, 51, 'check bytes from b')
|
||||
test:assertEquals(a, 51, 'check bytes from a')
|
||||
local r1, g1, b1, a1 = love.math.colorToBytes(0.2, 0.2, 0.2, 0.2)
|
||||
test:assertEquals(r1, 51, 'check bytes from r')
|
||||
test:assertEquals(g1, 51, 'check bytes from g')
|
||||
test:assertEquals(b1, 51, 'check bytes from b')
|
||||
test:assertEquals(a1, 51, 'check bytes from a')
|
||||
-- check "max" value
|
||||
r, g, b, a = love.math.colorToBytes(1, 1, 1, 1)
|
||||
test:assertEquals(r, 255, 'check bytes from r')
|
||||
test:assertEquals(g, 255, 'check bytes from g')
|
||||
test:assertEquals(b, 255, 'check bytes from b')
|
||||
test:assertEquals(a, 255, 'check bytes from a')
|
||||
local r2, g2, b2, a2 = love.math.colorToBytes(1, 1, 1, 1)
|
||||
test:assertEquals(r2, 255, 'check bytes from r')
|
||||
test:assertEquals(g2, 255, 'check bytes from g')
|
||||
test:assertEquals(b2, 255, 'check bytes from b')
|
||||
test:assertEquals(a2, 255, 'check bytes from a')
|
||||
-- check "min" value
|
||||
r, g, b, a = love.math.colorToBytes(0, 0, 0, 0)
|
||||
test:assertEquals(r, 0, 'check bytes from r')
|
||||
test:assertEquals(g, 0, 'check bytes from g')
|
||||
test:assertEquals(b, 0, 'check bytes from b')
|
||||
test:assertEquals(a, 0, 'check bytes from a')
|
||||
local r3, g3, b3, a3 = love.math.colorToBytes(0, 0, 0, 0)
|
||||
test:assertEquals(r3, 0, 'check bytes from r')
|
||||
test:assertEquals(g3, 0, 'check bytes from g')
|
||||
test:assertEquals(b3, 0, 'check bytes from b')
|
||||
test:assertEquals(a3, 0, 'check bytes from a')
|
||||
end
|
||||
|
||||
|
||||
@@ -229,8 +252,8 @@ end
|
||||
love.test.math.isConvex = function(test)
|
||||
local isconvex = love.math.isConvex({0, 0, 1, 0, 1, 1, 1, 0, 0, 0}) -- square
|
||||
local notconvex = love.math.isConvex({1, 2, 2, 4, 3, 4, 2, 1, 3, 1}) -- weird shape
|
||||
test:assertEquals(true, isconvex, 'check polygon convex')
|
||||
test:assertEquals(false, notconvex, 'check polygon not convex')
|
||||
test:assertTrue(isconvex, 'check polygon convex')
|
||||
test:assertFalse(notconvex, 'check polygon not convex')
|
||||
end
|
||||
|
||||
|
||||
@@ -276,11 +299,10 @@ love.test.math.perlinNoise = function(test)
|
||||
local noise2 = love.math.perlinNoise(1, 10)
|
||||
local noise3 = love.math.perlinNoise(1043, 31.123, 999)
|
||||
local noise4 = love.math.perlinNoise(99.222, 10067, 8, 1843)
|
||||
-- rounded to avoid floating point issues
|
||||
test:assertEquals(50000, math.floor(noise1*100000), 'check noise 1 dimension')
|
||||
test:assertEquals(50000, math.floor(noise2*100000), 'check noise 2 dimensions')
|
||||
test:assertEquals(56297, math.floor(noise3*100000), 'check noise 3 dimensions')
|
||||
test:assertEquals(52579, math.floor(noise4*100000), 'check noise 4 dimensions')
|
||||
test:assertRange(noise1, 0.5, 0.51, 'check noise 1 dimension')
|
||||
test:assertRange(noise2, 0.5, 0.51, 'check noise 2 dimensions')
|
||||
test:assertRange(noise3, 0.56, 0.57, 'check noise 3 dimensions')
|
||||
test:assertRange(noise4, 0.52, 0.53, 'check noise 4 dimensions')
|
||||
end
|
||||
|
||||
|
||||
@@ -293,10 +315,10 @@ love.test.math.simplexNoise = function(test)
|
||||
local noise3 = love.math.simplexNoise(1043, 31.123, 999)
|
||||
local noise4 = love.math.simplexNoise(99.222, 10067, 8, 1843)
|
||||
-- rounded to avoid floating point issues
|
||||
test:assertEquals(50000, math.floor(noise1*100000), 'check noise 1 dimension')
|
||||
test:assertEquals(47863, math.floor(noise2*100000), 'check noise 2 dimensions')
|
||||
test:assertEquals(26440, math.floor(noise3*100000), 'check noise 3 dimensions')
|
||||
test:assertEquals(53360, math.floor(noise4*100000), 'check noise 4 dimensions')
|
||||
test:assertRange(noise1, 0.5, 0.51, 'check noise 1 dimension')
|
||||
test:assertRange(noise2, 0.47, 0.48, 'check noise 2 dimensions')
|
||||
test:assertRange(noise3, 0.26, 0.27, 'check noise 3 dimensions')
|
||||
test:assertRange(noise4, 0.53, 0.54, 'check noise 4 dimensions')
|
||||
end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user