Пример #1
0
        public void TestPackThreeItemsOneDoesntFitInAnyBox()
        {
            var box1 = Factory.CreateBox("Le petite box", 300, 300, 10, 10, 296, 296, 8, 1000);
            var box2 = Factory.CreateBox("Le grande box", 3000, 3000, 100, 100, 2960, 2960, 80, 10000);

            var item1 = Factory.CreateItem("Item 1", 2500, 2500, 20, 2000, true);
            var item2 = Factory.CreateItem("Item 2", 25000, 2500, 20, 2000, true);
            var item3 = Factory.CreateItem("Item 3", 2500, 2500, 20, 2000, true);

            var packer = new Packer();

            packer.AddBox(box1);
            packer.AddBox(box2);
            packer.AddItem(item1);
            packer.AddItem(item2);
            packer.AddItem(item3);

            Exception exception = null;

            try
            {
                packer.Pack();
            }
            catch (Exception e)
            {
                exception = e;
            }

            Assert.IsType <ItemTooLargeException>(exception);
        }
Пример #2
0
        public void CanPackThreeItemsFitEasilyInLargerOfTwoBoxes()
        {
            var packer = new Packer();
            var box    = new Box()
            {
                Description = "Le Petite Box",
                OuterWidth  = 300,
                OuterLength = 300,
                OuterDepth  = 10,
                EmptyWeight = 10,
                InnerWidth  = 296,
                InnerLength = 296,
                InnerDepth  = 8,
                MaxWeight   = 1000
            };

            var box2 = new Box()
            {
                Description = "Le Grande Box",
                OuterWidth  = 3000,
                OuterLength = 3000,
                OuterDepth  = 100,
                EmptyWeight = 100,
                InnerWidth  = 2960,
                InnerLength = 2960,
                InnerDepth  = 80,
                MaxWeight   = 10000
            };

            var items = new ItemList();

            items.Insert(new Item()
            {
                Id = "Item 1", Description = "Item 1", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });
            items.Insert(new Item()
            {
                Id = "Item 2", Description = "Item 2", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });
            items.Insert(new Item()
            {
                Id = "Item 3", Description = "Item 3", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });

            packer.AddBox(box);
            packer.AddBox(box2);
            packer.AddItems(items);

            var packedBoxes = packer.Pack();

            Assert.AreEqual(1, packedBoxes.GetCount());
            Assert.AreEqual(3, packedBoxes.GetBest().GetItems().GetCount());
            Assert.AreEqual(box2, packedBoxes.GetBest().GetBox());
            Assert.AreEqual(6100, packedBoxes.GetBest().GetWeight());
        }
Пример #3
0
        public void TestIssue168()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Small", 85, 190, 230, 30, 85, 190, 230, 10000));
            packer.AddBox(Factory.CreateBox("Medium", 220, 160, 160, 50, 220, 160, 160, 10000));
            packer.AddItem(Factory.CreateItem("Item", 55, 85, 122, 350, false));

            var packedBoxes = packer.Pack().ToList();

            Assert.Single(packedBoxes);
            Assert.Equal("Small", packedBoxes[0].Box.Reference);
        }
Пример #4
0
        public void PackThreeItemsOneDoesntFitInAnyBoxThrowsTooLargeException()
        {
            var packer = new Packer();
            var box    = new Box()
            {
                Description = "Le Petite Box",
                OuterWidth  = 300,
                OuterLength = 300,
                OuterDepth  = 10,
                EmptyWeight = 10,
                InnerWidth  = 296,
                InnerLength = 296,
                InnerDepth  = 8,
                MaxWeight   = 1000
            };

            var box2 = new Box()
            {
                Description = "Le Grande Box",
                OuterWidth  = 3000,
                OuterLength = 3000,
                OuterDepth  = 100,
                EmptyWeight = 100,
                InnerWidth  = 2960,
                InnerLength = 2960,
                InnerDepth  = 80,
                MaxWeight   = 10000
            };

            var items = new ItemList();

            items.Insert(new Item()
            {
                Id = "Item 1", Description = "Item 1", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });
            items.Insert(new Item()
            {
                Id = "Item 2", Description = "Item 2", Width = 25000, Length = 25000, Depth = 20, Weight = 2000
            });
            items.Insert(new Item()
            {
                Id = "Item 3", Description = "Item 3", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });

            packer.AddBox(box);
            packer.AddBox(box2);
            packer.AddItems(items);

            Assert.Throws <ItemTooLargeException>(() => packer.Pack());
        }
Пример #5
0
        public void TestIssue117()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Box A", 36, 8, 3, 0, 36, 8, 3, 2));
            packer.AddBox(Factory.CreateBox("Box B", 36, 8, 8, 0, 36, 8, 8, 2));
            packer.AddItem(Factory.CreateItem("Item 1", 35, 7, 2, 1, false));
            packer.AddItem(Factory.CreateItem("Item 2", 6, 5, 1, 1, false));

            var packedBoxes = packer.Pack().ToList();

            Assert.Single(packedBoxes);
            Assert.Equal("Box A", packedBoxes[0].Box.Reference);
        }
Пример #6
0
        public void TestCanPackRepresentativeLargerSamples3D(string testId, BoxList boxes, List <ItemData> itemsData, Expected expected2D, Expected expected3D)
        {
            var expectedItemCount = 0;

            var packer = new Packer();

            foreach (var box in boxes)
            {
                packer.AddBox(box);
            }

            foreach (var iData in itemsData)
            {
                expectedItemCount += iData.qty;

                packer.AddItem(Factory.CreateItem(
                                   iData.name,
                                   iData.width,
                                   iData.length,
                                   iData.depth,
                                   iData.weight,
                                   true
                                   ), iData.qty
                               );
            }

            var packedBoxes3D = packer.Pack();

            var packedItemCount3D = packedBoxes3D.ToList().Sum(pb => pb?.PackedItems?.Count ?? 0);

            Assert.Equal(expected3D.boxes, packedBoxes3D.Count);
            Assert.Equal(expectedItemCount, packedItemCount3D);
            Assert.Equal(expected3D.utilisation, packedBoxes3D.GetVolumeUtilisation());
            Assert.Equal(expected3D.weightVariance, packedBoxes3D.GetWeightVariance());
        }
Пример #7
0
        public void OversizedItemsCanCreateCustomBoxesIfEnabled()
        {
            var item1 = new Item()
            {
                Description = "My Cube 5x5x5",
                Depth       = 5,
                Length      = 5,
                Weight      = 5,
                Width       = 5
            };

            var box1 = new Box()
            {
                Description = "My Box 1x1x1",
                OuterDepth  = 1,
                OuterLength = 1,
                OuterWidth  = 1,
                EmptyWeight = 1,
                InnerDepth  = 1,
                InnerLength = 1,
                InnerWidth  = 1,
                MaxWeight   = 2
            };

            var packer = new Packer(true);

            packer.AddBox(box1);
            packer.AddItem(item1, 1);

            var packedBoxes = packer.Pack();

            Assert.NotNull(packedBoxes);
            Assert.Greater(packedBoxes.GetCount(), 0);
        }
Пример #8
0
        public void CanPackByVolume()
        {
            var item1 = new Item()
            {
                Description = "My Cube 5x5x5",
                Depth       = 5,
                Length      = 5,
                Weight      = 5,
                Width       = 5
            };

            var box = new Box()
            {
                Description = "My Box 230x300x240",
                OuterDepth  = 230,
                OuterLength = 300,
                OuterWidth  = 240,
                EmptyWeight = 160,
                InnerDepth  = 230,
                InnerLength = 300,
                InnerWidth  = 240,
                MaxWeight   = 15000
            };

            var packer = new Packer();

            packer.AddBox(box);
            packer.AddItem(item1, 200);

            var packedBoxes = packer.PackByVolume();

            Assert.NotNull(packedBoxes);
            Assert.Greater(packedBoxes.GetCount(), 0);
        }
Пример #9
0
        public void CanPackByVolumeFor6OfSameItem()
        {
            var item1 = new Item()
            {
                Description = "6x6x7",
                Depth       = 178,
                Length      = 152,
                Weight      = 272,
                Width       = 152
            };

            var box = new Box()
            {
                Description = "20x20x7",
                OuterDepth  = 178,
                OuterLength = 508,
                OuterWidth  = 508,
                EmptyWeight = 1,
                InnerDepth  = 178,
                InnerLength = 508,
                InnerWidth  = 508,
                MaxWeight   = 32000
            };

            var packer = new Packer();

            packer.AddBox(box);
            packer.AddItem(item1, 6);

            var packedBoxes = packer.PackByVolume();

            TestContext.WriteLine(packedBoxes.GetCount());
        }
Пример #10
0
        public void CanAddBox()
        {
            var box = new Box()
            {
                Description = "My Box 5x5x5",
                OuterDepth  = 5,
                OuterLength = 5,
                OuterWidth  = 5,
                MaxWeight   = 100
            };

            var packer = new Packer();

            packer.AddBox(box);

            var boxes = packer.GetBoxes();

            Assert.AreEqual(1, boxes.GetCount());

            var castItems = boxes.GetContent().Cast <Box>().ToList();

            Assert.AreEqual(1, castItems.Count);

            var firstItem = castItems.FirstOrDefault();

            Assert.NotNull(firstItem);
            Assert.AreEqual(box.Description, firstItem.Description);
        }
Пример #11
0
        public void TestWeightRedistributionActivatesOrNot()
        {
            // first pack normally - expecting 2+2 after balancing
            var packer1 = new Packer();

            packer1.AddBox(Factory.CreateBox("Box", 1, 1, 3, 0, 1, 1, 3, 3));
            packer1.AddItem(Factory.CreateItem("Item", 1, 1, 1, 1, false), 4);

            var packedBoxes1 = packer1.Pack().ToList();

            Assert.Equal(2, packedBoxes1[0].PackedItems.Count);
            Assert.Equal(2, packedBoxes1[1].PackedItems.Count);

            // same items, but with redistribution turned off - expecting 3+1 based on pure fit
            var packer2 = new Packer();

            packer2.AddBox(Factory.CreateBox("Box", 1, 1, 3, 0, 1, 1, 3, 3));
            packer2.AddItem(Factory.CreateItem("Item", 1, 1, 1, 1, false), 4);
            packer2.MaxBoxesToBalanceWeight = 1;

            var packedBoxes2 = packer2.Pack().ToList();

            Assert.Equal(3, packedBoxes2[0].PackedItems.Count);
            Assert.Single(packedBoxes2[1].PackedItems);
        }
Пример #12
0
        public void TestIssue170()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Box", 170, 120, 120, 2000, 170, 120, 120, 60000));
            packer.AddItem(Factory.CreateItem("Item", 70, 130, 2, 657, false), 100);

            var packedBox = packer.Pack();

            Assert.Equal(2, packedBox.Count);
        }
Пример #13
0
        public void TestWeightRedistributionActivatesOrNot()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Box", 1, 1, 3, 0, 1, 1, 3, 3));
            packer.AddItem(Factory.CreateItem("Item", 1, 1, 1, 1, false), 4);

            var pBoxes = packer.Pack().ToList();

            Assert.Equal(2, pBoxes[0].PackedItems.Count);
            Assert.Equal(2, pBoxes[1].PackedItems.Count);
        }
Пример #14
0
        public void TestIssue38()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Box1", 2, 2, 2, 0, 2, 2, 2, 1000));
            packer.AddBox(Factory.CreateBox("Box2", 4, 4, 4, 0, 4, 4, 4, 1000));

            packer.AddItem(Factory.CreateItem("Item 1", 1, 1, 1, 100, false));
            packer.AddItem(Factory.CreateItem("Item 2", 1, 1, 1, 100, false));
            packer.AddItem(Factory.CreateItem("Item 3", 1, 1, 1, 100, false));
            packer.AddItem(Factory.CreateItem("Item 4", 1, 1, 1, 100, false));
            packer.AddItem(Factory.CreateItem("Item 5", 2, 2, 2, 100, false));
            packer.AddItem(Factory.CreateItem("Item 6", 2, 2, 2, 100, false));
            packer.AddItem(Factory.CreateItem("Item 7", 2, 2, 2, 100, false));
            packer.AddItem(Factory.CreateItem("Item 8", 2, 2, 2, 100, false));
            packer.AddItem(Factory.CreateItem("Item 9", 4, 4, 4, 100, false));

            var packedBoxes = packer.Pack();

            Assert.Equal(2, packedBoxes.Count);
        }
Пример #15
0
        public void TestIssue52A()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Box", 100, 50, 50, 0, 100, 50, 50, 5000));
            packer.AddItem(Factory.CreateItem("Item", 15, 13, 8, 407, true), 2);
            var packedBoxes = packer.Pack().ToList();

            Assert.Single(packedBoxes);
            Assert.Equal(26, packedBoxes[0].UsedWidth);
            Assert.Equal(15, packedBoxes[0].UsedLength);
            Assert.Equal(8, packedBoxes[0].UsedDepth);
        }
Пример #16
0
        public void TestIssue14()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("29x1x23Box", 29, 1, 23, 0, 29, 1, 23, 100));
            packer.AddItem(Factory.CreateItem("13x1x10Item", 13, 1, 10, 1, true));
            packer.AddItem(Factory.CreateItem("9x1x6Item", 9, 1, 6, 1, true));
            packer.AddItem(Factory.CreateItem("9x1x6Item", 9, 1, 6, 1, true));
            packer.AddItem(Factory.CreateItem("9x1x6Item", 9, 1, 6, 1, true));

            var pBoxes     = packer.Pack();
            var boxesCount = pBoxes.Count;

            Assert.Equal(1, boxesCount);
        }
Пример #17
0
        public void TestIssue52C()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Box", 230, 300, 240, 160, 230, 300, 240, 15000));
            packer.AddItem(Factory.CreateItem("Item 1", 210, 297, 4, 213, true));
            packer.AddItem(Factory.CreateItem("Item 2", 80, 285, 70, 199, true));
            packer.AddItem(Factory.CreateItem("Item 3", 80, 285, 70, 199, true));
            var packedBoxes = packer.Pack().ToList();

            Assert.Single(packedBoxes);
            Assert.Equal(210, packedBoxes[0].UsedWidth);
            Assert.Equal(297, packedBoxes[0].UsedLength);
            Assert.Equal(74, packedBoxes[0].UsedDepth);
        }
Пример #18
0
        public void TestIssue52B()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Box", 370, 375, 60, 140, 364, 374, 40, 3000));
            packer.AddItem(Factory.CreateItem("Item 1", 220, 310, 12, 679, true));
            packer.AddItem(Factory.CreateItem("Item 2", 210, 297, 11, 648, true));
            packer.AddItem(Factory.CreateItem("Item 3", 210, 297, 5, 187, true));
            packer.AddItem(Factory.CreateItem("Item 4", 148, 210, 32, 880, true));
            var packedBoxes = packer.Pack().ToList();

            Assert.Single(packedBoxes);
            Assert.Equal(310, packedBoxes[0].UsedWidth);
            Assert.Equal(368, packedBoxes[0].UsedLength);
            Assert.Equal(32, packedBoxes[0].UsedDepth);
        }
Пример #19
0
        public void CustomBoxesGeneratesIds()
        {
            var item1 = new Item()
            {
                Description = "My Cube 5x5x5",
                Depth       = 5,
                Length      = 5,
                Weight      = 5,
                Width       = 5
            };

            var box1 = new Box()
            {
                Description = "My Box 1x1x1",
                OuterDepth  = 1,
                OuterLength = 1,
                OuterWidth  = 1,
                EmptyWeight = 1,
                InnerDepth  = 1,
                InnerLength = 1,
                InnerWidth  = 1,
                MaxWeight   = 2
            };

            var packer = new Packer(true);

            packer.AddBox(box1);
            packer.AddItem(item1, 1);

            var packedBoxes = packer.Pack();

            foreach (var packedBox in packedBoxes.GetContent().Cast <PackedBox>())
            {
                var box = packedBox.GetBox();
                Assert.IsNotEmpty(box.GeneratedId);
            }
        }
Пример #20
0
        public void CanPackMultipleSameItemsInSingleBox()
        {
            var item = new Item()
            {
                Description = "My Cube 6x6x6",
                Depth       = (Int32)ConversionHelper.ConvertInchesToMillimeters(6),
                Length      = (Int32)ConversionHelper.ConvertInchesToMillimeters(6),
                Weight      = (Int32)ConversionHelper.ConvertPoundsToGrams(6),
                Width       = (Int32)ConversionHelper.ConvertInchesToMillimeters(6)
            };

            var box = new Box()
            {
                Description = "12x12x8",
                OuterDepth  = (Int32)ConversionHelper.ConvertInchesToMillimeters(8),
                OuterLength = (Int32)ConversionHelper.ConvertInchesToMillimeters(12),
                OuterWidth  = (Int32)ConversionHelper.ConvertInchesToMillimeters(12),
                EmptyWeight = (Int32)ConversionHelper.ConvertPoundsToGrams(1),
                InnerDepth  = (Int32)ConversionHelper.ConvertInchesToMillimeters(8),
                InnerLength = (Int32)ConversionHelper.ConvertInchesToMillimeters(12),
                InnerWidth  = (Int32)ConversionHelper.ConvertInchesToMillimeters(12),
                MaxWeight   = (Int32)ConversionHelper.ConvertPoundsToGrams(70)
            };

            var packer = new Packer();

            packer.AddBox(box);
            packer.AddItem(item, 6);

            var packedBoxes = packer.Pack();

            Assert.NotNull(packedBoxes);
            Assert.Greater(packedBoxes.GetCount(), 0);

            TestContext.WriteLine("Total packed boxes: " + packedBoxes.GetCount());
        }
Пример #21
0
        public void TestUsedWidthAndRemainingWidthHandleRotationsCorrectly()
        {
            var packer = new Packer();

            packer.AddBox(Factory.CreateBox("Box", 23, 27, 14, 0, 23, 27, 14, 30));
            packer.AddItem(Factory.CreateItem("Item 1", 11, 22, 2, 1, true), 3);
            packer.AddItem(Factory.CreateItem("Item 2", 11, 22, 2, 1, true), 4);
            packer.AddItem(Factory.CreateItem("Item 3", 6, 17, 2, 1, true), 3);

            var pBoxes = packer.Pack();

            var boxesCount = pBoxes.Count;

            Assert.Equal(1, boxesCount);

            var pBox = pBoxes.First();

            Assert.Equal(22, pBox.UsedWidth);
            Assert.Equal(23, pBox.UsedLength);
            Assert.Equal(10, pBox.UsedDepth);
            Assert.Equal(1, pBox.RemainingWidth);
            Assert.Equal(4, pBox.RemainingLength);
            Assert.Equal(4, pBox.RemainingDepth);
        }
Пример #22
0
        public void CanPackBoxes()
        {
            var item1 = new Item()
            {
                Description = "My Cube 5x5x5",
                Depth       = 5,
                Length      = 5,
                Weight      = 5,
                Width       = 5
            };

            var box1 = new Box()
            {
                Description = "My Box 230x300x240",
                OuterDepth  = 230,
                OuterLength = 300,
                OuterWidth  = 240,
                EmptyWeight = 10,
                InnerDepth  = 230,
                InnerLength = 300,
                InnerWidth  = 240,
                MaxWeight   = 250
            };

            var box2 = new Box()
            {
                Description = "My Box 100x200x300",
                OuterDepth  = 100,
                OuterLength = 200,
                OuterWidth  = 300,
                EmptyWeight = 10,
                InnerDepth  = 100,
                InnerLength = 200,
                InnerWidth  = 300,
                MaxWeight   = 150
            };

            var box3 = new Box()
            {
                Description = "My Box 75x250x100",
                OuterDepth  = 75,
                OuterLength = 250,
                OuterWidth  = 100,
                EmptyWeight = 10,
                InnerDepth  = 75,
                InnerLength = 250,
                InnerWidth  = 100,
                MaxWeight   = 150
            };

            var packer = new Packer();

            packer.AddBox(box1);
            packer.AddBox(box2);
            packer.AddBox(box3);
            packer.AddItem(item1, 120);

            var packedBoxes = packer.Pack();

            Assert.NotNull(packedBoxes);
            Assert.Greater(packedBoxes.GetCount(), 0);
        }
Пример #23
0
        public void CanPackFiveItemsTwoLargeOneSmallBoxButThreeAfterRepack()
        {
            var packer = new Packer();
            var box    = new Box()
            {
                Description = "Le Petite Box",
                OuterWidth  = 600,
                OuterLength = 600,
                OuterDepth  = 10,
                EmptyWeight = 10,
                InnerWidth  = 596,
                InnerLength = 596,
                InnerDepth  = 8,
                MaxWeight   = 1000
            };

            var box2 = new Box()
            {
                Description = "Le Grande Box",
                OuterWidth  = 3000,
                OuterLength = 3000,
                OuterDepth  = 50,
                EmptyWeight = 100,
                InnerWidth  = 2960,
                InnerLength = 2960,
                InnerDepth  = 40,
                MaxWeight   = 10000
            };

            var items = new ItemList();

            items.Insert(new Item()
            {
                Id = "Item 1", Description = "Item 1", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });
            items.Insert(new Item()
            {
                Id = "Item 2", Description = "Item 2", Width = 550, Length = 550, Depth = 2, Weight = 200
            });
            items.Insert(new Item()
            {
                Id = "Item 3", Description = "Item 3", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });
            items.Insert(new Item()
            {
                Id = "Item 4", Description = "Item 4", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });
            items.Insert(new Item()
            {
                Id = "Item 5", Description = "Item 5", Width = 2500, Length = 2500, Depth = 20, Weight = 2000
            });

            packer.AddBox(box);
            packer.AddBox(box2);
            packer.AddItems(items);

            var packedBoxes = packer.Pack();

            Assert.AreEqual(3, packedBoxes.GetCount());

            Assert.AreEqual(1, packedBoxes.GetBest().GetItems().GetCount());
            Assert.AreEqual(box, packedBoxes.GetBest().GetBox());
            Assert.AreEqual(210, packedBoxes.GetBest().GetWeight());

            packedBoxes.ExtractBest();
            Assert.AreEqual(2, packedBoxes.GetBest().GetItems().GetCount());
            Assert.AreEqual(box2, packedBoxes.GetBest().GetBox());
            Assert.AreEqual(4100, packedBoxes.GetBest().GetWeight());

            packedBoxes.ExtractBest();
            Assert.AreEqual(2, packedBoxes.GetBest().GetItems().GetCount());
            Assert.AreEqual(box2, packedBoxes.GetBest().GetBox());
            Assert.AreEqual(4100, packedBoxes.GetBest().GetWeight());
        }