Repository: dotnet/runtime
File: src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs (C#)

Code before:
        public static unsafe void AllocateTypeAssociatedMemoryValidArguments()
        {
            IntPtr memory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 32);
            Assert.NotEqual(memory, IntPtr.Zero);
            // Validate that the memory is zeroed out
            Assert.True(new Span<byte>((void*)memory, 32).SequenceEqual(new byte[32]));
        }

        [Fact]
        public static void AlignedAllocateTypeAssociatedMemoryInvalidArguments()
        {
            Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(null, 10, 1); });
            Assert.Throws<ArgumentOutOfRangeException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1, 1); });
            Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1, 0); });
            Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1, 3); });
        }


        [Fact]
        public static unsafe void AlignedAllocateTypeAssociatedMemoryValidArguments()
        {
            IntPtr memory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 32, 16);
            Assert.NotEqual(memory, IntPtr.Zero);
            // Validate that the memory is zeroed out
            Assert.True(new Span<byte>((void*)memory, 32).SequenceEqual(new byte[32]));
        }

#pragma warning disable CS0649
        [StructLayoutAttribute(LayoutKind.Sequential)]
        private struct StructWithoutReferences
        {
            public int a, b, c;
        }

        [StructLayoutAttribute(LayoutKind.Sequential)]
        private struct StructWithReferences
        {
            public int a, b, c;
            public object d;
        }

        private ref struct RefStructWithoutReferences
        {
            public int a;
            public long b;
        }

        private ref struct RefStructWithReferences
        {
            public int a;
            public object b;

Review comment (suggestion):
The test doesn't verify that the returned memory is actually aligned to the requested boundary (16 bytes). Consider adding an assertion like `Assert.True((long)memory % 16 == 0)` to ensure the alignment requirement is met.

Code after:
        {
            IntPtr memory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 32);

            // Validate that the allocation succeeded
            Assert.NotEqual(memory, IntPtr.Zero);

            // Validate that the memory is zeroed out
            Assert.True(new Span<byte>((void*)memory, 32).SequenceEqual(new byte[32]));
        }

        [Fact]
        public static void AllocateTypeAssociatedMemoryAlignedInvalidArguments()
        {
            Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(null, 10, 1); });
            Assert.Throws<ArgumentOutOfRangeException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1, 1); });
            Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 10, 0); });
            Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 10, 3); });
        }

        [Theory]
        [InlineData(1)]
        [InlineData(2)]
        [InlineData(4)]
        [InlineData(8)]      // .NET largest natural alignment
        [InlineData(16)]     // V128, typical max_align_t
        [InlineData(32)]     // V256
        [InlineData(64)]     // V512, typical cache line size
        [InlineData(128)]    // less typical cache line size
        [InlineData(512)]    // historical disk sector size
        [InlineData(4096)]   // typical disk sector and page size
        [InlineData(16384)]  // less typical disk sector and page size
        [InlineData(65536)]  // typical texture and buffer alignment for GPU
        [InlineData(262144)] // typical non-temporal chunk alignment
        public static unsafe void AllocateTypeAssociatedMemoryAlignedValidArguments(int alignment)
        {
            IntPtr memory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 32, alignment);

            // Validate that the allocation succeeded
            Assert.NotEqual(memory, IntPtr.Zero);

            // Validate that the memory is zeroed out
            Assert.True(new Span<byte>((void*)memory, 32).SequenceEqual(new byte[32]));

            // Validate that the memory is aligned
            Assert.True((memory % alignment) == 0);
        }

#pragma warning disable CS0649
        [StructLayoutAttribute(LayoutKind.Sequential)]
        private struct StructWithoutReferences
        {