Example #1
0
        // TODO: Test
        /// <summary>
        ///     Shuffles the items in the list according to the specified random source.
        /// </summary>
        /// <param name="list">The list to shuffle.</param>
        /// <param name="random">The random source.</param>
        public static void Shuffle <T>(this SCG.IList <T> list, Random random)
        {
            #region Code Contracts

            // Argument must be non-null
            Requires(list != null, ArgumentMustBeNonNull);

            // List must be non-read-only
            Requires(!list.IsReadOnly, CollectionMustBeNonReadOnly);

            // The elements are the same
            Ensures(list.HasSameAs(OldValue(list.ToList())));

            #endregion

            if (random == null)
            {
                random = new Random(); // TODO: Use C5.Random?
            }

            var n = list.Count;
            while (--n > 0)
            {
                list.Swap(random.Next(n + 1), n);
            }
        }
Example #2
0
        public void IList_Generic_IndexOf_ReturnsFirstMatchingValue(int count)
        {
            if (!IsReadOnly && !AddRemoveClear_ThrowsNotSupported)
            {
                SCG.IList <T> list = GenericIListFactory(count);
                foreach (T duplicate in list.ToList()) // hard copies list to circumvent enumeration error
                {
                    list.Add(duplicate);
                }
                List <T> expectedList = list.ToList();

                Assert.All(Enumerable.Range(0, count), (index =>
                                                        Assert.Equal(index, list.IndexOf(expectedList[index]))
                                                        ));
            }
        }