コード例 #1
0
        public void TestIListExtensions()
        {
            // Shuffle, Last Index
            {
                int[] values = new int[] { 1, 2, 3, 4, 5 };
                Assert.AreEqual(4, values.LastIndex());
                int[] shuffled = (int[])values.Clone();
                shuffled.Shuffle();
                Assert.AreNotEqual(values, shuffled);
            }

            // Swap
            {
                int[] values = new int[] { 1, 2, 3, 4, 5 };
                values.SwapAtIndex(0, 4);
                Assert.AreEqual(values, new int[] { 5, 2, 3, 4, 1 });
                values.Swap(1, 5);
                Assert.AreEqual(values, new int[] { 1, 2, 3, 4, 5 });
            }

            // First, Last, Random
            {
                int[] values = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                Assert.AreEqual(1, values.First());
                Assert.AreEqual(9, values.Last());

                // You see, random can end up hitting the same index... since it's random
                int a = values.Random();
                int b = -1;
                for (int i = 0; i < values.Length; ++i)
                {
                    b = values.Random();
                    if (a != b)
                    {
                        break;
                    }
                }
                Assert.AreNotEqual(a, b);
            }

            // Remove First/Last
            {
                List <int> values = new List <int>()
                {
                    1, 2, 3, 4
                };
                values.RemoveFirst();
                values.RemoveLast();
                Assert.AreEqual(2, values.First());
                Assert.AreEqual(3, values.Last());
            }

            // Has Index, AtIndexOrDefault
            {
                TestDataObject   a      = new TestDataObject("A", 1);
                TestDataObject[] values = new TestDataObject[]
                {
                    a
                };

                Assert.False(values.HasIndex(-1));
                Assert.True(values.HasIndex(0));
                Assert.False(values.HasIndex(1));

                Assert.AreEqual(a, values.AtIndexOrDefault(0));
                Assert.AreEqual(a, values.AtIndexOrDefault(5, a));
            }
        }