Skip to content

Commit

Permalink
Test for BasicVector4 casting to C array
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Mott committed Sep 1, 2021
1 parent 528c790 commit 0bdecfa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
16 changes: 4 additions & 12 deletions libs/math/Vector4.h
Expand Up @@ -268,19 +268,11 @@ class BasicVector4
_v[2] / _v[3]);
}

/** Implicit cast to C-style array. This allows a Vector4 to be
* passed directly to GL functions that expect an array (e.g.
* glFloat4dv()). These functions implicitly provide operator[]
* as well, since the C-style array provides this function.
*/

operator const Element* () const {
return _v;
}
/// Cast to const raw array
operator const Element* () const { return _v; }

operator Element* () {
return _v;
}
// Cast to non-const raw array
operator Element* () { return _v; }

/* Cast this Vector4 onto a Vector3, both const and non-const
*/
Expand Down
15 changes: 15 additions & 0 deletions test/math/Vector.cpp
Expand Up @@ -446,4 +446,19 @@ TEST(MathTest, Vector3fAsCArray)
EXPECT_EQ(array[2], 0);
}

TEST(MathTest, Vector4AsCArray)
{
Vector4 vec(-15, 15, 36.9, -0.5);

EXPECT_EQ(&vec.y(), &vec.x() + 1);
EXPECT_EQ(&vec.z(), &vec.y() + 1);
EXPECT_EQ(&vec.w(), &vec.z() + 1);

double* array = vec;
EXPECT_EQ(array[0], -15);
EXPECT_EQ(array[1], 15);
EXPECT_EQ(array[2], 36.9);
EXPECT_EQ(array[3], -0.5);
}

}

0 comments on commit 0bdecfa

Please sign in to comment.