コード例 #1
0
        public void UnknownSizePackerTestValidSquares3()
        {
            //Arrange
            var inputSequence          = (TestUtil.Shuffle(TestUtil.GetIncreasingSquares(3)));
            var placementAlgorithmMock = new Mock <IPlacementAlgorithm>();

            placementAlgorithmMock
            .Setup(x => x.PlaceRects(It.IsAny <int>(), It.IsAny <int>(), inputSequence, It.IsAny <CancellationToken>()))
            .Returns <int, int, IEnumerable <PPRect>, CancellationToken>((w, h, rects, _) =>
            {
                //Assume "dumb" placement algorithm that simple places all the rects consecutively next to each other
                int maxH = rects.Max(x => x.Height);
                int sumW = rects.Sum(x => x.Width);
                if (sumW > w || maxH > h)
                {
                    return(null);
                }
                int left = 0;
                return(new PackingResult(sumW, maxH, rects.Select(x =>
                {
                    left += x.Width;
                    return new PPRect(left, 0, left, x.Height);
                })));;
            });
            var packer = new UnknownSizePacker(placementAlgorithmMock.Object);

            //Act
            var result = packer.FindMinimumBoundingBox(inputSequence);

            //Assert
            Assert.AreNotEqual(null, result);
            Assert.AreEqual(true, TestUtil.IsPackingResultValid(result));
        }
コード例 #2
0
        public async Task UnknownSizePackerTestRunAfterCancellation()
        {
            //Now we know that it was called atleast three times (from the previous test case), check that the use of cancellation reduce
            //it to two calls (it is called two times before the "main-loop" starts and the cancellation is
            //being checked in the main loop)
            var calledTimes            = 0;
            var inputSequence          = (TestUtil.Shuffle(TestUtil.GetIncreasingSquares(50)));
            var placementAlgorithmMock = new Mock <IPlacementAlgorithm>();

            placementAlgorithmMock
            .Setup(x => x.PlaceRects(It.IsAny <int>(), It.IsAny <int>(), inputSequence, It.IsAny <CancellationToken>()))
            .Returns <int, int, IEnumerable <PPRect>, CancellationToken>((w, h, rects, _) =>
            {
                calledTimes++;
                //Assume "dumb" placement algorithm that simple places all the rects consecutively next to each other
                int maxH = rects.Max(x => x.Height);
                int sumW = rects.Sum(x => x.Width);
                if (sumW > w || maxH > h)
                {
                    return(null);
                }
                int left = 0;
                return(new PackingResult(sumW, maxH, rects.Select(x =>
                {
                    left += x.Width;
                    return new PPRect(left, 0, left, x.Height);
                })));;
            });

            var packer = new UnknownSizePacker(placementAlgorithmMock.Object);

            using (var source = new CancellationTokenSource())
            {
                var cancellationToken = source.Token;

                var task = Task.Run(() => packer.FindMinimumBoundingBox(inputSequence, cancellationToken));
                source.Cancel();

                var result = await task.ConfigureAwait(false);
            }


            //Obtain new cancellation token
            using (var source = new CancellationTokenSource())
            {
                var cancellationToken = source.Token;
                calledTimes = 0;
                var result2 = packer.FindMinimumBoundingBox(inputSequence, cancellationToken);
                Assert.AreNotEqual(null, result2);
                Assert.AreEqual(true, TestUtil.IsPackingResultValid(result2));
            }

            //Check that it was not cancelled
            Assert.IsTrue(calledTimes > 2);
        }
コード例 #3
0
        public void FixedSizePackerTestSimilarToUnknown4()
        {
            //Arrange
            var inputSequence          = (TestUtil.Shuffle(TestUtil.GetIncreasingSquares(4)));
            var placementAlgorithmMock = new Mock <IPlacementAlgorithm>();

            placementAlgorithmMock
            .Setup(x => x.PlaceRects(It.IsAny <int>(), It.IsAny <int>(), inputSequence, It.IsAny <CancellationToken>()))
            .Returns <int, int, IEnumerable <PPRect>, CancellationToken>((w, h, rects, _) =>
            {
                //Assume "dumb" placement algorithm that simple places all the rects consecutively next to each other
                int maxH = rects.Max(x => x.Height);
                int sumW = rects.Sum(x => x.Width);
                if (sumW > w || maxH > h)
                {
                    return(null);
                }
                int left = 0;
                return(new PackingResult(sumW, maxH, rects.Select(x =>
                {
                    left += x.Width;
                    return new PPRect(left, 0, left, x.Height);
                })));;
            });

            var packer  = new FixedSizePacker(12, 12, placementAlgorithmMock.Object);
            var packer2 = new UnknownSizePacker(placementAlgorithmMock.Object);

            //Act
            var result    = packer.FindMinimumBoundingBox(inputSequence);
            var unkResult = packer2.FindMinimumBoundingBox(inputSequence);

            //Assert (Check that for "unlimited" dimensions, it behaves same
            // to unknown size packer)
            var actualW = result.Rects.Max(x => x.Right);
            var actualH = result.Rects.Max(x => x.Bottom);

            Assert.AreEqual(unkResult.Width, actualW);
            Assert.AreEqual(unkResult.Height, actualH);
        }
コード例 #4
0
        public async Task UnknownSizePackerTestRunCancelled()
        {
            //Now we know that it was called atleast three times (from the previous test case), check that the use of cancellation reduce
            //it to two calls (it is called two times before the "main-loop" starts and the cancellation is
            //being checked in the main loop)
            var calledTimes            = 0;
            var inputSequence          = (TestUtil.Shuffle(TestUtil.GetIncreasingSquares(50)));
            var placementAlgorithmMock = new Mock <IPlacementAlgorithm>();

            placementAlgorithmMock
            .Setup(x => x.PlaceRects(It.IsAny <int>(), It.IsAny <int>(), inputSequence, It.IsAny <CancellationToken>()))
            .Returns <int, int, IEnumerable <PPRect>, CancellationToken>((w, h, rects, _) =>
            {
                //Ensure that it is not too fast ...
                Thread.Sleep(1000);
                calledTimes++;
                //Assume "dumb" placement algorithm that simple places all the rects consecutively next to each other
                int maxH = rects.Max(x => x.Height);
                int sumW = rects.Sum(x => x.Width);
                if (sumW > w || maxH > h)
                {
                    return(null);
                }
                int left = 0;
                return(new PackingResult(sumW, maxH, rects.Select(x =>
                {
                    left += x.Width;
                    return new PPRect(left, 0, left, x.Height);
                })));;
            });

            var packer = new UnknownSizePacker(placementAlgorithmMock.Object);

            using var source = new CancellationTokenSource();
            var cancellationToken = source.Token;

            var task = Task.Run(() => packer.FindMinimumBoundingBox(inputSequence, cancellationToken));

            source.Cancel();

            var result = await task.ConfigureAwait(false);

            //Check that even though it was canceled, some meaningful result was returned
            //This is property of UnknownSizePacker, not general property of all minimum bounding box finders
            Assert.AreNotEqual(null, result);
            //Check that the returned result is valid
            Assert.AreEqual(true, TestUtil.IsPackingResultValid(result));

            //Check that it was called exactly two times
            //The cancellation check occurs after initial two calls within a loop
            Assert.IsTrue(calledTimes == 2);

            calledTimes = 0;
            var result2 = packer.FindMinimumBoundingBox(inputSequence, cancellationToken);

            //Check that even though it was canceled, some meaningful result was returned
            //This is property of UnknownSizePacker, not general property of all minimum bounding box finders
            Assert.AreNotEqual(null, result2);
            //Check that the returned result is valid
            Assert.AreEqual(true, TestUtil.IsPackingResultValid(result2));
            //Check that it was called exactly two times
            //The cancellation check occurs after initial two calls within a loop
            Assert.IsTrue(calledTimes == 2);
        }