// MobileGL - MobileGL/MG_Test/Program/ProgramArtifactsTest.cpp // Copyright (c) 2025-2026 MobileGL-Dev // Licensed under the GNU Lesser General Public License v3.0: // https://www.gnu.org/licenses/gpl-3.0.txt // https://www.gnu.org/licenses/lgpl-3.0.txt // SPDX-License-Identifier: LGPL-3.0-only // End of Source File Header // First include on purpose: the artifacts header must be self-contained (P0.5 gate A). #include #include // For the alias checks only. #include #include #include #include #include namespace { using namespace MobileGL; using namespace MobileGL::MG_State::GLState; // P0.5 moved the five types to namespace scope and left in-class aliases behind so the // existing spellings compile unchanged. The classic failure of that move is a COPY: two // same-named definitions that both compile and silently split the type. is_same_v is the // compile-time proof that ProgramObject::X and GLState::X are one type. TEST(ProgramArtifacts, AliasesAreTheSameTypes) { static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(ProgramObject::kInvalidUniformOffset == kInvalidUniformOffset); EXPECT_EQ(ProgramObject::kInvalidUniformOffset, kInvalidUniformOffset); EXPECT_EQ(kInvalidUniformOffset, ~0u); } // 13 Bool + 3 bytes of padding + 7 x 4-byte scalars on every ABI. TEST(ProgramArtifacts, TypeFactsIsPodOf44Bytes) { static_assert(std::is_trivially_copyable_v); static_assert(std::is_standard_layout_v); EXPECT_EQ(sizeof(TypeFacts), 44u); EXPECT_EQ(alignof(TypeFacts), 4u); } // A visitor that counts what a table hands it and checks the names are distinct - the // archive's one field table per type has to name every member exactly once. struct CountingVisitor { std::size_t count = 0; std::set names; template void operator()(const char* name, Field&) { ++count; names.insert(name); } }; template std::size_t CountFields() { T value{}; CountingVisitor mutableVisitor; VisitFields(value, mutableVisitor); const T& constValue = value; CountingVisitor constVisitor; VisitFields(constValue, constVisitor); EXPECT_EQ(mutableVisitor.count, constVisitor.count) << "const and non-const walks disagree"; EXPECT_EQ(mutableVisitor.names.size(), mutableVisitor.count) << "a field name is listed twice"; EXPECT_EQ(mutableVisitor.names, constVisitor.names); return mutableVisitor.count; } // The field counts are the member counts of the moved structs (LinkArtifacts minus its // never-archived `program`). A member added without a table entry is caught by the sizeof // trip wires in the header; a table entry dropped without a member change is caught here. TEST(ProgramArtifacts, VisitFieldsCoversEveryMember) { EXPECT_EQ(CountFields(), 20u); EXPECT_EQ(CountFields(), 14u); EXPECT_EQ(CountFields(), 11u); // P7 OQ-8 added LinkArtifacts::storageBlocks and its own three-field table; since P13 W5 // the member follows the record arm (ProgramArtifacts.h), which every library builds. EXPECT_EQ(CountFields(), 3u); EXPECT_EQ(CountFields(), 59u); EXPECT_EQ(CountFields(), 8u); } // A const walk hands the visitor const references, a non-const walk mutable ones: the one // table really serves both directions. TEST(ProgramArtifacts, VisitFieldsPassesConstnessThrough) { LinkArtifacts artifacts; std::size_t mutableFields = 0; VisitFields(artifacts, [&](const char*, auto& field) { static_assert(!std::is_const_v>); ++mutableFields; }); const LinkArtifacts& constArtifacts = artifacts; std::size_t constFields = 0; VisitFields(constArtifacts, [&](const char*, auto& field) { static_assert(std::is_const_v>); ++constFields; }); EXPECT_EQ(mutableFields, constFields); // The table can write through: a deserializer's shape. VisitFields(artifacts, [](const char* name, auto& field) { if constexpr (std::is_same_v, Bool>) { if (std::string(name) == "linkStatus") field = true; } }); EXPECT_TRUE(artifacts.linkStatus); } // The sizeof numbers, visible in every `ctest -V` log on every platform: this is where the // integrator reads a new toolchain's values from before pinning them in the header. TEST(ProgramArtifacts, SizesArePinnedOnThisToolchain) { RecordProperty("sizeof_TypeFacts", static_cast(sizeof(TypeFacts))); RecordProperty("sizeof_ResourceReflection", static_cast(sizeof(ResourceReflection))); RecordProperty("sizeof_XfbVarying", static_cast(sizeof(XfbVarying))); RecordProperty("sizeof_LinkArtifacts", static_cast(sizeof(LinkArtifacts))); RecordProperty("sizeof_SpirvArtifacts", static_cast(sizeof(SpirvArtifacts))); std::printf( "sizeof: TypeFacts=%zu ResourceReflection=%zu XfbVarying=%zu LinkArtifacts=%zu SpirvArtifacts=%zu\n", sizeof(TypeFacts), sizeof(ResourceReflection), sizeof(XfbVarying), sizeof(LinkArtifacts), sizeof(SpirvArtifacts)); #ifdef MGL_LINKARTIFACTS_SIZE // whichever STL branch the header pinned, this twin follows it EXPECT_EQ(sizeof(ResourceReflection), static_cast(MGL_RESOURCEREFLECTION_SIZE)); EXPECT_EQ(sizeof(XfbVarying), static_cast(MGL_XFBVARYING_SIZE)); EXPECT_EQ(sizeof(LinkArtifacts), static_cast(MGL_LINKARTIFACTS_SIZE)); EXPECT_EQ(sizeof(SpirvArtifacts), static_cast(MGL_SPIRVARTIFACTS_SIZE)); #else RecordProperty("sizes_pinned", "no: not the libstdc++ 64-bit toolchain"); #endif } } // namespace