@@ -766,4 +766,103 @@ void test_user_defined_operators_constants() {
766766 l1 = 42L ; // NON_COMPLIANT
767767 l1 = 42LL ; // NON_COMPLIANT
768768 l1 = 42U ; // NON_COMPLIANT
769+ }
770+
771+ // Test aggregate initialization - struct with multiple members
772+ struct SimpleAggregate {
773+ std::uint8_t m1;
774+ std::uint16_t m2;
775+ std::int32_t m3;
776+ float m4;
777+ };
778+
779+ void test_aggregate_initialization_basic () {
780+ // Compliant cases - exact types or constants that fit
781+ SimpleAggregate l1{42 , 1000 , -50 , 3 .14f }; // COMPLIANT
782+ SimpleAggregate l2{u8 , u16 , s32, f}; // COMPLIANT
783+ SimpleAggregate l3{255 , 65535 , 2147483647 , 0 .0f }; // COMPLIANT
784+
785+ // Non-compliant cases - type violations
786+ SimpleAggregate l4{u16 , u8 , s32, // NON_COMPLIANT - narrowing u16 to uint8_t
787+ f};
788+ SimpleAggregate l5{u8 , u32 , s32, // NON_COMPLIANT - narrowing u32 to uint16_t
789+ f};
790+ SimpleAggregate l6{u8 , u16 , u32 , f}; // NON_COMPLIANT - different signedness
791+ SimpleAggregate l7{u8 , u16 , s32,
792+ s32}; // NON_COMPLIANT - different type category
793+
794+ // Constants that don't fit
795+ SimpleAggregate l8{256 , 65536 , // NON_COMPLIANT - constants don't fit
796+ 2147483648LL , // NON_COMPLIANT - constants don't fit
797+ 0 .0f };
798+
799+ // Widening of id-expressions is allowed
800+ SimpleAggregate l9{u8 , u8 , s8, f}; // COMPLIANT - widening allowed
801+ }
802+
803+ // Test aggregate initialization - arrays
804+ void test_aggregate_initialization_arrays () {
805+ // Basic arrays
806+ std::uint8_t l1[3 ]{10 , 20 , 30 }; // COMPLIANT
807+ std::uint8_t l2[3 ]{u8 , u8 , u8 }; // COMPLIANT
808+ std::uint8_t l3[3 ]{300 , 400 , 500 }; // NON_COMPLIANT - constants don't fit
809+ std::uint8_t l4[3 ]{s8, s8, s8}; // NON_COMPLIANT - signedness mismatch
810+ std::uint8_t l5[3 ]{u16 , u16 , u16 }; // NON_COMPLIANT - narrowing
811+
812+ // Multi-dimensional arrays
813+ std::int16_t l6[2 ][2 ]{{1 , 2 }, {3 , 4 }}; // COMPLIANT
814+ std::int16_t l7[2 ][2 ]{{s8, s8}, {s8, s8}}; // COMPLIANT - widening allowed
815+ std::int16_t l8[2 ][2 ]{{s32, s32}, {s32, s32}}; // NON_COMPLIANT - narrowing
816+ std::int16_t l9[2 ][2 ]{{u8 , u8 }, // NON_COMPLIANT - signedness mismatch
817+ {u8 , u8 }}; // NON_COMPLIANT - signedness mismatch
818+ }
819+
820+ // Test aggregate initialization - nested structs
821+ struct NestedAggregate {
822+ SimpleAggregate m1;
823+ std::uint32_t m2;
824+ };
825+
826+ void test_aggregate_initialization_nested () {
827+ // Compliant nested initialization
828+ NestedAggregate l1{{10 , 100 , -5 , 1 .0f }, 500 }; // COMPLIANT
829+ NestedAggregate l2{{u8 , u16 , s32, f}, u32 }; // COMPLIANT
830+
831+ // Non-compliant nested initialization
832+ NestedAggregate l3{
833+ {u16 , u8 , s32, f}, // NON_COMPLIANT - narrowing in nested struct
834+ u32 };
835+ NestedAggregate l4{
836+ {u8 , u16 , s32, f},
837+ s32}; // NON_COMPLIANT - signedness mismatch in outer member
838+ }
839+
840+ // Test aggregate initialization - struct with bit-fields
841+ struct BitfieldAggregate {
842+ std::uint32_t m1 : 8 ;
843+ std::uint32_t m2 : 16 ;
844+ std::int32_t m3 : 12 ;
845+ };
846+
847+ void test_aggregate_initialization_bitfields () {
848+ // Compliant cases
849+ BitfieldAggregate l1{100 , 30000 , -500 }; // COMPLIANT
850+ BitfieldAggregate l2{u8 , u16 , s16}; // COMPLIANT - appropriate sizes
851+
852+ // Non-compliant cases
853+ BitfieldAggregate l3{300 , 70000 , 5000 }; // NON_COMPLIANT - constants don't fit
854+ BitfieldAggregate l4{u16 , u32 , s32}; // NON_COMPLIANT - narrowing
855+ }
856+
857+ // Test aggregate initialization with designated initializers (C++20 feature,
858+ // but test for basic cases)
859+ void test_aggregate_initialization_designated () {
860+ // Note: Designated initializers are C++20, but we can test basic aggregate
861+ // init patterns
862+ SimpleAggregate l1{.m1 = 10 , .m2 = 100 , .m3 = -5 , .m4 = 1 .0f }; // COMPLIANT
863+ SimpleAggregate l2{.m1 = u8 , .m2 = u16 , .m3 = s32, .m4 = f}; // COMPLIANT
864+ SimpleAggregate l3{.m1 = u16 , // NON_COMPLIANT - type violation
865+ .m2 = u8 ,
866+ .m3 = s32,
867+ .m4 = f};
769868}
0 commit comments