Exemple #1
0
        /// <summary>
        /// Sort the collection.
        /// </summary>
        /// <typeparam name="T">The type of each element.</typeparam>
        /// <param name="this">(<see langword="this"/> parameter) The collection.</param>
        public static void Sort <T>(this IBag <T> @this) where T : notnull
        {
            var list = @this.ToList();

            list.Sort();
            @this.Clear();
            @this.AddRange(list);
        }
Exemple #2
0
        private static void TestBagOperation(IBag <Order> bag)
        {
            var total = 0;

            for (var i = 0; i < 1000; i++)
            {
                bag.Add(new Order {
                    Id = i + 1
                }, i + 1);
                total += i + 1;
            }

            var itemNumber = 0;

            foreach (var item in bag)
            {
                itemNumber++;
            }
            Assert.Equal(itemNumber, bag.Count);

            Assert.Equal(total, bag.Count);

            for (var i = 0; i < 1000; i++)
            {
                var count = bag[new Order {
                                    Id = i + 1
                                }];
                Assert.Equal(count, i + 1);
                Assert.True(bag.Contains(new Order {
                    Id = i + 1
                }));
            }

            for (var i = 2000; i < 3000; i++)
            {
                Assert.False(bag.Contains(new Order {
                    Id = i
                }));
            }

            Assert.Equal(1000, bag.ToUnique().Count);

            for (var i = 400; i < 600; i++)
            {
                Assert.True(bag.Remove(new Order {
                    Id = i
                }, 100));
                total -= 100;
                Assert.Equal(total, bag.Count);
            }

            Assert.Equal(1000, bag.ToUnique().Count);

            bag.Clear();
            Assert.Equal(0, bag.Count);
        }