Repository: dotnet/runtime
File: src/libraries/System.Diagnostics.DiagnosticSource/tests/TagListTests.cs (C#)

Code before:
        }

        [Fact]
        public void TestEmptyTagListCopyTo()
        {
            TagList emptyList = new TagList();
            Assert.Equal(0, emptyList.Count);

            KeyValuePair<string, object?>[] emptyArray = Array.Empty<KeyValuePair<string, object?>>();
            emptyList.CopyTo(emptyArray, 0);
            emptyList.CopyTo(emptyArray.AsSpan());
            emptyList.CopyTo(emptyArray);

            KeyValuePair<string, object?>[] result = emptyList.ToArray();
            Assert.Empty(result);
        }

        [Fact]
        public void TestNegativeCases()
        {
            TagList list = new TagList { new KeyValuePair<string, object?>("1", 1), new KeyValuePair<string, object?>("2", 2) } ;
            KeyValuePair<string, object?> kvp = default;

            Assert.Throws<ArgumentOutOfRangeException>(() => kvp = list[2]);
            Assert.Throws<ArgumentOutOfRangeException>(() => list[2] = kvp);
            Assert.Throws<ArgumentOutOfRangeException>(() => kvp = list[-1]);
            Assert.Throws<ArgumentOutOfRangeException>(() => list[-2] = kvp);

            KeyValuePair<string, object?>[] array = new KeyValuePair<string, object?>[1];
            Assert.Throws<ArgumentException>(() => list.CopyTo(array.AsSpan()));
            Assert.Throws<ArgumentException>(() => list.CopyTo(array, 0));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.CopyTo(array, 1));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.CopyTo(array, -1));
            Assert.Throws<ArgumentNullException>(() => list.CopyTo(null, 0));

            Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, default));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(3, default));

            Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(2));
        }

        internal static void ValidateTags(in TagList tagList, KeyValuePair<string, object?>[] array)
        {
            Assert.True(tagList.Count <= array.Length);
            for (int i = 0; i < tagList.Count; i++)
            {
                Assert.Equal(array[i].Key, tagList[i].Key);
                Assert.Equal(array[i].Value, tagList[i].Value);
            }
        }

Review comment (suggestion):
The test should also verify that invalid `arrayIndex` values still throw exceptions even when the TagList is empty. According to standard collection semantics, operations like `emptyList.CopyTo(array, -1)` or `emptyList.CopyTo(largeArray, largeArray.Length + 1)` should throw `ArgumentOutOfRangeException`.

Consider adding test cases for:
- Empty TagList with negative arrayIndex
- Empty TagList with arrayIndex > array.Length

Code after:
        }

        [Fact]
        public void TestEmptyTagListCopyTo()
        {
            TagList emptyList = new TagList();
            Assert.Equal(0, emptyList.Count);

            KeyValuePair<string, object?>[] emptyArray = Array.Empty<KeyValuePair<string, object?>>();
            emptyList.CopyTo(emptyArray, 0);
            emptyList.CopyTo(emptyArray.AsSpan());
            emptyList.CopyTo(emptyArray);

            KeyValuePair<string, object?>[] nonEmptyArray = new KeyValuePair<string, object?>[3];
            emptyList.CopyTo(nonEmptyArray, 0);
            emptyList.CopyTo(nonEmptyArray, 2);
            emptyList.CopyTo(nonEmptyArray, 3);
            emptyList.CopyTo(nonEmptyArray.AsSpan());
            emptyList.CopyTo(nonEmptyArray);

            Assert.Throws<ArgumentOutOfRangeException>(() => emptyList.CopyTo(nonEmptyArray, -1));
            Assert.Throws<ArgumentOutOfRangeException>(() => emptyList.CopyTo(nonEmptyArray, 4));

            KeyValuePair<string, object?>[] result = emptyList.ToArray();
            Assert.Empty(result);
        }

        [Fact]
        public void TestNegativeCases()
        {
            TagList list = new TagList { new KeyValuePair<string, object?>("1", 1), new KeyValuePair<string, object?>("2", 2) } ;
            KeyValuePair<string, object?> kvp = default;

            Assert.Throws<ArgumentOutOfRangeException>(() => kvp = list[2]);
            Assert.Throws<ArgumentOutOfRangeException>(() => list[2] = kvp);
            Assert.Throws<ArgumentOutOfRangeException>(() => kvp = list[-1]);
            Assert.Throws<ArgumentOutOfRangeException>(() => list[-2] = kvp);

            KeyValuePair<string, object?>[] array = new KeyValuePair<string, object?>[1];
            Assert.Throws<ArgumentException>(() => list.CopyTo(array.AsSpan()));
            Assert.Throws<ArgumentException>(() => list.CopyTo(array, 0));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.CopyTo(array, 1));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.CopyTo(array, -1));
            Assert.Throws<ArgumentNullException>(() => list.CopyTo(null, 0));

            Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, default));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(3, default));

            Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(2));
        }