[Feature] Complete unit tests (#3399)

This commit is contained in:
GengineJS
2021-04-14 13:33:08 +08:00
committed by GitHub
parent 77e6f2b0f2
commit f618c5328d
10 changed files with 1105 additions and 2 deletions
@@ -0,0 +1,91 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec2.h"
#include "cocos/math/Math.h"
#include "cocos/math/Geometry.h"
#include "utils.h"
#include <math.h>
TEST(mathGeometryTest, test7) {
// setSize
logLabel = "test the geometry_size setSize function";
cc::Size size(100, 80);
size.setSize(20, 10);
ExpectEq(size.width == 20 && size.height == 10, true);
// equals size
logLabel = "test the geometry_size equals function";
cc::Size target(20, 10);
ExpectEq(size.equals(target), true);
// Rect getMinX
logLabel = "test the geometry_rect getMinX function";
cc::Rect rect;
rect.setRect(0, 0, 100, 70);
ExpectEq(rect.getMinX() == 0, true);
// Rect getMidX
logLabel = "test the geometry_rect getMidX function";
ExpectEq(rect.getMidX() == 50, true);
// Rect getMaxX
logLabel = "test the geometry_rect getMaxX function";
ExpectEq(rect.getMaxX() == 100, true);
// Rect getMinY
logLabel = "test the geometry_rect getMinY function";
ExpectEq(rect.getMinY() == 0, true);
// Rect getMidY
logLabel = "test the geometry_rect getMidY function";
ExpectEq(rect.getMidY() == 35, true);
// Rect getMaxY
logLabel = "test the geometry_rect getMaxY function";
ExpectEq(rect.getMaxY() == 70, true);
// Rect equals
logLabel = "test the geometry_rect equals function";
cc::Rect equalTarget(0, 0, 100, 70);
ExpectEq(rect.equals(equalTarget), true);
// containsPoint
logLabel = "test the geometry_rect containsPoint function";
cc::Vec2 point(10, 10);
ExpectEq(rect.containsPoint(point), true);
// intersectsRect
logLabel = "test the geometry_rect intersectsRect function";
cc::Rect intersectRect(10, 10, 50, 40);
ExpectEq(rect.intersectsRect(intersectRect), true);
// intersectsCircle
logLabel = "test the geometry_rect intersectsCircle function";
cc::Vec2 intersectsCircle(50, 10);
ExpectEq(rect.intersectsCircle(intersectsCircle, 50), true);
// unionWithRect
logLabel = "test the geometry_rect unionWithRect function";
cc::Rect unionWithRect(10, 10, 150, 40);
rect = rect.unionWithRect(unionWithRect);
cc::Vec2 equalTar(0, 0);
size.setSize(160, 70);
ExpectEq(rect.origin.equals(equalTar) && rect.size.equals(size), true);
// merge
logLabel = "test the geometry_rect merge function";
cc::Rect merge(30, 10, 200, 180);
rect.merge(merge);
size.setSize(230, 190);
ExpectEq(rect.size.equals(size), true);
}
+124
View File
@@ -0,0 +1,124 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec2.h"
#include "cocos/math/Mat3.h"
#include "cocos/math/Mat4.h"
#include "cocos/math/Math.h"
#include "cocos/math/Quaternion.h"
#include "utils.h"
#include <math.h>
TEST(mathMat3Test, test4) {
cc::Mat3 mat3(0,2,3, 4,5,6, 7,8,9);
// identity
logLabel = "test the mat3 identity function";
cc::Mat3::identity(mat3);
ExpectEq(mat3.m[0] == 1 && mat3.m[4] == 1 && mat3.m[8] == 1, true);
// transpose
logLabel = "test the mat3 transpose function";
mat3.set(11,21,31, 12,22,32, 13,23,33);
mat3.transpose();
ExpectEq(mat3.m[1] == 21 && mat3.m[4] == 22 && mat3.m[7] == 23, true);
cc::Mat3 matTemp(11,21,31, 12,22,32, 13,23,33);
cc::Mat3::transpose(mat3, matTemp);
ExpectEq(mat3.m[1] == 21 && mat3.m[4] == 22 && mat3.m[7] == 23, true);
// inverse
logLabel = "test the mat3 inverse function";
mat3.set(0, 2, 0, 3, 0, 0, 2, 0, 1);
mat3.inverse();
ExpectEq(mat3.m[1] == 0.5f && mat3.m[8] == 1, true);
// adjoint
logLabel = "test the mat3 adjoint function";
cc::Mat3 matAdj(0, 2, 0, 3, 0, 0, 2, 0, 1);
cc::Mat3::adjoint(mat3, matAdj);
ExpectEq(mat3.m[1] == -3 && mat3.m[3] == -2 && mat3.m[8] == -6, true);
// determinant
logLabel = "test the mat3 determinant function";
mat3.set(1, 0, 3, 2, 1, 0, 4, 0, 2);
float det = mat3.determinant();
ExpectEq(det == -10, true);
// multiply
logLabel = "test the mat3 multiply function";
cc::Mat3 lm(2, 3, 1, 4, 7, 3, 8, 2, 0);
cc::Mat3 rm(5, 2, 6, 8, 1, 0, 6, 5, 4);
cc::Mat3::multiply(mat3, lm, rm);
ExpectEq(mat3.m[0] == 40 && mat3.m[4] == 30 && mat3.m[8] == 48, true);
// translate
logLabel = "test the mat3 translate function";
cc::Mat3 translate;
translate.m[6] = 3;
translate.m[7] = 2;
cc::Vec2 pos(1, 2);
cc::Mat3 transOut;
cc::Mat3::translate(transOut, translate, pos);
ExpectEq(transOut.m[6] == 4 && transOut.m[7] == 4, true);
cc::Mat3::fromTranslation(transOut, pos);
ExpectEq(transOut.m[7] == 2, true);
// rotate
logLabel = "test the mat3 rotate function";
cc::Mat3 rotate;
cc::Mat3 rotateOut;
cc::Mat3::rotate(rotateOut, rotate, cc::math::PI_DIV2);
ExpectEq(rotateOut.m[1] == 1 && rotateOut.m[3] == -1, true);
cc::Mat3::fromRotation(rotateOut, cc::math::PI_DIV2);
ExpectEq(rotateOut.m[1] == 1 && rotateOut.m[3] == -1, true);
// scale
logLabel = "test the mat3 scale function";
cc::Mat3 scale;
cc::Mat3 scaleOut;
cc::Vec2 scaleVec(2, 1);
cc::Mat3::scale(scaleOut, scale, scaleVec);
ExpectEq(scaleOut.m[0] == 2, true);
cc::Mat3::fromScaling(scaleOut, scaleVec);
ExpectEq(scaleOut.m[0] == 2, true);
// fromMat4
logLabel = "test the mat3 fromMat4 function";
cc::Mat4 fromMat4;
fromMat4.m[0] = 3;
fromMat4.m[4] = 6;
fromMat4.m[5] = 7;
cc::Mat3 copyMat;
cc::Mat3::fromMat4(copyMat, fromMat4);
ExpectEq(copyMat.m[0] == 3 && copyMat.m[3] == 6 && copyMat.m[4] == 7, true);
// fromQuat
logLabel = "test the mat3 fromQuat function";
cc::Mat3 fromQuat;
cc::Quaternion quat(0, 0, 3, 1);
cc::Mat3::fromQuat(fromQuat, quat);
ExpectEq(copyMat.m[0] == 3 && copyMat.m[3] == 6 && copyMat.m[4] == 7, true);
// add
logLabel = "test the mat3 add function";
cc::Mat3 lAdd(9,0,0, 3,8,0, 9,0,0);
cc::Mat3 rAdd(1,0,0, 5,2,0, 1,0,0);
cc::Mat3 addOut;
cc::Mat3::add(addOut, lAdd, rAdd);
ExpectEq(addOut.m[0] == 10 && addOut.m[2] == 10 && addOut.m[4] == 10, true);
// subtract
logLabel = "test the mat3 subtract function";
cc::Mat3 subOut;
cc::Mat3::subtract(subOut, lAdd, rAdd);
ExpectEq(subOut.m[0] == 8 && subOut.m[2] == 8 && subOut.m[4] == 6, true);
}
+271
View File
@@ -0,0 +1,271 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec3.h"
#include "cocos/math/Vec4.h"
#include "cocos/math/Mat3.h"
#include "cocos/math/Mat4.h"
#include "cocos/math/Math.h"
#include "cocos/math/Quaternion.h"
#include "utils.h"
#include <math.h>
TEST(mathMat4Test, test5) {
cc::Mat4 mat4;
// createLookAt
logLabel = "test the mat4 createLookAt function";
cc::Vec3 eyePos(0, 0, -10);
cc::Vec3 tarPos(0, 0, 0);
cc::Vec3 up(0, 1, 0);
cc::Mat4 outMat;
cc::Mat4::createLookAt(eyePos, tarPos, up, &outMat);
ExpectEq(outMat.m[0] == -1 && outMat.m[10] == -1 && outMat.m[14] == -10, true);
cc::Mat4::createLookAt(eyePos.x, eyePos.y, eyePos.z, tarPos.x, tarPos.y, tarPos.z, up.x, up.y, up.z, &outMat);
ExpectEq(outMat.m[0] == -1 && outMat.m[10] == -1 && outMat.m[14] == -10, true);
// createPerspective
logLabel = "test the mat4 createPerspective function";
cc::Mat4::createPerspective(60, 1, 0, 100, &outMat);
ExpectEq(outMat.m[10] == -1 && outMat.m[11] == -1, true);
// createOrthographic
logLabel = "test the mat4 createOrthographic function";
cc::Mat4::createOrthographic(20, 40, 100, 200, 10, 100, &outMat);
ExpectEq(outMat.m[12] == -3 && outMat.m[13] == -3, true);
// createOrthographicOffCenter
logLabel = "test the mat4 createOrthographicOffCenter function";
cc::Mat4::createOrthographicOffCenter(20, 40, 100, 200, 10, 100, &outMat);
ExpectEq(outMat.m[12] == -3 && outMat.m[13] == -3, true);
cc::Mat4::createOrthographicOffCenter(20, 40, 100, 200, 10, 100, 0, 1, &outMat);
ExpectEq(outMat.m[12] == -3 && outMat.m[13] == -3, true);
// createBillboard
logLabel = "test the mat4 createBillboard function";
cc::Vec3 objectPosition(0, 0, 6);
cc::Mat4::createBillboard(objectPosition, eyePos, up, &outMat);
ExpectEq(outMat.m[10] == 1 && outMat.m[11] == 6, true);
cc::Mat4::createBillboard(objectPosition, eyePos, up, &outMat);
cc::Vec3 camForward(0, 0, 1);
cc::Mat4::createBillboard(objectPosition, eyePos, up, camForward, &outMat);
ExpectEq(outMat.m[10] == 1 && outMat.m[11] == 6, true);
// createScale
logLabel = "test the mat4 createScale function";
cc::Vec3 scale(2, 3, 1);
cc::Mat4::createScale(scale, &outMat);
ExpectEq(outMat.m[0] == 2 && outMat.m[5] == 3, true);
cc::Mat4::createScale(scale.x, scale.y, scale.z, &outMat);
ExpectEq(outMat.m[0] == 2 && outMat.m[5] == 3, true);
// createRotation
logLabel = "test the mat4 createRotation function";
cc::Quaternion rot(0, 0, 5, 1);
cc::Mat4::createRotation(rot, &outMat);
ExpectEq(outMat.m[4] == -10 && outMat.m[5] == -49, true);
cc::Vec3 axis(0, 1, 0);
cc::Mat4::createRotation(axis, cc::math::PI_DIV2, &outMat);
ExpectEq(outMat.m[2] == -1 && outMat.m[8] == 1, true);
// createRotationX
logLabel = "test the mat4 createRotationX function";
cc::Mat4::createRotationX(cc::math::PI, &outMat);
ExpectEq(outMat.m[5] == -1 && outMat.m[10] == -1, true);
// createRotationY
logLabel = "test the mat4 createRotationY function";
cc::Mat4::createRotationY(cc::math::PI, &outMat);
ExpectEq(outMat.m[0] == -1 && outMat.m[10] == -1, true);
// createRotationZ
logLabel = "test the mat4 createRotationZ function";
cc::Mat4::createRotationZ(cc::math::PI, &outMat);
ExpectEq(outMat.m[0] == -1 && outMat.m[5] == -1, true);
// createTranslation
logLabel = "test the mat4 createTranslation function";
cc::Vec3 trans(3, 2, 4);
cc::Mat4::createTranslation(trans, &outMat);
ExpectEq(outMat.m[12] == 3 && outMat.m[13] == 2 && outMat.m[14] == 4, true);
cc::Mat4::createTranslation(trans.x, trans.y, trans.z, &outMat);
ExpectEq(outMat.m[12] == 3 && outMat.m[13] == 2 && outMat.m[14] == 4, true);
// add
logLabel = "test the mat4 add function";
cc::Mat4 addMat;
addMat.add(5);
ExpectEq(addMat.m[0] == 6 && addMat.m[5] == 6 && addMat.m[10] == 6, true);
addMat.setIdentity();
addMat.add(6, &addMat);
ExpectEq(addMat.m[0] == 7 && addMat.m[5] == 7 && addMat.m[10] == 7, true);
cc::Mat4 m1;
m1.m[0] = 2;
cc::Mat4 m2;
m2.m[0] = 3;
cc::Mat4::add(m1, m2, &outMat);
ExpectEq(outMat.m[0] == 5, true);
// fromRT
logLabel = "test the mat4 fromRT function";
cc::Vec4 rotVec(1, 0, 1, 1);
cc::Vec3 transVec(1, 1, 2);
cc::Mat4::fromRT(rotVec, transVec, &outMat);
ExpectEq(outMat.m[5] == -3 && outMat.m[9] == -2 && outMat.m[14] == 2, true);
// decompose
logLabel = "test the mat4 decompose function";
outMat.decompose(&scale, &rot, &transVec);
ExpectEq(transVec.x == 1 && transVec.y == 1 && transVec.z == 2, true);
// determinant
logLabel = "test the mat4 determinant function";
cc::Mat4 det;
det.m[0] = 5;
det.m[1] = 7;
det.m[6] = 2;
float detVal = det.determinant();
ExpectEq(detVal == 5, true);
// getScale
logLabel = "test the mat4 getScale function";
cc::Mat4 getScaleMat;
getScaleMat.m[0] = 5;
getScaleMat.m[5] = 7;
getScaleMat.m[10] = 2;
cc::Vec3 scaleOut;
getScaleMat.getScale(&scaleOut);
ExpectEq(scaleOut.x == 5 && scaleOut.y == 7 && scaleOut.z == 2, true);
// getRotation
logLabel = "test the mat4 getRotation function";
cc::Quaternion quat;
outMat.getRotation(&quat);
ExpectEq(IsEqualF(quat.x, 0.507208) && IsEqualF(quat.y, -0.06908) && IsEqualF(quat.z, 0.657192), true);
// getTranslation
logLabel = "test the mat4 getTranslation function";
outMat.getTranslation(&transVec);
ExpectEq(transVec.x == 1 && transVec.y == 1 && transVec.z == 2, true);
// getUpVector
logLabel = "test the mat4 getUpVector function";
cc::Vec3 upVec3;
outMat.getUpVector(&upVec3);
ExpectEq(upVec3.x == -2 && upVec3.y == -3 && upVec3.z == 2, true);
// getDownVector
logLabel = "test the mat4 getDownVector function";
cc::Vec3 downVec3;
outMat.getDownVector(&downVec3);
ExpectEq(downVec3.x == 2 && downVec3.y == 3 && downVec3.z == -2, true);
// getLeftVector
logLabel = "test the mat4 getLeftVector function";
cc::Vec3 leftVec3;
outMat.getLeftVector(&leftVec3);
ExpectEq(leftVec3.x == 1 && leftVec3.y == -2 && leftVec3.z == -2, true);
// getRightVector
logLabel = "test the mat4 getRightVector function";
cc::Vec3 rightVec3;
outMat.getRightVector(&rightVec3);
ExpectEq(rightVec3.x == -1 && rightVec3.y == 2 && rightVec3.z == 2, true);
// getForwardVector
logLabel = "test the mat4 getForwardVector function";
cc::Vec3 forwardVec3;
outMat.getForwardVector(&forwardVec3);
ExpectEq(forwardVec3.x == -2 && forwardVec3.y == 2 && forwardVec3.z == 1, true);
// getBackVector
logLabel = "test the mat4 getBackVector function";
cc::Vec3 backVec3;
outMat.getBackVector(&backVec3);
ExpectEq(backVec3.x == 2 && backVec3.y == -2 && backVec3.z == -1, true);
// inverse
logLabel = "test the mat4 inverse function";
bool isInverse = outMat.inverse();
ExpectEq(isInverse == true, true);
// getInversed
logLabel = "test the mat4 getInversed function";
cc::Mat4 inversed;
inversed.m[1] = 10;
inversed.m[2] = 13;
inversed.m[4] = 2;
cc::Mat4 inversedMat = inversed.getInversed();
ExpectEq(IsEqualF(inversedMat.m[6], -1.36842108), true);
// identity
logLabel = "test the mat4 isIdentity function";
ExpectEq(outMat.isIdentity(), false);
outMat.setIdentity();
ExpectEq(outMat.isIdentity(), true);
// multiply
logLabel = "test the mat4 multiply function";
outMat.multiply(7);
ExpectEq(outMat.m[0] == 7, true);
outMat.setIdentity();
outMat.multiply(6, &outMat);
ExpectEq(outMat.m[0] == 6, true);
// negate
logLabel = "test the mat4 negate function";
outMat.negate();
ExpectEq(outMat.m[0] == -6, true);
outMat.multiply(2);
outMat = outMat.getNegated();
ExpectEq(outMat.m[0] == 12, true);
// rotate
logLabel = "test the mat4 rotate function";
outMat.setIdentity();
cc::Quaternion rotateQuat(3, 0, 0, 1);
outMat.rotate(rotateQuat);
ExpectEq(outMat.m[5] == -17 && outMat.m[9] == -6, true);
outMat.setIdentity();
cc::Vec3 rAxis(1, 0, 0);
outMat.rotate(rAxis, cc::math::PI_DIV2);
ExpectEq(IsEqualF(outMat.m[0], 1) && outMat.m[9] == -1, true);
outMat.setIdentity();
outMat.rotateX(cc::math::PI_DIV2);
ExpectEq(IsEqualF(outMat.m[0], 1) && outMat.m[9] == -1, true);
outMat.setIdentity();
outMat.rotateY(cc::math::PI_DIV2);
ExpectEq(IsEqualF(outMat.m[2], -1), true);
outMat.setIdentity();
outMat.rotateZ(cc::math::PI_DIV2);
ExpectEq(outMat.m[4] == -1, true);
// scale
logLabel = "test the mat4 scale function";
outMat.setIdentity();
outMat.scale(6);
ExpectEq(outMat.m[0] == 6 && outMat.m[5] == 6, true);
outMat.setIdentity();
outMat.scale(6, 4, 2);
ExpectEq(outMat.m[0] == 6 && outMat.m[5] == 4, true);
// setZero
logLabel = "test the mat4 setZero function";
outMat.setZero();
ExpectEq(outMat.m[0] == 0 && outMat.m[5] == 0, true);
// subtract
logLabel = "test the mat4 subtract function";
cc::Mat4::subtract(m1, m2, &outMat);
ExpectEq(outMat.m[0] == -1, true);
// transformPoint
logLabel = "test the mat4 transformPoint function";
cc::Vec3 transformPot(0, 3, 0);
outMat.setIdentity();
outMat.m[13] = 2;
outMat.transformPoint(&transformPot);
ExpectEq(transformPot.y == 5, true);
// translate
logLabel = "test the mat4 translate function";
cc::Mat4 translate;
translate.m[13] = 3;
translate.m[14] = 2;
cc::Vec3 pos(1, 2, 0);
cc::Mat4 transOut;
translate.translate(2, 3, 0);
ExpectEq(translate.m[12] = 2 && translate.m[13] == 6, true);
// transpose
logLabel = "test the mat4 transpose function";
cc::Mat4 matTranspose(11,21,31,2, 12,22,32,4, 13,23,33,5, 0,0,0,1);
matTranspose.transpose();
ExpectEq(matTranspose.m[1] == 21 && matTranspose.m[4] == 12 && matTranspose.m[7] == 4, true);
}
@@ -0,0 +1,111 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec3.h"
#include "cocos/math/Vec4.h"
#include "cocos/math/Mat3.h"
#include "cocos/math/Mat4.h"
#include "cocos/math/Math.h"
#include "cocos/math/Quaternion.h"
#include "utils.h"
#include <math.h>
TEST(mathQuaternionTest, test6) {
// identity
logLabel = "test the quaternion identity function";
cc::Quaternion quat(1,2,1,1);
quat = cc::Quaternion::identity();
ExpectEq(quat.x == 0 && quat.y == 0 && quat.z == 0 && quat.w == 1, true);
// isIdentity
logLabel = "test the quaternion isIdentity function";
quat.set(5, 10, 10, 2);
quat.setIdentity();
ExpectEq(quat.isIdentity(), true);
// zero
logLabel = "test the quaternion zero function";
cc::Quaternion zero(1,2,1,1);
zero = cc::Quaternion::zero();
ExpectEq(zero.x == 0 && zero.y == 0 && zero.z == 0 && zero.w == 0, true);
// isZero
logLabel = "test the quaternion isZero function";
ExpectEq(zero.isZero(), true);
// createFromRotationMatrix
logLabel = "test the quaternion createFromRotationMatrix function";
cc::Mat4 rotMat;
rotMat.m[0] = cos(cc::math::PI_DIV2);
rotMat.m[2] = sin(cc::math::PI_DIV2);
rotMat.m[8] = -sin(cc::math::PI_DIV2);
rotMat.m[10] = cos(cc::math::PI_DIV2);
cc::Quaternion::createFromRotationMatrix(rotMat, &quat);
ExpectEq(IsEqualF(-0.707106, quat.y), true);
// createFromAxisAngle
logLabel = "test the quaternion createFromAxisAngle function";
cc::Vec3 axis(0, 1, 0);
cc::Quaternion::createFromAxisAngle(axis, cc::math::PI_DIV2, &quat);
ExpectEq(IsEqualF(0.707106, quat.y), true);
// conjugate
logLabel = "test the quaternion conjugate function";
quat.set(1, 2, 1, 1);
quat.conjugate();
ExpectEq(quat.x == -1 && quat.y == -2 && quat.z == -1, true);
quat = quat.getConjugated();
ExpectEq(quat.x == 1 && quat.y == 2 && quat.z == 1, true);
// inverse
logLabel = "test the quaternion inverse function";
quat.inverse();
ExpectEq(IsEqualF(-0.142857149, quat.x) && IsEqualF(-0.142857149, quat.z), true);
quat = quat.getInversed();
ExpectEq(IsEqualF(1, quat.x) && IsEqualF(1, quat.z), true);
// multiply
logLabel = "test the quaternion multiply function";
cc::Quaternion mul(3, 2, 0, 1);
quat.multiply(mul);
ExpectEq(IsEqualF(2, quat.x) && IsEqualF(-3, quat.z), true);
// normalize
logLabel = "test the quaternion normalize function";
quat.normalize();
ExpectEq(IsEqualF(0.20203051, quat.x) && IsEqualF(-0.30304575, quat.z), true);
quat = quat.getNormalized();
ExpectEq(IsEqualF(0.20203051, quat.x) && IsEqualF(-0.30304575, quat.z), true);
// toAxisAngle
logLabel = "test the quaternion toAxisAngle function";
cc::Vec3 e;
quat.toAxisAngle(&e);
ExpectEq(IsEqualF(0.254000247, e.x) && IsEqualF(-0.38100034, e.z), true);
// lerp
logLabel = "test the quaternion lerp function";
cc::Quaternion q1(5, 2, 1, 1);
cc::Quaternion q2(10, 0, 0, 1);
cc::Quaternion::lerp(q1, q2, 0.5, &quat);
ExpectEq(IsEqualF(7.5, quat.x) && IsEqualF(0.5, quat.z), true);
// slerp
logLabel = "test the quaternion slerp function";
q1.set(1, 2, 10, 1);
q2.set(30, 10, 20, 1);
cc::Quaternion::slerp(q1, q2, 0, &quat);
ExpectEq(IsEqualF(1, quat.x) && IsEqualF(10, quat.z), true);
}
+44
View File
@@ -0,0 +1,44 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec2.h"
#include "cocos/math/Math.h"
#include "cocos/math/MathUtil.h"
#include "utils.h"
#include <math.h>
#include <vector>
TEST(mathUtilsTest, test9) {
// smooth
logLabel = "test the MathUtil smooth function";
float startP = 3;
cc::MathUtil::smooth(&startP, 10, 0.3, 0.7);
ExpectEq(IsEqualF(startP, 5.1), true);
cc::MathUtil::smooth(&startP, 10, 0.6, 0.4, 0.9);
ExpectEq(IsEqualF(startP, 8.039999), true);
// lerp
logLabel = "test the MathUtil lerp function";
float res = cc::MathUtil::lerp(2, 15, 0.8);
ExpectEq(IsEqualF(res, 12.3999996), true);
}
+110 -2
View File
@@ -23,9 +23,117 @@ THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec2.h"
#include "utils.h"
#include <math.h>
TEST(mathVec2Test, test1) {
cc::Vec2 vec2(1, 2);
cc::Vec2 vec2(1, 2);
EXPECT_EQ(vec2.isZero(), false);
EXPECT_EQ(vec2.isZero(), false);
// isZero
logLabel = "test whether vec2 is 0";
ExpectEq(vec2.isZero(), false);
vec2.set(0, 0);
ExpectEq(vec2.isZero(), true);
vec2.set(1, 1);
vec2.setZero();
ExpectEq(vec2.isZero(), true);
// isOne
logLabel = "test whether vec2 is 1";
ExpectEq(vec2.isOne(), false);
vec2.set(1, 1);
ExpectEq(vec2.isOne(), true);
vec2.set(1.1f, 1.0f);
ExpectEq(vec2.isOne(), false);
// angle
logLabel = "test the vec2 angle function";
ExpectEq(IsEqualF(vec2.angle(cc::Vec2(2, 0), cc::Vec2(-2, 0)), M_PI), true);
ExpectEq(IsEqualF(vec2.angle(cc::Vec2(2, 0), cc::Vec2(0, 2)), M_PI * 0.5f), true);
ExpectEq(IsEqualF(vec2.angle(cc::Vec2(2, 0), cc::Vec2(0, -2)), M_PI * 0.5f), true);
vec2.set(2, 0);
ExpectEq(IsEqualF(vec2.getAngle(), 0), true);
ExpectEq(IsEqualF(vec2.getAngle(cc::Vec2(0, 2)), M_PI * 0.5f), true);
cc::Vec2 rtVec = vec2.rotateByAngle(cc::Vec2(0, 0), M_PI * 0.5);
ExpectEq(IsEqualF(rtVec.x, 0) && IsEqualF(rtVec.y, 2), true);
// add
logLabel = "test the vec2 add function";
vec2.set(3.9, 1.3);
vec2.add(cc::Vec2(2.1f, 1.7f));
ExpectEq(vec2 == cc::Vec2(6, 3), true);
cc::Vec2::add(cc::Vec2(6, 3), cc::Vec2(2, 1), &vec2);
ExpectEq(vec2 == cc::Vec2(8, 4), true);
// clamp
logLabel = "test the vec2 clamp function";
vec2.clamp(cc::Vec2(3, 1), cc::Vec2(6, 2));
ExpectEq(vec2 == cc::Vec2(6, 2), true);
cc::Vec2::clamp(cc::Vec2(6, 2), cc::Vec2(7, 1), cc::Vec2(11, 3), &vec2);
ExpectEq(vec2 == cc::Vec2(7, 2), true);
// distance
logLabel = "test the vec2 distance function";
cc::Vec2 distVec(10, 2);
float dist = vec2.distance(distVec);
ExpectEq(IsEqualF(dist, 3), true);
dist = vec2.getDistance(distVec);
ExpectEq(IsEqualF(dist, 3), true);
dist = vec2.distanceSquared(distVec);
ExpectEq(IsEqualF(dist, 9), true);
dist = vec2.getDistanceSq(distVec);
ExpectEq(IsEqualF(dist, 9), true);
// dot
logLabel = "test the vec2 dot function";
cc::Vec2 dotVec2(2, 3);
float dotRes = vec2.dot(dotVec2);
ExpectEq(IsEqualF(dotRes, 20), true);
dotRes = cc::Vec2::dot(vec2, dotVec2);
ExpectEq(IsEqualF(dotRes, 20), true);
// length
logLabel = "test the vec2 length function";
vec2.set(8, 6);
ExpectEq(IsEqualF(vec2.length(), 10), true);
// normalize
logLabel = "test whether vec2 is normalized";
vec2.normalize();
ExpectEq(IsEqualF(vec2.length(), 1.0f), true);
vec2.set(1, 0);
ExpectEq(IsEqualF(vec2.length(), 1.0f), true);
vec2.set(0.345, 123.34);
vec2.normalize();
ExpectEq(IsEqualF(vec2.length(), 1.0f), true);
// rotate
logLabel = "test whether vec2 rotate function";
vec2.set(3, 0);
vec2.rotate(cc::Vec2(0, 0), M_PI * 0.5f);
ExpectEq(IsEqualF(vec2.x, 0) && IsEqualF(vec2.y, 3), true);
// subtract
logLabel = "test whether vec2 subtract function";
vec2.set(4, 5);
vec2.subtract(cc::Vec2(1, 2));
ExpectEq(vec2 == cc::Vec2(3, 3), true);
cc::Vec2::subtract(cc::Vec2(4, 5), cc::Vec2(1, 2), &vec2);
ExpectEq(vec2 == cc::Vec2(3, 3), true);
// equals
logLabel = "test whether vec2 equals function";
vec2.set(4, 5);
ExpectEq(vec2.equals(cc::Vec2(4, 5)), true);
ExpectEq(vec2.fuzzyEquals(cc::Vec2(3.2, 4.3), 1), true);
// isLineIntersect
logLabel = "test whether vec2 isLineIntersect function";
ExpectEq(cc::Vec2::isLineIntersect(cc::Vec2(0, 1), cc::Vec2(3, 1), cc::Vec2(0, 0), cc::Vec2(1, -1)), true);
// isSegmentIntersect
logLabel = "test whether vec2 isSegmentIntersect function";
ExpectEq(cc::Vec2::isSegmentIntersect(cc::Vec2(0, 1), cc::Vec2(3, 1), cc::Vec2(0, 1), cc::Vec2(1, -1)), true);
// isLineParallel
logLabel = "test whether vec2 isLineParallel function";
ExpectEq(cc::Vec2::isLineParallel(cc::Vec2(0, 1), cc::Vec2(3, 1), cc::Vec2(0, -1), cc::Vec2(2, -1)), true);
// isLineOverlap
logLabel = "test whether vec2 isLineOverlap function";
ExpectEq(cc::Vec2::isLineOverlap(cc::Vec2(0, 1), cc::Vec2(3, 1), cc::Vec2(-1, 1), cc::Vec2(0, 1)), true);
// isSegmentOverlap
logLabel = "test whether vec2 isSegmentOverlap function";
cc::Vec2 s(0, 0);
cc::Vec2 t(0, 0);
ExpectEq(cc::Vec2::isSegmentOverlap(cc::Vec2(0, 1), cc::Vec2(3, 1), cc::Vec2(-1, 1), cc::Vec2(0, 1), &s, &t), true);
// getIntersectPoint
logLabel = "test whether vec2 getIntersectPoint function";
ExpectEq(cc::Vec2::getIntersectPoint(cc::Vec2(0, 1), cc::Vec2(3, 1), cc::Vec2(0, 1), cc::Vec2(1, -1)) == cc::Vec2(0, 1), true);
}
+159
View File
@@ -0,0 +1,159 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec3.h"
#include "cocos/math/Mat3.h"
#include "cocos/math/Mat4.h"
#include "cocos/math/Quaternion.h"
#include "utils.h"
#include <math.h>
TEST(mathVec3Test, test2) {
cc::Vec3 vec3(1, 2, 3);
// isZero
logLabel = "test whether vec3 is 0";
ExpectEq(vec3.isZero(), false);
vec3.set(0, 0, 0);
ExpectEq(vec3.isZero(), true);
vec3.set(1, 1, 1);
vec3.setZero();
ExpectEq(vec3.isZero(), true);
// isOne
logLabel = "test whether vec3 is 1";
ExpectEq(vec3.isOne(), false);
vec3.set(1, 1, 1);
ExpectEq(vec3.isOne(), true);
vec3.set(1.1f, 1.0f, 1.0f);
ExpectEq(vec3.isOne(), false);
// angle
logLabel = "test the vec3 angle function";
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(-2, 0, 0)), M_PI), true);
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(0, 2, 0)), M_PI * 0.5f), true);
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(0, -2, 0)), M_PI * 0.5f), true);
// add
logLabel = "test the vec3 add function";
vec3.set(3.9, 1.3, 0);
vec3.add(cc::Vec3(2.1f, 1.7f, 0));
ExpectEq(vec3 == cc::Vec3(6, 3, 0), true);
cc::Vec3::add(cc::Vec3(6, 3, 0), cc::Vec3(2, 1, 0), &vec3);
ExpectEq(vec3 == cc::Vec3(8, 4, 0), true);
// clamp
logLabel = "test the vec3 clamp function";
vec3.clamp(cc::Vec3(3, 1, 0), cc::Vec3(6, 2, 0));
ExpectEq(vec3 == cc::Vec3(6, 2, 0), true);
cc::Vec3::clamp(cc::Vec3(6, 2, 0), cc::Vec3(7, 1, 0), cc::Vec3(11, 3, 0), &vec3);
ExpectEq(vec3 == cc::Vec3(7, 2, 0), true);
// cross
logLabel = "test the vec3 cross function";
vec3.set(1, 2, 3);
vec3.cross(cc::Vec3(3, 2, 1));
ExpectEq(vec3 == cc::Vec3(-4, 8, -4), true);
vec3.set(0, 0, 0);
cc::Vec3::cross(cc::Vec3(1, 2, 3), cc::Vec3(3, 2, 1), &vec3);
ExpectEq(vec3 == cc::Vec3(-4, 8, -4), true);
// multiply
logLabel = "test the vec3 multiply function";
vec3.set(1, 3, 2);
vec3.multiply(cc::Vec3(4, 7, 9));
ExpectEq(vec3 == cc::Vec3(4, 21, 18), true);
cc::Vec3::multiply(cc::Vec3(1, 3, 2), cc::Vec3(4, 7, 9), &vec3);
ExpectEq(vec3 == cc::Vec3(4, 21, 18), true);
// transformMat3
logLabel = "test the vec3 transformMat3 function";
cc::Mat3 matScale;
matScale.m[0] = 2;
matScale.m[4] = 1;
matScale.m[8] = 2;
vec3.transformMat3(cc::Vec3(1, 1, 2), matScale);
ExpectEq(vec3 == cc::Vec3(2, 1, 4), true);
// transformMat4
logLabel = "test the vec3 transformMat4 function";
cc::Mat4 matTranslate;
matTranslate.m[12] = 2;
matTranslate.m[13] = 1;
matTranslate.m[14] = 2;
vec3.transformMat4(cc::Vec3(1, 1, 2), matTranslate);
ExpectEq(vec3 == cc::Vec3(3, 2, 4), true);
// transformQuat
logLabel = "test the vec3 transformQuat function";
cc::Quaternion quaternion(10, 0, 0, 0);
vec3.transformQuat(quaternion);
ExpectEq(vec3 == cc::Vec3(300, -200, -400), true);
// distance
logLabel = "test the vec3 distance function";
vec3.set(7, 2, 0);
cc::Vec3 distVec(10, 2, 0);
float dist = vec3.distance(distVec);
ExpectEq(IsEqualF(dist, 3), true);
dist = vec3.distanceSquared(distVec);
ExpectEq(IsEqualF(dist, 9), true);
// dot
logLabel = "test the vec3 dot function";
cc::Vec3 dotVec3(2, 3, 0);
float dotRes = vec3.dot(dotVec3);
ExpectEq(IsEqualF(dotRes, 20), true);
dotRes = cc::Vec3::dot(vec3, dotVec3);
ExpectEq(IsEqualF(dotRes, 20), true);
// length
logLabel = "test the vec3 length function";
vec3.set(8, 6, 0);
ExpectEq(IsEqualF(vec3.length(), 10), true);
// normalize
logLabel = "test whether vec3 is normalized";
vec3.normalize();
ExpectEq(IsEqualF(vec3.length(), 1.0f), true);
vec3.set(1, 0, 0);
ExpectEq(IsEqualF(vec3.length(), 1.0f), true);
vec3.set(0.345, 123.34, 0);
vec3.normalize();
ExpectEq(IsEqualF(vec3.length(), 1.0f), true);
vec3.set(10.345, 12.675, 20.231);
vec3 = vec3.getNormalized();
ExpectEq(IsEqualF(vec3.length(), 1.0f), true);
// subtract
logLabel = "test whether vec3 subtract function";
vec3.set(4, 5, 0);
vec3.subtract(cc::Vec3(1, 2, 0));
ExpectEq(vec3 == cc::Vec3(3, 3, 0), true);
cc::Vec3::subtract(cc::Vec3(4, 5, 0), cc::Vec3(1, 2, 0), &vec3);
ExpectEq(vec3 == cc::Vec3(3, 3, 0), true);
// max
logLabel = "test whether vec3 max function";
cc::Vec3::max(cc::Vec3(10, 1, 5), cc::Vec3(11, 3, 2), &vec3);
ExpectEq(vec3 == cc::Vec3(11, 3, 5), true);
// min
logLabel = "test whether vec3 min function";
cc::Vec3::min(cc::Vec3(10, 1, 5), cc::Vec3(11, 3, 2), &vec3);
ExpectEq(vec3 == cc::Vec3(10, 1, 2), true);
// smooth
logLabel = "test whether vec3 smooth function";
vec3.set(2, 0, 0);
vec3.smooth(cc::Vec3(-1, 0, 0), 0.5, 0.5);
ExpectEq(vec3 == cc::Vec3(0.5, 0, 0), true);
// fromColor
logLabel = "test whether vec3 fromColor function";
vec3 = cc::Vec3::fromColor(255);
ExpectEq(vec3.z == 1, true);
}
+109
View File
@@ -0,0 +1,109 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec4.h"
#include "utils.h"
#include <math.h>
TEST(mathVec4Test, test3) {
cc::Vec4 vec4(1, 2, 3, 1);
// isZero
logLabel = "test whether vec4 is 0";
ExpectEq(vec4.isZero(), false);
vec4.set(0, 0, 0, 0);
ExpectEq(vec4.isZero(), true);
vec4.set(1, 2, 1, 1);
ExpectEq(vec4.isZero(), false);
// isOne
logLabel = "test whether vec4 is 1";
vec4.set(2, 1, 1, 1);
ExpectEq(vec4.isOne(), false);
vec4.set(1, 1, 1, 1);
ExpectEq(vec4.isOne(), true);
vec4.set(1.1f, 1.0f, 1.0f, 1.0f);
ExpectEq(vec4.isOne(), false);
// angle
logLabel = "test the vec4 angle function";
ExpectEq(IsEqualF(vec4.angle(cc::Vec4(2, 0, 0, 0), cc::Vec4(-2, 0, 0, 0)), M_PI), true);
ExpectEq(IsEqualF(vec4.angle(cc::Vec4(2, 0, 0, 0), cc::Vec4(0, 2, 0, 0)), M_PI * 0.5f), true);
ExpectEq(IsEqualF(vec4.angle(cc::Vec4(2, 0, 0, 0), cc::Vec4(0, -2, 0, 0)), M_PI * 0.5f), true);
// add
logLabel = "test the vec4 add function";
vec4.set(3.9, 1.3, 0, 0);
vec4.add(cc::Vec4(2.1f, 1.7f, 0, 0));
ExpectEq(vec4 == cc::Vec4(6, 3, 0, 0), true);
cc::Vec4::add(cc::Vec4(6, 3, 0, 0), cc::Vec4(2, 1, 0, 1), &vec4);
ExpectEq(vec4 == cc::Vec4(8, 4, 0, 1), true);
// clamp
logLabel = "test the vec4 clamp function";
vec4.clamp(cc::Vec4(3, 1, 0, 1), cc::Vec4(6, 2, 0, 1));
ExpectEq(vec4 == cc::Vec4(6, 2, 0, 1), true);
cc::Vec4::clamp(cc::Vec4(6, 2, 0, 1), cc::Vec4(7, 1, 0, 1), cc::Vec4(11, 3, 0, 1), &vec4);
ExpectEq(vec4 == cc::Vec4(7, 2, 0, 1), true);
// distance
logLabel = "test the vec4 distance function";
vec4.set(7, 2, 0, 1);
cc::Vec4 distVec(10, 2, 0, 1);
float dist = vec4.distance(distVec);
ExpectEq(IsEqualF(dist, 3), true);
dist = vec4.distanceSquared(distVec);
ExpectEq(IsEqualF(dist, 9), true);
// dot
logLabel = "test the vec4 dot function";
vec4.set(3, 5, 0, 0);
cc::Vec4 dotVec4(2, 3, 0, 0);
float dotRes = vec4.dot(dotVec4);
ExpectEq(IsEqualF(dotRes, 21), true);
dotRes = cc::Vec4::dot(vec4, dotVec4);
ExpectEq(IsEqualF(dotRes, 21), true);
// length
logLabel = "test the vec4 length function";
vec4.set(8, 6, 0, 0);
ExpectEq(IsEqualF(vec4.length(), 10), true);
// normalize
logLabel = "test whether vec4 is normalized";
vec4.normalize();
ExpectEq(IsEqualF(vec4.length(), 1.0f), true);
vec4.set(1, 0, 0, 1);
vec4.normalize();
ExpectEq(IsEqualF(vec4.length(), 1.0f), true);
vec4.set(0.345, 123.34, 0, 1);
vec4.normalize();
ExpectEq(IsEqualF(vec4.length(), 1.0f), true);
vec4.set(10.345, 12.675, 20.231, 1);
vec4 = vec4.getNormalized();
ExpectEq(IsEqualF(vec4.length(), 1.0f), true);
// subtract
logLabel = "test whether vec4 subtract function";
vec4.set(4, 5, 0, 1);
vec4.subtract(cc::Vec4(1, 2, 0, 1));
ExpectEq(vec4 == cc::Vec4(3, 3, 0, 0), true);
cc::Vec4::subtract(cc::Vec4(4, 5, 0, 1), cc::Vec4(1, 2, 0, 1), &vec4);
ExpectEq(vec4 == cc::Vec4(3, 3, 0, 0), true);
// fromColor
logLabel = "test whether vec3 fromColor function";
vec4 = cc::Vec4::fromColor(255);
ExpectEq(vec4.w == 1, true);
}
+54
View File
@@ -0,0 +1,54 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec2.h"
#include "cocos/math/Math.h"
#include "cocos/math/Vertex.h"
#include "utils.h"
#include <math.h>
#include <vector>
TEST(mathVertexTest, test8) {
// ccVertexLineToPolygon
logLabel = "test the vertex ccVertexLineToPolygon function";
cc::Vec2 points[4];
cc::Vec2 p1(1, 1);
points[0] = p1;
cc::Vec2 p2(10, 10);
points[1] = p2;
cc::Vec2 p3(0, -10);
points[2] = p3;
cc::Vec2 p4(20, 70);
points[3] = p4;
cc::Vec2 outP[6];
cc::ccVertexLineToPolygon(points, 1, outP, 0, 5);
ExpectEq(IsEqualF(outP[5].x, -0.468942523) && IsEqualF(outP[5].y, -9.82652664), true);
// ccVertexLineIntersect
logLabel = "test the vertex ccVertexLineIntersect function";
float res;
bool isFind = cc::ccVertexLineIntersect(5, 5, 1, 10, 12, 20, 12, 40, &res);
ExpectEq(isFind, true);
}
+32
View File
@@ -0,0 +1,32 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
#include "cocos/math/Math.h"
static std::string logLabel;
static bool IsEqualF(float l, float r) {
return cc::math::IsEqualF(l, r);
};
static void ExpectEq(bool lf, bool rt) {
EXPECT_EQ(lf, rt) << "ERROR in: " <<logLabel;
}