From 8d138295ee3458e2a6b1e64a3d731e4db5e606b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 15 Nov 2022 19:13:09 +0100 Subject: [PATCH] Trade: test that MeshData::*AsArray() APIs pick the right attribute. Instead of always testing with exactly one attribute. Now the test cases are back to the original verbosity, but doing something actually useful. --- src/Magnum/Trade/Test/MeshDataTest.cpp | 223 ++++++++++++++++--------- 1 file changed, 148 insertions(+), 75 deletions(-) diff --git a/src/Magnum/Trade/Test/MeshDataTest.cpp b/src/Magnum/Trade/Test/MeshDataTest.cpp index 957266dba..02256b488 100644 --- a/src/Magnum/Trade/Test/MeshDataTest.cpp +++ b/src/Magnum/Trade/Test/MeshDataTest.cpp @@ -2624,17 +2624,25 @@ void MeshDataTest::indicesIntoArrayInvalidSize() { template void MeshDataTest::positions2DAsArray() { setTestCaseTemplateName(NameTraits::name()); + /* Testing also that it picks the correct attribute */ typedef typename T::Type TT; - T positions[]{ - T::pad(Math::Vector2{TT(2.0f), TT(1.0f)}), - T::pad(Math::Vector2{TT(0.0f), TT(-1.0f)}), - T::pad(Math::Vector2{TT(-2.0f), TT(3.0f)}) + struct Vertex { + Vector2 otherPosition; + UnsignedShort objectId; + T position; + } vertices[]{ + {{}, 0, T::pad(Math::Vector2{TT(2.0f), TT(1.0f)})}, + {{}, 0, T::pad(Math::Vector2{TT(0.0f), TT(-1.0f)})}, + {{}, 0, T::pad(Math::Vector2{TT(-2.0f), TT(3.0f)})} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, positions, { - MeshAttributeData{MeshAttribute::Position, Containers::arrayView(positions)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::Position, view.slice(&Vertex::otherPosition)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)}, + MeshAttributeData{MeshAttribute::Position, view.slice(&Vertex::position)} }}; - CORRADE_COMPARE_AS(data.positions2DAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.positions2DAsArray(1), Containers::arrayView({ {2.0f, 1.0f}, {0.0f, -1.0f}, {-2.0f, 3.0f} }), TestSuite::Compare::Container); } @@ -2732,19 +2740,27 @@ void MeshDataTest::positions2DIntoArrayInvalidSize() { template void MeshDataTest::positions3DAsArray() { setTestCaseTemplateName(NameTraits::name()); - /* Needs to be sufficiently representable to have the test work also for - half floats */ + /* Testing also that it picks the correct attribute. Needs to be + sufficiently representable to have the test work also for half + floats. */ typedef typename T::Type TT; - T positions[]{ - T::pad(Math::Vector3{TT(2.0f), TT(1.0f), TT(0.75f)}), - T::pad(Math::Vector3{TT(0.0f), TT(-1.0f), TT(1.25f)}), - T::pad(Math::Vector3{TT(-2.0f), TT(3.0f), TT(2.5f)}) + struct Vertex { + Vector3 otherPosition; + UnsignedShort objectId; + T position; + } vertices[]{ + {{}, 0, T::pad(Math::Vector3{TT(2.0f), TT(1.0f), TT(0.75f)})}, + {{}, 0, T::pad(Math::Vector3{TT(0.0f), TT(-1.0f), TT(1.25f)})}, + {{}, 0, T::pad(Math::Vector3{TT(-2.0f), TT(3.0f), TT(2.5f)})} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, positions, { - MeshAttributeData{MeshAttribute::Position, Containers::arrayView(positions)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::Position, view.slice(&Vertex::otherPosition)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)}, + MeshAttributeData{MeshAttribute::Position, view.slice(&Vertex::position)} }}; - CORRADE_COMPARE_AS(data.positions3DAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.positions3DAsArray(1), Containers::arrayView({ Vector3::pad(Math::Vector::pad(Vector3{2.0f, 1.0f, 0.75f})), Vector3::pad(Math::Vector::pad(Vector3{0.0f, -1.0f, 1.25f})), Vector3::pad(Math::Vector::pad(Vector3{-2.0f, 3.0f, 2.5f})) @@ -2850,19 +2866,27 @@ void MeshDataTest::positions3DIntoArrayInvalidSize() { template void MeshDataTest::tangentsAsArray() { setTestCaseTemplateName(NameTraits::name()); - /* Needs to be sufficiently representable to have the test work also for - half floats */ + /* Testing also that it picks the correct attribute. Needs to be + sufficiently representable to have the test work also for half + floats. */ typedef typename T::Type TT; - T tangents[]{ - T::pad(Math::Vector3{TT(2.0f), TT(1.0f), TT(0.75f)}), - T::pad(Math::Vector3{TT(0.0f), TT(-1.0f), TT(1.25f)}), - T::pad(Math::Vector3{TT(-2.0f), TT(3.0f), TT(2.5f)}) + struct Vertex { + Vector3 otherTangent; + UnsignedShort objectId; + T tangent; + } vertices[]{ + {{}, 0, T::pad(Math::Vector3{TT(2.0f), TT(1.0f), TT(0.75f)})}, + {{}, 0, T::pad(Math::Vector3{TT(0.0f), TT(-1.0f), TT(1.25f)})}, + {{}, 0, T::pad(Math::Vector3{TT(-2.0f), TT(3.0f), TT(2.5f)})} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, tangents, { - MeshAttributeData{MeshAttribute::Tangent, Containers::arrayView(tangents)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::Tangent, view.slice(&Vertex::otherTangent)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)}, + MeshAttributeData{MeshAttribute::Tangent, view.slice(&Vertex::tangent)} }}; - CORRADE_COMPARE_AS(data.tangentsAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.tangentsAsArray(1), Containers::arrayView({ {2.0f, 1.0f, 0.75f}, {0.0f, -1.0f, 1.25f}, {-2.0f, 3.0f, 2.5f}, }), TestSuite::Compare::Container); } @@ -2905,18 +2929,26 @@ void MeshDataTest::tangentsIntoArrayInvalidSize() { template void MeshDataTest::bitangentSignsAsArray() { setTestCaseTemplateName(Math::TypeTraits::name()); - /* Needs to be sufficiently representable to have the test work also for - half floats */ - Math::Vector4 tangents[]{ - {T(2.0f), T(1.0f), T(0.75f), T(-1.0f)}, - {T(0.0f), T(-1.0f), T(1.25f), T(1.0f)}, - {T(-2.0f), T(3.0f), T(2.5f), T(-1.0f)} + /* Testing also that it picks the correct attribute. Needs to be + sufficiently representable to have the test work also for half + floats. */ + struct Vertex { + Vector3 otherTangent; + UnsignedShort objectId; + Math::Vector4 tangent; + } vertices[]{ + {{}, 0, {T(2.0f), T(1.0f), T(0.75f), T(-1.0f)}}, + {{}, 0, {T(0.0f), T(-1.0f), T(1.25f), T(1.0f)}}, + {{}, 0, {T(-2.0f), T(3.0f), T(2.5f), T(-1.0f)}} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, tangents, { - MeshAttributeData{MeshAttribute::Tangent, Containers::arrayView(tangents)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::Tangent, view.slice(&Vertex::otherTangent)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)}, + MeshAttributeData{MeshAttribute::Tangent, view.slice(&Vertex::tangent)} }}; - CORRADE_COMPARE_AS(data.bitangentSignsAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.bitangentSignsAsArray(1), Containers::arrayView({ -1.0f, 1.0f, -1.0f }), TestSuite::Compare::Container); } @@ -2974,19 +3006,27 @@ void MeshDataTest::bitangentSignsIntoArrayInvalidSize() { template void MeshDataTest::bitangentsAsArray() { setTestCaseTemplateName(NameTraits::name()); - /* Needs to be sufficiently representable to have the test work also for - half floats */ + /* Testing also that it picks the correct attribute. Needs to be + sufficiently representable to have the test work also for half + floats. */ typedef typename T::Type TT; - T bitangents[]{ - {TT(2.0f), TT(1.0f), TT(0.75f)}, - {TT(0.0f), TT(-1.0f), TT(1.25f)}, - {TT(-2.0f), TT(3.0f), TT(2.5f)} + struct Vertex { + Vector3 otherBitangent; + UnsignedShort objectId; + T bitangent; + } vertices[]{ + {{}, 0, {TT(2.0f), TT(1.0f), TT(0.75f)}}, + {{}, 0, {TT(0.0f), TT(-1.0f), TT(1.25f)}}, + {{}, 0, {TT(-2.0f), TT(3.0f), TT(2.5f)}} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, bitangents, { - MeshAttributeData{MeshAttribute::Bitangent, Containers::arrayView(bitangents)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::Bitangent, view.slice(&Vertex::otherBitangent)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)}, + MeshAttributeData{MeshAttribute::Bitangent, view.slice(&Vertex::bitangent)} }}; - CORRADE_COMPARE_AS(data.bitangentsAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.bitangentsAsArray(1), Containers::arrayView({ {2.0f, 1.0f, 0.75f}, {0.0f, -1.0f, 1.25f}, {-2.0f, 3.0f, 2.5f}, }), TestSuite::Compare::Container); } @@ -3029,19 +3069,27 @@ void MeshDataTest::bitangentsIntoArrayInvalidSize() { template void MeshDataTest::normalsAsArray() { setTestCaseTemplateName(NameTraits::name()); - /* Needs to be sufficiently representable to have the test work also for - half floats */ + /* Testing also that it picks the correct attribute. Needs to be + sufficiently representable to have the test work also for half + floats. */ typedef typename T::Type TT; - T normals[]{ - {TT(2.0f), TT(1.0f), TT(0.75f)}, - {TT(0.0f), TT(-1.0f), TT(1.25f)}, - {TT(-2.0f), TT(3.0f), TT(2.5f)} + struct Vertex { + Vector3 otherNormal; + UnsignedShort objectId; + T normal; + } vertices[]{ + {{}, 0, {TT(2.0f), TT(1.0f), TT(0.75f)}}, + {{}, 0, {TT(0.0f), TT(-1.0f), TT(1.25f)}}, + {{}, 0, {TT(-2.0f), TT(3.0f), TT(2.5f)}} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, normals, { - MeshAttributeData{MeshAttribute::Normal, Containers::arrayView(normals)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::Normal, view.slice(&Vertex::otherNormal)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)}, + MeshAttributeData{MeshAttribute::Normal, view.slice(&Vertex::normal)} }}; - CORRADE_COMPARE_AS(data.normalsAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.normalsAsArray(1), Containers::arrayView({ {2.0f, 1.0f, 0.75f}, {0.0f, -1.0f, 1.25f}, {-2.0f, 3.0f, 2.5f}, }), TestSuite::Compare::Container); } @@ -3084,17 +3132,27 @@ void MeshDataTest::normalsIntoArrayInvalidSize() { template void MeshDataTest::textureCoordinates2DAsArray() { setTestCaseTemplateName(NameTraits::name()); + /* Testing also that it picks the correct attribute. Needs to be + sufficiently representable to have the test work also for half + floats. */ typedef typename T::Type TT; - T textureCoordinates[]{ - {TT(2.0f), TT(1.0f)}, - {TT(0.0f), TT(-1.0f)}, - {TT(-2.0f), TT(3.0f)} + struct Vertex { + Vector2 otherTextureCoordinate; + UnsignedShort objectId; + T textureCoordinate; + } vertices[]{ + {{}, 0, {TT(2.0f), TT(1.0f)}}, + {{}, 0, {TT(0.0f), TT(-1.0f)}}, + {{}, 0, {TT(-2.0f), TT(3.0f)}} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, textureCoordinates, { - MeshAttributeData{MeshAttribute::TextureCoordinates, Containers::arrayView(textureCoordinates)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::TextureCoordinates, view.slice(&Vertex::otherTextureCoordinate)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)}, + MeshAttributeData{MeshAttribute::TextureCoordinates, view.slice(&Vertex::textureCoordinate)} }}; - CORRADE_COMPARE_AS(data.textureCoordinates2DAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.textureCoordinates2DAsArray(1), Containers::arrayView({ {2.0f, 1.0f}, {0.0f, -1.0f}, {-2.0f, 3.0f}, }), TestSuite::Compare::Container); } @@ -3190,19 +3248,26 @@ void MeshDataTest::textureCoordinates2DIntoArrayInvalidSize() { template void MeshDataTest::colorsAsArray() { setTestCaseTemplateName(NameTraits::name()); - /* Can't use e.g. 0xff3366_rgbf because that's not representable in - half-floats */ + /* Testing also that it picks the correct attribute. Can't use e.g. + 0xff3366_rgbf because that's not representable in half-floats. */ typedef typename T::Type TT; - T colors[]{ - {TT(2.0f), TT(1.0f), TT(0.75f)}, - {TT(0.0f), TT(-1.0f), TT(1.25f)}, - {TT(-2.0f), TT(3.0f), TT(2.5f)} + struct Vertex { + Color4 otherColor; + UnsignedShort objectId; + T color; + } vertices[]{ + {{}, 0, {TT(2.0f), TT(1.0f), TT(0.75f)}}, + {{}, 0, {TT(0.0f), TT(-1.0f), TT(1.25f)}}, + {{}, 0, {TT(-2.0f), TT(3.0f), TT(2.5f)}} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, colors, { - MeshAttributeData{MeshAttribute::Color, Containers::arrayView(colors)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::Color, view.slice(&Vertex::otherColor)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)}, + MeshAttributeData{MeshAttribute::Color, view.slice(&Vertex::color)} }}; - CORRADE_COMPARE_AS(data.colorsAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.colorsAsArray(1), Containers::arrayView({ {2.0f, 1.0f, 0.75f}, {0.0f, -1.0f, 1.25f}, {-2.0f, 3.0f, 2.5f}, }), TestSuite::Compare::Container); } @@ -3244,16 +3309,24 @@ void MeshDataTest::colorsIntoArrayInvalidSize() { template void MeshDataTest::objectIdsAsArray() { setTestCaseTemplateName(Math::TypeTraits::name()); - T objectIds[]{ - 157, - 24, - 1 + /* Testing also that it picks the correct attribute */ + struct Vertex { + UnsignedByte otherObjectId; + Vector2 position; + T objectId; + } vertices[]{ + {0, {}, 157}, + {0, {}, 24}, + {0, {}, 1} }; + auto view = Containers::stridedArrayView(vertices); - MeshData data{MeshPrimitive::Points, {}, objectIds, { - MeshAttributeData{MeshAttribute::ObjectId, Containers::arrayView(objectIds)} + MeshData data{MeshPrimitive::Points, {}, vertices, { + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::otherObjectId)}, + MeshAttributeData{MeshAttribute::Position, view.slice(&Vertex::position)}, + MeshAttributeData{MeshAttribute::ObjectId, view.slice(&Vertex::objectId)} }}; - CORRADE_COMPARE_AS(data.objectIdsAsArray(), Containers::arrayView({ + CORRADE_COMPARE_AS(data.objectIdsAsArray(1), Containers::arrayView({ 157, 24, 1 }), TestSuite::Compare::Container); }