Explorar el Código

math: fix a nasty bug involving swizzling vectors, add two safeguards so

that it never happens again, and unit tests in case it happens again.
undefined
Sam Hocevar hace 10 años
padre
commit
7babf9dfde
Se han modificado 3 ficheros con 38 adiciones y 1 borrados
  1. +1
    -1
      src/lol/math/ops.h
  2. +7
    -0
      src/lol/math/vector.h
  3. +30
    -0
      test/unit/vector.cpp

+ 1
- 1
src/lol/math/ops.h Ver fichero

@@ -78,7 +78,7 @@ static inline typename std::enable_if<SWIZZLE1 != FULL_SWIZZLE || SWIZZLE2 != FU
\
template<typename T, int N, int SWIZZLE> \
inline typename std::enable_if<SWIZZLE != FULL_SWIZZLE,vec_t<T,N>>::type \
operator op(vec_t<T,N,SWIZZLE> a, T const &b) \
operator op(vec_t<T,N,SWIZZLE> const &a, T const &b) \
{ \
return vec_t<T,N>(a) op b; \
}


+ 7
- 0
src/lol/math/vector.h Ver fichero

@@ -81,6 +81,13 @@ struct vec_t
int i = (SWIZZLE >> (4 * (N - 1 - n))) & 3;
return static_cast<T const*>(static_cast<void const *>(this))[i];
}

private:
/* Disable all default constructors and destructors; this object
* is only intended to exist as part of a union. */
vec_t();
vec_t(vec_t<T, N, SWIZZLE> const &);
~vec_t();
};

/* The generic “vec_t” type, which is a fixed-size vector with no


+ 30
- 0
test/unit/vector.cpp Ver fichero

@@ -141,6 +141,36 @@ LOLUNIT_FIXTURE(VectorTest)
#endif
}

LOLUNIT_TEST(VectorSwizzleMul)
{
ivec3 a(1, 2, 3);

ivec3 b = a * 2;
LOLUNIT_ASSERT_EQUAL(b.x, 2);
LOLUNIT_ASSERT_EQUAL(b.y, 4);
LOLUNIT_ASSERT_EQUAL(b.z, 6);

ivec3 c = (ivec3)a.zyx * 2;
LOLUNIT_ASSERT_EQUAL(c.x, 6);
LOLUNIT_ASSERT_EQUAL(c.y, 4);
LOLUNIT_ASSERT_EQUAL(c.z, 2);

ivec3 d = 2 * (ivec3)a.zyx;
LOLUNIT_ASSERT_EQUAL(d.x, 6);
LOLUNIT_ASSERT_EQUAL(d.y, 4);
LOLUNIT_ASSERT_EQUAL(d.z, 2);

ivec3 e = a.zyx * 2;
LOLUNIT_ASSERT_EQUAL(e.x, 6);
LOLUNIT_ASSERT_EQUAL(e.y, 4);
LOLUNIT_ASSERT_EQUAL(e.z, 2);

ivec3 f = 2 * a.zyx;
LOLUNIT_ASSERT_EQUAL(f.x, 6);
LOLUNIT_ASSERT_EQUAL(f.y, 4);
LOLUNIT_ASSERT_EQUAL(f.z, 2);
}

LOLUNIT_TEST(VectorUnaryMinus)
{
vec2 a(1.0f, 3.0f);


Cargando…
Cancelar
Guardar