Ejemplo n.º 1
0
    public void ChangeQuantity_100000_OnSameProduct_ShouldWorkFast()
    {
        // Arrange
        IProductStock  stock    = new Instock();
        const int      count    = 100000;
        List <Product> products = new List <Product>(100000);

        for (int i = 0; i < count; i++)
        {
            Product p = new Product(i.ToString(), i, i);
            stock.Add(p);
            products.Add(p);
        }

        // Act & Assert
        Stopwatch sw   = Stopwatch.StartNew();
        Random    rand = new Random();

        for (int i = 0; i < 50000; i++)
        {
            int qty = rand.Next(50, 10000);
            stock.ChangeQuantity(products[576].Label, qty);
            Assert.AreEqual(products[576].Quantity, qty);
        }
        sw.Stop();
        Assert.Less(sw.ElapsedMilliseconds, 200);
    }
Ejemplo n.º 2
0
    static void Main(string[] args)
    {
        IProductStock stock    = new Instock();
        Product       product1 = new Product("SalamShpekov", 3.50, 560);
        Product       product2 = new Product("BekonNov", 2.65, 43);
        Product       product3 = new Product("MayonezaNiskomaslena", 1.30, 13);
        Product       product4 = new Product("Ketchup", 1.80, 73);
        Product       product5 = new Product("Jelqzo", 0.70, 130);
        Product       product6 = new Product("Belina", .75, 240);
        Product       product7 = new Product("Sirene", .77, 30);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        stock.Add(product4);
        stock.Add(product5);
        stock.Add(product6);
        stock.Add(product7);

        stock.ChangeQuantity(product4.Label, 50);
        stock.ChangeQuantity(product7.Label, 50);
        stock.ChangeQuantity(product3.Label, 50);

        //Assert
        List <Product> expected = new List <Product>()
        {
            product4, product7, product3
        };
        List <Product> actual = stock.FindAllByQuantity(50).ToList();

        ;
    }
Ejemplo n.º 3
0
    public void FindAllByQuantity_On_WrongArgument_ShouldReturnEmpty_Enumeration()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("SalamShpekov", 3.50, 50);
        Product       product2 = new Product("BekonNov", 2.65, 43);
        Product       product3 = new Product("MayonezaNiskomaslena", 1.30, 13);
        Product       product4 = new Product("Ketchup", 1.80, 73);
        Product       product5 = new Product("Jelqzo", 0.70, 20);
        Product       product6 = new Product("Belina", .75, 50);
        Product       product7 = new Product("Sirene", .77, 50);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        stock.Add(product4);
        stock.Add(product5);
        stock.Add(product6);
        stock.Add(product7);
        stock.ChangeQuantity("Sirene", 5);
        stock.ChangeQuantity("SalamShpekov", 5);
        stock.ChangeQuantity("Belina", 5);
        //Assert
        List <Product> expected = new List <Product>();
        List <Product> actual   = stock.FindAllByQuantity(500).ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
Ejemplo n.º 4
0
    public void FindAllByQuantity_On_100000_Elements_ShouldWorkFast()
    {
        // Arrange
        IProductStock stock = new Instock();
        const int     count = 100000;
        Dictionary <int, List <Product> > products
            = new Dictionary <int, List <Product> >();

        for (int i = 0; i < count; i += 400)
        {
            for (int j = 0; j < 400; j++)
            {
                Product p = new Product((i + j).ToString(), j, j);
                if (!products.ContainsKey(j))
                {
                    products[j] = new List <Product>();
                }
                stock.Add(p);
                products[j].Add(p);
            }
        }
        Stopwatch sw = Stopwatch.StartNew();

        // Act

        // Assert
        for (int i = 0; i < 100; i++)
        {
            CollectionAssert.AreEqual(products[i], stock.FindAllByQuantity(i));
        }
        sw.Stop();

        Assert.Less(sw.ElapsedMilliseconds, 100);
    }
    public void FindFirstMostExpensiveProducts_Should_Return_CorrectEnumeration()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product2 = new Product("BekonNov", 2.65, 43);
        Product       product3 = new Product("MayonezaNiskomaslena", 1.30, 13);
        Product       product1 = new Product("SalamShpekov", 3.50, 50);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        //Assert
        List <Product> expected = new List <Product>()
        {
            product1, product2
        };

        CollectionAssert.AreEqual(expected, stock.FindFirstMostExpensiveProducts(2));
        expected = new List <Product>()
        {
            product1
        };
        CollectionAssert.AreEqual(expected, stock.FindFirstMostExpensiveProducts(1));
    }
Ejemplo n.º 6
0
    public void FindAtIndex_On_100_000_Elements_ShouldWorkFast()
    {
        // Arrange
        IProductStock  stock  = new Instock();
        const int      count  = 100000;
        List <Product> people = new List <Product>();

        for (int i = 0; i < count; i++)
        {
            people.Add(new Product(i.ToString(), i, i));
            stock.Add(people[i]);
        }

        // Act
        Stopwatch sw   = Stopwatch.StartNew();
        Random    rand = new Random();

        for (int i = 0; i < 50000; i++)
        {
            int rnd = rand.Next(0, count - 1);
            Assert.AreEqual(people[rnd], stock.Find(rnd));
        }
        // Assert
        sw.Stop();
        Assert.Less(sw.ElapsedMilliseconds, 200);
    }
Ejemplo n.º 7
0
    public void ChangeQuantity_On_Multiple_Elements_ShouldWorkCorrectly()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("SalamShpekov", 3.50, 560);
        Product       product2 = new Product("BekonNov", 2.65, 43);
        Product       product3 = new Product("MayonezaNiskomaslena", 1.30, 13);
        Product       product4 = new Product("Ketchup", 1.80, 73);
        Product       product5 = new Product("Jelqzo", 0.70, 130);
        Product       product6 = new Product("Belina", .75, 240);
        Product       product7 = new Product("Sirene", .77, 30);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        stock.Add(product4);
        stock.Add(product5);
        stock.Add(product6);
        stock.Add(product7);

        stock.ChangeQuantity(product4.Label, 50);
        stock.ChangeQuantity(product7.Label, 50);
        stock.ChangeQuantity(product3.Label, 50);

        //Assert
        List <Product> expected = new List <Product>()
        {
            product4, product7, product3
        };
        List <Product> actual = stock.FindAllByQuantity(50).ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
Ejemplo n.º 8
0
    public void FindAllByPrice_On100000ElementsWithRandomPrice_ShouldWorkFast()
    {
        // Arrange
        IProductStock  stock    = new Instock();
        const int      count    = 100_000;
        List <Product> expected = new List <Product>();

        Random random = new Random();

        for (int i = 0; i < count; i++)
        {
            int     price = random.Next(5, 30);
            Product p     = new Product(i.ToString(), price, 25);
            if (price == 21)
            {
                expected.Add(p);
            }
            stock.Add(p);
        }

        Stopwatch sw = Stopwatch.StartNew();

        // Act
        IEnumerable <Product> FindAllByPrice() => stock.FindAllByPrice(21);

        // Assert
        CollectionAssert.AreEqual(expected, FindAllByPrice());
        sw.Stop();
        Assert.Less(sw.ElapsedMilliseconds, 25);
    }
Ejemplo n.º 9
0
    public void FindAllInPriceRange_OnLargeRange_ShouldWorkFast()
    {
        // Arrange
        IProductStock org      = new Instock();
        const int     count    = 100_000;
        int           expected = 0;

        Random random = new Random();

        for (int i = 0; i < count; i++)
        {
            int price = random.Next(10, 50000);
            if (price > 105 && price <= 10000)
            {
                expected++;
            }

            org.Add(new Product(i.ToString(), price, i));
        }

        Stopwatch sw = Stopwatch.StartNew();

        // Act
        IEnumerable <Product> FindInPriceRange() => org.FindAllInRange(105, 10000);

        // Assert
        Assert.AreEqual(expected, FindInPriceRange().Count());

        sw.Stop();
        Assert.Less(sw.ElapsedMilliseconds, 300);
    }
    public void Contains_100000_Elements_ShouldExecuteFast()
    {
        // Arrange
        IProductStock        stock    = new Instock();
        const int            count    = 100000;
        LinkedList <Product> products = new LinkedList <Product>();

        for (int i = 0; i < count; i++)
        {
            products.AddLast(new Product(i.ToString(), i, i));
            stock.Add(products.Last.Value);
        }

        // Act
        Stopwatch sw = Stopwatch.StartNew();
        LinkedListNode <Product> node = products.First;

        while (node != null)
        {
            Assert.True(stock.Contains(node.Value));
            node = node.Next;
        }

        sw.Stop();
        Assert.Less(sw.ElapsedMilliseconds, 250);
    }
Ejemplo n.º 11
0
    public void FindFirstByAlphabeticalOrder_Should_ReturnCorrectEnumeration()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("Abra", 3.50, 50);
        Product       product2 = new Product("Bobar", 2.65, 43);
        Product       product3 = new Product("Caza", 1.30, 13);
        Product       product4 = new Product("Darfield", 1.80, 73);
        Product       product5 = new Product("Eil*", 0.70, 20);
        Product       product6 = new Product("Flen", .75, 50);
        Product       product7 = new Product("Giilqzo", .77, 50);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        stock.Add(product4);
        stock.Add(product5);
        stock.Add(product6);
        stock.Add(product7);
        //Assert
        List <Product> expected = new List <Product>()
        {
            product1, product2, product3, product4, product5, product6, product7
        };
        List <Product> actual = stock.FindFirstByAlphabeticalOrder(stock.Count).ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
Ejemplo n.º 12
0
    public void FindAllByPriceRange_On_ExistingRange_ShouldReturn_Correct_Enumeration()
    {
        //Arrange
        IProductStock  stock    = new Instock();
        Product        product1 = new Product("SalamShpekov", 3.50, 50);
        Product        product2 = new Product("BekonNov", 2.65, 43);
        Product        product3 = new Product("MayonezaNiskomaslena", 1.30, 13);
        Product        product4 = new Product("Ketchup", 1.80, 73);
        Product        product5 = new Product("Jelqzo", 0.70, 20);
        Product        product6 = new Product("Belina", .75, 50);
        Product        product7 = new Product("Sirene", .77, 50);
        List <Product> expected = new List <Product>()
        {
            product7, product6
        };

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        stock.Add(product4);
        stock.Add(product5);
        stock.Add(product6);
        stock.Add(product7);

        //Assert
        List <Product> actual = stock.FindAllInRange(0.70, 0.77).ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
Ejemplo n.º 13
0
    public void FindByLabel_Shoul_WorkFast_On_100000_Products()
    {
        // Arrange
        IProductStock stock = new Instock();
        const int     count = 100000;
        List <KeyValuePair <string, Product> > names = new List <KeyValuePair <string, Product> >(100000);

        for (int i = 0; i < count; i++)
        {
            Product p = new Product(i.ToString(), i, i);
            stock.Add(p);
            names.Add(new KeyValuePair <string, Product>(p.Label, p));
        }

        // Act
        Stopwatch sw = Stopwatch.StartNew();

        // Assert
        for (int i = 0; i < count; i++)
        {
            Assert.True(stock.FindByLabel(names[i].Key) == names[i].Value);
        }

        sw.Stop();
        Assert.Less(sw.ElapsedMilliseconds, 230);
    }
Ejemplo n.º 14
0
    public void Enumerator_ShouldReturn_ProductsInInsertionOrder_After_Adding_Multiple()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("SalamShpekov", 3.50, 50);
        Product       product2 = new Product("BekonNov", 2.65, 43);
        Product       product3 = new Product("MayonezaNiskomaslena", 1.30, 13);
        Product       product4 = new Product("Ketchup", 1.80, 73);
        Product       product5 = new Product("Jelqzo", 0.70, 20);
        Product       product6 = new Product("Belina", .75, 50);
        Product       product7 = new Product("Sirene", .77, 50);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        stock.Add(product4);
        stock.Add(product5);
        stock.Add(product6);
        stock.Add(product7);
        //Assert
        List <Product> expected = new List <Product>()
        {
            product1, product2,
            product3, product4,
            product5, product6,
            product7,
        };
        List <Product> actual = stock.Take(stock.Count).ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
Ejemplo n.º 15
0
    public void FindFirstMostExpensiveProducts_ShouldReturn_OrderedEnumeration()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("SalamShpekov", 3.50, 50);
        Product       product2 = new Product("BekonNov", 2.65, 43);
        Product       product3 = new Product("MayonezaNiskomaslena", 1.30, 13);
        Product       product4 = new Product("Ketchup", 1.80, 73);
        Product       product5 = new Product("Jelqzo", 0.70, 20);
        Product       product6 = new Product("Belina", .75, 50);
        Product       product7 = new Product("Sirene", .77, 50);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        stock.Add(product4);
        stock.Add(product5);
        stock.Add(product6);
        stock.Add(product7);
        //Assert
        List <Product> expected = new List <Product>()
        {
            product1, product2, product4, product3, product7, product6, product5
        };
        List <Product> actual = stock.FindFirstMostExpensiveProducts(stock.Count).ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
 public void FindFirstByAlphabeticalOrder_On_EmptyStock_ShouldReturn_EmptyEnumeration()
 {
     //Arrange
     IProductStock stock = new Instock();
     //Act
     //Assert
     CollectionAssert.AreEqual(new List<Product>(), stock.FindFirstByAlphabeticalOrder(0));
 }
Ejemplo n.º 17
0
    static void Main(string[] args)
    {
        var instock = new Instock();

        instock.Add(new Product("bob1", 23.1, 2));
        instock.Add(new Product("bob2", 23.5, 2));
        instock.Add(new Product("bob3", 21.2, 2));
        System.Console.WriteLine(string.Join(" - ", instock.FindAllInRange(21.3, 24).Select(a => a.Label)));
    }
Ejemplo n.º 18
0
    public void Contains_On_Empty_Collection_ShouldReturnFalse()
    {
        //Arrange
        IProductStock stock = new Instock();

        //Act
        //Assert
        Assert.IsFalse(stock.Contains(new Product("Bakar", 5.5, 15)));
    }
Ejemplo n.º 19
0
    public void Enumerator_ShouldReturn_EmptyEnumeration_On_Empty_Stock()
    {
        //Arrange
        IProductStock stock = new Instock();
        //Act
        //Assert
        List <Product> expected = new List <Product>();
        List <Product> actual   = stock.Take(stock.Count).ToList();

        CollectionAssert.AreEqual(expected, actual);
    }
Ejemplo n.º 20
0
    public void Add_Single_Product_ShouldBeAt_0_Index()
    {
        //Arrange
        IProductStock stock   = new Instock();
        Product       product = new Product("Salam", 2.50, 50);

        //Act
        stock.Add(product);
        //Assert
        Assert.AreEqual(product, stock.Find(0), "Added item should be on index 0");
    }
    public void ChangeQuantity_On_NonExisting_Product_ShouldThrow()
    {
        //Arrange
        IProductStock stock = new Instock();
        Product product1 = new Product("SalamShpekov", 3.50, 50);
        Product product2 = new Product("BekonNov", 2.65, 43);
        Product product3 = new Product("MayonezaNiskomaslena", 1.30, 13);

        //Act

        //Assert
        Assert.Throws<ArgumentException>(() => stock.ChangeQuantity("Barekov", 0));
    }
Ejemplo n.º 22
0
    public void Add_Many_Count_ShouldWork()
    {
        // Arrange
        IProductStock stock = new Instock();
        const int     count = 100_000;

        // Act & Assert
        for (int i = 0; i < count; i++)
        {
            Assert.AreEqual(i, stock.Count);
            stock.Add(new Product(i.ToString(), i, i));
        }
    }
    public void Add_SingleElement_Contains_ShouldReturnTrue()
    {
        //Arrange
        IProductStock stock   = new Instock();
        Product       product = new Product("Pizza", 4.30, 1510);

        //Act
        stock.Add(product);

        //Assert
        var actual = stock.Contains(product);

        Assert.True(actual, "Contains on existing element return true");
    }
    public void Contains_On_Non_ExistingElement_ShouldReturnFalse()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product  = new Product("Pizza", 4.30, 1510);
        Product       product2 = new Product("Rizza", 4.30, 1510);

        //Act
        stock.Add(product);

        //Assert
        var actual = stock.Contains(product2);

        Assert.False(actual, "Contains on non-existing element should return false");
    }
Ejemplo n.º 25
0
    public void Add_MultipleElements_Should_Increase_Count()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("Getter", 20.5, 15);
        Product       product2 = new Product("OtherPRoduct", 206.1, 67);
        Product       product3 = new Product("50CentPoster", 50, 50);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        //Assert
        Assert.AreEqual(3, stock.Count, "Count should increase with every item.");
    }
Ejemplo n.º 26
0
    public void FindByLabel_NoExistingProduct_ShouldThrow()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("SalamShpekov", 3.50, 50);
        Product       product2 = new Product("BekonNov", 2.65, 43);
        Product       product3 = new Product("MayonezaNiskomaslena", 1.30, 13);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        //Assert
        Assert.Throws <ArgumentException>(() => stock.FindByLabel("BoroTreti"));
    }
Ejemplo n.º 27
0
    public void Find_Product_WrongIndex_ShouldThrow()
    {
        //Arrange
        IProductStock stock   = new Instock();
        Product       product = new Product("Salam", 2.50, 50);

        //Act
        stock.Add(product);
        //Assert
        Assert.Throws <IndexOutOfRangeException>(() =>
                                                 stock.Find(-5)
                                                 , "Accessing index -5 should not be possible");
        Assert.Throws <IndexOutOfRangeException>(() =>
                                                 stock.Find(1)
                                                 , "Accessing index 1 when 1 element present should not be possile");
    }
Ejemplo n.º 28
0
    static void Main(string[] args)
    {
        Instock products = new Instock();

        var pr1 = new Product("salam", 10, 2);
        var pr2 = new Product("masla", 10, 1);
        var pr3 = new Product("bob", 10, 3);

        products.Add(pr1);
        products.Add(pr2);
        products.Add(pr3);

        var res = products.FindAllByPrice(10);

        System.Console.WriteLine(   );
    }
Ejemplo n.º 29
0
    public void Find_Product_On_ExistingProduct_ShouldWorkCorrectly()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("Balsam", 5.3, 12);
        Product       product2 = new Product("Korab", 12.6, 1255);
        Product       product3 = new Product("Meduza", 53.1, 55);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        //Assert
        Assert.AreSame(product3, stock.Find(2));
        Assert.AreNotSame(product1, stock.Find(1));
        Assert.AreSame(product1, stock.Find(0));
    }
Ejemplo n.º 30
0
    public void FindFirstMostExpensiveProducts_On_WrongArgumentPassed_ShouldThrow()
    {
        //Arrange
        IProductStock stock    = new Instock();
        Product       product1 = new Product("SalamShpekov", 3.50, 50);
        Product       product2 = new Product("BekonNov", 2.65, 43);
        Product       product3 = new Product("MayonezaNiskomaslena", 1.30, 13);

        //Act
        stock.Add(product1);
        stock.Add(product2);
        stock.Add(product3);
        //Assert
        Assert.AreEqual(3, stock.Count);
        Assert.Throws <ArgumentException>(() => {
            stock.FindFirstMostExpensiveProducts(5).First();
        });
    }