mirror of
https://github.com/yuzu-emu/FasTC
synced 2024-11-22 11:03:57 +00:00
Add some tests for VectorBase
This commit is contained in:
parent
92f5893650
commit
45b739a44a
2 changed files with 152 additions and 6 deletions
|
@ -61,7 +61,7 @@ namespace FasTC {
|
|||
const T &operator[](int idx) const { return vec[idx]; }
|
||||
|
||||
// Allow casts to the respective array representation...
|
||||
operator T *() const { return vec; }
|
||||
operator const T *() const { return vec; }
|
||||
VectorBase<T, N> &operator=(const T *v) {
|
||||
for(int i = 0; i < N; i++)
|
||||
vec[i] = v[i];
|
||||
|
@ -71,7 +71,11 @@ namespace FasTC {
|
|||
// Allows casting to other vector types if the underlying type system does as well...
|
||||
template<typename _T>
|
||||
operator VectorBase<_T, N>() const {
|
||||
return VectorBase<_T, N>(vec);
|
||||
VectorBase<_T, N> ret;
|
||||
for(int i = 0; i < N; i++) {
|
||||
ret[i] = static_cast<_T>(vec[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Vector operations
|
||||
|
|
|
@ -51,19 +51,20 @@
|
|||
*/
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "TexCompTypes.h"
|
||||
#include "VectorBase.h"
|
||||
|
||||
static const float kEpsilon = 1e-6;
|
||||
|
||||
TEST(VectorBase, Constructors) {
|
||||
FasTC::VectorBase<float, 3> v3f;
|
||||
FasTC::VectorBase<double, 1> v1d;
|
||||
FasTC::VectorBase<int, 7> v7i;
|
||||
FasTC::VectorBase<uint32, 16> v16u;
|
||||
FasTC::VectorBase<unsigned, 16> v16u;
|
||||
|
||||
#define TEST_VECTOR_COPY_CONS(v, t, n) \
|
||||
do { \
|
||||
FasTC::VectorBase<t, n> d##v (v); \
|
||||
for(uint32 i = 0; i < n; i++) { \
|
||||
for(int i = 0; i < n; i++) { \
|
||||
EXPECT_EQ(d##v [i], v[i]); \
|
||||
} \
|
||||
} while(0) \
|
||||
|
@ -71,5 +72,146 @@ TEST(VectorBase, Constructors) {
|
|||
TEST_VECTOR_COPY_CONS(v3f, float, 3);
|
||||
TEST_VECTOR_COPY_CONS(v1d, double, 1);
|
||||
TEST_VECTOR_COPY_CONS(v7i, int, 7);
|
||||
TEST_VECTOR_COPY_CONS(v16u, uint32, 16);
|
||||
TEST_VECTOR_COPY_CONS(v16u, unsigned, 16);
|
||||
|
||||
#undef TEST_VECTOR_COPY_CONS
|
||||
}
|
||||
|
||||
TEST(VectorBase, Accessors) {
|
||||
FasTC::VectorBase<float, 3> v3f;
|
||||
v3f[0] = 1.0f;
|
||||
v3f[1] = -2.3f;
|
||||
v3f[2] = 1000;
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
EXPECT_EQ(v3f[i], v3f(i));
|
||||
}
|
||||
|
||||
v3f(0) = -1.0f;
|
||||
v3f(1) = 2.3f;
|
||||
v3f(2) = -1000;
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
EXPECT_EQ(v3f(i), v3f[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(VectorBase, PointerConversion) {
|
||||
FasTC::VectorBase<float, 3> v3f;
|
||||
v3f[0] = 1.0f;
|
||||
v3f[1] = -2.3f;
|
||||
v3f[2] = 1000;
|
||||
|
||||
float cmp[3] = { 1.0f, -2.3f, 1000 };
|
||||
const float *v3fp = v3f;
|
||||
int result = memcmp(cmp, v3fp, 3 * sizeof(float));
|
||||
EXPECT_EQ(result, 0);
|
||||
|
||||
cmp[0] = -1.0f;
|
||||
cmp[1] = 2.3f;
|
||||
cmp[2] = 1000.0f;
|
||||
v3f = cmp;
|
||||
for(int i = 0; i < 3; i++) {
|
||||
EXPECT_EQ(v3f[i], cmp[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(VectorBase, CastVector) {
|
||||
FasTC::VectorBase<float, 3> v3f;
|
||||
FasTC::VectorBase<double, 3> v3d = v3f;
|
||||
FasTC::VectorBase<int, 3> v3i = v3f;
|
||||
for(int i = 0; i < 3; i++) {
|
||||
EXPECT_EQ(v3d(i), static_cast<double>(v3f(i)));
|
||||
EXPECT_EQ(v3i(i), static_cast<int>(v3f(i)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(VectorBase, DotProduct) {
|
||||
int iv[5] = { -2, -1, 0, 1, 2 };
|
||||
FasTC::VectorBase<int, 5> v5i(iv);
|
||||
|
||||
unsigned uv[5] = { 1, 2, 3, 4, 5 };
|
||||
FasTC::VectorBase<unsigned, 5> v5u(uv);
|
||||
|
||||
EXPECT_EQ(v5i.Dot(v5u), 10);
|
||||
EXPECT_EQ(v5u.Dot(v5i), 10);
|
||||
}
|
||||
|
||||
TEST(VectorBase, Length) {
|
||||
int iv[5] = { 1, 2, 3, 4, 5 };
|
||||
FasTC::VectorBase<int, 5> v5i (iv);
|
||||
|
||||
EXPECT_EQ(v5i.LengthSq(), 55);
|
||||
EXPECT_EQ(v5i.Length(), 7);
|
||||
|
||||
float fv[6] = {1, 2, 3, 4, 5, 6};
|
||||
FasTC::VectorBase<float, 6> v6f (fv);
|
||||
|
||||
EXPECT_EQ(v6f.LengthSq(), 91);
|
||||
EXPECT_NEAR(v6f.Length(), sqrt(91.0f), kEpsilon);
|
||||
}
|
||||
|
||||
TEST(VectorBase, Normalization) {
|
||||
float fv[2] = {1, 0};
|
||||
FasTC::VectorBase<float, 2> v2f (fv);
|
||||
v2f.Normalize();
|
||||
EXPECT_EQ(v2f[0], 1);
|
||||
EXPECT_EQ(v2f[1], 0);
|
||||
|
||||
// Normalized vector should be sqrt(2) for each axis, although
|
||||
// this can't be represented as integers...
|
||||
unsigned uv[2] = {2, 2};
|
||||
FasTC::VectorBase<unsigned, 2> v2u (uv);
|
||||
v2u.Normalize();
|
||||
EXPECT_EQ(v2u[0], 1);
|
||||
EXPECT_EQ(v2u[1], 1);
|
||||
|
||||
const float sqrt2 = sqrt(2)/2.0f;
|
||||
for(int i = 2; i < 10; i++) {
|
||||
v2f[0] = static_cast<float>(i);
|
||||
v2f[1] = static_cast<float>(i);
|
||||
v2f.Normalize();
|
||||
EXPECT_NEAR(v2f[0], sqrt2, kEpsilon);
|
||||
EXPECT_NEAR(v2f[1], sqrt2, kEpsilon);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(VectorBase, Scaling) {
|
||||
float fv[2] = {1.0f, 3.0f};
|
||||
FasTC::VectorBase<float, 2> v2f (fv);
|
||||
FasTC::VectorBase<float, 2> v2fd = v2f * 3.0f;
|
||||
EXPECT_NEAR(v2fd[0], 3.0f, kEpsilon);
|
||||
EXPECT_NEAR(v2fd[1], 9.0f, kEpsilon);
|
||||
|
||||
v2fd = -1.0 * v2f;
|
||||
EXPECT_NEAR(v2fd[0], -1.0f, kEpsilon);
|
||||
EXPECT_NEAR(v2fd[1], -3.0f, kEpsilon);
|
||||
|
||||
v2fd = v2f / 3;
|
||||
EXPECT_NEAR(v2fd[0], 1.0f / 3.0f, kEpsilon);
|
||||
EXPECT_NEAR(v2fd[1], 1.0f, kEpsilon);
|
||||
}
|
||||
|
||||
TEST(VectorBase, Addition) {
|
||||
float fv[2] = {1.1f, 3.2f};
|
||||
FasTC::VectorBase<float, 2> v2f (fv);
|
||||
|
||||
unsigned uv[2] = {5, 2};
|
||||
FasTC::VectorBase<unsigned, 2> v2u (uv);
|
||||
|
||||
FasTC::VectorBase<unsigned, 2> au = v2u + v2f;
|
||||
EXPECT_EQ(au[0], 6);
|
||||
EXPECT_EQ(au[1], 5);
|
||||
|
||||
FasTC::VectorBase<float, 2> af = v2f + v2u;
|
||||
EXPECT_NEAR(af[0], 6.1f, kEpsilon);
|
||||
EXPECT_NEAR(af[1], 5.2f, kEpsilon);
|
||||
|
||||
au = v2u - v2f;
|
||||
EXPECT_EQ(au[0], 3);
|
||||
EXPECT_EQ(au[1], 0);
|
||||
|
||||
af = v2f - v2u;
|
||||
EXPECT_NEAR(af[0], -3.9f, kEpsilon);
|
||||
EXPECT_NEAR(af[1], 1.2f, kEpsilon);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue