Beispiel #1
0
        public void Add3Items_Last0Items_ShouldReturn0Items()
        {
            // Arrange
            var items = FirstLastListFactory.Create <int>();

            items.Add(5);
            items.Add(10);
            items.Add(-2);

            // Act
            var returnedItems = items.Last(0).ToList();

            // Assert
            var expectedItems = new int[] { };

            CollectionAssert.AreEqual(expectedItems, returnedItems);
        }
    public void Add5Items_RemoveNonExistingItem()
    {
        // Arrange
        var items = FirstLastListFactory.Create <int>();

        items.Add(5);
        items.Add(10);
        items.Add(-2);
        items.Add(8);
        items.Add(1);

        // Act
        var countRemoved = items.RemoveAll(0);

        // Assert
        Assert.AreEqual(0, countRemoved);
        Assert.AreEqual(5, items.Count);
    }
    public void Add3ItemsDuplicates_First3Items_ShouldReturnFirst3Items()
    {
        // Arrange
        var items = FirstLastListFactory.Create <string>();

        items.Add("coffee");
        items.Add("coffee");
        items.Add("milk");

        // Act
        var returnedItems = items.First(3).ToList();

        // Assert
        var expectedItems = new string[] {
            "coffee", "coffee", "milk"
        };

        CollectionAssert.AreEqual(expectedItems, returnedItems);
    }
    public void Add5Items_Max3Items_ShouldReturnMax3Items()
    {
        // Arrange
        var items = FirstLastListFactory.Create <int>();

        items.Add(5);
        items.Add(10);
        items.Add(-2);
        items.Add(8);
        items.Add(1);

        // Act
        var returnedItems = items.Max(3).ToList();

        // Assert
        var expectedItems = new int[] { 10, 8, 5 };

        CollectionAssert.AreEqual(expectedItems, returnedItems);
    }
Beispiel #5
0
    public void Add5ItemsDuplicates_Max4Items_ShouldReturnMax4Items()
    {
        // Arrange
        var items = FirstLastListFactory.Create <Product>();

        items.Add(new Product(0.50m, "coffee"));
        items.Add(new Product(1.20m, "mint drops"));
        items.Add(new Product(1.20m, "beer"));
        items.Add(new Product(0.50m, "candy"));
        items.Add(new Product(1.20m, "cola"));

        // Act
        var returnedItems = items.Max(4).Select(p => p.Title).ToList();

        // Assert
        var expectedItems = new string[] { "mint drops", "beer", "cola", "coffee" };

        CollectionAssert.AreEqual(expectedItems, returnedItems);
    }
Beispiel #6
0
    static void Main(string[] args)
    {
        FirstLastList <int> list = new FirstLastList <int>();

        /*list.Add(5);
         * list.Add(10);
         * list.Add(-2);
         * list.Add(10);
         * list.Add(7);
         * list.Add(70);*/


        /*Console.WriteLine(list.Count);
         *
         * var removed = list.RemoveAll(10);
         * var returnedItems = list.First(3).ToList();
         *
         * Console.WriteLine(removed);
         * Console.WriteLine(string.Join(" ", returnedItems));
         * //Console.WriteLine("List: " + string.Join(" ", list.Count));*/

        var items = FirstLastListFactory.Create <Product>();

        items.Add(new Product(1.11m, "first"));
        items.Add(new Product(0.50m, "coffee"));
        items.Add(new Product(2.50m, "chocolate"));
        items.Add(new Product(1.20m, "mint drops"));
        items.Add(new Product(1.20m, "beer"));
        items.Add(new Product(0.50m, "candy"));
        items.Add(new Product(1.20m, "cola"));

        var countRemoved  = items.RemoveAll(new Product(1.20m, null));
        var returnedItems = items.Last(3).Select(p => p.Title).ToList();

        Console.WriteLine(string.Join(" ", returnedItems));

        // Assert
        //Assert.AreEqual(3, countRemoved);
        var expectedItems = new string[] { "candy", "chocolate", "coffee" };
        //CollectionAssert.AreEqual(expectedItems, returnedItems);
    }
    public void Add6Items_Remove2Items_Max3Items()
    {
        // Arrange
        var items = FirstLastListFactory.Create <int>();

        items.Add(5);
        items.Add(77);
        items.Add(10);
        items.Add(-2);
        items.Add(10);
        items.Add(7);

        // Act
        var countRemoved  = items.RemoveAll(10);
        var returnedItems = items.Max(3).ToList();

        // Assert
        Assert.AreEqual(2, countRemoved);
        var expectedItems = new int[] { 77, 7, 5 };

        CollectionAssert.AreEqual(expectedItems, returnedItems);
    }
    public void Add7ItemsDuplicates_Remove3Items_Max2Items()
    {
        // Arrange
        var items = FirstLastListFactory.Create <Product>();

        items.Add(new Product(0.50m, "coffee"));
        items.Add(new Product(1.20m, "mint drops"));
        items.Add(new Product(1.20m, "beer"));
        items.Add(new Product(2.22m, "chocolate"));
        items.Add(new Product(0.50m, "candy"));
        items.Add(new Product(0.01m, "cent"));
        items.Add(new Product(1.20m, "cola"));

        // Act
        var countRemoved  = items.RemoveAll(new Product(1.20m, null));
        var returnedItems = items.Max(3).Select(p => p.Title).ToList();

        // Assert
        Assert.AreEqual(3, countRemoved);
        var expectedItems = new string[] { "chocolate", "coffee", "candy" };

        CollectionAssert.AreEqual(expectedItems, returnedItems);
    }
    public void ComplexTest_AllOperationsProducts()
    {
        var items = FirstLastListFactory.Create <Product>();

        Assert.AreEqual(0, items.Count);

        items.Add(new Product(0.50m, "coffee"));
        Assert.AreEqual(1, items.Count);
        var first = items.First(1).FirstOrDefault();
        var last  = items.Last(1).FirstOrDefault();
        var min   = items.Min(1).FirstOrDefault();
        var max   = items.Max(1).FirstOrDefault();

        Assert.AreEqual("coffee", first.Title);
        Assert.AreEqual("coffee", last.Title);
        Assert.AreEqual("coffee", min.Title);
        Assert.AreEqual("coffee", max.Title);

        items.Add(new Product(0.50m, "candy"));
        Assert.AreEqual(2, items.Count);
        first = items.First(1).FirstOrDefault();
        last  = items.Last(1).FirstOrDefault();
        min   = items.Min(1).FirstOrDefault();
        max   = items.Max(1).FirstOrDefault();
        Assert.AreEqual("coffee", first.Title);
        Assert.AreEqual("candy", last.Title);
        Assert.AreEqual("coffee", min.Title);
        Assert.AreEqual("coffee", max.Title);

        items.Add(new Product(2.99m, "chocolate"));
        Assert.AreEqual(3, items.Count);
        first = items.First(1).FirstOrDefault();
        last  = items.Last(1).FirstOrDefault();
        min   = items.Min(1).FirstOrDefault();
        max   = items.Max(1).FirstOrDefault();
        Assert.AreEqual("coffee", first.Title);
        Assert.AreEqual("chocolate", last.Title);
        Assert.AreEqual("coffee", min.Title);
        Assert.AreEqual("chocolate", max.Title);

        items.Add(new Product(0.50m, "candy"));
        Assert.AreEqual(4, items.Count);
        first = items.First(1).FirstOrDefault();
        last  = items.Last(1).FirstOrDefault();
        min   = items.Min(1).FirstOrDefault();
        max   = items.Max(1).FirstOrDefault();
        Assert.AreEqual("coffee", first.Title);
        Assert.AreEqual("candy", last.Title);
        Assert.AreEqual("coffee", min.Title);
        Assert.AreEqual("chocolate", max.Title);

        var countRemoved =
            items.RemoveAll(new Product(0.50m, "some product"));

        Assert.AreEqual(3, countRemoved);
        Assert.AreEqual(1, items.Count);
        first = items.First(1).FirstOrDefault();
        last  = items.Last(1).FirstOrDefault();
        min   = items.Min(1).FirstOrDefault();
        max   = items.Max(1).FirstOrDefault();
        Assert.AreEqual("chocolate", first.Title);
        Assert.AreEqual("chocolate", last.Title);
        Assert.AreEqual("chocolate", min.Title);
        Assert.AreEqual("chocolate", max.Title);

        items.Add(new Product(0.50m, "candy"));
        Assert.AreEqual(2, items.Count);
        first = items.First(1).FirstOrDefault();
        last  = items.Last(1).FirstOrDefault();
        min   = items.Min(1).FirstOrDefault();
        max   = items.Max(1).FirstOrDefault();
        Assert.AreEqual("chocolate", first.Title);
        Assert.AreEqual("candy", last.Title);
        Assert.AreEqual("candy", min.Title);
        Assert.AreEqual("chocolate", max.Title);

        items.Clear();
        Assert.AreEqual(0, items.Count);

        items.Add(new Product(2.50m, "beer"));
        items.Add(new Product(7.35m, "vodka"));
        items.Add(new Product(0.50m, "coffee"));
        items.Add(new Product(0.50m, "candy"));
        Assert.AreEqual(4, items.Count);
        var first2 = items.First(2).Select(p => p.Title).ToList();
        var last2  = items.Last(2).Select(p => p.Title).ToList();
        var min2   = items.Min(2).Select(p => p.Title).ToList();
        var max2   = items.Max(2).Select(p => p.Title).ToList();

        CollectionAssert.AreEqual(new string[] { "beer", "vodka" }, first2);
        CollectionAssert.AreEqual(new string[] { "candy", "coffee" }, last2);
        CollectionAssert.AreEqual(new string[] { "coffee", "candy" }, min2);
        CollectionAssert.AreEqual(new string[] { "vodka", "beer" }, max2);

        var removedCount = items.RemoveAll(new Product(12345, null));

        Assert.AreEqual(0, removedCount);

        removedCount = items.RemoveAll(new Product(7.35m, null));
        Assert.AreEqual(1, removedCount);

        removedCount = items.RemoveAll(new Product(2.50m, null));
        Assert.AreEqual(1, removedCount);

        Assert.AreEqual(2, items.Count);
        first2 = items.First(2).Select(p => p.Title).ToList();
        last2  = items.Last(2).Select(p => p.Title).ToList();
        min2   = items.Min(2).Select(p => p.Title).ToList();
        max2   = items.Max(2).Select(p => p.Title).ToList();
        CollectionAssert.AreEqual(new string[] { "coffee", "candy" }, first2);
        CollectionAssert.AreEqual(new string[] { "candy", "coffee" }, last2);
        CollectionAssert.AreEqual(new string[] { "coffee", "candy" }, min2);
        CollectionAssert.AreEqual(new string[] { "coffee", "candy" }, max2);

        removedCount = items.RemoveAll(new Product(0.50m, null));
        Assert.AreEqual(2, removedCount);
        Assert.AreEqual(0, items.Count);

        items.Add(new Product(0.50m, "milk"));
        items.Add(new Product(1.20m, "amstel"));
        items.Add(new Product(1.20m, "xxx"));
        Assert.AreEqual(3, items.Count);
        first2 = items.First(2).Select(p => p.Title).ToList();
        last2  = items.Last(2).Select(p => p.Title).ToList();
        min2   = items.Min(2).Select(p => p.Title).ToList();
        max2   = items.Max(2).Select(p => p.Title).ToList();
        CollectionAssert.AreEqual(new string[] { "milk", "amstel" }, first2);
        CollectionAssert.AreEqual(new string[] { "xxx", "amstel" }, last2);
        CollectionAssert.AreEqual(new string[] { "milk", "amstel" }, min2);
        CollectionAssert.AreEqual(new string[] { "amstel", "xxx" }, max2);

        removedCount = items.RemoveAll(new Product(0.50m, null));
        Assert.AreEqual(1, removedCount);
        Assert.AreEqual(2, items.Count);

        removedCount = items.RemoveAll(new Product(1.20m, null));
        Assert.AreEqual(2, removedCount);
        Assert.AreEqual(0, items.Count);

        items.Add(new Product(0.50m, "coffee"));
        Assert.AreEqual(1, items.Count);
        first = items.First(1).FirstOrDefault();
        last  = items.Last(1).FirstOrDefault();
        min   = items.Min(1).FirstOrDefault();
        max   = items.Max(1).FirstOrDefault();
        Assert.AreEqual("coffee", first.Title);
        Assert.AreEqual("coffee", last.Title);
        Assert.AreEqual("coffee", min.Title);
        Assert.AreEqual("coffee", max.Title);
    }