Ejemplo n.º 1
0
        public void TestIfItCanStartAndEndSort()
        {
            // Arrange
            ISocket sendSocket = TestHelper.GetSocket();
            ISocket recvSocket = TestHelper.GetSocket();

            PipeMaster target = TestHelper.GetPipeMaster(sendSocket, recvSocket);

            int[] input    = new int[] { 1, 6, 3, 4, 5, 2, 7 };
            int[] expected = input.OrderBy(n => n).ToArray();
            recvSocket.SetRecvData(expected);
            int[] actual = null;

            // Act
            actual = target.Sort(input);

            // Assert
            int[] actualSentData = sendSocket.GetSentIntArray();
            Verify.That(actual).IsEqualTo(expected).Now();
            Verify.That(actualSentData).IsEqualTo(input).Now();

            // Reset
        }
Ejemplo n.º 2
0
        public void TestIfItCanSortNumbers()
        {
            // Arrange
            ManualResetEvent waitForLeft  = new ManualResetEvent(false);
            ManualResetEvent waitForRight = new ManualResetEvent(false);

            int[] dataFromLeft  = new int[] { 1, 3, 2 };
            int   leftRecvIndex = 0;

            int[] dataFromRight  = new int[] { 2, 1 };
            int   rightRecvIndex = 0;

            int[] expectedSentToRight = new int[] { 1, 2 };
            int[] expectedSentToLeft  = new int[] { 3, 2, 1 };

            PipeNode target            = GetPipeNode();
            ISocket  senderToRight     = target.GetSenderToRight();
            ISocket  receiverFromLeft  = target.GetReceiverFromLeft();
            ISocket  receiverFromRight = target.GetReceiverFromRight();
            ISocket  senderToLeft      = target.GetSenderToLeft();

            receiverFromLeft.Recv(Arg.Any <int>()).Returns(call =>
            {
                if (leftRecvIndex == dataFromLeft.Length)
                {
                    waitForLeft.Set();
                    Thread.Sleep(call.Arg <int>() + 1);
                    return(new byte[] { 0, 0, 0, 0 });
                }

                return(BitConverter.GetBytes(dataFromLeft[leftRecvIndex++]));
            });

            receiverFromRight.Recv(Arg.Any <int>()).Returns(call =>
            {
                if (leftRecvIndex == dataFromLeft.Length)
                {
                    if (rightRecvIndex < dataFromRight.Length)
                    {
                        return(BitConverter.GetBytes(dataFromRight[rightRecvIndex++]));
                    }
                    else
                    {
                        waitForRight.Set();
                        Thread.Sleep(call.Arg <int>() + 1);
                    }
                }
                else
                {
                    waitForLeft.WaitOne();
                    Thread.Sleep(call.Arg <int>() + 1);
                }

                return(new byte[] { 0, 0, 0, 0 });
            });

            int expectedKept = 3;
            int actualKept   = -1;

            // Act
            target.Start(0, 3);
            waitForRight.WaitOne();

            // Assert
            var actualSentToRight = senderToRight.GetSentIntArray();
            var actualSentToLeft  = senderToLeft.GetSentIntArray();

            actualKept = target.GetKept();

            Verify.That(actualKept).IsEqualTo(expectedKept, "Bad expected number kept").Now();
            Verify.That(actualSentToRight).IsEqualTo(expectedSentToRight, "Bad expected sent to right numbers").Now();
            Verify.That(actualSentToLeft).IsEqualTo(expectedSentToLeft, "Bad expected sent to left numbers.").Now();

            // Reset
        }